WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/18/2015 01:27:15 PM (7 years ago)
Author:
boonebgorges
Message:

Move and tests to their own files.

File:
1 copied

Legend:

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

    r31811 r31812  
    44 * @group taxonomy
    55 */
    6 class Tests_Term extends WP_UnitTestCase {
    7     var $taxonomy = 'category';
    8 
    9     function setUp() {
    10         parent::setUp();
    11 
    12         _clean_term_filters();
    13         // insert one term into every post taxonomy
    14         // otherwise term_ids and term_taxonomy_ids might be identical, which could mask bugs
    15         $term = rand_str();
    16         foreach(get_object_taxonomies('post') as $tax)
    17             wp_insert_term( $term, $tax );
    18     }
    19 
    20     function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) {
    21         $this->assertInternalType( 'object', $deleted_term );
    22         $this->assertInternalType( 'int', $term );
    23         // Pesky string $this->assertInternalType( 'int', $tt_id );
    24         $this->assertEquals( $term, $deleted_term->term_id );
    25         $this->assertEquals( $taxonomy, $deleted_term->taxonomy );
    26         $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id );
    27     }
    28 
    29     function test_wp_insert_delete_term() {
    30         // a new unused term
    31         $term = rand_str();
    32         $this->assertNull( term_exists($term) );
    33 
    34         $initial_count = wp_count_terms( $this->taxonomy );
    35 
    36         $t = wp_insert_term( $term, $this->taxonomy );
    37         $this->assertInternalType( 'array', $t );
    38         $this->assertFalse( is_wp_error($t) );
    39         $this->assertTrue( $t['term_id'] > 0 );
    40         $this->assertTrue( $t['term_taxonomy_id'] > 0 );
    41         $this->assertEquals( $initial_count + 1, wp_count_terms($this->taxonomy) );
    42 
    43         // make sure the term exists
    44         $this->assertTrue( term_exists($term) > 0 );
    45         $this->assertTrue( term_exists($t['term_id']) > 0 );
    46 
    47         // now delete it
    48         add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
    49         $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
    50         remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
    51         $this->assertNull( term_exists($term) );
    52         $this->assertNull( term_exists($t['term_id']) );
    53         $this->assertEquals( $initial_count, wp_count_terms($this->taxonomy) );
    54     }
    55 
    56     public function test_wp_insert_term_taxonomy_does_not_exist() {
    57         $found = wp_insert_term( 'foo', 'bar' );
    58 
    59         $this->assertTrue( is_wp_error( $found ) );
    60         $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
    61     }
    62 
    63     public function test_wp_insert_term_pre_insert_term_filter_returns_wp_error() {
    64         add_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
    65         $found = wp_insert_term( 'foo', 'post_tag' );
    66         remove_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
    67 
    68         $this->assertTrue( is_wp_error( $found ) );
    69         $this->assertSame( 'custom_error', $found->get_error_code() );
    70     }
    71 
    72     public function test_wp_insert_term_term_0() {
    73         $found = wp_insert_term( 0, 'post_tag' );
    74 
    75         $this->assertTrue( is_wp_error( $found ) );
    76         $this->assertSame( 'invalid_term_id', $found->get_error_code() );
    77     }
    78 
    79     public function test_wp_insert_term_term_trims_to_empty_string() {
    80         $found = wp_insert_term( '  ', 'post_tag' );
    81 
    82         $this->assertTrue( is_wp_error( $found ) );
    83         $this->assertSame( 'empty_term_name', $found->get_error_code() );
    84     }
    85 
    86     public function test_wp_insert_term_parent_does_not_exist() {
    87         $found = wp_insert_term( 'foo', 'post_tag', array(
    88             'parent' => 999999,
    89         ) );
    90 
    91         $this->assertTrue( is_wp_error( $found ) );
    92         $this->assertSame( 'missing_parent', $found->get_error_code() );
    93     }
    94 
    95     public function test_wp_insert_term_unslash_name() {
    96         register_taxonomy( 'wptests_tax', 'post' );
    97         $found = wp_insert_term( 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy', 'wptests_tax' );
    98 
    99         $term = get_term( $found['term_id'], 'wptests_tax' );
    100         _unregister_taxonomy( 'wptests_tax' );
    101 
    102         $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name );
    103     }
    104 
    105     public function test_wp_insert_term_unslash_description() {
    106         register_taxonomy( 'wptests_tax', 'post' );
    107         $found = wp_insert_term( 'Quality', 'wptests_tax', array(
    108             'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',
    109         ) );
    110 
    111         $term = get_term( $found['term_id'], 'wptests_tax' );
    112         _unregister_taxonomy( 'wptests_tax' );
    113 
    114         $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description );
    115     }
    116 
    117     public function test_wp_insert_term_parent_string() {
    118         register_taxonomy( 'wptests_tax', 'post' );
    119         $found = wp_insert_term( 'Quality', 'wptests_tax', array(
    120             'parent' => 'foo1',
    121         ) );
    122 
    123         $term = get_term( $found['term_id'], 'wptests_tax' );
    124         _unregister_taxonomy( 'wptests_tax' );
    125 
    126         // 'foo1' is cast to 0 in sanitize_term().
    127         $this->assertSame( 0, $term->parent );
    128     }
    129 
    130     public function test_wp_insert_term_slug_empty_string() {
    131         register_taxonomy( 'wptests_tax', 'post' );
    132         $found = wp_insert_term( 'Quality', 'wptests_tax', array(
    133             'slug' => '',
    134         ) );
    135 
    136         $term = get_term( $found['term_id'], 'wptests_tax' );
    137         _unregister_taxonomy( 'wptests_tax' );
    138 
    139         $this->assertSame( 'quality', $term->slug );
    140 
    141     }
    142 
    143     public function test_wp_insert_term_slug_whitespace_string() {
    144         register_taxonomy( 'wptests_tax', 'post' );
    145         $found = wp_insert_term( 'Quality', 'wptests_tax', array(
    146             'slug' => '  ',
    147         ) );
    148 
    149         $term = get_term( $found['term_id'], 'wptests_tax' );
    150         _unregister_taxonomy( 'wptests_tax' );
    151 
    152         $this->assertSame( 'quality', $term->slug );
    153     }
    154 
    155     public function test_wp_insert_term_slug_0() {
    156         register_taxonomy( 'wptests_tax', 'post' );
    157         $found = wp_insert_term( 'Quality', 'wptests_tax', array(
    158             'slug' => 0,
    159         ) );
    160 
    161         $term = get_term( $found['term_id'], 'wptests_tax' );
    162         _unregister_taxonomy( 'wptests_tax' );
    163 
    164         $this->assertSame( 'quality', $term->slug );
    165     }
    166 
    167     /**
    168      * @ticket 17689
    169      */
    170     public function test_wp_insert_term_duplicate_name() {
    171         $term = $this->factory->tag->create_and_get( array( 'name' => 'Bozo' ) );
    172         $this->assertFalse( is_wp_error( $term ) );
    173         $this->assertTrue( empty( $term->errors ) );
    174 
    175         // Test existing term name with unique slug
    176         $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );
    177         $this->assertFalse( is_wp_error( $term1 ) );
    178 
    179         // Test an existing term name
    180         $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) );
    181         $this->assertTrue( is_wp_error( $term2 ) );
    182         $this->assertNotEmpty( $term2->errors );
    183 
    184         // Test named terms ending in special characters
    185         $term3 = $this->factory->tag->create( array( 'name' => 'T$' ) );
    186         $term4 = $this->factory->tag->create( array( 'name' => 'T$$' ) );
    187         $term5 = $this->factory->tag->create( array( 'name' => 'T$$$' ) );
    188         $term6 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
    189         $term7 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
    190         $this->assertTrue( is_wp_error( $term7 ) );
    191         $this->assertNotEmpty( $term7->errors );
    192         $this->assertEquals( $term6, $term7->error_data['term_exists'] );
    193 
    194         $terms = array_map( 'get_tag', array( $term3, $term4, $term5, $term6 ) );
    195         $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
    196 
    197         // Test named terms with only special characters
    198         $term8 = $this->factory->tag->create( array( 'name' => '$' ) );
    199         $term9 = $this->factory->tag->create( array( 'name' => '$$' ) );
    200         $term10 = $this->factory->tag->create( array( 'name' => '$$$' ) );
    201         $term11 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
    202         $term12 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
    203         $this->assertTrue( is_wp_error( $term12 ) );
    204         $this->assertNotEmpty( $term12->errors );
    205         $this->assertEquals( $term11, $term12->error_data['term_exists'] );
    206 
    207         $terms = array_map( 'get_tag', array( $term8, $term9, $term10, $term11 ) );
    208         $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
    209 
    210         $term13 = $this->factory->tag->create( array( 'name' => 'A' ) );
    211         $this->assertFalse( is_wp_error( $term13 ) );
    212         $term14 = $this->factory->tag->create( array( 'name' => 'A' ) );
    213         $this->assertTrue( is_wp_error( $term14 ) );
    214         $term15 = $this->factory->tag->create( array( 'name' => 'A+', 'slug' => 'a' ) );
    215         $this->assertFalse( is_wp_error( $term15 ) );
    216         $term16 = $this->factory->tag->create( array( 'name' => 'A+' ) );
    217         $this->assertTrue( is_wp_error( $term16 ) );
    218         $term17 = $this->factory->tag->create( array( 'name' => 'A++' ) );
    219         $this->assertFalse( is_wp_error( $term17 ) );
    220         $term18 = $this->factory->tag->create( array( 'name' => 'A-', 'slug' => 'a' ) );
    221         $this->assertFalse( is_wp_error( $term18 ) );
    222         $term19 = $this->factory->tag->create( array( 'name' => 'A-' ) );
    223         $this->assertTrue( is_wp_error( $term19 ) );
    224         $term20 = $this->factory->tag->create( array( 'name' => 'A--' ) );
    225         $this->assertFalse( is_wp_error( $term20 ) );
    226     }
    227 
    228     /**
    229      * @ticket 31328
    230      */
    231     public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_non_hierarchical_taxonomy() {
    232         register_taxonomy( 'wptests_tax', 'post' );
    233         $t1 = $this->factory->term->create( array(
    234             'name' => 'Foo',
    235             'slug' => 'foo',
    236             'taxonomy' => 'wptests_tax',
    237         ) );
    238 
    239         $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
    240             'slug' => 'foo',
    241         ) );
    242 
    243         $this->assertWPError( $t2 );
    244         $this->assertSame( 'term_exists', $t2->get_error_code() );
    245     }
    246 
    247     /**
    248      * @ticket 31328
    249      */
    250     public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_in_non_hierarchical_taxonomy() {
    251         register_taxonomy( 'wptests_tax', 'post' );
    252         $t1 = $this->factory->term->create( array(
    253             'name' => 'Foo',
    254             'slug' => 'foo',
    255             'taxonomy' => 'wptests_tax',
    256         ) );
    257 
    258         $t2 = $this->factory->term->create( array(
    259             'name' => 'Bar',
    260             'slug' => 'bar',
    261             'taxonomy' => 'wptests_tax',
    262         ) );
    263 
    264         $t3 = wp_insert_term( 'Foo', 'wptests_tax', array(
    265             'slug' => 'bar',
    266         ) );
    267 
    268         $this->assertWPError( $t3 );
    269         $this->assertSame( 'term_exists', $t3->get_error_code() );
    270     }
    271 
    272     /**
    273      * @ticket 31328
    274      */
    275     public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_non_hierarchical_taxonomy() {
    276         register_taxonomy( 'wptests_tax', 'post' );
    277         $t1 = $this->factory->term->create( array(
    278             'name' => 'Foo',
    279             'slug' => 'foo',
    280             'taxonomy' => 'wptests_tax',
    281         ) );
    282 
    283         $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
    284             'slug' => 'foo-unique',
    285         ) );
    286 
    287         $this->assertFalse( is_wp_error( $t2 ) );
    288 
    289         $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
    290         $this->assertSame( 'foo-unique', $t2_term->slug );
    291         $this->assertSame( 'Foo', $t2_term->name );
    292     }
    293 
    294     /**
    295      * @ticket 31328
    296      */
    297     public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_non_hierarchical_taxonomy() {
    298         register_taxonomy( 'wptests_tax', 'post' );
    299         $t1 = $this->factory->term->create( array(
    300             'name' => 'Foo',
    301             'slug' => 'foo',
    302             'taxonomy' => 'wptests_tax',
    303         ) );
    304 
    305         $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
    306 
    307         $this->assertWPError( $t2 );
    308         $this->assertSame( 'term_exists', $t2->get_error_code() );
    309     }
    310 
    311     /**
    312      * @ticket 31328
    313      */
    314     public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_hierarchical_taxonomy() {
    315         register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    316         $t1 = $this->factory->term->create( array(
    317             'name' => 'Foo',
    318             'slug' => 'foo',
    319             'taxonomy' => 'wptests_tax',
    320         ) );
    321 
    322         $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
    323             'slug' => 'foo',
    324         ) );
    325 
    326         $this->assertWPError( $t2 );
    327         $this->assertSame( 'term_exists', $t2->get_error_code() );
    328     }
    329 
    330     /**
    331      * @ticket 31328
    332      */
    333     public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_at_same_hierarchy_level_in_hierarchical_taxonomy() {
    334         register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    335         $t1 = $this->factory->term->create( array(
    336             'name' => 'Foo',
    337             'slug' => 'foo',
    338             'taxonomy' => 'wptests_tax',
    339         ) );
    340 
    341         $t2 = $this->factory->term->create( array(
    342             'name' => 'Bar',
    343             'slug' => 'bar',
    344             'taxonomy' => 'wptests_tax',
    345         ) );
    346 
    347         $t3 = wp_insert_term( 'Foo', 'wptests_tax', array(
    348             'slug' => 'bar',
    349         ) );
    350 
    351         $this->assertWPError( $t3 );
    352         $this->assertSame( 'term_exists', $t3->get_error_code() );
    353     }
    354 
    355     /**
    356      * @ticket 31328
    357      */
    358     public function test_wp_insert_term_should_allow_duplicate_names_when_slug_is_a_duplicate_of_a_term_at_different_hierarchy_level_in_hierarchical_taxonomy() {
    359         register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    360         $t1 = $this->factory->term->create( array(
    361             'name' => 'Foo',
    362             'slug' => 'foo',
    363             'taxonomy' => 'wptests_tax',
    364         ) );
    365 
    366         $t2 = $this->factory->term->create();
    367 
    368         $t3 = $this->factory->term->create( array(
    369             'name' => 'Bar',
    370             'slug' => 'bar',
    371             'parent' => $t2,
    372             'taxonomy' => 'wptests_tax',
    373         ) );
    374 
    375         $t4 = wp_insert_term( 'Foo', 'wptests_tax', array(
    376             'slug' => 'bar',
    377         ) );
    378 
    379         $this->assertFalse( is_wp_error( $t4 ) );
    380         $t4_term = get_term( $t4['term_id'], 'wptests_tax' );
    381 
    382         // `wp_unique_term_slug()` allows term creation but iterates the slug.
    383         $this->assertSame( 'bar-2', $t4_term->slug );
    384         $this->assertSame( 'Foo', $t4_term->name );
    385     }
    386 
    387     /**
    388      * @ticket 31328
    389      */
    390     public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_hierarchical_taxonomy() {
    391         register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    392         $t1 = $this->factory->term->create( array(
    393             'name' => 'Foo',
    394             'slug' => 'foo',
    395             'taxonomy' => 'wptests_tax',
    396         ) );
    397 
    398         $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
    399             'slug' => 'foo-unique',
    400         ) );
    401 
    402         $this->assertFalse( is_wp_error( $t2 ) );
    403 
    404         $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
    405         $this->assertSame( 'foo-unique', $t2_term->slug );
    406         $this->assertSame( 'Foo', $t2_term->name );
    407     }
    408 
    409     /**
    410      * @ticket 31328
    411      */
    412     public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_hierarchical_taxonomy() {
    413         register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    414         $t1 = $this->factory->term->create( array(
    415             'name' => 'Foo',
    416             'slug' => 'foo',
    417             'taxonomy' => 'wptests_tax',
    418         ) );
    419 
    420         $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
    421 
    422         $this->assertWPError( $t2 );
    423         $this->assertSame( 'term_exists', $t2->get_error_code() );
    424     }
    425     /**
    426      * @ticket 5809
    427      */
    428     public function test_wp_insert_term_duplicate_slug_same_taxonomy() {
    429         register_taxonomy( 'wptests_tax', 'post' );
    430         $t = $this->factory->term->create( array(
    431             'name' => 'Foo',
    432             'slug' => 'foo',
    433             'taxonomy' => 'wptests_tax',
    434         ) );
    435 
    436         $term = get_term( $t, 'wptests_tax' );
    437 
    438         $created = wp_insert_term( 'Foo 2', 'wptests_tax', array(
    439             'slug' => 'foo',
    440         ) );
    441 
    442         $created_term = get_term( $created['term_id'], 'wptests_tax' );
    443         $this->assertSame( 'foo-2', $created_term->slug );
    444 
    445         _unregister_taxonomy( 'wptests_tax', 'post' );
    446     }
    447 
    448     /**
    449      * @ticket 5809
    450      */
    451     public function test_wp_insert_term_duplicate_slug_different_taxonomy() {
    452         register_taxonomy( 'wptests_tax', 'post' );
    453         register_taxonomy( 'wptests_tax_2', 'post' );
    454         $t = $this->factory->term->create( array(
    455             'name' => 'Foo',
    456             'slug' => 'foo',
    457             'taxonomy' => 'wptests_tax',
    458         ) );
    459 
    460         $term = get_term( $t, 'wptests_tax' );
    461 
    462         $created = wp_insert_term( 'Foo 2', 'wptests_tax_2', array(
    463             'slug' => 'foo',
    464         ) );
    465 
    466         $this->assertFalse( is_wp_error( $created ) );
    467 
    468         $new_term = get_term( $created['term_id'], 'wptests_tax_2' );
    469 
    470         $this->assertSame( 'foo', $new_term->slug );
    471 
    472         _unregister_taxonomy( 'wptests_tax', 'post' );
    473     }
    474 
    475     /**
    476      * @ticket 5809
    477      */
    478     public function test_wp_insert_term_duplicate_slug_different_taxonomy_before_410_schema_change() {
    479 
    480         $db_version = get_option( 'db_version' );
    481         update_option( 'db_version', 30055 );
    482 
    483         register_taxonomy( 'wptests_tax', 'post' );
    484         register_taxonomy( 'wptests_tax_2', 'post' );
    485         $t = $this->factory->term->create( array(
    486             'name' => 'Foo',
    487             'slug' => 'foo',
    488             'taxonomy' => 'wptests_tax',
    489         ) );
    490 
    491         $term = get_term( $t, 'wptests_tax' );
    492 
    493         $created = wp_insert_term( 'Foo 2', 'wptests_tax_2', array(
    494             'slug' => 'foo',
    495         ) );
    496 
    497         $this->assertFalse( is_wp_error( $created ) );
    498 
    499         $new_term = get_term( $created['term_id'], 'wptests_tax_2' );
    500 
    501         /*
    502          * As of 4.1, we no longer create a shared term, but we also do not
    503          * allow for duplicate slugs.
    504          */
    505         $this->assertSame( 'foo-2', $new_term->slug );
    506         $this->assertNotEquals( $new_term->term_id, $term->term_id );
    507 
    508         _unregister_taxonomy( 'wptests_tax', 'post' );
    509         update_option( 'db_version', $db_version );
    510     }
    511 
    512     public function test_wp_insert_term_alias_of_no_term_group() {
    513         register_taxonomy( 'wptests_tax', 'post' );
    514         $t1 = $this->factory->term->create( array(
    515             'taxonomy' => 'wptests_tax',
    516         ) );
    517         $term_1 = get_term( $t1, 'wptests_tax' );
    518 
    519         $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
    520             'alias_of' => $term_1->slug,
    521         ) );
    522         $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
    523 
    524         $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' );
    525 
    526         $term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
    527         _unregister_taxonomy( 'wptests_tax' );
    528 
    529         $this->assertSame( 0, $term_1->term_group );
    530         $this->assertNotEmpty( $created_term->term_group );
    531         $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
    532     }
    533 
    534     public function test_wp_insert_term_alias_of_existing_term_group() {
    535         register_taxonomy( 'wptests_tax', 'post' );
    536         $t1 = $this->factory->term->create( array(
    537             'taxonomy' => 'wptests_tax',
    538         ) );
    539         $term_1 = get_term( $t1, 'wptests_tax' );
    540 
    541         $t2 = $this->factory->term->create( array(
    542             'taxonomy' => 'wptests_tax',
    543             'alias_of' => $term_1->slug,
    544         ) );
    545         $term_2 = get_term( $t2, 'wptests_tax' );
    546 
    547         $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
    548             'alias_of' => $term_2->slug,
    549         ) );
    550         $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
    551         _unregister_taxonomy( 'wptests_tax' );
    552 
    553         $this->assertNotEmpty( $created_term->term_group );
    554         $this->assertSame( $created_term->term_group, $term_2->term_group );
    555     }
    556 
    557     public function test_wp_insert_term_alias_of_nonexistent_term() {
    558         register_taxonomy( 'wptests_tax', 'post' );
    559         $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
    560             'alias_of' => 'foo',
    561         ) );
    562         $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
    563         _unregister_taxonomy( 'wptests_tax' );
    564 
    565         $this->assertSame( 0, $created_term->term_group );
    566     }
    567 
    568     /**
    569      * @ticket 5809
    570      */
    571     public function test_wp_insert_term_should_not_create_shared_term() {
    572         register_taxonomy( 'wptests_tax', 'post' );
    573         register_taxonomy( 'wptests_tax_2', 'post' );
    574 
    575         $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
    576         $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
    577 
    578         $this->assertNotEquals( $t1['term_id'], $t2['term_id'] );
    579     }
    580 
    581     public function test_wp_insert_term_should_return_term_id_and_term_taxonomy_id() {
    582         register_taxonomy( 'wptests_tax', 'post' );
    583         $found = wp_insert_term( 'foo', 'wptests_tax' );
    584 
    585         $term_by_id = get_term( $found['term_id'], 'wptests_tax' );
    586         $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' );
    587         $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' );
    588 
    589         _unregister_taxonomy( 'wptests_tax' );
    590 
    591         $this->assertInternalType( 'array', $found );
    592         $this->assertNotEmpty( $found['term_id'] );
    593         $this->assertNotEmpty( $found['term_taxonomy_id'] );
    594         $this->assertNotEmpty( $term_by_id );
    595         $this->assertEquals( $term_by_id, $term_by_slug );
    596         $this->assertEquals( $term_by_id, $term_by_ttid );
    597     }
    598 
    599     public function test_wp_insert_term_should_clean_term_cache() {
    600         register_taxonomy( 'wptests_tax', 'post', array(
    601             'hierarchical' => true,
    602         ) );
    603 
    604         $t = $this->factory->term->create( array(
    605             'taxonomy' => 'wptests_tax',
    606         ) );
    607 
    608         /**
    609          * It doesn't appear that WordPress itself ever sets these
    610          * caches, but we should ensure that they're being cleared for
    611          * compatibility with third-party addons. Prime the caches
    612          * manually.
    613          */
    614         wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' );
    615         wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' );
    616 
    617         $found = wp_insert_term( 'foo', 'wptests_tax', array(
    618             'parent' => $t,
    619         ) );
    620         _unregister_taxonomy( 'wptests_tax' );
    621 
    622         $this->assertSame( false, wp_cache_get( 'all_ids', 'wptests_tax' ) );
    623         $this->assertSame( false, wp_cache_get( 'get', 'wptests_tax' ) );
    624 
    625         $cached_children = get_option( 'wptests_tax_children' );
    626         $this->assertNotEmpty( $cached_children[ $t ] );
    627         $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ] ) );
    628     }
    629 
     6class Tests_Term_WpUpdateTerm extends WP_UnitTestCase {
    6307    public function test_wp_update_term_taxonomy_does_not_exist() {
    6318        $found = wp_update_term( 1, 'bar' );
     
    1209586
    1210587    /**
    1211      * @ticket 29911
    1212      */
    1213     public function test_wp_delete_term_should_invalidate_cache_for_child_terms() {
    1214         register_taxonomy( 'wptests_tax', 'post', array(
    1215             'hierarchical' => true,
    1216         ) );
    1217 
    1218         $parent = $this->factory->term->create( array(
    1219             'taxonomy' => 'wptests_tax',
    1220         ) );
    1221 
    1222         $child = $this->factory->term->create( array(
    1223             'taxonomy' => 'wptests_tax',
    1224             'parent' => $parent,
    1225             'slug' => 'foo',
    1226         ) );
    1227 
    1228         // Prime the cache.
    1229         $child_term = get_term( $child, 'wptests_tax' );
    1230         $this->assertSame( $parent, $child_term->parent );
    1231 
    1232         wp_delete_term( $parent, 'wptests_tax' );
    1233         $child_term = get_term( $child, 'wptests_tax' );
    1234         $this->assertSame( 0, $child_term->parent );
    1235     }
    1236 
    1237     /**
    1238      * @ticket 5381
    1239      */
    1240     function test_is_term_type() {
    1241         // insert a term
    1242         $term = rand_str();
    1243         $t = wp_insert_term( $term, $this->taxonomy );
    1244         $this->assertInternalType( 'array', $t );
    1245         $term_obj = get_term_by('name', $term, $this->taxonomy);
    1246         $this->assertEquals( $t['term_id'], term_exists($term_obj->slug) );
    1247 
    1248         // clean up
    1249         $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
    1250     }
    1251 
    1252     /**
    1253      * @ticket 21651
    1254      */
    1255     function test_get_term_by_tt_id() {
    1256         $term1 = wp_insert_term( 'Foo', 'category' );
    1257         $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
    1258         $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
    1259     }
    1260 
    1261     /**
    1262      * @ticket 15919
    1263      */
    1264     function test_wp_count_terms() {
    1265         $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
    1266         // the terms inserted in setUp aren't attached to any posts, so should return 0
    1267         // this previously returned 2
    1268         $this->assertEquals( 0, $count );
    1269     }
    1270 
    1271     /**
    1272      * @ticket 26570
    1273      */
    1274     function test_set_object_terms() {
    1275         $non_hier = rand_str( 10 );
    1276         $hier     = rand_str( 10 );
    1277 
    1278         // Register taxonomies
    1279         register_taxonomy( $non_hier, array() );
    1280         register_taxonomy( $hier, array( 'hierarchical' => true ) );
    1281 
    1282         // Create a post.
    1283         $post_id = $this->factory->post->create();
    1284 
    1285         /*
    1286          * Set a single term (non-hierarchical) by ID.
    1287          */
    1288         $tag = wp_insert_term( 'Foo', $non_hier );
    1289         $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
    1290 
    1291         wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
    1292         $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
    1293 
    1294         /*
    1295          * Set a single term (non-hierarchical) by slug.
    1296          */
    1297         $tag = wp_insert_term( 'Bar', $non_hier );
    1298         $tag = get_term( $tag['term_id'], $non_hier );
    1299 
    1300         $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) );
    1301 
    1302         wp_set_object_terms( $post_id, $tag->slug, $non_hier );
    1303         $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) );
    1304 
    1305         /*
    1306          * Set a single term (hierarchical) by ID.
    1307          */
    1308         $cat = wp_insert_term( 'Baz', $hier );
    1309         $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) );
    1310 
    1311         wp_set_object_terms( $post_id, $cat['term_id'], $hier );
    1312         $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) );
    1313 
    1314         /*
    1315          * Set a single term (hierarchical) by slug.
    1316          */
    1317         $cat = wp_insert_term( 'Qux', $hier );
    1318         $cat = get_term( $cat['term_id'], $hier );
    1319 
    1320         $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
    1321 
    1322         wp_set_object_terms( $post_id, $cat->slug, $hier );
    1323         $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
    1324 
    1325         /*
    1326          * Set an array of term IDs (non-hierarchical) by ID.
    1327          */
    1328         $tag1 = wp_insert_term( '_tag1', $non_hier );
    1329         $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
    1330 
    1331         $tag2 = wp_insert_term( '_tag2', $non_hier );
    1332         $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
    1333 
    1334         $tag3 = wp_insert_term( '_tag3', $non_hier );
    1335         $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
    1336 
    1337         wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
    1338         $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
    1339 
    1340         /*
    1341          * Set an array of term slugs (hierarchical) by slug.
    1342          */
    1343         $cat1 = wp_insert_term( '_cat1', $hier );
    1344         $cat1 = get_term( $cat1['term_id'], $hier );
    1345         $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
    1346 
    1347         $cat2 = wp_insert_term( '_cat2', $hier );
    1348         $cat2 = get_term( $cat2['term_id'], $hier );
    1349         $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
    1350 
    1351         $cat3 = wp_insert_term( '_cat3', $hier );
    1352         $cat3 = get_term( $cat3['term_id'], $hier );
    1353         $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
    1354 
    1355         wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
    1356         $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
    1357     }
    1358 
    1359     function test_set_object_terms_by_id() {
    1360         $ids = $this->factory->post->create_many(5);
    1361 
    1362         $terms = array();
    1363         for ($i=0; $i<3; $i++ ) {
    1364             $term = rand_str();
    1365             $result = wp_insert_term( $term, $this->taxonomy );
    1366             $this->assertInternalType( 'array', $result );
    1367             $term_id[$term] = $result['term_id'];
    1368         }
    1369 
    1370         foreach ($ids as $id) {
    1371                 $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
    1372                 // should return three term taxonomy ids
    1373                 $this->assertEquals( 3, count($tt) );
    1374         }
    1375 
    1376         // each term should be associated with every post
    1377         foreach ($term_id as $term=>$id) {
    1378             $actual = get_objects_in_term($id, $this->taxonomy);
    1379             $this->assertEquals( $ids, array_map('intval', $actual) );
    1380         }
    1381 
    1382         // each term should have a count of 5
    1383         foreach (array_keys($term_id) as $term) {
    1384             $t = get_term_by('name', $term, $this->taxonomy);
    1385             $this->assertEquals( 5, $t->count );
    1386         }
    1387     }
    1388 
    1389     function test_set_object_terms_by_name() {
    1390         $ids = $this->factory->post->create_many(5);
    1391 
    1392         $terms = array(
    1393                 rand_str(),
    1394                 rand_str(),
    1395                 rand_str());
    1396 
    1397         foreach ($ids as $id) {
    1398                 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
    1399                 // should return three term taxonomy ids
    1400                 $this->assertEquals( 3, count($tt) );
    1401                 // remember which term has which term_id
    1402                 for ($i=0; $i<3; $i++) {
    1403                     $term = get_term_by('name', $terms[$i], $this->taxonomy);
    1404                     $term_id[$terms[$i]] = intval($term->term_id);
    1405                 }
    1406         }
    1407 
    1408         // each term should be associated with every post
    1409         foreach ($term_id as $term=>$id) {
    1410             $actual = get_objects_in_term($id, $this->taxonomy);
    1411             $this->assertEquals( $ids, array_map('intval', $actual) );
    1412         }
    1413 
    1414         // each term should have a count of 5
    1415         foreach ($terms as $term) {
    1416             $t = get_term_by('name', $term, $this->taxonomy);
    1417             $this->assertEquals( 5, $t->count );
    1418         }
    1419     }
    1420 
    1421     function test_set_object_terms_invalid() {
    1422         $post_id = $this->factory->post->create();
    1423 
    1424         // bogus taxonomy
    1425         $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() );
    1426         $this->assertTrue( is_wp_error($result) );
    1427     }
    1428 
    1429     public function test_wp_set_object_terms_append_true() {
    1430         register_taxonomy( 'wptests_tax', 'post' );
    1431         $p = $this->factory->post->create();
    1432         $t1 = $this->factory->term->create( array(
    1433             'taxonomy' => 'wptests_tax',
    1434         ) );
    1435         $t2 = $this->factory->term->create( array(
    1436             'taxonomy' => 'wptests_tax',
    1437         ) );
    1438 
    1439         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1440         $this->assertNotEmpty( $added1 );
    1441         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1442 
    1443         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', true );
    1444         $this->assertNotEmpty( $added2 );
    1445         $this->assertEqualSets( array( $t1, $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1446 
    1447         _unregister_taxonomy( 'wptests_tax' );
    1448     }
    1449 
    1450     public function test_wp_set_object_terms_append_false() {
    1451         register_taxonomy( 'wptests_tax', 'post' );
    1452         $p = $this->factory->post->create();
    1453         $t1 = $this->factory->term->create( array(
    1454             'taxonomy' => 'wptests_tax',
    1455         ) );
    1456         $t2 = $this->factory->term->create( array(
    1457             'taxonomy' => 'wptests_tax',
    1458         ) );
    1459 
    1460         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1461         $this->assertNotEmpty( $added1 );
    1462         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1463 
    1464         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', false );
    1465         $this->assertNotEmpty( $added2 );
    1466         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1467 
    1468         _unregister_taxonomy( 'wptests_tax' );
    1469     }
    1470 
    1471     public function test_wp_set_object_terms_append_default_to_false() {
    1472         register_taxonomy( 'wptests_tax', 'post' );
    1473         $p = $this->factory->post->create();
    1474         $t1 = $this->factory->term->create( array(
    1475             'taxonomy' => 'wptests_tax',
    1476         ) );
    1477         $t2 = $this->factory->term->create( array(
    1478             'taxonomy' => 'wptests_tax',
    1479         ) );
    1480 
    1481         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1482         $this->assertNotEmpty( $added1 );
    1483         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1484 
    1485         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax' );
    1486         $this->assertNotEmpty( $added2 );
    1487         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1488 
    1489         _unregister_taxonomy( 'wptests_tax' );
    1490     }
    1491 
    1492     function test_change_object_terms_by_id() {
    1493         // set some terms on an object; then change them while leaving one intact
    1494 
    1495         $post_id = $this->factory->post->create();
    1496 
    1497         // first set: 3 terms
    1498         $terms_1 = array();
    1499         for ($i=0; $i<3; $i++ ) {
    1500             $term = rand_str();
    1501             $result = wp_insert_term( $term, $this->taxonomy );
    1502             $this->assertInternalType( 'array', $result );
    1503             $terms_1[$i] = $result['term_id'];
    1504         }
    1505 
    1506         // second set: one of the original terms, plus one new term
    1507         $terms_2 = array();
    1508         $terms_2[0] = $terms_1[1];
    1509 
    1510         $term = rand_str();
    1511         $result = wp_insert_term( $term, $this->taxonomy );
    1512         $terms_2[1] = $result['term_id'];
    1513 
    1514 
    1515         // set the initial terms
    1516         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1517         $this->assertEquals( 3, count($tt_1) );
    1518 
    1519         // make sure they're correct
    1520         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    1521         $this->assertEquals( $terms_1, $terms );
    1522 
    1523         // change the terms
    1524         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1525         $this->assertEquals( 2, count($tt_2) );
    1526 
    1527         // make sure they're correct
    1528         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    1529         $this->assertEquals( $terms_2, $terms );
    1530 
    1531         // make sure the tt id for 'bar' matches
    1532         $this->assertEquals( $tt_1[1], $tt_2[0] );
    1533 
    1534     }
    1535 
    1536     function test_change_object_terms_by_name() {
    1537         // set some terms on an object; then change them while leaving one intact
    1538 
    1539         $post_id = $this->factory->post->create();
    1540 
    1541         $terms_1 = array('foo', 'bar', 'baz');
    1542         $terms_2 = array('bar', 'bing');
    1543 
    1544         // set the initial terms
    1545         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1546         $this->assertEquals( 3, count($tt_1) );
    1547 
    1548         // make sure they're correct
    1549         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    1550         $this->assertEquals( $terms_1, $terms );
    1551 
    1552         // change the terms
    1553         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1554         $this->assertEquals( 2, count($tt_2) );
    1555 
    1556         // make sure they're correct
    1557         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    1558         $this->assertEquals( $terms_2, $terms );
    1559 
    1560         // make sure the tt id for 'bar' matches
    1561         $this->assertEquals( $tt_1[1], $tt_2[0] );
    1562 
    1563     }
    1564 
    1565     /**
    1566      * @ticket 15475
    1567      */
    1568     function test_wp_add_remove_object_terms() {
    1569         $posts = $this->factory->post->create_many( 5 );
    1570         $tags = $this->factory->tag->create_many( 5 );
    1571 
    1572         $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
    1573         $this->assertEquals( 1, count( $tt ) );
    1574         $this->assertEquals( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
    1575 
    1576         $three_tags = array( $tags[0], $tags[1], $tags[2] );
    1577         $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
    1578         $this->assertEquals( 3, count( $tt ) );
    1579         $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
    1580 
    1581         $this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
    1582         $this->assertFalse( wp_remove_object_terms( $posts[0], $tags[0], 'post_tag' ) );
    1583         $this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
    1584         $this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
    1585         $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
    1586 
    1587         foreach ( $tags as $term_id )
    1588             $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
    1589 
    1590         foreach ( $posts as $post_id )
    1591             $this->assertTrue( (bool) wp_delete_post( $post_id, true ) );
    1592     }
    1593 
    1594     /**
    1595      * @group category.php
    1596      */
    1597     function test_term_is_ancestor_of( ) {
    1598         $term = rand_str();
    1599         $term2 = rand_str();
    1600 
    1601         $t = wp_insert_term( $term, 'category' );
    1602         $this->assertInternalType( 'array', $t );
    1603         $t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
    1604         $this->assertInternalType( 'array', $t2 );
    1605         if ( function_exists( 'term_is_ancestor_of' ) ) {
    1606             $this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
    1607             $this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
    1608         }
    1609         $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id']) );
    1610         $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id']) );
    1611 
    1612         wp_delete_term($t['term_id'], 'category');
    1613         wp_delete_term($t2['term_id'], 'category');
    1614     }
    1615 
    1616     function test_wp_insert_delete_category() {
    1617         $term = rand_str();
    1618         $this->assertNull( category_exists( $term ) );
    1619 
    1620         $initial_count = wp_count_terms( 'category' );
    1621 
    1622         $t = wp_insert_category( array( 'cat_name' => $term ) );
    1623         $this->assertTrue( is_numeric($t) );
    1624         $this->assertFalse( is_wp_error($t) );
    1625         $this->assertTrue( $t > 0 );
    1626         $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
    1627 
    1628         // make sure the term exists
    1629         $this->assertTrue( term_exists($term) > 0 );
    1630         $this->assertTrue( term_exists($t) > 0 );
    1631 
    1632         // now delete it
    1633         $this->assertTrue( wp_delete_category($t) );
    1634         $this->assertNull( term_exists($term) );
    1635         $this->assertNull( term_exists($t) );
    1636         $this->assertEquals( $initial_count, wp_count_terms('category') );
    1637     }
    1638 
    1639     /**
    1640      * @ticket 16550
    1641      */
    1642     function test_wp_set_post_categories() {
    1643         $post_id = $this->factory->post->create();
    1644         $post = get_post( $post_id );
    1645 
    1646         $this->assertInternalType( 'array', $post->post_category );
    1647         $this->assertEquals( 1, count( $post->post_category ) );
    1648         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1649         $term1 = wp_insert_term( 'Foo', 'category' );
    1650         $term2 = wp_insert_term( 'Bar', 'category' );
    1651         $term3 = wp_insert_term( 'Baz', 'category' );
    1652         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
    1653         $this->assertEquals( 2, count( $post->post_category ) );
    1654         $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );
    1655 
    1656         wp_set_post_categories( $post_id, $term3['term_id'], true );
    1657         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );
    1658 
    1659         $term4 = wp_insert_term( 'Burrito', 'category' );
    1660         wp_set_post_categories( $post_id, $term4['term_id'] );
    1661         $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
    1662 
    1663         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
    1664         $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
    1665 
    1666         wp_set_post_categories( $post_id, array(), true );
    1667         $this->assertEquals( 1, count( $post->post_category ) );
    1668         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1669 
    1670         wp_set_post_categories( $post_id, array() );
    1671         $this->assertEquals( 1, count( $post->post_category ) );
    1672         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1673     }
    1674 
    1675     function test_wp_unique_term_slug() {
    1676         // set up test data
    1677         $a = wp_insert_term( 'parent', $this->taxonomy );
    1678         $this->assertInternalType( 'array', $a );
    1679         $b = wp_insert_term( 'child',  $this->taxonomy, array( 'parent' => $a['term_id'] ) );
    1680         $this->assertInternalType( 'array', $b );
    1681         $c = wp_insert_term( 'neighbor', $this->taxonomy );
    1682         $this->assertInternalType( 'array', $c );
    1683         $d = wp_insert_term( 'pet',  $this->taxonomy, array( 'parent' => $c['term_id'] )  );
    1684         $this->assertInternalType( 'array', $c );
    1685 
    1686         $a_term = get_term( $a['term_id'], $this->taxonomy );
    1687         $b_term = get_term( $b['term_id'], $this->taxonomy );
    1688         $c_term = get_term( $c['term_id'], $this->taxonomy );
    1689         $d_term = get_term( $d['term_id'], $this->taxonomy );
    1690 
    1691         // a unique slug gets unchanged
    1692         $this->assertEquals( 'unique-term', wp_unique_term_slug( 'unique-term', $c_term ) );
    1693 
    1694         // a non-hierarchicial dupe gets suffixed with "-#"
    1695         $this->assertEquals( 'parent-2', wp_unique_term_slug( 'parent', $c_term ) );
    1696 
    1697         // a hierarchical dupe initially gets suffixed with its parent term
    1698         $this->assertEquals( 'child-neighbor', wp_unique_term_slug( 'child', $d_term ) );
    1699 
    1700         // a hierarchical dupe whose parent already contains the {term}-{parent term}
    1701         // term gets suffixed with parent term name and then '-#'
    1702         $e = wp_insert_term( 'child-neighbor', $this->taxonomy, array( 'parent' => $c['term_id'] ) );
    1703         $this->assertEquals( 'child-neighbor-2', wp_unique_term_slug( 'child', $d_term ) );
    1704 
    1705         $f = wp_insert_term( 'foo', $this->taxonomy );
    1706         $this->assertInternalType( 'array', $f );
    1707         $f_term = get_term( $f['term_id'], $this->taxonomy );
    1708         $this->assertEquals( 'foo', $f_term->slug );
    1709         $this->assertEquals( 'foo', wp_unique_term_slug(  'foo', $f_term ) );
    1710 
    1711         $g = wp_insert_term( 'foo',  $this->taxonomy );
    1712         $this->assertInstanceOf( 'WP_Error', $g );
    1713 
    1714         // clean up
    1715         foreach ( array( $a, $b, $c, $d, $e, $f ) as $t )
    1716             $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) );
    1717     }
    1718 
    1719     /**
    1720      * @ticket 25852
    1721      */
    1722     function test_sanitize_term_field() {
    1723         $term = wp_insert_term( 'foo', $this->taxonomy );
    1724 
    1725         $this->assertEquals( 0, sanitize_term_field( 'parent',  0, $term['term_id'], $this->taxonomy, 'raw' ) );
    1726         $this->assertEquals( 1, sanitize_term_field( 'parent',  1, $term['term_id'], $this->taxonomy, 'raw' ) );
    1727         $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
    1728         $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
    1729     }
    1730 
    1731     private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) {
    1732         $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array(
    1733             'fields' => 'ids'
    1734         ) );
    1735 
    1736         $this->assertEquals( $expected_term_ids, $assigned_term_ids );
    1737     }
    1738 
    1739     /**
    1740      * @ticket 22560
    1741      */
    1742     function test_object_term_cache() {
    1743         $post_id = $this->factory->post->create();
    1744 
    1745         $terms_1 = array('foo', 'bar', 'baz');
    1746         $terms_2 = array('bar', 'bing');
    1747 
    1748         // Cache should be empty after a set.
    1749         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1750         $this->assertEquals( 3, count($tt_1) );
    1751         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1752 
    1753         // wp_get_object_terms() does not prime the cache.
    1754         wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') );
    1755         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1756 
    1757         // get_the_terms() does prime the cache.
    1758         $terms = get_the_terms( $post_id, $this->taxonomy );
    1759         $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    1760         $this->assertInternalType( 'array', $cache );
    1761 
    1762         // Cache should be empty after a set.
    1763         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1764         $this->assertEquals( 2, count($tt_2) );
    1765         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1766     }
    1767 
    1768     /**
    1769      * @ticket 24189
    1770      */
    1771     function test_object_term_cache_when_term_changes() {
    1772         $post_id = $this->factory->post->create();
    1773         $tag_id = $this->factory->tag->create( array( 'description' => 'My Amazing Tag' ) );
    1774 
    1775         $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
    1776 
    1777         $terms = get_the_terms( $post_id, 'post_tag' );
    1778         $this->assertEquals( $tag_id, $terms[0]->term_id );
    1779         $this->assertEquals( 'My Amazing Tag', $terms[0]->description );
    1780 
    1781         $_updated = wp_update_term( $tag_id, 'post_tag', array(
    1782             'description' => 'This description is even more amazing!'
    1783         ) );
    1784 
    1785         $_new_term = get_term( $tag_id, 'post_tag' );
    1786         $this->assertEquals( $tag_id, $_new_term->term_id );
    1787         $this->assertEquals( 'This description is even more amazing!', $_new_term->description );
    1788 
    1789         $terms = get_the_terms( $post_id, 'post_tag' );
    1790         $this->assertEquals( $tag_id, $terms[0]->term_id );
    1791         $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
    1792     }
    1793 
    1794     /**
    1795      * @ticket 31086
    1796      */
    1797     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
    1798         register_taxonomy( 'wptests_tax', 'post' );
    1799         $p = $this->factory->post->create();
    1800         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    1801 
    1802         $found = get_the_terms( $p, 'wptests_tax' );
    1803 
    1804         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    1805     }
    1806 
    1807     /**
    1808      * @ticket 31086
    1809      */
    1810     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
    1811         register_taxonomy( 'wptests_tax', 'post' );
    1812         $p = $this->factory->post->create();
    1813         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    1814 
    1815         // Prime cache.
    1816         update_object_term_cache( array( $p ), array( 'post' ) );
    1817 
    1818         $found = get_the_terms( $p, 'wptests_tax' );
    1819 
    1820         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    1821     }
    1822 
    1823     /**
    1824      * @ticket 19205
    1825      */
    1826     function test_orphan_category() {
    1827         $cat_id1 = $this->factory->category->create();
    1828 
    1829         wp_delete_category( $cat_id1 );
    1830 
    1831         $cat_id2 = $this->factory->category->create( array( 'parent' => $cat_id1 ) );
    1832         $this->assertWPError( $cat_id2 );
    1833     }
    1834 
    1835     /**
    1836588     * @ticket 30780
    1837589     */
     
    1902654        _unregister_taxonomy( 'wptests_tax' );
    1903655    }
    1904 
    1905     /** Helpers **********************************************************/
    1906 
    1907     public function _pre_insert_term_callback() {
    1908         return new WP_Error( 'custom_error' );
    1909     }
    1910656}
Note: See TracChangeset for help on using the changeset viewer.