Make WordPress Core

Changeset 55924


Ignore:
Timestamp:
06/15/2023 03:23:25 AM (2 years ago)
Author:
peterwilsoncc
Message:

Tests/Build tools: Various term related test improvements.

Modifies the tests for get_tag_link(), get_term() and get_term_field() to:

  • use shared fixtures as possible
  • improves variable names
  • add @covers annotation as required

Props peterwilsoncc, costdev.
See #57841.

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

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTagLink.php

    r46586 r55924  
    66 */
    77class Tests_Term_GetTagLink extends WP_UnitTestCase {
    8     public function test_success() {
    9         $t = self::factory()->term->create(
     8    /**
     9     * Tag ID.
     10     *
     11     * @var int
     12     */
     13    public static $tag_id;
     14
     15    /**
     16     * Test taxonomy term ID.
     17     *
     18     * @var int
     19     */
     20    public static $term_id;
     21
     22    /**
     23     * Set up shared fixtures.
     24     *
     25     * @param WP_UnitTest_Factory $factory
     26     */
     27    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     28        self::$tag_id = $factory->term->create(
    1029            array(
    1130                'taxonomy' => 'post_tag',
    12                 'slug'     => 'term-slug',
     31                'slug'     => 'test-tag',
    1332            )
    1433        );
    1534
    16         $found    = get_tag_link( $t );
    17         $expected = home_url( '?tag=term-slug' );
     35        register_taxonomy( 'wptests_tax', 'post' );
     36        self::$term_id = self::factory()->term->create(
     37            array(
     38                'taxonomy' => 'wptests_tax',
     39                'slug'     => 'test-term',
     40            )
     41        );
     42    }
     43
     44    /**
     45     * Set up the test fixture.
     46     */
     47    public function set_up() {
     48        parent::set_up();
     49        // Required as taxonomies are reset between tests.
     50        register_taxonomy( 'wptests_tax', 'post' );
     51    }
     52
     53    public function test_success() {
     54        $tag_id = self::$tag_id;
     55
     56        $found    = get_tag_link( $tag_id );
     57        $expected = home_url( '?tag=test-tag' );
    1858
    1959        $this->assertSame( $expected, $found );
     
    2464     */
    2565    public function test_should_return_link_for_term_from_another_taxonomy_on_primed_cache() {
    26         register_taxonomy( 'wptests_tax', 'post' );
     66        $term_id = self::$term_id;
    2767
    28         $t = self::factory()->term->create(
    29             array(
    30                 'taxonomy' => 'wptests_tax',
    31                 'slug'     => 'test-term',
    32             )
    33         );
     68        $term = get_term( $term_id );
    3469
    35         $term = get_term( $t );
    36 
    37         $found    = get_tag_link( $t );
     70        $found    = get_tag_link( $term_id );
    3871        $expected = home_url( '?wptests_tax=test-term' );
    3972
     
    4578     */
    4679    public function test_should_return_link_for_term_from_another_taxonomy_on_empty_cache() {
    47         register_taxonomy( 'wptests_tax', 'post' );
     80        $term_id = self::$term_id;
    4881
    49         $t = self::factory()->term->create(
    50             array(
    51                 'taxonomy' => 'wptests_tax',
    52                 'slug'     => 'test-term',
    53             )
    54         );
     82        clean_term_cache( $term_id );
    5583
    56         clean_term_cache( $t );
    57 
    58         $found    = get_tag_link( $t );
     84        $found    = get_tag_link( $term_id );
    5985        $expected = home_url( '?wptests_tax=test-term' );
    6086
  • trunk/tests/phpunit/tests/term/getTerm.php

    r55745 r55924  
    33/**
    44 * @group taxonomy
     5 *
     6 * @covers ::get_term
    57 */
    68class Tests_Term_GetTerm extends WP_UnitTestCase {
     9    /**
     10     * Shared terms.
     11     *
     12     * @var array[]
     13     */
     14    public static $shared_terms = array();
     15
     16    /**
     17     * Test taxonomy term object.
     18     *
     19     * @var WP_Term
     20     */
     21    public static $term;
     22
     23    /**
     24     * Set up shared fixtures.
     25     *
     26     * @param WP_UnitTest_Factory $factory
     27     */
     28    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     29        register_taxonomy( 'wptests_tax', 'post' );
     30        self::$shared_terms = self::generate_shared_terms();
     31
     32        self::$term = $factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );
     33    }
     34
     35    /**
     36     * Set up the test fixtures.
     37     */
    738    public function set_up() {
    839        parent::set_up();
     40        // Required as taxonomies are reset between tests.
    941        register_taxonomy( 'wptests_tax', 'post' );
     42        register_taxonomy( 'wptests_tax_2', 'post' );
    1043    }
    1144
     
    1548     * @return array Array of term_id/old_term_id/term_taxonomy_id triplets.
    1649     */
    17     protected function generate_shared_terms() {
     50    protected static function generate_shared_terms() {
    1851        global $wpdb;
    1952
    2053        register_taxonomy( 'wptests_tax_2', 'post' );
    2154
    22         $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
    23         $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     55        $term_1 = wp_insert_term( 'Foo', 'wptests_tax' );
     56        $term_2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
    2457
    2558        // Manually modify because shared terms shouldn't naturally occur.
    2659        $wpdb->update(
    2760            $wpdb->term_taxonomy,
    28             array( 'term_id' => $t1['term_id'] ),
    29             array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     61            array( 'term_id' => $term_1['term_id'] ),
     62            array( 'term_taxonomy_id' => $term_2['term_taxonomy_id'] ),
    3063            array( '%d' ),
    3164            array( '%d' )
    3265        );
    3366
    34         clean_term_cache( $t1['term_id'] );
     67        clean_term_cache( $term_1['term_id'] );
    3568
    3669        return array(
    3770            array(
    38                 'term_id'          => $t1['term_id'],
    39                 'old_term_id'      => $t1['term_id'],
    40                 'term_taxonomy_id' => $t1['term_taxonomy_id'],
     71                'term_id'          => $term_1['term_id'],
     72                'old_term_id'      => $term_1['term_id'],
     73                'term_taxonomy_id' => $term_1['term_taxonomy_id'],
    4174            ),
    4275            array(
    43                 'term_id'          => $t1['term_id'],
    44                 'old_term_id'      => $t2['term_id'],
    45                 'term_taxonomy_id' => $t2['term_taxonomy_id'],
     76                'term_id'          => $term_1['term_id'],
     77                'old_term_id'      => $term_2['term_id'],
     78                'term_taxonomy_id' => $term_2['term_taxonomy_id'],
    4679            ),
    4780        );
     
    6194
    6295    public function test_passing_term_object_should_skip_database_query_when_filter_property_is_empty() {
    63         $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );
     96        $term = self::$term;
    6497        clean_term_cache( $term->term_id, 'wptests_tax' );
    6598
     
    81114
    82115    public function test_cache_should_be_populated_by_successful_fetch() {
    83         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    84         clean_term_cache( $t, 'wptests_tax' );
     116        $term_id = self::$term->term_id;
     117        clean_term_cache( $term_id, 'wptests_tax' );
    85118
    86119        // Prime cache.
    87         $term_a      = get_term( $t, 'wptests_tax' );
     120        $term_a      = get_term( $term_id, 'wptests_tax' );
    88121        $num_queries = get_num_queries();
    89122
    90123        // Second call shouldn't require a database query.
    91         $term_b = get_term( $t, 'wptests_tax' );
     124        $term_b = get_term( $term_id, 'wptests_tax' );
    92125        $this->assertSame( $num_queries, get_num_queries() );
    93126        $this->assertEquals( $term_a, $term_b );
     
    95128
    96129    public function test_output_object() {
    97         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    98         $this->assertIsObject( get_term( $t, 'wptests_tax', OBJECT ) );
     130        $term_id = self::$term->term_id;
     131        $this->assertIsObject( get_term( $term_id, 'wptests_tax', OBJECT ) );
    99132    }
    100133
    101134    public function test_output_array_a() {
    102         $t    = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    103         $term = get_term( $t, 'wptests_tax', ARRAY_A );
     135        $term_id = self::$term->term_id;
     136        $term    = get_term( $term_id, 'wptests_tax', ARRAY_A );
    104137        $this->assertIsArray( $term );
    105138        $this->assertArrayHasKey( 'term_id', $term );
     
    107140
    108141    public function test_output_array_n() {
    109         $t    = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    110         $term = get_term( $t, 'wptests_tax', ARRAY_N );
     142        $term_id = self::$term->term_id;
     143        $term    = get_term( $term_id, 'wptests_tax', ARRAY_N );
    111144        $this->assertIsArray( $term );
    112145        $this->assertArrayNotHasKey( 'term_id', $term );
     
    117150
    118151    public function test_output_should_fall_back_to_object_for_invalid_input() {
    119         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    120         $this->assertIsObject( get_term( $t, 'wptests_tax', 'foo' ) );
     152        $term_id = self::$term->term_id;
     153        $this->assertIsObject( get_term( $term_id, 'wptests_tax', 'foo' ) );
    121154    }
    122155
     
    128161        global $wpdb;
    129162
    130         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     163        $term_id = self::$term->term_id;
    131164
    132165        // Get raw data from the database.
    133         $term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $t ) );
     166        $term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $term_id ) );
    134167
    135168        $contexts = array( 'raw', 'edit', 'db', 'display', 'rss', 'attribute', 'js' );
     
    151184     */
    152185    public function test_should_return_null_when_provided_taxonomy_does_not_match_actual_term_taxonomy() {
    153         $term_id = self::factory()->term->create( array( 'taxonomy' => 'post_tag' ) );
     186        $term_id = self::$term->term_id;
    154187        $this->assertNull( get_term( $term_id, 'category' ) );
    155188    }
     
    159192     */
    160193    public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_is_specified() {
    161         $terms = $this->generate_shared_terms();
     194        $terms = self::$shared_terms;
    162195
    163196        $found = get_term( $terms[0]['term_id'] );
     
    170203     */
    171204    public function test_should_return_term_when_term_is_shared_and_correct_taxonomy_is_specified() {
    172         $terms = $this->generate_shared_terms();
     205        $terms = self::$shared_terms;
    173206
    174207        $found = get_term( $terms[0]['term_id'], 'wptests_tax' );
     
    182215     */
    183216    public function test_should_return_null_when_term_is_shared_and_incorrect_taxonomy_is_specified() {
    184         $terms = $this->generate_shared_terms();
     217        $terms = self::$shared_terms;
    185218
    186219        $found = get_term( $terms[0]['term_id'], 'post_tag' );
     
    193226     */
    194227    public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() {
    195         $terms = $this->generate_shared_terms();
     228        $terms = self::$shared_terms;
    196229
    197230        // Prime cache for 'wptests_tax'.
     
    212245     */
    213246    public function test_should_return_error_when_only_matching_term_is_in_an_invalid_taxonomy() {
    214         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     247        $term_id = self::$term->term_id;
    215248
    216249        _unregister_taxonomy( 'wptests_tax' );
    217250
    218         $found = get_term( $t );
     251        $found = get_term( $term_id );
    219252        $this->assertWPError( $found );
    220253        $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
     
    225258     */
    226259    public function test_term_should_be_returned_when_id_is_shared_only_with_invalid_taxonomies() {
    227         $terms = $this->generate_shared_terms();
     260        $terms = self::$shared_terms;
    228261
    229262        _unregister_taxonomy( 'wptests_tax' );
  • trunk/tests/phpunit/tests/term/getTermField.php

    r52389 r55924  
    33/**
    44 * @group taxonomy
     5 *
     6 * @covers ::get_term_field
    57 */
    68class Tests_Term_getTermField extends WP_UnitTestCase {
    79
    8     public $taxonomy = 'wptests_tax';
     10    public static $taxonomy = 'wptests_tax';
     11
     12    public static $term;
     13
     14    /**
     15     * Set up shared fixtures.
     16     *
     17     * @param WP_UnitTest_Factory $factory
     18     */
     19    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     20        register_taxonomy( self::$taxonomy, 'post' );
     21        self::$term = $factory->term->create_and_get(
     22            array(
     23                'taxonomy'    => self::$taxonomy,
     24                'description' => wpautop( 'Test term description' ),
     25            )
     26        );
     27    }
    928
    1029    public function set_up() {
    1130        parent::set_up();
    12 
    13         register_taxonomy( $this->taxonomy, 'post' );
     31        // Required as taxonomies are reset between tests.
     32        register_taxonomy( self::$taxonomy, 'post' );
    1433    }
    1534
     
    1837     */
    1938    public function test_get_term_field_should_not_return_error_for_empty_taxonomy() {
    20         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     39        $term = self::$term;
    2140
    2241        $found = get_term_field( 'taxonomy', $term->term_id, '' );
    2342        $this->assertNotWPError( $found );
    24         $this->assertSame( $this->taxonomy, $found );
     43        $this->assertSame( self::$taxonomy, $found );
    2544    }
    2645
     
    2948     */
    3049    public function test_get_term_field_supplying_a_taxonomy() {
    31         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     50        $term = self::$term;
    3251
    3352        $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy );
    34         $this->assertSame( $this->taxonomy, $found );
     53        $this->assertSame( self::$taxonomy, $found );
    3554    }
    3655
     
    3958     */
    4059    public function test_get_term_field_supplying_no_taxonomy() {
    41         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     60        $term = self::$term;
    4261
    4362        $found = get_term_field( 'taxonomy', $term->term_id );
    44         $this->assertSame( $this->taxonomy, $found );
     63        $this->assertSame( self::$taxonomy, $found );
    4564    }
    4665
     
    4968     */
    5069    public function test_get_term_field_should_accept_a_WP_Term_id_or_object() {
    51         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     70        $term = self::$term;
    5271
    5372        $this->assertInstanceOf( 'WP_Term', $term );
     
    6180     */
    6281    public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() {
    63         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     82        $term = self::$term;
    6483
    6584        $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' );
     
    7291     */
    7392    public function test_get_term_field_invalid_term_should_return_WP_Error() {
    74         $found = get_term_field( 'taxonomy', 0, $this->taxonomy );
     93        $found = get_term_field( 'taxonomy', 0, self::$taxonomy );
    7594
    7695        $this->assertWPError( $found );
     
    84103
    85104    public function test_get_term_field_term_id() {
    86         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     105        $term = self::$term;
    87106
    88107        $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
     
    97116            array(
    98117                'name'     => $name,
    99                 'taxonomy' => $this->taxonomy,
     118                'taxonomy' => self::$taxonomy,
    100119            )
    101120        );
     
    111130        $term = self::factory()->term->create_and_get(
    112131            array(
    113                 'taxonomy' => $this->taxonomy,
     132                'taxonomy' => self::$taxonomy,
    114133                'slug'     => $slug,
    115134            )
     
    126145        $term = self::factory()->term->create_and_get(
    127146            array(
    128                 'taxonomy' => $this->taxonomy,
     147                'taxonomy' => self::$taxonomy,
    129148                'name'     => $name,
    130149            )
     
    139158        $term = self::factory()->term->create_and_get(
    140159            array(
    141                 'taxonomy' => $this->taxonomy,
     160                'taxonomy' => self::$taxonomy,
    142161            )
    143162        );
     
    149168
    150169    public function test_get_term_field_taxonomy() {
    151         $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
    152 
    153         $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) );
    154         $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) );
    155         $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
     170        $term = self::$term;
     171
     172        $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term ) );
     173        $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->data ) );
     174        $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
    156175    }
    157176
    158177    public function test_get_term_field_description() {
    159         $desc = wpautop( 'baz' );
    160 
    161         $term = self::factory()->term->create_and_get(
    162             array(
    163                 'taxonomy'    => $this->taxonomy,
    164                 'description' => $desc,
    165             )
    166         );
    167 
    168         $this->assertSame( $desc, get_term_field( 'description', $term ) );
    169         $this->assertSame( $desc, get_term_field( 'description', $term->data ) );
    170         $this->assertSame( $desc, get_term_field( 'description', $term->term_id ) );
     178        $description = wpautop( 'Test term description' );
     179
     180        $term = self::$term;
     181
     182        $this->assertSame( $description, get_term_field( 'description', $term ) );
     183        $this->assertSame( $description, get_term_field( 'description', $term->data ) );
     184        $this->assertSame( $description, get_term_field( 'description', $term->term_id ) );
    171185    }
    172186
    173187    public function test_get_term_field_parent() {
    174         $parent = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     188        $parent = self::$term;
    175189        $term   = self::factory()->term->create_and_get(
    176190            array(
    177                 'taxonomy' => $this->taxonomy,
     191                'taxonomy' => self::$taxonomy,
    178192                'parent'   => $parent->term_id,
    179193            )
Note: See TracChangeset for help on using the changeset viewer.