Make WordPress Core

Changeset 54061


Ignore:
Timestamp:
09/02/2022 01:14:29 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Simplify and correct get_term_link() and get_edit_term_link() tests:

  • Some assertions were unnecessarily duplicated. They aim to test the function behavior both when passing a term ID and term object, however that is already handled via the $use_id parameter of the get_term() helper in the same test class. The data providers already supply test cases both with a term ID and term object, so there is no need for a second assertion or a whole second test method with a term object.
  • One get_term_feed_link() test was unnecessarily skipped half of the time, when term object was passed instead of term ID. Instead, it can use a dedicated data provider and avoid skipping.

Includes:

  • Using more descriptive test method names to clarify the intention of the tests.
  • Some documentation updates for clarity.

Follow-up to [52180], [52255], [52258], [52305], [53833], [53836].

See #55652.

Location:
trunk/tests/phpunit/tests
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/link/editTermLink.php

    r53836 r54061  
    4343         *
    4444         * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    45          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    46          * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
     45         * @param bool   $use_id   Whether to return term ID or term object.
     46         * @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
    4747         */
    4848        private function get_term( $taxonomy, $use_id ) {
     
    6060         * @ticket 50225
    6161         *
    62          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    63          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    64          * @param string $expected Expected URL within admin of edit link.
     62         * @param string $taxonomy Taxonomy being tested.
     63         * @param bool   $use_id   Whether to pass term ID or term object to `edit_term_link()`.
     64         * @param string $expected Expected part of admin URL for the edit link.
    6565         */
    66         public function test_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
     66        public function test_edit_term_link_should_return_the_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
    6767                $term = $this->get_term( $taxonomy, $use_id );
    6868
     
    7272
    7373                $this->assertStringContainsString( $expected, edit_term_link( '', '', '', $term, false ) );
    74                 $this->assertStringContainsString( $expected, edit_term_link( '', '', '', get_term( $term, $taxonomy ), false ) );
    7574        }
    7675
     
    8079         * @ticket 50225
    8180         *
    82          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    83          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
     81         * @param string $taxonomy Taxonomy being tested.
     82         * @param bool   $use_id   Whether to pass term ID or term object to `edit_term_link()`.
    8483         */
    85         public function test_edit_term_link_for_denied_user( $taxonomy, $use_id ) {
     84        public function test_edit_term_link_should_return_null_for_denied_user( $taxonomy, $use_id ) {
    8685                wp_set_current_user( self::$user_ids['subscriber'] );
    8786                $term = $this->get_term( $taxonomy, $use_id );
    8887
    8988                $this->assertNull( edit_term_link( '', '', '', $term, false ) );
    90                 $this->assertNull( edit_term_link( '', '', '', get_term( $term, $taxonomy ), false ) );
    9189        }
    9290
     
    9694         * @ticket 50225
    9795         *
    98          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    99          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
     96         * @param string $taxonomy Taxonomy being tested.
     97         * @param bool   $use_id   Whether to pass term ID or term object to `edit_term_link()`.
    10098         */
    101         public function test_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
     99        public function test_edit_term_link_filter_should_receive_term_id( $taxonomy, $use_id ) {
    102100                $term = $this->get_term( $taxonomy, $use_id );
    103101
     
    112110
    113111                edit_term_link( '', '', '', $term, false );
    114         }
    115 
    116         /**
    117          * @dataProvider data_edit_term_link
    118          *
    119          * @ticket 50225
    120          *
    121          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    122          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    123          */
    124         public function test_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
    125                 $term = $this->get_term( $taxonomy, $use_id );
    126 
    127                 add_filter(
    128                         'edit_term_link',
    129                         function( $location, $term ) {
    130                                 $this->assertIsInt( $term );
    131                         },
    132                         10,
    133                         2
    134                 );
    135 
    136                 edit_term_link( '', '', '', get_term( $term, $taxonomy ), false );
    137112        }
    138113
  • trunk/tests/phpunit/tests/link/getEditTermLink.php

    r53836 r54061  
    4343         *
    4444         * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    45          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    46          * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
     45         * @param bool   $use_id   Whether to return term ID or term object.
     46         * @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
    4747         */
    4848        private function get_term( $taxonomy, $use_id ) {
     
    146146         * @ticket 50225
    147147         *
    148          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    149          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    150          * @param string $expected Expected URL within admin of edit link.
    151          */
    152         public function test_get_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
     148         * @param string $taxonomy Taxonomy being tested.
     149         * @param bool   $use_id   Whether to pass term ID or term object to `get_edit_term_link()`.
     150         * @param string $expected Expected part of admin URL for the edit link.
     151         */
     152        public function test_get_edit_term_link_should_return_the_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
    153153                $term = $this->get_term( $taxonomy, $use_id );
    154154
     
    158158
    159159                $this->assertSame( $expected, get_edit_term_link( $term, $taxonomy ) );
    160                 $this->assertSame( $expected, get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy ) );
    161160        }
    162161
     
    166165         * @ticket 50225
    167166         *
    168          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    169          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    170          */
    171         public function test_get_edit_term_link_for_denied_user( $taxonomy, $use_id ) {
     167         * @param string $taxonomy Taxonomy being tested.
     168         * @param bool   $use_id   Whether to pass term ID or term object to `get_edit_term_link()`.
     169         */
     170        public function test_get_edit_term_link_should_return_null_for_denied_user( $taxonomy, $use_id ) {
    172171                wp_set_current_user( self::$user_ids['subscriber'] );
    173172                $term = $this->get_term( $taxonomy, $use_id );
    174173
    175174                $this->assertNull( get_edit_term_link( $term, $taxonomy ) );
    176                 $this->assertNull( get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy ) );
    177175        }
    178176
     
    182180         * @ticket 50225
    183181         *
    184          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    185          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    186          */
    187         public function test_get_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
     182         * @param string $taxonomy Taxonomy being tested.
     183         * @param bool   $use_id   Whether to pass term ID or term object to `get_edit_term_link()`.
     184         */
     185        public function test_get_edit_term_link_filter_should_receive_term_id( $taxonomy, $use_id ) {
    188186                $term = $this->get_term( $taxonomy, $use_id );
    189187
     
    201199
    202200        /**
    203          * @dataProvider data_get_edit_term_link
    204          *
    205          * @ticket 50225
    206          *
    207          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    208          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    209          */
    210         public function test_get_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
    211                 $term = $this->get_term( $taxonomy, $use_id );
    212 
    213                 add_filter(
    214                         'get_edit_term_link',
    215                         function( $location, $term ) {
    216                                 $this->assertIsInt( $term );
    217                         },
    218                         10,
    219                         2
    220                 );
    221 
    222                 get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy );
    223         }
    224 
    225         /**
    226201         * Data provider.
    227202         *
  • trunk/tests/phpunit/tests/term/getTermLink.php

    r53836 r54061  
    3838         *
    3939         * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    40          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    41          * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
     40         * @param bool   $use_id   Whether to return term ID or term object.
     41         * @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
    4242         */
    4343        private function get_term( $taxonomy, $use_id ) {
     
    248248
    249249        /**
    250          * @dataProvider data_get_term_link
     250         * @dataProvider data_term_link_filter_should_receive_term_object
    251251         *
    252252         * @ticket 50225
    253253         *
    254          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    255          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    256          */
    257         public function test_get_term_link_filter_is_object_by_term_id( $taxonomy, $use_id ) {
     254         * @param string $taxonomy Taxonomy being tested.
     255         * @param bool   $use_id   Whether to pass term ID or term object to `get_term_link()`.
     256         */
     257        public function test_term_link_filter_should_receive_term_object( $taxonomy, $use_id ) {
    258258                $term = $this->get_term( $taxonomy, $use_id );
    259259
     
    271271
    272272        /**
    273          * @dataProvider data_get_term_link
    274          *
    275          * @ticket 50225
    276          *
    277          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    278          * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    279          */
    280         public function test_get_term_link_filter_is_object_by_term_object( $taxonomy, $use_id ) {
    281                 $term = $this->get_term( $taxonomy, $use_id );
    282 
    283                 add_filter(
    284                         'term_link',
    285                         function( $location, $term ) {
    286                                 $this->assertInstanceOf( 'WP_Term', $term );
    287                         },
    288                         10,
    289                         2
    290                 );
    291 
    292                 get_term_link( get_term( $term, $taxonomy ), $taxonomy );
    293         }
    294 
    295         /**
    296          * @dataProvider data_get_term_link
    297          *
    298          * @ticket 50225
    299          *
    300          * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    301          * @param bool   $use_id   When true, pass term ID. Else, skip the test.
    302          */
    303         public function test_get_term_feed_link_backward_compatibility( $taxonomy, $use_id ) {
    304                 if ( $use_id ) {
    305                         $term = $this->get_term( $taxonomy, $use_id );
    306 
    307                         $term_feed_link = get_term_feed_link( $term, $taxonomy );
    308                         $this->assertIsString( $term_feed_link );
    309 
    310                         $term_feed_link = get_term_feed_link( $term, '' );
    311                         $this->assertIsString( $term_feed_link );
    312                 } else {
    313                         $this->markTestSkipped( 'This test requires to pass an ID to get_term_feed_link()' );
    314                 }
    315         }
    316 
    317         /**
    318273         * Data provider.
    319274         *
    320275         * @return array
    321276         */
    322         public function data_get_term_link() {
     277        public function data_term_link_filter_should_receive_term_object() {
    323278                return array(
    324279                        'category passing term_id'              => array(
     
    348303                );
    349304        }
     305
     306        /**
     307         * @dataProvider data_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed
     308         *
     309         * @ticket 50225
     310         *
     311         * @param string $taxonomy Taxonomy being tested.
     312         */
     313        public function test_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed( $taxonomy ) {
     314                $term = $this->get_term( $taxonomy, true );
     315
     316                $term_feed_link = get_term_feed_link( $term, $taxonomy );
     317                $this->assertIsString( $term_feed_link );
     318
     319                $term_feed_link = get_term_feed_link( $term, '' );
     320                $this->assertIsString( $term_feed_link );
     321        }
     322
     323        /**
     324         * Data provider.
     325         *
     326         * @return array
     327         */
     328        public function data_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed() {
     329                $taxonomies = array( 'category', 'post_tag', 'wptests_tax' );
     330
     331                return $this->text_array_to_dataprovider( $taxonomies );
     332        }
    350333}
Note: See TracChangeset for help on using the changeset viewer.