Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.