Make WordPress Core


Ignore:
Timestamp:
09/02/2022 01:14:29 AM (2 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/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     *
Note: See TracChangeset for help on using the changeset viewer.