Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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' );
Note: See TracChangeset for help on using the changeset viewer.