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/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.