Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions tests/phpunit/tests/general/getTheGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/**
* Tests for `get_the_generator()`, `the_generator()`, and `wp_generator()`.
*
* @group general
* @group template
*
* @covers ::get_the_generator
* @covers ::the_generator
* @covers ::wp_generator
*/
class Tests_General_GetTheGenerator extends WP_UnitTestCase {

/**
* Tests get_the_generator() with supported generator types.
*
* @ticket 65819
*
* @dataProvider data_generator_types
*
* @param string $type Generator type.
* @param string $expected Expected markup template with placeholder for version.
*/
public function test_get_the_generator_types( $type, $expected ) {
$version = ( 'atom' === $type || 'rss2' === $type || 'rdf' === $type )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: get_bloginfo() and get_bloginfo_rss() are the calls the source makes, so the expected value partly mirrors it. Feel free to use wp_get_wp_version() for all types, which also removes this conditional.

? get_bloginfo_rss( 'version' )
: get_bloginfo( 'version' );

$this->assertSame( sprintf( $expected, $version ), get_the_generator( $type ) );
}

/**
* Data provider for test_get_the_generator_types().
*
* @return array[]
*/
public static function data_generator_types() {
return array(
'html' => array(
'html',
'<meta name="generator" content="WordPress %s">',
),
'xhtml' => array(
'xhtml',
'<meta name="generator" content="WordPress %s" />',
),
'atom' => array(
'atom',
'<generator uri="https://wordpress.org/" version="%s">WordPress</generator>',
),
'rss2' => array(
'rss2',
'<generator>https://wordpress.org/?v=%s</generator>',
),
'rdf' => array(
'rdf',
'<admin:generatorAgent rdf:resource="https://wordpress.org/?v=%s" />',
),
'comment' => array(
'comment',
'<!-- generator="WordPress/%s" -->',
),
);
}

/**
* Tests get_the_generator( 'export' ) formats export comment markup.
*
* @ticket 65819
*/
public function test_get_the_generator_export() {
$output = get_the_generator( 'export' );
$version = get_bloginfo_rss( 'version' );

$this->assertMatchesRegularExpression(
'/^<!-- generator="WordPress\/' . preg_quote( $version, '/' ) . '" created="\d{4}-\d{2}-\d{2} \d{2}:\d{2}" -->$/',
$output
);
}

/**
* Tests that get_the_generator() returns null when $type is empty and no action hook is active.
*
* @ticket 65819
*/
public function test_get_the_generator_returns_null_when_type_empty() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks like a small coverage gap: with an empty $type, only the null return is reached. The current_filter() switch that picks the type (rss2_head, rss_head, rdf_header, atom_head and so on) has no test, and changing its mappings failed nothing locally. Could you add a provider that pushes each hook onto $wp_current_filter, inside try/finally, and checks the markup?

$this->assertNull( get_the_generator() );
$this->assertNull( get_the_generator( '' ) );
}

/**
* Tests that get_the_generator_$type filter modifies the generator markup.
*
* @ticket 65819
*/
public function test_get_the_generator_filter() {
add_filter(
'get_the_generator_xhtml',
static function ( $gen, $type ) {
return '<meta name="custom-generator" content="' . $type . '">';
},
10,
2
);

$this->assertSame( '<meta name="custom-generator" content="xhtml">', get_the_generator( 'xhtml' ) );
}

/**
* Tests that the_generator() echoes generator markup with a newline.
*
* @ticket 65819
*/
public function test_the_generator_echoes_with_newline() {
$expected = get_the_generator( 'xhtml' ) . "\n";

$this->expectOutputString( $expected );
the_generator( 'xhtml' );
}

/**
* Tests that the the_generator filter modifies the displayed generator markup.
*
* @ticket 65819
*/
public function test_the_generator_filter() {
add_filter(
'the_generator',
static function ( $generator, $type ) {
return '<!-- custom ' . $type . ' -->';
},
10,
2
);

$this->expectOutputString( "<!-- custom html -->\n" );
the_generator( 'html' );
}

/**
* Tests that wp_generator() displays the XHTML generator.
*
* @ticket 65819
*/
public function test_wp_generator_displays_xhtml_generator() {
$expected = get_the_generator( 'xhtml' ) . "\n";

$this->expectOutputString( $expected );
wp_generator();
}

/**
* Tests that the wp_generator_type filter changes the generator type used by wp_generator().
*
* @ticket 65819
*/
public function test_wp_generator_type_filter() {
add_filter(
'wp_generator_type',
static function () {
return 'html';
}
);

$expected = get_the_generator( 'html' ) . "\n";

$this->expectOutputString( $expected );
wp_generator();
}
}
Loading