From ad31a2e4244fc72f9abc725b0c55c351edbe759e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 7 Nov 2022 17:45:29 +0000 Subject: [PATCH] Tests: Combine duplicate `update_posts_count()` tests. This combines the newer test for `update_posts_count()` located in its own file with the pre-existing one from `tests/multisite/site.php`, which was essentially testing the same thing in a similar way. Includes: * Renaming the test class per the [https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#naming-and-organization naming conventions]. * Adjusting comments per the documentation standards. * Updating `@covers` tags for accuracy. * Removing unnecessary blog switching. * Using `assertSame()` to check the value type. Follow-up to [28835], [29667], [52207]. See #57023, #56793. git-svn-id: https://develop.svn.wordpress.org/trunk@54760 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/multisite/site.php | 12 ---- .../tests/multisite/updatePostsCount.php | 58 +++++++++---------- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 600bee38b4..990003cde9 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -440,18 +440,6 @@ if ( is_multisite() ) : $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); } - /** - * @ticket 27952 - */ - public function test_posts_count() { - self::factory()->post->create(); - $post2 = self::factory()->post->create(); - $this->assertSame( 2, get_site()->post_count ); - - wp_delete_post( $post2 ); - $this->assertSame( 1, get_site()->post_count ); - } - /** * @ticket 26410 */ diff --git a/tests/phpunit/tests/multisite/updatePostsCount.php b/tests/phpunit/tests/multisite/updatePostsCount.php index 2806aaedd5..850d9fff59 100644 --- a/tests/phpunit/tests/multisite/updatePostsCount.php +++ b/tests/phpunit/tests/multisite/updatePostsCount.php @@ -2,54 +2,48 @@ if ( is_multisite() ) : /** - * Test update_posts_count() get called via filters of WP_Site in multisite. + * Test that update_posts_count() gets called via default filters on multisite. * * @group ms-site * @group multisite * - * @covers ::_update_posts_count_on_delete + * @covers ::update_posts_count */ - class Tests_update_posts_count_on_delete extends WP_UnitTestCase { + class Tests_Multisite_UpdatePostsCount extends WP_UnitTestCase { /** - * Test that the posts count is updated correctly when a posts are added and deleted. + * Tests that posts count is updated correctly when posts are added or deleted. + * + * @ticket 27952 * @ticket 53443 + * + * @covers ::_update_posts_count_on_transition_post_status + * @covers ::_update_posts_count_on_delete */ - public function test_update_posts_count_on_delete() { + public function test_update_posts_count() { + $original_post_count = (int) get_site()->post_count; - $blog_id = self::factory()->blog->create(); - switch_to_blog( $blog_id ); + $post_id = self::factory()->post->create(); - $current_post_count = (int) get_option( 'post_count' ); - - $post_id = self::factory()->post->create( - array( - 'post_type' => 'post', - 'post_author' => '1', - 'post_date' => '2012-10-23 19:34:42', - 'post_status' => 'publish', - ) - ); - - /** - * Check that add_action( 'deleted_post', '_update_posts_count_on_delete' ) is called when a post is created. - * Check that _update_posts_count_on_transition_post_status() is called on that filter which then calls - * update_posts_count to update the count. + /* + * Check that posts count is updated when a post is created: + * add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 ); + * + * Check that _update_posts_count_on_transition_post_status() is called on that filter, + * which then calls update_posts_count() to update the count. */ - $this->assertEquals( $current_post_count + 1, (int) get_option( 'post_count' ), 'post added' ); + $this->assertSame( $original_post_count + 1, get_site()->post_count, 'Post count should be incremented by 1.' ); wp_delete_post( $post_id ); - /** - * Check that add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 ) - * is called when a post is deleted. - * Check that _update_posts_count_on_delete() is called on that filter which then calls update_posts_count - * to update the count. + /* + * Check that posts count is updated when a post is deleted: + * add_action( 'deleted_post', '_update_posts_count_on_delete' ); + * + * Check that _update_posts_count_on_delete() is called on that filter, + * which then calls update_posts_count() to update the count. */ - $this->assertEquals( $current_post_count, (int) get_option( 'post_count' ), 'post deleted' ); - - restore_current_blog(); - + $this->assertSame( $original_post_count, get_site()->post_count, 'Post count should match the original count.' ); } }