Make WordPress Core


Ignore:
Timestamp:
08/04/2022 06:28:33 PM (4 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.