Make WordPress Core

Changeset 53836


Ignore:
Timestamp:
08/04/2022 06:28:33 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Combine test classes for get_edit_term_link() tests.

There were two sets of tests for the function:

  • One in the link directory, based on the link-template.php file name.
  • One in the term directory, based on the component name.

To avoid confusion and make it easier to decide where new tests should go in the future, the existing tests are now combined in the former location.

Includes:

  • Setting the current user in ::set_up() instead of each individual test method.
  • Changing the custom taxonomy name to wptests_tax for consistency with other tests.
  • Moving ::register_custom_taxonomy() and ::get_term() helpers to the beginning of the class.

Follow-up to [32954], [36646], [52180], [52255], [53833].

See #55652.

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

Legend:

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

    r53833 r53836  
    1313        self::register_custom_taxonomy();
    1414
    15         $taxonomies = array( 'category', 'post_tag', 'custom_taxonomy' );
     15        $taxonomies = array( 'category', 'post_tag', 'wptests_tax' );
    1616        foreach ( $taxonomies as $taxonomy ) {
    1717            self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) );
     
    2424    public function set_up() {
    2525        parent::set_up();
     26        wp_set_current_user( self::$user_ids['admin'] );
    2627        self::register_custom_taxonomy();
     28    }
     29
     30    /**
     31     * Helper to register a custom taxonomy for use in tests.
     32     *
     33     * @since 5.9.0
     34     */
     35    private static function register_custom_taxonomy() {
     36        register_taxonomy( 'wptests_tax', 'post' );
     37    }
     38
     39    /**
     40     * Helper to get the term for the given taxonomy.
     41     *
     42     * @since 5.9.0
     43     *
     44     * @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.
     47     */
     48    private function get_term( $taxonomy, $use_id ) {
     49        $term = self::$terms[ $taxonomy ];
     50        if ( $use_id ) {
     51            $term = $term->term_id;
     52        }
     53
     54        return $term;
    2755    }
    2856
     
    3765     */
    3866    public function test_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
    39         wp_set_current_user( self::$user_ids['admin'] );
    4067        $term = $this->get_term( $taxonomy, $use_id );
    4168
     
    73100     */
    74101    public function test_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
    75         wp_set_current_user( self::$user_ids['admin'] );
    76102        $term = $this->get_term( $taxonomy, $use_id );
    77103
     
    97123     */
    98124    public function test_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
    99         wp_set_current_user( self::$user_ids['admin'] );
    100125        $term = $this->get_term( $taxonomy, $use_id );
    101126
     
    140165            ),
    141166            'a custom taxonomy passing term_id'     => array(
    142                 'taxonomy' => 'custom_taxonomy',
     167                'taxonomy' => 'wptests_tax',
    143168                'use_id'   => true,
    144                 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post',
     169                'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post',
    145170            ),
    146171            'a custom taxonomy passing term object' => array(
    147                 'taxonomy' => 'custom_taxonomy',
     172                'taxonomy' => 'wptests_tax',
    148173                'use_id'   => false,
    149                 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post',
     174                'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post',
    150175            ),
    151176        );
    152177    }
    153 
    154     /**
    155      * Helper to register a custom taxonomy for use in tests.
    156      *
    157      * @since 5.9.0
    158      */
    159     private static function register_custom_taxonomy() {
    160         register_taxonomy( 'custom_taxonomy', 'post' );
    161     }
    162 
    163     /**
    164      * Helper to get the term for the given taxonomy.
    165      *
    166      * @since 5.9.0
    167      *
    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      * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
    171      */
    172     private function get_term( $taxonomy, $use_id ) {
    173         $term = self::$terms[ $taxonomy ];
    174         if ( $use_id ) {
    175             $term = $term->term_id;
    176         }
    177 
    178         return $term;
    179     }
    180178}
  • trunk/tests/phpunit/tests/link/getEditTermLink.php

    r53833 r53836  
    1313        self::register_custom_taxonomy();
    1414
    15         $taxonomies = array( 'category', 'post_tag', 'custom_taxonomy' );
     15        $taxonomies = array( 'category', 'post_tag', 'wptests_tax' );
    1616        foreach ( $taxonomies as $taxonomy ) {
    1717            self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) );
     
    2424    public function set_up() {
    2525        parent::set_up();
     26        wp_set_current_user( self::$user_ids['admin'] );
    2627        self::register_custom_taxonomy();
    2728    }
    2829
    2930    /**
     31     * Helper to register a custom taxonomy for use in tests.
     32     *
     33     * @since 5.9.0
     34     */
     35    private static function register_custom_taxonomy() {
     36        register_taxonomy( 'wptests_tax', 'post' );
     37    }
     38
     39    /**
     40     * Helper to get the term for the given taxonomy.
     41     *
     42     * @since 5.9.0
     43     *
     44     * @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.
     47     */
     48    private function get_term( $taxonomy, $use_id ) {
     49        $term = self::$terms[ $taxonomy ];
     50        if ( $use_id ) {
     51            $term = $term->term_id;
     52        }
     53
     54        return $term;
     55    }
     56
     57    public function test_get_edit_term_link_default() {
     58        $term1 = self::factory()->term->create(
     59            array(
     60                'taxonomy' => 'wptests_tax',
     61                'name'     => 'foo',
     62            )
     63        );
     64
     65        $actual   = get_edit_term_link( $term1, 'wptests_tax' );
     66        $expected = 'http://' . WP_TESTS_DOMAIN . '/wp-admin/term.php?taxonomy=wptests_tax&tag_ID=' . $term1 . '&post_type=post';
     67        $this->assertSame( $expected, $actual );
     68    }
     69
     70    /**
     71     * @ticket 32786
     72     */
     73    public function test_get_edit_term_link_invalid_id() {
     74        $term1 = self::factory()->term->create(
     75            array(
     76                'taxonomy' => 'wptests_tax',
     77                'name'     => 'foo',
     78            )
     79        );
     80
     81        $actual = get_edit_term_link( 12345, 'wptests_tax' );
     82        $this->assertNull( $actual );
     83    }
     84
     85    /**
     86     * @ticket 32786
     87     */
     88    public function test_get_edit_term_link_empty_id() {
     89        $actual = get_edit_term_link( '', 'wptests_tax' );
     90        $this->assertNull( $actual );
     91    }
     92
     93    /**
     94     * @ticket 32786
     95     */
     96    public function test_get_edit_term_link_bad_tax() {
     97        $actual = get_edit_term_link( '', 'bad_tax' );
     98        $this->assertNull( $actual );
     99    }
     100
     101    /**
     102     * @ticket 35922
     103     */
     104    public function test_taxonomy_should_not_be_required() {
     105        $t = self::factory()->term->create(
     106            array(
     107                'taxonomy' => 'wptests_tax',
     108                'name'     => 'foo',
     109            )
     110        );
     111
     112        $actual = get_edit_term_link( $t );
     113        $this->assertNotNull( $actual );
     114    }
     115
     116    /**
     117     * @ticket 35922
     118     */
     119    public function test_cap_check_should_use_correct_taxonomy_when_taxonomy_is_not_specified() {
     120        register_taxonomy(
     121            'wptests_tax_subscriber',
     122            'post',
     123            array(
     124                'capabilities' => array(
     125                    'edit_terms' => 'read',
     126                ),
     127            )
     128        );
     129
     130        $t = self::factory()->term->create(
     131            array(
     132                'taxonomy' => 'wptests_tax_subscriber',
     133                'name'     => 'foo',
     134            )
     135        );
     136
     137        wp_set_current_user( self::$user_ids['subscriber'] );
     138
     139        $actual = get_edit_term_link( $t );
     140        $this->assertNotNull( $actual );
     141    }
     142
     143    /**
    30144     * @dataProvider data_get_edit_term_link
    31145     *
     
    37151     */
    38152    public function test_get_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
    39         wp_set_current_user( self::$user_ids['admin'] );
    40153        $term = $this->get_term( $taxonomy, $use_id );
    41154
     
    73186     */
    74187    public function test_get_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
    75         wp_set_current_user( self::$user_ids['admin'] );
    76188        $term = $this->get_term( $taxonomy, $use_id );
    77189
     
    97209     */
    98210    public function test_get_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
    99         wp_set_current_user( self::$user_ids['admin'] );
    100211        $term = $this->get_term( $taxonomy, $use_id );
    101212
     
    140251            ),
    141252            'a custom taxonomy passing term_id'     => array(
    142                 'taxonomy' => 'custom_taxonomy',
     253                'taxonomy' => 'wptests_tax',
    143254                'use_id'   => true,
    144                 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post',
     255                'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post',
    145256            ),
    146257            'a custom taxonomy passing term object' => array(
    147                 'taxonomy' => 'custom_taxonomy',
     258                'taxonomy' => 'wptests_tax',
    148259                'use_id'   => false,
    149                 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post',
    150             ),
    151         );
    152     }
    153 
    154     /**
    155      * Helper to register a custom taxonomy for use in tests.
    156      *
    157      * @since 5.9.0
    158      */
    159     private static function register_custom_taxonomy() {
    160         register_taxonomy( 'custom_taxonomy', 'post' );
    161     }
    162 
    163     /**
    164      * Helper to get the term for the given taxonomy.
    165      *
    166      * @since 5.9.0
    167      *
    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      * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
    171      */
    172     private function get_term( $taxonomy, $use_id ) {
    173         $term = self::$terms[ $taxonomy ];
    174         if ( $use_id ) {
    175             $term = $term->term_id;
    176         }
    177 
    178         return $term;
     260                'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post',
     261            ),
     262        );
    179263    }
    180264}
  • trunk/tests/phpunit/tests/term/getTermLink.php

    r53833 r53836  
    2121        parent::set_up();
    2222        self::register_custom_taxonomy();
     23    }
     24
     25    /**
     26     * Helper to register a custom taxonomy for use in tests.
     27     *
     28     * @since 5.9.0
     29     */
     30    private static function register_custom_taxonomy() {
     31        register_taxonomy( 'wptests_tax', 'post' );
     32    }
     33
     34    /**
     35     * Helper to get the term for the given taxonomy.
     36     *
     37     * @since 5.9.0
     38     *
     39     * @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.
     42     */
     43    private function get_term( $taxonomy, $use_id ) {
     44        $term = self::$terms[ $taxonomy ];
     45        if ( $use_id ) {
     46            $term = $term->term_id;
     47        }
     48
     49        return $term;
    2350    }
    2451
     
    321348        );
    322349    }
    323 
    324     /**
    325      * Helper to register a custom taxonomy for use in tests.
    326      *
    327      * @since 5.9.0
    328      */
    329     private static function register_custom_taxonomy() {
    330         register_taxonomy( 'wptests_tax', 'post' );
    331     }
    332 
    333     /**
    334      * Helper to get the term for the given taxonomy.
    335      *
    336      * @since 5.9.0
    337      *
    338      * @param string $taxonomy Taxonomy being tested (used for index of term keys).
    339      * @param bool   $use_id   When true, pass term ID. Else, pass term object.
    340      * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
    341      */
    342     private function get_term( $taxonomy, $use_id ) {
    343         $term = self::$terms[ $taxonomy ];
    344         if ( $use_id ) {
    345             $term = $term->term_id;
    346         }
    347 
    348         return $term;
    349     }
    350350}
Note: See TracChangeset for help on using the changeset viewer.