Make WordPress Core


Ignore:
Timestamp:
10/09/2014 02:31:35 AM (9 years ago)
Author:
boonebgorges
Message:

Improve 'alias_of' handling in wp_insert_term() and wp_update_term().

Using get_term_by() rather than direct SQL queries to fetch the alias term
fixes a number of issues:

  • Object cache for aliased term is properly cleared after update.
  • If the aliased term is in the object cache, it's served from there, saving a database query.
  • Duplicate 'edit_terms' and 'edited_terms' hooks can be removed.
  • Fix a PHP notice when the 'alias_of' term is not found.
  • Prevent the incorrect creation of a new term group for the primary term when the 'alias_of' term is not found.

Adds unit tests for 'alias_of' functionality in both functions.

Fixes #29848.

File:
1 edited

Legend:

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

    r29830 r29862  
    418418    }
    419419
     420    public function test_wp_insert_term_alias_of_no_term_group() {
     421        register_taxonomy( 'wptests_tax', 'post' );
     422        $t1 = $this->factory->term->create( array(
     423            'taxonomy' => 'wptests_tax',
     424        ) );
     425        $term_1 = get_term( $t1, 'wptests_tax' );
     426
     427        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     428            'alias_of' => $term_1->slug,
     429        ) );
     430        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     431
     432        $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' );
     433
     434        $term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     435        _unregister_taxonomy( 'wptests_tax' );
     436
     437        $this->assertSame( 0, $term_1->term_group );
     438        $this->assertNotEmpty( $created_term->term_group );
     439        $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
     440    }
     441
     442    public function test_wp_insert_term_alias_of_existing_term_group() {
     443        register_taxonomy( 'wptests_tax', 'post' );
     444        $t1 = $this->factory->term->create( array(
     445            'taxonomy' => 'wptests_tax',
     446        ) );
     447        $term_1 = get_term( $t1, 'wptests_tax' );
     448
     449        $t2 = $this->factory->term->create( array(
     450            'taxonomy' => 'wptests_tax',
     451            'alias_of' => $term_1->slug,
     452        ) );
     453        $term_2 = get_term( $t2, 'wptests_tax' );
     454
     455        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     456            'alias_of' => $term_2->slug,
     457        ) );
     458        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     459        _unregister_taxonomy( 'wptests_tax' );
     460
     461        $this->assertNotEmpty( $created_term->term_group );
     462        $this->assertSame( $created_term->term_group, $term_2->term_group );
     463    }
     464
     465    public function test_wp_insert_term_alias_of_nonexistent_term() {
     466        register_taxonomy( 'wptests_tax', 'post' );
     467        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     468            'alias_of' => 'foo',
     469        ) );
     470        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     471        _unregister_taxonomy( 'wptests_tax' );
     472
     473        $this->assertSame( 0, $created_term->term_group );
     474    }
     475
    420476    public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
    421477        register_taxonomy( 'foo', 'post', array() );
     
    538594    }
    539595
     596    public function test_wp_update_term_alias_of_no_term_group() {
     597        register_taxonomy( 'wptests_tax', 'post' );
     598        $t1 = $this->factory->term->create( array(
     599            'taxonomy' => 'wptests_tax',
     600        ) );
     601        $term_1 = get_term( $t1, 'wptests_tax' );
     602
     603        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     604        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     605            'alias_of' => $term_1->slug,
     606        ) );
     607        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     608
     609        $updated_term_1 = get_term( $t1, 'wptests_tax' );
     610        _unregister_taxonomy( 'wptests_tax' );
     611
     612        $this->assertSame( 0, $term_1->term_group );
     613        $this->assertNotEmpty( $created_term->term_group );
     614        $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
     615    }
     616
     617    public function test_wp_update_term_alias_of_existing_term_group() {
     618        register_taxonomy( 'wptests_tax', 'post' );
     619        $t1 = $this->factory->term->create( array(
     620            'taxonomy' => 'wptests_tax',
     621        ) );
     622        $term_1 = get_term( $t1, 'wptests_tax' );
     623
     624        $t2 = $this->factory->term->create( array(
     625            'taxonomy' => 'wptests_tax',
     626            'alias_of' => $term_1->slug,
     627        ) );
     628        $term_2 = get_term( $t2, 'wptests_tax' );
     629
     630        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     631        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     632            'alias_of' => $term_2->slug,
     633        ) );
     634        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     635        _unregister_taxonomy( 'wptests_tax' );
     636
     637        $this->assertNotEmpty( $created_term->term_group );
     638        $this->assertSame( $created_term->term_group, $term_2->term_group );
     639    }
     640
     641    public function test_wp_update_term_alias_of_nonexistent_term() {
     642        register_taxonomy( 'wptests_tax', 'post' );
     643        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     644        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     645            'alias_of' => 'bar',
     646        ) );
     647        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     648        _unregister_taxonomy( 'wptests_tax' );
     649
     650        $this->assertSame( 0, $created_term->term_group );
     651    }
    540652    /**
    541653     * @ticket 5381
Note: See TracChangeset for help on using the changeset viewer.