Make WordPress Core


Ignore:
Timestamp:
09/29/2016 10:35:32 PM (7 years ago)
Author:
ocean90
Message:

Taxonomy: Use WP_Term_Query in get_term_by().

WP_Term_Query already supports querying terms by 'slug', 'name', and 'term_taxonomy_id'. Its additional arguments allow us to generate nearly the same SQL queries as before.
This change has one yuge benefit: the term queries are now cached.

Add tests to increase coverage of get_term_by().

Props spacedmonkey, boonebgorges, johnjamesjacoby, pento, ocean90.
Fixes #21760.

File:
1 edited

Legend:

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

    r35242 r38677  
    55 */
    66class Tests_Term_GetTermBy extends WP_UnitTestCase {
     7
     8    function test_get_term_by_slug() {
     9        $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     10        $term2 = get_term_by( 'slug', 'foo', 'category' );
     11        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     12    }
     13
     14    function test_get_term_by_name() {
     15        $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     16        $term2 = get_term_by( 'name', 'Foo', 'category' );
     17        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     18    }
     19
     20    function test_get_term_by_id() {
     21        $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     22        $term2 = get_term_by( 'id', $term1['term_id'], 'category' );
     23        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     24    }
     25
     26    /**
     27     * 'term_id' is an alias of 'id'.
     28     */
     29    function test_get_term_by_term_id() {
     30        $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     31        $term2 = get_term_by( 'term_id', $term1['term_id'], 'category' );
     32        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     33    }
     34
    735    /**
    836     * @ticket 21651
     
    1240        $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
    1341        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     42    }
     43
     44    function test_get_term_by_unknown() {
     45        wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     46        $term2 = get_term_by( 'unknown', 'foo', 'category' );
     47        $this->assertFalse( $term2 );
    1448    }
    1549
     
    88122        $this->assertSame( $num_queries, $wpdb->num_queries );
    89123    }
     124
     125    /**
     126     * @ticket 21760
     127     */
     128    public function test_should_unslash_name() {
     129        register_taxonomy( 'wptests_tax', 'post' );
     130        $term_name = 'Foo " \o/';
     131        $term_name_slashed = wp_slash( $term_name );
     132        $t = self::factory()->term->create( array(
     133            'taxonomy' => 'wptests_tax',
     134            'name' => $term_name_slashed,
     135        ) );
     136
     137        $found = get_term_by( 'name', $term_name_slashed, 'wptests_tax' );
     138
     139        $this->assertTrue( $found instanceof WP_Term );
     140        $this->assertSame( $t, $found->term_id );
     141        $this->assertSame( $term_name, $found->name );
     142    }
     143
     144    /**
     145     * @ticket 21760
     146     */
     147    public function test_should_sanitize_slug() {
     148        register_taxonomy( 'wptests_tax', 'post' );
     149        $t1 = self::factory()->term->create( array(
     150            'taxonomy' => 'wptests_tax',
     151            'slug' => 'foo-foo',
     152        ) );
     153
     154        // Whitespace should get replaced by a '-'.
     155        $found1 = get_term_by( 'slug', 'foo foo', 'wptests_tax' );
     156
     157        $this->assertTrue( $found1 instanceof WP_Term );
     158        $this->assertSame( $t1, $found1->term_id );
     159
     160        $t2 = self::factory()->term->create( array(
     161            'taxonomy' => 'wptests_tax',
     162            'slug' => '%e4%bb%aa%e8%a1%a8%e7%9b%98',
     163        ) );
     164
     165        // Slug should get urlencoded.
     166        $found2 = get_term_by( 'slug', '仪表盘', 'wptests_tax' );
     167
     168        $this->assertTrue( $found2 instanceof WP_Term );
     169        $this->assertSame( $t2, $found2->term_id );
     170    }
     171
     172    /**
     173     * @ticket 21760
     174     */
     175    public function test_query_should_not_contain_order_by_clause() {
     176        global $wpdb;
     177
     178        $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
     179        $found = get_term_by( 'name', 'burrito', 'post_tag' );
     180        $this->assertSame( $term_id, $found->term_id );
     181        $this->assertNotContains( 'ORDER BY', $wpdb->last_query );
     182    }
     183
     184    /**
     185     * @ticket 21760
     186     */
     187    public function test_query_should_contain_limit_clause() {
     188        global $wpdb;
     189
     190        $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
     191        $found = get_term_by( 'name', 'burrito', 'post_tag' );
     192        $this->assertSame( $term_id, $found->term_id );
     193        $this->assertContains( 'LIMIT 1', $wpdb->last_query );
     194    }
    90195}
Note: See TracChangeset for help on using the changeset viewer.