Make WordPress Core

Changeset 54760


Ignore:
Timestamp:
11/07/2022 05:45:29 PM (19 months ago)
Author:
SergeyBiryukov
Message:

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 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.

Location:
trunk/tests/phpunit/tests/multisite
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/site.php

    r54757 r54760  
    442442
    443443        /**
    444          * @ticket 27952
    445          */
    446         public function test_posts_count() {
    447             self::factory()->post->create();
    448             $post2 = self::factory()->post->create();
    449             $this->assertSame( 2, get_site()->post_count );
    450 
    451             wp_delete_post( $post2 );
    452             $this->assertSame( 1, get_site()->post_count );
    453         }
    454 
    455         /**
    456444         * @ticket 26410
    457445         */
  • trunk/tests/phpunit/tests/multisite/updatePostsCount.php

    r52207 r54760  
    33if ( is_multisite() ) :
    44    /**
    5      * Test update_posts_count() get called via filters of WP_Site in multisite.
     5     * Test that update_posts_count() gets called via default filters on multisite.
    66     *
    77     * @group ms-site
    88     * @group multisite
    99     *
    10      * @covers ::_update_posts_count_on_delete
     10     * @covers ::update_posts_count
    1111     */
    12     class Tests_update_posts_count_on_delete extends WP_UnitTestCase {
     12    class Tests_Multisite_UpdatePostsCount extends WP_UnitTestCase {
    1313
    1414        /**
    15          * Test that the posts count is updated correctly when a posts are added and deleted.
     15         * Tests that posts count is updated correctly when posts are added or deleted.
     16         *
     17         * @ticket 27952
    1618         * @ticket 53443
     19         *
     20         * @covers ::_update_posts_count_on_transition_post_status
     21         * @covers ::_update_posts_count_on_delete
    1722         */
    18         public function test_update_posts_count_on_delete() {
     23        public function test_update_posts_count() {
     24            $original_post_count = (int) get_site()->post_count;
    1925
    20             $blog_id = self::factory()->blog->create();
    21             switch_to_blog( $blog_id );
     26            $post_id = self::factory()->post->create();
    2227
    23             $current_post_count = (int) get_option( 'post_count' );
    24 
    25             $post_id = self::factory()->post->create(
    26                 array(
    27                     'post_type'   => 'post',
    28                     'post_author' => '1',
    29                     'post_date'   => '2012-10-23 19:34:42',
    30                     'post_status' => 'publish',
    31                 )
    32             );
    33 
    34             /**
    35              * Check that add_action( 'deleted_post', '_update_posts_count_on_delete' ) is called when a post is created.
    36              * Check that _update_posts_count_on_transition_post_status() is called on that filter which then calls
    37              * update_posts_count to update the count.
     28            /*
     29             * Check that posts count is updated when a post is created:
     30             * add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 );
     31             *
     32             * Check that _update_posts_count_on_transition_post_status() is called on that filter,
     33             * which then calls update_posts_count() to update the count.
    3834             */
    39             $this->assertEquals( $current_post_count + 1, (int) get_option( 'post_count' ), 'post added' );
     35            $this->assertSame( $original_post_count + 1, get_site()->post_count, 'Post count should be incremented by 1.' );
    4036
    4137            wp_delete_post( $post_id );
    4238
    43             /**
    44              * Check that add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 )
    45              * is called when a post is deleted.
    46              * Check that _update_posts_count_on_delete() is called on that filter which then calls update_posts_count
    47              * to update the count.
     39            /*
     40             * Check that posts count is updated when a post is deleted:
     41             * add_action( 'deleted_post', '_update_posts_count_on_delete' );
     42             *
     43             * Check that _update_posts_count_on_delete() is called on that filter,
     44             * which then calls update_posts_count() to update the count.
    4845             */
    49             $this->assertEquals( $current_post_count, (int) get_option( 'post_count' ), 'post deleted' );
    50 
    51             restore_current_blog();
    52 
     46            $this->assertSame( $original_post_count, get_site()->post_count, 'Post count should match the original count.' );
    5347        }
    5448    }
Note: See TracChangeset for help on using the changeset viewer.