Make WordPress Core

Changeset 64007


Ignore:
Timestamp:
09/29/2026 09:50:38 PM (4 weeks ago)
Author:
lancewillett
Message:

Tests: Reduce setup overhead in canonical post status tests.

Loop through the plain and pretty permalink cases within their respective test methods instead of repeating test setup for each data set. Preserve all 848 assertions and reset request state between cases.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13818

Props shailu25.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical/postStatus.php

    r57357 r64007  
    215215         * Test canonical redirect does not reveal private posts presence.
    216216         *
     217         * Asserts every plain-permalink case inside one test to avoid repeated
     218         * PHPUnit setup/teardown while preserving each case's mutable state reset
     219         * (attachment pages option, current user) and all expected URLs.
     220         *
    217221         * @ticket 5272
    218          * @dataProvider data_canonical_redirects_to_plain_permalinks
    219          *
    220          * @param string $post_key  Post key used for creating fixtures.
    221          * @param string $user_role User role.
    222          * @param string $requested Requested URL.
    223          * @param string $expected  Expected URL.
    224          * @param string $enable_attachment_pages Whether to enable attachment pages. Default true.
     222         * @ticket 66074
    225223         */
    226         public function test_canonical_redirects_to_plain_permalinks( $post_key, $user_role, $requested, $expected, $enable_attachment_pages = true ) {
    227                 if ( $enable_attachment_pages ) {
    228                         update_option( 'wp_attachment_pages_enabled', 1 );
    229                 } else {
    230                         update_option( 'wp_attachment_pages_enabled', 0 );
     224        public function test_canonical_redirects_to_plain_permalinks() {
     225                // Identical for every case in this method; set once to avoid rewrite churn.
     226                $this->set_permalink_structure( '' );
     227
     228                foreach ( $this->get_canonical_redirects_to_plain_permalinks_cases() as $case ) {
     229                        $post_key                = $case[0];
     230                        $user_role               = $case[1];
     231                        $requested               = $case[2];
     232                        $expected                = $case[3];
     233                        $enable_attachment_pages = array_key_exists( 4, $case ) ? $case[4] : true;
     234
     235                        update_option( 'wp_attachment_pages_enabled', $enable_attachment_pages ? 1 : 0 );
     236                        wp_set_current_user( self::$users[ $user_role ] );
     237
     238                        $post = self::$posts[ $post_key ];
     239                        clean_post_cache( $post->ID );
     240
     241                        $requested = str_replace( '%ID%', (string) $post->ID, $requested );
     242                        $expected  = str_replace( '%ID%', (string) $post->ID, $expected );
     243
     244                        try {
     245                                $this->assertCanonical( $requested, $expected );
     246                        } catch ( \PHPUnit\Framework\ExpectationFailedException $e ) {
     247                                $this->fail(
     248                                        sprintf(
     249                                                'Canonical mismatch for post_key=%s user_role=%s attachment_pages=%s requested=%s expected=%s. %s',
     250                                                $post_key,
     251                                                $user_role,
     252                                                $enable_attachment_pages ? '1' : '0',
     253                                                $requested,
     254                                                $expected,
     255                                                $e->getMessage()
     256                                        )
     257                                );
     258                        }
    231259                }
    232 
    233                 wp_set_current_user( self::$users[ $user_role ] );
    234                 $this->set_permalink_structure( '' );
    235                 $post = self::$posts[ $post_key ];
    236                 clean_post_cache( $post->ID );
    237 
    238                 /*
    239                  * The dataProvider runs before the fixures are set up, therefore the
    240                  * post object IDs are placeholders that needs to be replaced.
    241                  */
    242                 $requested = str_replace( '%ID%', $post->ID, $requested );
    243                 $expected  = str_replace( '%ID%', $post->ID, $expected );
    244 
    245                 $this->assertCanonical( $requested, $expected );
    246260        }
    247261
    248262        /**
    249          * Data provider for test_canonical_redirects_to_plain_permalinks.
     263         * Cases for test_canonical_redirects_to_plain_permalinks.
    250264         *
    251265         * @return array[]
    252266         */
    253         public function data_canonical_redirects_to_plain_permalinks() {
     267        private function get_canonical_redirects_to_plain_permalinks_cases() {
    254268                $data              = array();
    255269                $all_user_list     = array( 'anon', 'subscriber', 'content_author', 'editor' );
    … …  
    841855         * Test canonical redirect does not reveal private slugs.
    842856         *
     857         * Asserts every pretty-permalink case inside one test to avoid repeated
     858         * PHPUnit setup/teardown while preserving each case's mutable state reset
     859         * (attachment pages option, current user) and all expected URLs.
     860         *
    843861         * @ticket 5272
    844          * @dataProvider data_canonical_redirects_to_pretty_permalinks
    845          *
    846          * @param string $post_key  Post key used for creating fixtures.
    847          * @param string $user_role User role.
    848          * @param string $requested Requested URL.
    849          * @param string $expected  Expected URL.
    850          * @param string $enable_attachment_pages Whether to enable attachment pages. Default true.
     862         * @ticket 66074
    851863         */
    852         public function test_canonical_redirects_to_pretty_permalinks( $post_key, $user_role, $requested, $expected, $enable_attachment_pages = true ) {
    853                 if ( $enable_attachment_pages ) {
    854                         update_option( 'wp_attachment_pages_enabled', 1 );
    855                 } else {
    856                         update_option( 'wp_attachment_pages_enabled', 0 );
     864        public function test_canonical_redirects_to_pretty_permalinks() {
     865                // Identical for every case in this method; set once to avoid rewrite churn.
     866                $this->set_permalink_structure( '/%postname%/' );
     867
     868                foreach ( $this->get_canonical_redirects_to_pretty_permalinks_cases() as $case ) {
     869                        $post_key                = $case[0];
     870                        $user_role               = $case[1];
     871                        $requested               = $case[2];
     872                        $expected                = $case[3];
     873                        $enable_attachment_pages = array_key_exists( 4, $case ) ? $case[4] : true;
     874
     875                        update_option( 'wp_attachment_pages_enabled', $enable_attachment_pages ? 1 : 0 );
     876                        wp_set_current_user( self::$users[ $user_role ] );
     877
     878                        $post = self::$posts[ $post_key ];
     879                        clean_post_cache( $post->ID );
     880
     881                        $requested = str_replace( '%ID%', (string) $post->ID, $requested );
     882                        $expected  = str_replace( '%ID%', (string) $post->ID, $expected );
     883
     884                        try {
     885                                $this->assertCanonical( $requested, $expected );
     886                        } catch ( \PHPUnit\Framework\ExpectationFailedException $e ) {
     887                                $this->fail(
     888                                        sprintf(
     889                                                'Canonical mismatch for post_key=%s user_role=%s attachment_pages=%s requested=%s expected=%s. %s',
     890                                                $post_key,
     891                                                $user_role,
     892                                                $enable_attachment_pages ? '1' : '0',
     893                                                $requested,
     894                                                $expected,
     895                                                $e->getMessage()
     896                                        )
     897                                );
     898                        }
    857899                }
    858 
    859                 wp_set_current_user( self::$users[ $user_role ] );
    860                 $this->set_permalink_structure( '/%postname%/' );
    861                 $post = self::$posts[ $post_key ];
    862                 clean_post_cache( $post->ID );
    863 
    864                 /*
    865                  * The dataProvider runs before the fixures are set up, therefore the
    866                  * post object IDs are placeholders that needs to be replaced.
    867                  */
    868                 $requested = str_replace( '%ID%', $post->ID, $requested );
    869                 $expected  = str_replace( '%ID%', $post->ID, $expected );
    870 
    871                 $this->assertCanonical( $requested, $expected );
    872900        }
    873901
    874902        /**
    875          * Data provider for test_canonical_redirects_to_pretty_permalinks.
     903         * Cases for test_canonical_redirects_to_pretty_permalinks.
    876904         *
    877905         * @return array[] Array of arguments for tests {
    … …  
    882910         * }
    883911         */
    884         public function data_canonical_redirects_to_pretty_permalinks() {
     912        private function get_canonical_redirects_to_pretty_permalinks_cases() {
    885913                $data              = array();
    886914                $all_user_list     = array( 'anon', 'subscriber', 'content_author', 'editor' );
Note: See TracChangeset for help on using the changeset viewer.