wordpress-develop/tests/phpunit/tests/canonical/sitemaps.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

115 lines
4.3 KiB
PHP

<?php
/**
* @group canonical
* @group rewrite
* @group query
* @group sitemaps
*/
class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {
public function set_up() {
parent::set_up();
$wp_sitemaps = new WP_Sitemaps();
$wp_sitemaps->init();
}
public function test_remove_trailing_slashes_for_sitemap_index_requests() {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( '/wp-sitemap.xml', '/wp-sitemap.xml' );
$this->assertCanonical( '/wp-sitemap.xml/', '/wp-sitemap.xml' );
}
public function test_remove_trailing_slashes_for_sitemap_index_stylesheet_requests() {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( '/wp-sitemap-index.xsl', '/wp-sitemap-index.xsl' );
$this->assertCanonical( '/wp-sitemap-index.xsl/', '/wp-sitemap-index.xsl' );
}
public function test_remove_trailing_slashes_for_sitemap_requests() {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' );
$this->assertCanonical( '/wp-sitemap-posts-post-1.xml/', '/wp-sitemap-posts-post-1.xml' );
$this->assertCanonical( '/wp-sitemap-users-1.xml', '/wp-sitemap-users-1.xml' );
$this->assertCanonical( '/wp-sitemap-users-1.xml/', '/wp-sitemap-users-1.xml' );
}
public function test_remove_trailing_slashes_for_sitemap_stylesheet_requests() {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( '/wp-sitemap.xsl', '/wp-sitemap.xsl' );
$this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' );
}
/**
* Ensure sitemaps redirects work as expected with pretty permalinks.
*
* @dataProvider data_sitemaps_canonical_pretty_redirects
* @ticket 50910
*/
public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected ) {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( $test_url, $expected, 50910 );
}
/**
* Data provider for test_sitemaps_canonical_pretty_redirects.
*
* @return array[] {
* Data to test with.
*
* @type string $0 The test URL.
* @type string $1 The expected canonical URL.
* }
*/
public function data_sitemaps_canonical_pretty_redirects() {
return array(
// Ugly/incorrect versions redirect correctly.
array( '/?sitemap=index', '/wp-sitemap.xml' ),
array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ),
array( '/?sitemap=posts&sitemap-subtype=post', '/wp-sitemap-posts-post-1.xml' ),
array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/wp-sitemap-posts-post-2.xml' ),
array( '/?sitemap=taxonomies&sitemap-subtype=category', '/wp-sitemap-taxonomies-category-1.xml' ),
array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/wp-sitemap-taxonomies-category-2.xml' ),
// Pretty versions don't redirect incorrectly.
array( '/wp-sitemap.xml', '/wp-sitemap.xml' ),
array( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' ),
array( '/wp-sitemap-posts-post-2.xml', '/wp-sitemap-posts-post-2.xml' ),
array( '/wp-sitemap-taxonomies-category-1.xml', '/wp-sitemap-taxonomies-category-1.xml' ),
array( '/wp-sitemap-taxonomies-category-2.xml', '/wp-sitemap-taxonomies-category-2.xml' ),
);
}
/**
* Ensure sitemaps redirects work as expected with ugly permalinks.
*
* @dataProvider data_sitemaps_canonical_ugly_redirects
* @ticket 50910
*/
public function test_sitemaps_canonical_ugly_redirects( $test_url, $expected ) {
$this->set_permalink_structure( '' );
$this->assertCanonical( $test_url, $expected, 50910 );
}
/**
* Data provider for test_sitemaps_canonical_ugly_redirects.
*
* @return array[] {
* Data to test with.
*
* @type string $0 The test URL.
* @type string $1 The expected canonical URL.
* }
*/
public function data_sitemaps_canonical_ugly_redirects() {
return array(
// Ugly permalinks remain ugly.
array( '/?sitemap=index', '/?sitemap=index' ),
array( '/?sitemap=posts&sitemap-subtype=post', '/?sitemap=posts&sitemap-subtype=post' ),
array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/?sitemap=posts&sitemap-subtype=post&paged=2' ),
array( '/?sitemap=taxonomies&sitemap-subtype=category', '/?sitemap=taxonomies&sitemap-subtype=category' ),
array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/?sitemap=taxonomies&sitemap-subtype=category&paged=2' ),
);
}
}