Make WordPress Core


Ignore:
Timestamp:
10/03/2014 01:30:22 PM (10 years ago)
Author:
boonebgorges
Message:

Improve unit test coverage for wp_insert_term().

See #5809, #22023.

File:
1 edited

Legend:

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

    r29798 r29830  
    245245    }
    246246
     247    public function test_wp_insert_term_taxonomy_does_not_exist() {
     248        $found = wp_insert_term( 'foo', 'bar' );
     249
     250        $this->assertTrue( is_wp_error( $found ) );
     251        $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
     252    }
     253
     254    public function test_wp_insert_term_pre_insert_term_filter_returns_wp_error() {
     255        add_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
     256        $found = wp_insert_term( 'foo', 'post_tag' );
     257        remove_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
     258
     259        $this->assertTrue( is_wp_error( $found ) );
     260        $this->assertSame( 'custom_error', $found->get_error_code() );
     261    }
     262
     263    public function test_wp_insert_term_term_0() {
     264        $found = wp_insert_term( 0, 'post_tag' );
     265
     266        $this->assertTrue( is_wp_error( $found ) );
     267        $this->assertSame( 'invalid_term_id', $found->get_error_code() );
     268    }
     269
     270    public function test_wp_insert_term_term_trims_to_empty_string() {
     271        $found = wp_insert_term( '  ', 'post_tag' );
     272
     273        $this->assertTrue( is_wp_error( $found ) );
     274        $this->assertSame( 'empty_term_name', $found->get_error_code() );
     275    }
     276
     277    public function test_wp_insert_term_parent_does_not_exist() {
     278        $found = wp_insert_term( 'foo', 'post_tag', array(
     279            'parent' => 999999,
     280        ) );
     281
     282        $this->assertTrue( is_wp_error( $found ) );
     283        $this->assertSame( 'missing_parent', $found->get_error_code() );
     284    }
     285
     286    public function test_wp_insert_term_unslash_name() {
     287        register_taxonomy( 'wptests_tax', 'post' );
     288        $found = wp_insert_term( 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy', 'wptests_tax' );
     289
     290        $term = get_term( $found['term_id'], 'wptests_tax' );
     291        _unregister_taxonomy( 'wptests_tax' );
     292
     293        $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name );
     294    }
     295
     296    public function test_wp_insert_term_unslash_description() {
     297        register_taxonomy( 'wptests_tax', 'post' );
     298        $found = wp_insert_term( 'Quality', 'wptests_tax', array(
     299            'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',
     300        ) );
     301
     302        $term = get_term( $found['term_id'], 'wptests_tax' );
     303        _unregister_taxonomy( 'wptests_tax' );
     304
     305        $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description );
     306    }
     307
     308    public function test_wp_insert_term_parent_string() {
     309        register_taxonomy( 'wptests_tax', 'post' );
     310        $found = wp_insert_term( 'Quality', 'wptests_tax', array(
     311            'parent' => 'foo1',
     312        ) );
     313
     314        $term = get_term( $found['term_id'], 'wptests_tax' );
     315        _unregister_taxonomy( 'wptests_tax' );
     316
     317        // 'foo1' is cast to 0 in sanitize_term().
     318        $this->assertSame( 0, $term->parent );
     319    }
     320
     321    public function test_wp_insert_term_slug_empty_string() {
     322        register_taxonomy( 'wptests_tax', 'post' );
     323        $found = wp_insert_term( 'Quality', 'wptests_tax', array(
     324            'slug' => '',
     325        ) );
     326
     327        $term = get_term( $found['term_id'], 'wptests_tax' );
     328        _unregister_taxonomy( 'wptests_tax' );
     329
     330        $this->assertSame( 'quality', $term->slug );
     331
     332    }
     333
     334    public function test_wp_insert_term_slug_whitespace_string() {
     335        register_taxonomy( 'wptests_tax', 'post' );
     336        $found = wp_insert_term( 'Quality', 'wptests_tax', array(
     337            'slug' => '  ',
     338        ) );
     339
     340        $term = get_term( $found['term_id'], 'wptests_tax' );
     341        _unregister_taxonomy( 'wptests_tax' );
     342
     343        $this->assertSame( 'quality', $term->slug );
     344    }
     345
     346    public function test_wp_insert_term_slug_0() {
     347        register_taxonomy( 'wptests_tax', 'post' );
     348        $found = wp_insert_term( 'Quality', 'wptests_tax', array(
     349            'slug' => 0,
     350        ) );
     351
     352        $term = get_term( $found['term_id'], 'wptests_tax' );
     353        _unregister_taxonomy( 'wptests_tax' );
     354
     355        $this->assertSame( 'quality', $term->slug );
     356    }
     357
     358    /**
     359     * @ticket 17689
     360     */
     361    public function test_wp_insert_term_duplicate_name() {
     362        $term = $this->factory->tag->create_and_get( array( 'name' => 'Bozo' ) );
     363        $this->assertFalse( is_wp_error( $term ) );
     364        $this->assertTrue( empty( $term->errors ) );
     365
     366        // Test existing term name with unique slug
     367        $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );
     368        $this->assertFalse( is_wp_error( $term1 ) );
     369        $this->assertTrue( empty($term1->errors ) );
     370
     371        // Test an existing term name
     372        $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) );
     373        $this->assertTrue( is_wp_error( $term2 ) );
     374        $this->assertNotEmpty( $term2->errors );
     375
     376        // Test named terms ending in special characters
     377        $term3 = $this->factory->tag->create( array( 'name' => 'T$' ) );
     378        $term4 = $this->factory->tag->create( array( 'name' => 'T$$' ) );
     379        $term5 = $this->factory->tag->create( array( 'name' => 'T$$$' ) );
     380        $term6 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
     381        $term7 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
     382        $this->assertTrue( is_wp_error( $term7 ) );
     383        $this->assertNotEmpty( $term7->errors );
     384        $this->assertEquals( $term6, $term7->error_data['term_exists'] );
     385
     386        $terms = array_map( 'get_tag', array( $term3, $term4, $term5, $term6 ) );
     387        $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
     388
     389        // Test named terms with only special characters
     390        $term8 = $this->factory->tag->create( array( 'name' => '$' ) );
     391        $term9 = $this->factory->tag->create( array( 'name' => '$$' ) );
     392        $term10 = $this->factory->tag->create( array( 'name' => '$$$' ) );
     393        $term11 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
     394        $term12 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
     395        $this->assertTrue( is_wp_error( $term12 ) );
     396        $this->assertNotEmpty( $term12->errors );
     397        $this->assertEquals( $term11, $term12->error_data['term_exists'] );
     398
     399        $terms = array_map( 'get_tag', array( $term8, $term9, $term10, $term11 ) );
     400        $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
     401
     402        $term13 = $this->factory->tag->create( array( 'name' => 'A' ) );
     403        $this->assertFalse( is_wp_error( $term13 ) );
     404        $term14 = $this->factory->tag->create( array( 'name' => 'A' ) );
     405        $this->assertTrue( is_wp_error( $term14 ) );
     406        $term15 = $this->factory->tag->create( array( 'name' => 'A+', 'slug' => 'a' ) );
     407        $this->assertFalse( is_wp_error( $term15 ) );
     408        $term16 = $this->factory->tag->create( array( 'name' => 'A+' ) );
     409        $this->assertTrue( is_wp_error( $term16 ) );
     410        $term17 = $this->factory->tag->create( array( 'name' => 'A++' ) );
     411        $this->assertFalse( is_wp_error( $term17 ) );
     412        $term18 = $this->factory->tag->create( array( 'name' => 'A-', 'slug' => 'a' ) );
     413        $this->assertFalse( is_wp_error( $term18 ) );
     414        $term19 = $this->factory->tag->create( array( 'name' => 'A-' ) );
     415        $this->assertTrue( is_wp_error( $term19 ) );
     416        $term20 = $this->factory->tag->create( array( 'name' => 'A--' ) );
     417        $this->assertFalse( is_wp_error( $term20 ) );
     418    }
     419
    247420    public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
    248421        register_taxonomy( 'foo', 'post', array() );
     
    314487        $this->assertTrue( is_wp_error( $found ) );
    315488        $this->assertEquals( $existing_term, $found->get_error_data() );
     489    }
     490
     491    public function test_wp_insert_term_should_return_term_id_and_term_taxonomy_id() {
     492        register_taxonomy( 'wptests_tax', 'post' );
     493        $found = wp_insert_term( 'foo', 'wptests_tax' );
     494
     495        $term_by_id = get_term( $found['term_id'], 'wptests_tax' );
     496        $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' );
     497        $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' );
     498
     499        _unregister_taxonomy( 'wptests_tax' );
     500
     501        $this->assertInternalType( 'array', $found );
     502        $this->assertNotEmpty( $found['term_id'] );
     503        $this->assertNotEmpty( $found['term_taxonomy_id'] );
     504        $this->assertNotEmpty( $term_by_id );
     505        $this->assertEquals( $term_by_id, $term_by_slug );
     506        $this->assertEquals( $term_by_id, $term_by_ttid );
     507    }
     508
     509    public function test_wp_insert_term_should_clean_term_cache() {
     510        register_taxonomy( 'wptests_tax', 'post', array(
     511            'hierarchical' => true,
     512        ) );
     513
     514        $t = $this->factory->term->create( array(
     515            'taxonomy' => 'wptests_tax',
     516        ) );
     517
     518        /**
     519         * It doesn't appear that WordPress itself ever sets these
     520         * caches, but we should ensure that they're being cleared for
     521         * compatibility with third-party addons. Prime the caches
     522         * manually.
     523         */
     524        wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' );
     525        wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' );
     526
     527        $found = wp_insert_term( 'foo', 'wptests_tax', array(
     528            'parent' => $t,
     529        ) );
     530        _unregister_taxonomy( 'wptests_tax' );
     531
     532        $this->assertSame( false, wp_cache_get( 'all_ids', 'wptests_tax' ) );
     533        $this->assertSame( false, wp_cache_get( 'get', 'wptests_tax' ) );
     534
     535        $cached_children = get_option( 'wptests_tax_children' );
     536        $this->assertNotEmpty( $cached_children[ $t ] );
     537        $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ] ) );
    316538    }
    317539
     
    9461168        $this->assertWPError( $cat_id2 );
    9471169    }
     1170
     1171    /** Helpers **********************************************************/
     1172
     1173    public function _pre_insert_term_callback() {
     1174        return new WP_Error( 'custom_error' );
     1175    }
    9481176}
Note: See TracChangeset for help on using the changeset viewer.