From 2c11931a4a1ac45501ae3414fe460aa6f484ea33 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 22 Jun 2022 16:50:14 +0000 Subject: [PATCH] Tests: Improve the test for sticky posts not being moved to the front in sitemaps. * Give the test method a most descriptive name. * Use a single assertion for the URL list instead of a `foreach` loop to provide more context in case of failure. * Add a failure message to each assertion, as there are multiple assertions used in the test. Follow-up to [53548]. See #55633. git-svn-id: https://develop.svn.wordpress.org/trunk@53556 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/sitemaps/wpSitemapsPosts.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php b/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php index 262cb0b22b..2bdeb92e8e 100644 --- a/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php +++ b/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php @@ -62,12 +62,21 @@ class Tests_Sitemaps_wpSitemapsPosts extends WP_UnitTestCase { $this->assertArrayHasKey( 'lastmod', $sitemap_entry ); } + /** + * Callback for 'wp_sitemaps_posts_show_on_front_entry' filter. + */ + public function _show_on_front_entry( $sitemap_entry ) { + $sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() ); + + return $sitemap_entry; + } + /** * Tests that sticky posts are not moved to the front of the first page of the post sitemap. * * @ticket 55633 */ - public function test_posts_sticky_posts() { + public function test_posts_sticky_posts_not_moved_to_front() { $factory = self::factory(); // Create 4 posts, and stick the last one. @@ -79,19 +88,15 @@ class Tests_Sitemaps_wpSitemapsPosts extends WP_UnitTestCase { $url_list = $posts_provider->get_url_list( 1, 'post' ); - $this->assertCount( count( $post_ids ), $url_list ); - // Check that the URL list is still in the order of the post IDs (i.e., sticky post wasn't moved to the front). - foreach ( $post_ids as $idx => $post_id ) { - $this->assertSame( array( 'loc' => home_url( "?p={$post_id}" ) ), $url_list[ $idx ] ); + $this->assertCount( count( $post_ids ), $url_list, 'The post count did not match.' ); + + $expected = array(); + + foreach ( $post_ids as $post_id ) { + $expected[] = array( 'loc' => home_url( "?p={$post_id}" ) ); } - } - /** - * Callback for 'wp_sitemaps_posts_show_on_front_entry' filter. - */ - public function _show_on_front_entry( $sitemap_entry ) { - $sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() ); - - return $sitemap_entry; + // Check that the URL list is still in the order of the post IDs (i.e., sticky post wasn't moved to the front). + $this->assertSame( $expected, $url_list, 'The post order did not match.' ); } }