Changeset 31812
- Timestamp:
- 03/18/2015 01:27:15 PM (10 years ago)
- Location:
- trunk/tests/phpunit/tests
- Files:
-
- 1 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term.php
r31792 r31812 6 6 class Tests_Term extends WP_UnitTestCase { 7 7 var $taxonomy = 'category'; 8 9 function setUp() {10 parent::setUp();11 12 _clean_term_filters();13 // insert one term into every post taxonomy14 // otherwise term_ids and term_taxonomy_ids might be identical, which could mask bugs15 $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 term31 $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 exists44 $this->assertTrue( term_exists($term) > 0 );45 $this->assertTrue( term_exists($t['term_id']) > 0 );46 47 // now delete it48 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 17689169 */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 slug176 $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );177 $this->assertFalse( is_wp_error( $term1 ) );178 179 // Test an existing term name180 $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 characters185 $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 characters198 $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 31328230 */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 31328249 */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 31328274 */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 31328296 */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 31328313 */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 31328332 */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 31328357 */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 31328389 */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 31328411 */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 5809427 */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 5809450 */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 5809477 */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 not503 * 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 5809570 */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 these610 * caches, but we should ensure that they're being cleared for611 * compatibility with third-party addons. Prime the caches612 * 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 630 public function test_wp_update_term_taxonomy_does_not_exist() {631 $found = wp_update_term( 1, 'bar' );632 633 $this->assertTrue( is_wp_error( $found ) );634 $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );635 }636 637 public function test_wp_update_term_term_empty_string_should_return_wp_error() {638 $found = wp_update_term( '', 'post_tag' );639 640 $this->assertTrue( is_wp_error( $found ) );641 $this->assertSame( 'invalid_term', $found->get_error_code() );642 }643 644 public function test_wp_update_term_unslash_name() {645 register_taxonomy( 'wptests_tax', 'post' );646 $t = $this->factory->term->create( array(647 'taxonomy' => 'wptests_tax',648 ) );649 650 $found = wp_update_term( $t, 'wptests_tax', array(651 'name' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',652 ) );653 654 $term = get_term( $found['term_id'], 'wptests_tax' );655 _unregister_taxonomy( 'wptests_tax' );656 657 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name );658 }659 660 public function test_wp_update_term_unslash_description() {661 register_taxonomy( 'wptests_tax', 'post' );662 $t = $this->factory->term->create( array(663 'taxonomy' => 'wptests_tax',664 ) );665 666 $found = wp_update_term( $t, 'wptests_tax', array(667 'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',668 ) );669 670 $term = get_term( $found['term_id'], 'wptests_tax' );671 _unregister_taxonomy( 'wptests_tax' );672 673 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description );674 }675 676 public function test_wp_update_term_name_empty_string() {677 register_taxonomy( 'wptests_tax', 'post' );678 $t = $this->factory->term->create( array(679 'taxonomy' => 'wptests_tax',680 ) );681 682 $found = wp_update_term( $t, 'wptests_tax', array(683 'name' => '',684 ) );685 686 $this->assertTrue( is_wp_error( $found ) );687 $this->assertSame( 'empty_term_name', $found->get_error_code() );688 _unregister_taxonomy( 'wptests_tax' );689 }690 691 /**692 * @ticket 29614693 */694 function test_wp_update_term_parent_does_not_exist() {695 register_taxonomy( 'wptests_tax', array(696 'hierarchical' => true,697 ) );698 $fake_term_id = 787878;699 700 $this->assertNull( term_exists( $fake_term_id, 'wptests_tax' ) );701 702 $t = $this->factory->term->create( array(703 'taxonomy' => 'wptests_tax',704 ) );705 706 $found = wp_update_term( $t, 'wptests_tax', array(707 'parent' => $fake_term_id,708 ) );709 710 $this->assertWPError( $found );711 $this->assertSame( 'missing_parent', $found->get_error_code() );712 713 $term = get_term( $t, 'wptests_tax' );714 $this->assertEquals( 0, $term->parent );715 _unregister_taxonomy( 'wptests_tax' );716 }717 718 public function test_wp_update_term_slug_empty_string_while_not_updating_name() {719 register_taxonomy( 'wptests_tax', 'post' );720 $t = $this->factory->term->create( array(721 'taxonomy' => 'wptests_tax',722 'name' => 'Foo Bar',723 ) );724 725 $found = wp_update_term( $t, 'wptests_tax', array(726 'slug' => '',727 ) );728 729 $term = get_term( $t, 'wptests_tax' );730 $this->assertSame( 'foo-bar', $term->slug );731 _unregister_taxonomy( 'wptests_tax' );732 }733 734 public function test_wp_update_term_slug_empty_string_while_updating_name() {735 register_taxonomy( 'wptests_tax', 'post' );736 $t = $this->factory->term->create( array(737 'taxonomy' => 'wptests_tax',738 ) );739 740 $found = wp_update_term( $t, 'wptests_tax', array(741 'name' => 'Foo Bar',742 'slug' => '',743 ) );744 745 $term = get_term( $t, 'wptests_tax' );746 $this->assertSame( 'foo-bar', $term->slug );747 _unregister_taxonomy( 'wptests_tax' );748 }749 750 public function test_wp_update_term_slug_set_slug() {751 register_taxonomy( 'wptests_tax', 'post' );752 $t = $this->factory->term->create( array(753 'taxonomy' => 'wptests_tax',754 ) );755 756 $found = wp_update_term( $t, 'wptests_tax', array(757 'slug' => 'foo-bar',758 ) );759 760 $term = get_term( $t, 'wptests_tax' );761 $this->assertSame( 'foo-bar', $term->slug );762 _unregister_taxonomy( 'wptests_tax' );763 }764 765 /**766 * @ticket 5809767 */768 public function test_wp_update_term_should_not_create_duplicate_slugs_within_the_same_taxonomy() {769 register_taxonomy( 'wptests_tax', 'post' );770 771 $t1 = $this->factory->term->create( array(772 'name' => 'Foo',773 'slug' => 'foo',774 'taxonomy' => 'wptests_tax',775 ) );776 777 $t2 = $this->factory->term->create( array(778 'name' => 'Bar',779 'slug' => 'bar',780 'taxonomy' => 'wptests_tax',781 ) );782 783 $updated = wp_update_term( $t2, 'wptests_tax', array(784 'slug' => 'foo',785 ) );786 787 $this->assertWPError( $updated );788 $this->assertSame( 'duplicate_term_slug', $updated->get_error_code() );789 }790 791 /**792 * @ticket 5809793 */794 public function test_wp_update_term_should_allow_duplicate_slugs_in_different_taxonomy() {795 register_taxonomy( 'wptests_tax', 'post' );796 register_taxonomy( 'wptests_tax_2', 'post' );797 798 $t1 = $this->factory->term->create( array(799 'name' => 'Foo',800 'slug' => 'foo',801 'taxonomy' => 'wptests_tax',802 ) );803 804 $t2 = $this->factory->term->create( array(805 'name' => 'Foo',806 'slug' => 'bar',807 'taxonomy' => 'wptests_tax_2',808 ) );809 810 $updated = wp_update_term( $t2, 'wptests_tax_2', array(811 'slug' => 'foo',812 ) );813 814 $this->assertFalse( is_wp_error( $updated ) );815 816 $t1_term = get_term( $t1, 'wptests_tax' );817 $t2_term = get_term( $t2, 'wptests_tax_2' );818 $this->assertSame( $t1_term->slug, $t2_term->slug );819 }820 821 /**822 * @ticket 30780823 */824 public function test_wp_update_term_should_allow_duplicate_names_in_different_taxonomies() {825 register_taxonomy( 'wptests_tax', 'post' );826 register_taxonomy( 'wptests_tax_2', 'post' );827 828 $t1 = $this->factory->term->create( array(829 'name' => 'Foo',830 'slug' => 'foo',831 'taxonomy' => 'wptests_tax',832 ) );833 834 $t2 = $this->factory->term->create( array(835 'name' => 'Bar',836 'slug' => 'bar',837 'taxonomy' => 'wptests_tax_2',838 ) );839 840 $updated = wp_update_term( $t2, 'wptests_tax_2', array(841 'name' => 'Foo',842 ) );843 844 $this->assertFalse( is_wp_error( $updated ) );845 846 $t2_term = get_term( $t2, 'wptests_tax_2' );847 $this->assertSame( 'Foo', $t2_term->name );848 }849 850 /**851 * @ticket 30780852 */853 public function test_wp_update_term_should_allow_duplicate_names_at_different_levels_of_the_same_taxonomy() {854 register_taxonomy( 'wptests_tax', 'post', array(855 'hierarchical' => true,856 ) );857 858 $t1 = $this->factory->term->create( array(859 'name' => 'Foo',860 'slug' => 'foo',861 'taxonomy' => 'wptests_tax',862 ) );863 864 $t2 = $this->factory->term->create( array(865 'name' => 'Bar',866 'slug' => 'bar',867 'taxonomy' => 'wptests_tax',868 'parent' => $t1,869 ) );870 871 $t3 = $this->factory->term->create( array(872 'name' => 'Bar Child',873 'slug' => 'bar-child',874 'taxonomy' => 'wptests_tax',875 'parent' => $t2,876 ) );877 878 $updated = wp_update_term( $t3, 'wptests_tax', array(879 'name' => 'Bar',880 ) );881 882 $this->assertFalse( is_wp_error( $updated ) );883 884 $t3_term = get_term( $t3, 'wptests_tax' );885 $this->assertSame( 'Bar', $t3_term->name );886 }887 888 /**889 * @ticket 5809890 */891 public function test_wp_update_term_should_split_shared_term() {892 global $wpdb;893 894 register_taxonomy( 'wptests_tax', 'post' );895 register_taxonomy( 'wptests_tax_2', 'post' );896 897 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );898 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );899 900 // Manually modify because split terms shouldn't naturally occur.901 $wpdb->update( $wpdb->term_taxonomy,902 array( 'term_id' => $t1['term_id'] ),903 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),904 array( '%d' ),905 array( '%d' )906 );907 908 $posts = $this->factory->post->create_many( 2 );909 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );910 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );911 912 // Verify that the terms are shared.913 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );914 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );915 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );916 917 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(918 'name' => 'New Foo',919 ) );920 921 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );922 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );923 $this->assertNotEquals( $t1_terms[0]->term_id, $t2_terms[0]->term_id );924 }925 926 /**927 * @ticket 5809928 */929 public function test_wp_update_term_should_not_split_shared_term_before_410_schema_change() {930 global $wpdb;931 932 $db_version = get_option( 'db_version' );933 update_option( 'db_version', 30055 );934 935 register_taxonomy( 'wptests_tax', 'post' );936 register_taxonomy( 'wptests_tax_2', 'post' );937 938 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );939 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );940 941 // Manually modify because split terms shouldn't naturally occur.942 $wpdb->update( $wpdb->term_taxonomy,943 array( 'term_id' => $t1['term_id'] ),944 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),945 array( '%d' ),946 array( '%d' )947 );948 949 $posts = $this->factory->post->create_many( 2 );950 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );951 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );952 953 // Verify that the term is shared.954 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );955 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );956 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );957 958 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(959 'name' => 'New Foo',960 ) );961 962 // Term should still be shared.963 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );964 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );965 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );966 967 update_option( 'db_version', $db_version );968 }969 970 public function test_wp_update_term_alias_of_no_term_group() {971 register_taxonomy( 'wptests_tax', 'post' );972 $t1 = $this->factory->term->create( array(973 'taxonomy' => 'wptests_tax',974 ) );975 $term_1 = get_term( $t1, 'wptests_tax' );976 977 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );978 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(979 'alias_of' => $term_1->slug,980 ) );981 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );982 983 $updated_term_1 = get_term( $t1, 'wptests_tax' );984 _unregister_taxonomy( 'wptests_tax' );985 986 $this->assertSame( 0, $term_1->term_group );987 $this->assertNotEmpty( $created_term->term_group );988 $this->assertSame( $created_term->term_group, $updated_term_1->term_group );989 }990 991 public function test_wp_update_term_alias_of_existing_term_group() {992 register_taxonomy( 'wptests_tax', 'post' );993 $t1 = $this->factory->term->create( array(994 'taxonomy' => 'wptests_tax',995 ) );996 $term_1 = get_term( $t1, 'wptests_tax' );997 998 $t2 = $this->factory->term->create( array(999 'taxonomy' => 'wptests_tax',1000 'alias_of' => $term_1->slug,1001 ) );1002 $term_2 = get_term( $t2, 'wptests_tax' );1003 1004 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );1005 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(1006 'alias_of' => $term_2->slug,1007 ) );1008 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );1009 _unregister_taxonomy( 'wptests_tax' );1010 1011 $this->assertNotEmpty( $created_term->term_group );1012 $this->assertSame( $created_term->term_group, $term_2->term_group );1013 }1014 1015 public function test_wp_update_term_alias_of_nonexistent_term() {1016 register_taxonomy( 'wptests_tax', 'post' );1017 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );1018 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(1019 'alias_of' => 'bar',1020 ) );1021 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );1022 _unregister_taxonomy( 'wptests_tax' );1023 1024 $this->assertSame( 0, $created_term->term_group );1025 }1026 1027 public function test_wp_update_term_slug_same_as_old_slug() {1028 register_taxonomy( 'wptests_tax', 'post' );1029 $t = $this->factory->term->create( array(1030 'taxonomy' => 'wptests_tax',1031 'slug' => 'foo',1032 ) );1033 1034 $found = wp_update_term( $t, 'wptests_tax', array(1035 'slug' => 'foo',1036 ) );1037 1038 $term = get_term( $t, 'wptests_tax' );1039 1040 $this->assertSame( $t, $found['term_id'] );1041 $this->assertSame( 'foo', $term->slug );1042 _unregister_taxonomy( 'wptests_tax' );1043 }1044 1045 public function test_wp_update_term_duplicate_slug_generated_due_to_empty_slug_param() {1046 register_taxonomy( 'wptests_tax', 'post' );1047 $t1 = $this->factory->term->create( array(1048 'taxonomy' => 'wptests_tax',1049 'slug' => 'foo-bar',1050 ) );1051 $t2 = $this->factory->term->create( array(1052 'taxonomy' => 'wptests_tax',1053 'name' => 'not foo bar',1054 ) );1055 1056 $found = wp_update_term( $t2, 'wptests_tax', array(1057 'slug' => '',1058 'name' => 'Foo? Bar!', // Will sanitize to 'foo-bar'.1059 ) );1060 1061 $term = get_term( $t2, 'wptests_tax' );1062 1063 $this->assertSame( $t2, $found['term_id'] );1064 $this->assertSame( 'foo-bar-2', $term->slug );1065 _unregister_taxonomy( 'wptests_tax' );1066 }1067 1068 public function test_wp_update_term_duplicate_slug_with_changed_parent() {1069 register_taxonomy( 'wptests_tax', 'post', array(1070 'hierarchical' => true,1071 ) );1072 $p = $this->factory->term->create( array(1073 'taxonomy' => 'wptests_tax',1074 ) );1075 $t1 = $this->factory->term->create( array(1076 'taxonomy' => 'wptests_tax',1077 'slug' => 'foo-bar',1078 ) );1079 $t2 = $this->factory->term->create( array(1080 'taxonomy' => 'wptests_tax',1081 ) );1082 1083 $found = wp_update_term( $t2, 'wptests_tax', array(1084 'parent' => $p,1085 'slug' => 'foo-bar',1086 ) );1087 1088 $term = get_term( $t2, 'wptests_tax' );1089 $parent_term = get_term( $p, 'wptests_tax' );1090 1091 $this->assertSame( $t2, $found['term_id'] );1092 $this->assertSame( 'foo-bar-' . $parent_term->slug, $term->slug );1093 _unregister_taxonomy( 'wptests_tax' );1094 }1095 1096 public function test_wp_update_term_duplicate_slug_failure() {1097 register_taxonomy( 'wptests_tax', 'post' );1098 $t1 = $this->factory->term->create( array(1099 'taxonomy' => 'wptests_tax',1100 'slug' => 'foo-bar',1101 ) );1102 $t2 = $this->factory->term->create( array(1103 'taxonomy' => 'wptests_tax',1104 'slug' => 'my-old-slug',1105 ) );1106 1107 $found = wp_update_term( $t2, 'wptests_tax', array(1108 'slug' => 'foo-bar',1109 ) );1110 1111 $term = get_term( $t2, 'wptests_tax' );1112 1113 $this->assertWPError( $found );1114 $this->assertSame( 'duplicate_term_slug', $found->get_error_code() );1115 $this->assertSame( 'my-old-slug', $term->slug );1116 _unregister_taxonomy( 'wptests_tax' );1117 }1118 1119 public function test_wp_update_term_should_return_term_id_and_term_taxonomy_id() {1120 register_taxonomy( 'wptests_tax', 'post' );1121 $t = $this->factory->term->create( array(1122 'taxonomy' => 'wptests_tax',1123 ) );1124 $found = wp_update_term( $t, 'wptests_tax', array(1125 'slug' => 'foo',1126 ) );1127 1128 $term_by_id = get_term( $found['term_id'], 'wptests_tax' );1129 $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' );1130 $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' );1131 1132 _unregister_taxonomy( 'wptests_tax' );1133 1134 $this->assertInternalType( 'array', $found );1135 $this->assertNotEmpty( $found['term_id'] );1136 $this->assertNotEmpty( $found['term_taxonomy_id'] );1137 $this->assertNotEmpty( $term_by_id );1138 $this->assertEquals( $term_by_id, $term_by_slug );1139 $this->assertEquals( $term_by_id, $term_by_ttid );1140 }1141 1142 public function test_wp_update_term_should_clean_object_term_cache() {1143 register_taxonomy( 'wptests_tax_for_post', 'post' );1144 register_taxonomy( 'wptests_tax_for_page', 'page' );1145 $post = $this->factory->post->create();1146 $page = $this->factory->post->create( array(1147 'post_type' => 'page',1148 ) );1149 1150 $t_for_post = $this->factory->term->create( array(1151 'taxonomy' => 'wptests_tax_for_post',1152 ) );1153 $t_for_page = $this->factory->term->create( array(1154 'taxonomy' => 'wptests_tax_for_page',1155 ) );1156 1157 wp_set_post_terms( $post, array( $t_for_post ), 'wptests_tax_for_post' );1158 wp_set_post_terms( $page, array( $t_for_page ), 'wptests_tax_for_page' );1159 1160 // Prime caches and verify.1161 update_object_term_cache( array( $post ), 'post' );1162 update_object_term_cache( array( $page ), 'page' );1163 $this->assertNotEmpty( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );1164 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );1165 1166 // Update a term in just one of the taxonomies.1167 $found = wp_update_term( $t_for_post, 'wptests_tax_for_post', array(1168 'slug' => 'foo',1169 ) );1170 1171 // Only the relevant cache should have been cleared.1172 $this->assertFalse( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );1173 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );1174 }1175 1176 public function test_wp_update_term_should_clean_term_cache() {1177 register_taxonomy( 'wptests_tax', 'post', array(1178 'hierarchical' => true,1179 ) );1180 1181 $t1 = $this->factory->term->create( array(1182 'taxonomy' => 'wptests_tax',1183 ) );1184 $t2 = $this->factory->term->create( array(1185 'taxonomy' => 'wptests_tax',1186 ) );1187 1188 /*1189 * It doesn't appear that WordPress itself ever sets these1190 * caches, but we should ensure that they're being cleared for1191 * compatibility with third-party addons. Prime the caches1192 * manually.1193 */1194 wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' );1195 wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' );1196 1197 $found = wp_update_term( $t1, 'wptests_tax', array(1198 'parent' => $t2,1199 ) );1200 _unregister_taxonomy( 'wptests_tax' );1201 1202 $this->assertSame( false, wp_cache_get( 'all_ids', 'wptests_tax' ) );1203 $this->assertSame( false, wp_cache_get( 'get', 'wptests_tax' ) );1204 1205 $cached_children = get_option( 'wptests_tax_children' );1206 $this->assertNotEmpty( $cached_children[ $t2 ] );1207 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t2 ] ) );1208 }1209 8 1210 9 /** … … 1832 631 $this->assertWPError( $cat_id2 ); 1833 632 } 1834 1835 /**1836 * @ticket 307801837 */1838 public function test_wp_update_term_should_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_term_with_the_same_slug() {1839 register_taxonomy( 'wptests_tax', 'post', array(1840 'hierarchical' => true,1841 ) );1842 register_taxonomy( 'wptests_tax_2', 'post', array(1843 'hierarchical' => true,1844 ) );1845 1846 $t1 = $this->factory->term->create( array(1847 'taxonomy' => 'wptests_tax',1848 'slug' => 'parent-term',1849 ) );1850 1851 $t2 = $this->factory->term->create( array(1852 'taxonomy' => 'wptests_tax',1853 'slug' => 'foo',1854 ) );1855 1856 wp_update_term( $t2, 'wptests_tax', array(1857 'parent' => $t1,1858 ) );1859 1860 $t2_term = get_term( $t2, 'wptests_tax' );1861 1862 $this->assertSame( 'foo', $t2_term->slug );1863 1864 _unregister_taxonomy( 'wptests_tax' );1865 }1866 1867 /**1868 * @ticket 307801869 */1870 public function test_wp_update_term_should_not_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_slug_conflict_within_the_taxonomy() {1871 register_taxonomy( 'wptests_tax', 'post', array(1872 'hierarchical' => true,1873 ) );1874 register_taxonomy( 'wptests_tax_2', 'post', array(1875 'hierarchical' => true,1876 ) );1877 1878 $t1 = $this->factory->term->create( array(1879 'taxonomy' => 'wptests_tax',1880 'slug' => 'parent-term',1881 ) );1882 1883 // Same slug but in a different tax.1884 $t2 = $this->factory->term->create( array(1885 'taxonomy' => 'wptests_tax_2',1886 'slug' => 'foo',1887 ) );1888 1889 $t3 = $this->factory->term->create( array(1890 'taxonomy' => 'wptests_tax',1891 'slug' => 'foo',1892 ) );1893 1894 wp_update_term( $t3, 'wptests_tax', array(1895 'parent' => $t1,1896 ) );1897 1898 $t3_term = get_term( $t3, 'wptests_tax' );1899 1900 $this->assertSame( 'foo', $t3_term->slug );1901 1902 _unregister_taxonomy( 'wptests_tax' );1903 }1904 1905 /** Helpers **********************************************************/1906 1907 public function _pre_insert_term_callback() {1908 return new WP_Error( 'custom_error' );1909 }1910 633 } -
trunk/tests/phpunit/tests/term/wpInsertTerm.php
r31811 r31812 4 4 * @group taxonomy 5 5 */ 6 class Tests_Term extends WP_UnitTestCase { 7 var $taxonomy = 'category'; 8 9 function setUp() { 6 class Tests_Term_WpInsertTerm extends WP_UnitTestCase { 7 public function setUp() { 10 8 parent::setUp(); 11 9 … … 18 16 } 19 17 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() { 18 public function test_wp_insert_delete_term() { 19 $taxonomy = 'wptests_tax'; 20 register_taxonomy( $taxonomy, 'post' ); 21 30 22 // a new unused term 31 23 $term = rand_str(); 32 24 $this->assertNull( term_exists($term) ); 33 25 34 $initial_count = wp_count_terms( $t his->taxonomy );35 36 $t = wp_insert_term( $term, $t his->taxonomy );26 $initial_count = wp_count_terms( $taxonomy ); 27 28 $t = wp_insert_term( $term, $taxonomy ); 37 29 $this->assertInternalType( 'array', $t ); 38 30 $this->assertFalse( is_wp_error($t) ); 39 31 $this->assertTrue( $t['term_id'] > 0 ); 40 32 $this->assertTrue( $t['term_taxonomy_id'] > 0 ); 41 $this->assertEquals( $initial_count + 1, wp_count_terms( $this->taxonomy) );33 $this->assertEquals( $initial_count + 1, wp_count_terms( $taxonomy ) ); 42 34 43 35 // make sure the term exists … … 47 39 // now delete it 48 40 add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 ); 49 $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy) );41 $this->assertTrue( wp_delete_term( $t['term_id'], $taxonomy ) ); 50 42 remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 ); 51 43 $this->assertNull( term_exists($term) ); 52 44 $this->assertNull( term_exists($t['term_id']) ); 53 $this->assertEquals( $initial_count, wp_count_terms( $this->taxonomy) );45 $this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) ); 54 46 } 55 47 … … 628 620 } 629 621 630 public function test_wp_update_term_taxonomy_does_not_exist() {631 $found = wp_update_term( 1, 'bar' );632 633 $this->assertTrue( is_wp_error( $found ) );634 $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );635 }636 637 public function test_wp_update_term_term_empty_string_should_return_wp_error() {638 $found = wp_update_term( '', 'post_tag' );639 640 $this->assertTrue( is_wp_error( $found ) );641 $this->assertSame( 'invalid_term', $found->get_error_code() );642 }643 644 public function test_wp_update_term_unslash_name() {645 register_taxonomy( 'wptests_tax', 'post' );646 $t = $this->factory->term->create( array(647 'taxonomy' => 'wptests_tax',648 ) );649 650 $found = wp_update_term( $t, 'wptests_tax', array(651 'name' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',652 ) );653 654 $term = get_term( $found['term_id'], 'wptests_tax' );655 _unregister_taxonomy( 'wptests_tax' );656 657 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name );658 }659 660 public function test_wp_update_term_unslash_description() {661 register_taxonomy( 'wptests_tax', 'post' );662 $t = $this->factory->term->create( array(663 'taxonomy' => 'wptests_tax',664 ) );665 666 $found = wp_update_term( $t, 'wptests_tax', array(667 'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',668 ) );669 670 $term = get_term( $found['term_id'], 'wptests_tax' );671 _unregister_taxonomy( 'wptests_tax' );672 673 $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description );674 }675 676 public function test_wp_update_term_name_empty_string() {677 register_taxonomy( 'wptests_tax', 'post' );678 $t = $this->factory->term->create( array(679 'taxonomy' => 'wptests_tax',680 ) );681 682 $found = wp_update_term( $t, 'wptests_tax', array(683 'name' => '',684 ) );685 686 $this->assertTrue( is_wp_error( $found ) );687 $this->assertSame( 'empty_term_name', $found->get_error_code() );688 _unregister_taxonomy( 'wptests_tax' );689 }690 691 /**692 * @ticket 29614693 */694 function test_wp_update_term_parent_does_not_exist() {695 register_taxonomy( 'wptests_tax', array(696 'hierarchical' => true,697 ) );698 $fake_term_id = 787878;699 700 $this->assertNull( term_exists( $fake_term_id, 'wptests_tax' ) );701 702 $t = $this->factory->term->create( array(703 'taxonomy' => 'wptests_tax',704 ) );705 706 $found = wp_update_term( $t, 'wptests_tax', array(707 'parent' => $fake_term_id,708 ) );709 710 $this->assertWPError( $found );711 $this->assertSame( 'missing_parent', $found->get_error_code() );712 713 $term = get_term( $t, 'wptests_tax' );714 $this->assertEquals( 0, $term->parent );715 _unregister_taxonomy( 'wptests_tax' );716 }717 718 public function test_wp_update_term_slug_empty_string_while_not_updating_name() {719 register_taxonomy( 'wptests_tax', 'post' );720 $t = $this->factory->term->create( array(721 'taxonomy' => 'wptests_tax',722 'name' => 'Foo Bar',723 ) );724 725 $found = wp_update_term( $t, 'wptests_tax', array(726 'slug' => '',727 ) );728 729 $term = get_term( $t, 'wptests_tax' );730 $this->assertSame( 'foo-bar', $term->slug );731 _unregister_taxonomy( 'wptests_tax' );732 }733 734 public function test_wp_update_term_slug_empty_string_while_updating_name() {735 register_taxonomy( 'wptests_tax', 'post' );736 $t = $this->factory->term->create( array(737 'taxonomy' => 'wptests_tax',738 ) );739 740 $found = wp_update_term( $t, 'wptests_tax', array(741 'name' => 'Foo Bar',742 'slug' => '',743 ) );744 745 $term = get_term( $t, 'wptests_tax' );746 $this->assertSame( 'foo-bar', $term->slug );747 _unregister_taxonomy( 'wptests_tax' );748 }749 750 public function test_wp_update_term_slug_set_slug() {751 register_taxonomy( 'wptests_tax', 'post' );752 $t = $this->factory->term->create( array(753 'taxonomy' => 'wptests_tax',754 ) );755 756 $found = wp_update_term( $t, 'wptests_tax', array(757 'slug' => 'foo-bar',758 ) );759 760 $term = get_term( $t, 'wptests_tax' );761 $this->assertSame( 'foo-bar', $term->slug );762 _unregister_taxonomy( 'wptests_tax' );763 }764 765 /**766 * @ticket 5809767 */768 public function test_wp_update_term_should_not_create_duplicate_slugs_within_the_same_taxonomy() {769 register_taxonomy( 'wptests_tax', 'post' );770 771 $t1 = $this->factory->term->create( array(772 'name' => 'Foo',773 'slug' => 'foo',774 'taxonomy' => 'wptests_tax',775 ) );776 777 $t2 = $this->factory->term->create( array(778 'name' => 'Bar',779 'slug' => 'bar',780 'taxonomy' => 'wptests_tax',781 ) );782 783 $updated = wp_update_term( $t2, 'wptests_tax', array(784 'slug' => 'foo',785 ) );786 787 $this->assertWPError( $updated );788 $this->assertSame( 'duplicate_term_slug', $updated->get_error_code() );789 }790 791 /**792 * @ticket 5809793 */794 public function test_wp_update_term_should_allow_duplicate_slugs_in_different_taxonomy() {795 register_taxonomy( 'wptests_tax', 'post' );796 register_taxonomy( 'wptests_tax_2', 'post' );797 798 $t1 = $this->factory->term->create( array(799 'name' => 'Foo',800 'slug' => 'foo',801 'taxonomy' => 'wptests_tax',802 ) );803 804 $t2 = $this->factory->term->create( array(805 'name' => 'Foo',806 'slug' => 'bar',807 'taxonomy' => 'wptests_tax_2',808 ) );809 810 $updated = wp_update_term( $t2, 'wptests_tax_2', array(811 'slug' => 'foo',812 ) );813 814 $this->assertFalse( is_wp_error( $updated ) );815 816 $t1_term = get_term( $t1, 'wptests_tax' );817 $t2_term = get_term( $t2, 'wptests_tax_2' );818 $this->assertSame( $t1_term->slug, $t2_term->slug );819 }820 821 /**822 * @ticket 30780823 */824 public function test_wp_update_term_should_allow_duplicate_names_in_different_taxonomies() {825 register_taxonomy( 'wptests_tax', 'post' );826 register_taxonomy( 'wptests_tax_2', 'post' );827 828 $t1 = $this->factory->term->create( array(829 'name' => 'Foo',830 'slug' => 'foo',831 'taxonomy' => 'wptests_tax',832 ) );833 834 $t2 = $this->factory->term->create( array(835 'name' => 'Bar',836 'slug' => 'bar',837 'taxonomy' => 'wptests_tax_2',838 ) );839 840 $updated = wp_update_term( $t2, 'wptests_tax_2', array(841 'name' => 'Foo',842 ) );843 844 $this->assertFalse( is_wp_error( $updated ) );845 846 $t2_term = get_term( $t2, 'wptests_tax_2' );847 $this->assertSame( 'Foo', $t2_term->name );848 }849 850 /**851 * @ticket 30780852 */853 public function test_wp_update_term_should_allow_duplicate_names_at_different_levels_of_the_same_taxonomy() {854 register_taxonomy( 'wptests_tax', 'post', array(855 'hierarchical' => true,856 ) );857 858 $t1 = $this->factory->term->create( array(859 'name' => 'Foo',860 'slug' => 'foo',861 'taxonomy' => 'wptests_tax',862 ) );863 864 $t2 = $this->factory->term->create( array(865 'name' => 'Bar',866 'slug' => 'bar',867 'taxonomy' => 'wptests_tax',868 'parent' => $t1,869 ) );870 871 $t3 = $this->factory->term->create( array(872 'name' => 'Bar Child',873 'slug' => 'bar-child',874 'taxonomy' => 'wptests_tax',875 'parent' => $t2,876 ) );877 878 $updated = wp_update_term( $t3, 'wptests_tax', array(879 'name' => 'Bar',880 ) );881 882 $this->assertFalse( is_wp_error( $updated ) );883 884 $t3_term = get_term( $t3, 'wptests_tax' );885 $this->assertSame( 'Bar', $t3_term->name );886 }887 888 /**889 * @ticket 5809890 */891 public function test_wp_update_term_should_split_shared_term() {892 global $wpdb;893 894 register_taxonomy( 'wptests_tax', 'post' );895 register_taxonomy( 'wptests_tax_2', 'post' );896 897 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );898 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );899 900 // Manually modify because split terms shouldn't naturally occur.901 $wpdb->update( $wpdb->term_taxonomy,902 array( 'term_id' => $t1['term_id'] ),903 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),904 array( '%d' ),905 array( '%d' )906 );907 908 $posts = $this->factory->post->create_many( 2 );909 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );910 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );911 912 // Verify that the terms are shared.913 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );914 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );915 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );916 917 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(918 'name' => 'New Foo',919 ) );920 921 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );922 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );923 $this->assertNotEquals( $t1_terms[0]->term_id, $t2_terms[0]->term_id );924 }925 926 /**927 * @ticket 5809928 */929 public function test_wp_update_term_should_not_split_shared_term_before_410_schema_change() {930 global $wpdb;931 932 $db_version = get_option( 'db_version' );933 update_option( 'db_version', 30055 );934 935 register_taxonomy( 'wptests_tax', 'post' );936 register_taxonomy( 'wptests_tax_2', 'post' );937 938 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );939 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );940 941 // Manually modify because split terms shouldn't naturally occur.942 $wpdb->update( $wpdb->term_taxonomy,943 array( 'term_id' => $t1['term_id'] ),944 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),945 array( '%d' ),946 array( '%d' )947 );948 949 $posts = $this->factory->post->create_many( 2 );950 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );951 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );952 953 // Verify that the term is shared.954 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );955 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );956 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );957 958 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(959 'name' => 'New Foo',960 ) );961 962 // Term should still be shared.963 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );964 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );965 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );966 967 update_option( 'db_version', $db_version );968 }969 970 public function test_wp_update_term_alias_of_no_term_group() {971 register_taxonomy( 'wptests_tax', 'post' );972 $t1 = $this->factory->term->create( array(973 'taxonomy' => 'wptests_tax',974 ) );975 $term_1 = get_term( $t1, 'wptests_tax' );976 977 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );978 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(979 'alias_of' => $term_1->slug,980 ) );981 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );982 983 $updated_term_1 = get_term( $t1, 'wptests_tax' );984 _unregister_taxonomy( 'wptests_tax' );985 986 $this->assertSame( 0, $term_1->term_group );987 $this->assertNotEmpty( $created_term->term_group );988 $this->assertSame( $created_term->term_group, $updated_term_1->term_group );989 }990 991 public function test_wp_update_term_alias_of_existing_term_group() {992 register_taxonomy( 'wptests_tax', 'post' );993 $t1 = $this->factory->term->create( array(994 'taxonomy' => 'wptests_tax',995 ) );996 $term_1 = get_term( $t1, 'wptests_tax' );997 998 $t2 = $this->factory->term->create( array(999 'taxonomy' => 'wptests_tax',1000 'alias_of' => $term_1->slug,1001 ) );1002 $term_2 = get_term( $t2, 'wptests_tax' );1003 1004 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );1005 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(1006 'alias_of' => $term_2->slug,1007 ) );1008 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );1009 _unregister_taxonomy( 'wptests_tax' );1010 1011 $this->assertNotEmpty( $created_term->term_group );1012 $this->assertSame( $created_term->term_group, $term_2->term_group );1013 }1014 1015 public function test_wp_update_term_alias_of_nonexistent_term() {1016 register_taxonomy( 'wptests_tax', 'post' );1017 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );1018 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(1019 'alias_of' => 'bar',1020 ) );1021 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );1022 _unregister_taxonomy( 'wptests_tax' );1023 1024 $this->assertSame( 0, $created_term->term_group );1025 }1026 1027 public function test_wp_update_term_slug_same_as_old_slug() {1028 register_taxonomy( 'wptests_tax', 'post' );1029 $t = $this->factory->term->create( array(1030 'taxonomy' => 'wptests_tax',1031 'slug' => 'foo',1032 ) );1033 1034 $found = wp_update_term( $t, 'wptests_tax', array(1035 'slug' => 'foo',1036 ) );1037 1038 $term = get_term( $t, 'wptests_tax' );1039 1040 $this->assertSame( $t, $found['term_id'] );1041 $this->assertSame( 'foo', $term->slug );1042 _unregister_taxonomy( 'wptests_tax' );1043 }1044 1045 public function test_wp_update_term_duplicate_slug_generated_due_to_empty_slug_param() {1046 register_taxonomy( 'wptests_tax', 'post' );1047 $t1 = $this->factory->term->create( array(1048 'taxonomy' => 'wptests_tax',1049 'slug' => 'foo-bar',1050 ) );1051 $t2 = $this->factory->term->create( array(1052 'taxonomy' => 'wptests_tax',1053 'name' => 'not foo bar',1054 ) );1055 1056 $found = wp_update_term( $t2, 'wptests_tax', array(1057 'slug' => '',1058 'name' => 'Foo? Bar!', // Will sanitize to 'foo-bar'.1059 ) );1060 1061 $term = get_term( $t2, 'wptests_tax' );1062 1063 $this->assertSame( $t2, $found['term_id'] );1064 $this->assertSame( 'foo-bar-2', $term->slug );1065 _unregister_taxonomy( 'wptests_tax' );1066 }1067 1068 public function test_wp_update_term_duplicate_slug_with_changed_parent() {1069 register_taxonomy( 'wptests_tax', 'post', array(1070 'hierarchical' => true,1071 ) );1072 $p = $this->factory->term->create( array(1073 'taxonomy' => 'wptests_tax',1074 ) );1075 $t1 = $this->factory->term->create( array(1076 'taxonomy' => 'wptests_tax',1077 'slug' => 'foo-bar',1078 ) );1079 $t2 = $this->factory->term->create( array(1080 'taxonomy' => 'wptests_tax',1081 ) );1082 1083 $found = wp_update_term( $t2, 'wptests_tax', array(1084 'parent' => $p,1085 'slug' => 'foo-bar',1086 ) );1087 1088 $term = get_term( $t2, 'wptests_tax' );1089 $parent_term = get_term( $p, 'wptests_tax' );1090 1091 $this->assertSame( $t2, $found['term_id'] );1092 $this->assertSame( 'foo-bar-' . $parent_term->slug, $term->slug );1093 _unregister_taxonomy( 'wptests_tax' );1094 }1095 1096 public function test_wp_update_term_duplicate_slug_failure() {1097 register_taxonomy( 'wptests_tax', 'post' );1098 $t1 = $this->factory->term->create( array(1099 'taxonomy' => 'wptests_tax',1100 'slug' => 'foo-bar',1101 ) );1102 $t2 = $this->factory->term->create( array(1103 'taxonomy' => 'wptests_tax',1104 'slug' => 'my-old-slug',1105 ) );1106 1107 $found = wp_update_term( $t2, 'wptests_tax', array(1108 'slug' => 'foo-bar',1109 ) );1110 1111 $term = get_term( $t2, 'wptests_tax' );1112 1113 $this->assertWPError( $found );1114 $this->assertSame( 'duplicate_term_slug', $found->get_error_code() );1115 $this->assertSame( 'my-old-slug', $term->slug );1116 _unregister_taxonomy( 'wptests_tax' );1117 }1118 1119 public function test_wp_update_term_should_return_term_id_and_term_taxonomy_id() {1120 register_taxonomy( 'wptests_tax', 'post' );1121 $t = $this->factory->term->create( array(1122 'taxonomy' => 'wptests_tax',1123 ) );1124 $found = wp_update_term( $t, 'wptests_tax', array(1125 'slug' => 'foo',1126 ) );1127 1128 $term_by_id = get_term( $found['term_id'], 'wptests_tax' );1129 $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' );1130 $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' );1131 1132 _unregister_taxonomy( 'wptests_tax' );1133 1134 $this->assertInternalType( 'array', $found );1135 $this->assertNotEmpty( $found['term_id'] );1136 $this->assertNotEmpty( $found['term_taxonomy_id'] );1137 $this->assertNotEmpty( $term_by_id );1138 $this->assertEquals( $term_by_id, $term_by_slug );1139 $this->assertEquals( $term_by_id, $term_by_ttid );1140 }1141 1142 public function test_wp_update_term_should_clean_object_term_cache() {1143 register_taxonomy( 'wptests_tax_for_post', 'post' );1144 register_taxonomy( 'wptests_tax_for_page', 'page' );1145 $post = $this->factory->post->create();1146 $page = $this->factory->post->create( array(1147 'post_type' => 'page',1148 ) );1149 1150 $t_for_post = $this->factory->term->create( array(1151 'taxonomy' => 'wptests_tax_for_post',1152 ) );1153 $t_for_page = $this->factory->term->create( array(1154 'taxonomy' => 'wptests_tax_for_page',1155 ) );1156 1157 wp_set_post_terms( $post, array( $t_for_post ), 'wptests_tax_for_post' );1158 wp_set_post_terms( $page, array( $t_for_page ), 'wptests_tax_for_page' );1159 1160 // Prime caches and verify.1161 update_object_term_cache( array( $post ), 'post' );1162 update_object_term_cache( array( $page ), 'page' );1163 $this->assertNotEmpty( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );1164 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );1165 1166 // Update a term in just one of the taxonomies.1167 $found = wp_update_term( $t_for_post, 'wptests_tax_for_post', array(1168 'slug' => 'foo',1169 ) );1170 1171 // Only the relevant cache should have been cleared.1172 $this->assertFalse( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );1173 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );1174 }1175 1176 public function test_wp_update_term_should_clean_term_cache() {1177 register_taxonomy( 'wptests_tax', 'post', array(1178 'hierarchical' => true,1179 ) );1180 1181 $t1 = $this->factory->term->create( array(1182 'taxonomy' => 'wptests_tax',1183 ) );1184 $t2 = $this->factory->term->create( array(1185 'taxonomy' => 'wptests_tax',1186 ) );1187 1188 /*1189 * It doesn't appear that WordPress itself ever sets these1190 * caches, but we should ensure that they're being cleared for1191 * compatibility with third-party addons. Prime the caches1192 * manually.1193 */1194 wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' );1195 wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' );1196 1197 $found = wp_update_term( $t1, 'wptests_tax', array(1198 'parent' => $t2,1199 ) );1200 _unregister_taxonomy( 'wptests_tax' );1201 1202 $this->assertSame( false, wp_cache_get( 'all_ids', 'wptests_tax' ) );1203 $this->assertSame( false, wp_cache_get( 'get', 'wptests_tax' ) );1204 1205 $cached_children = get_option( 'wptests_tax_children' );1206 $this->assertNotEmpty( $cached_children[ $t2 ] );1207 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t2 ] ) );1208 }1209 1210 /**1211 * @ticket 299111212 */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 53811239 */1240 function test_is_term_type() {1241 // insert a term1242 $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 up1249 $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );1250 }1251 1252 /**1253 * @ticket 216511254 */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 159191263 */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 01267 // this previously returned 21268 $this->assertEquals( 0, $count );1269 }1270 1271 /**1272 * @ticket 265701273 */1274 function test_set_object_terms() {1275 $non_hier = rand_str( 10 );1276 $hier = rand_str( 10 );1277 1278 // Register taxonomies1279 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 ids1373 $this->assertEquals( 3, count($tt) );1374 }1375 1376 // each term should be associated with every post1377 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 51383 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 ids1400 $this->assertEquals( 3, count($tt) );1401 // remember which term has which term_id1402 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 post1409 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 51415 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 taxonomy1425 $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 intact1494 1495 $post_id = $this->factory->post->create();1496 1497 // first set: 3 terms1498 $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 term1507 $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 terms1516 $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 correct1520 $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 terms1524 $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 correct1528 $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' matches1532 $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 intact1538 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 terms1545 $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 correct1549 $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 terms1553 $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 correct1557 $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' matches1561 $this->assertEquals( $tt_1[1], $tt_2[0] );1562 1563 }1564 1565 /**1566 * @ticket 154751567 */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.php1596 */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 exists1629 $this->assertTrue( term_exists($term) > 0 );1630 $this->assertTrue( term_exists($t) > 0 );1631 1632 // now delete it1633 $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 165501641 */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 data1677 $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 unchanged1692 $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 term1698 $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 up1715 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 258521721 */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 225601741 */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 241891770 */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 310861796 */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 310861809 */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 192051825 */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 /**1836 * @ticket 307801837 */1838 public function test_wp_update_term_should_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_term_with_the_same_slug() {1839 register_taxonomy( 'wptests_tax', 'post', array(1840 'hierarchical' => true,1841 ) );1842 register_taxonomy( 'wptests_tax_2', 'post', array(1843 'hierarchical' => true,1844 ) );1845 1846 $t1 = $this->factory->term->create( array(1847 'taxonomy' => 'wptests_tax',1848 'slug' => 'parent-term',1849 ) );1850 1851 $t2 = $this->factory->term->create( array(1852 'taxonomy' => 'wptests_tax',1853 'slug' => 'foo',1854 ) );1855 1856 wp_update_term( $t2, 'wptests_tax', array(1857 'parent' => $t1,1858 ) );1859 1860 $t2_term = get_term( $t2, 'wptests_tax' );1861 1862 $this->assertSame( 'foo', $t2_term->slug );1863 1864 _unregister_taxonomy( 'wptests_tax' );1865 }1866 1867 /**1868 * @ticket 307801869 */1870 public function test_wp_update_term_should_not_assign_new_slug_when_reassigning_parent_as_long_as_there_is_no_other_slug_conflict_within_the_taxonomy() {1871 register_taxonomy( 'wptests_tax', 'post', array(1872 'hierarchical' => true,1873 ) );1874 register_taxonomy( 'wptests_tax_2', 'post', array(1875 'hierarchical' => true,1876 ) );1877 1878 $t1 = $this->factory->term->create( array(1879 'taxonomy' => 'wptests_tax',1880 'slug' => 'parent-term',1881 ) );1882 1883 // Same slug but in a different tax.1884 $t2 = $this->factory->term->create( array(1885 'taxonomy' => 'wptests_tax_2',1886 'slug' => 'foo',1887 ) );1888 1889 $t3 = $this->factory->term->create( array(1890 'taxonomy' => 'wptests_tax',1891 'slug' => 'foo',1892 ) );1893 1894 wp_update_term( $t3, 'wptests_tax', array(1895 'parent' => $t1,1896 ) );1897 1898 $t3_term = get_term( $t3, 'wptests_tax' );1899 1900 $this->assertSame( 'foo', $t3_term->slug );1901 1902 _unregister_taxonomy( 'wptests_tax' );1903 }1904 1905 622 /** Helpers **********************************************************/ 623 624 public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) { 625 $this->assertInternalType( 'object', $deleted_term ); 626 $this->assertInternalType( 'int', $term ); 627 // Pesky string $this->assertInternalType( 'int', $tt_id ); 628 $this->assertEquals( $term, $deleted_term->term_id ); 629 $this->assertEquals( $taxonomy, $deleted_term->taxonomy ); 630 $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id ); 631 } 1906 632 1907 633 public function _pre_insert_term_callback() { -
trunk/tests/phpunit/tests/term/wpUpdateTerm.php
r31811 r31812 4 4 * @group taxonomy 5 5 */ 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 6 class Tests_Term_WpUpdateTerm extends WP_UnitTestCase { 630 7 public function test_wp_update_term_taxonomy_does_not_exist() { 631 8 $found = wp_update_term( 1, 'bar' ); … … 1209 586 1210 587 /** 1211 * @ticket 299111212 */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 53811239 */1240 function test_is_term_type() {1241 // insert a term1242 $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 up1249 $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );1250 }1251 1252 /**1253 * @ticket 216511254 */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 159191263 */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 01267 // this previously returned 21268 $this->assertEquals( 0, $count );1269 }1270 1271 /**1272 * @ticket 265701273 */1274 function test_set_object_terms() {1275 $non_hier = rand_str( 10 );1276 $hier = rand_str( 10 );1277 1278 // Register taxonomies1279 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 ids1373 $this->assertEquals( 3, count($tt) );1374 }1375 1376 // each term should be associated with every post1377 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 51383 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 ids1400 $this->assertEquals( 3, count($tt) );1401 // remember which term has which term_id1402 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 post1409 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 51415 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 taxonomy1425 $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 intact1494 1495 $post_id = $this->factory->post->create();1496 1497 // first set: 3 terms1498 $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 term1507 $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 terms1516 $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 correct1520 $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 terms1524 $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 correct1528 $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' matches1532 $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 intact1538 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 terms1545 $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 correct1549 $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 terms1553 $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 correct1557 $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' matches1561 $this->assertEquals( $tt_1[1], $tt_2[0] );1562 1563 }1564 1565 /**1566 * @ticket 154751567 */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.php1596 */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 exists1629 $this->assertTrue( term_exists($term) > 0 );1630 $this->assertTrue( term_exists($t) > 0 );1631 1632 // now delete it1633 $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 165501641 */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 data1677 $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 unchanged1692 $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 term1698 $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 up1715 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 258521721 */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 225601741 */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 241891770 */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 310861796 */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 310861809 */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 192051825 */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 /**1836 588 * @ticket 30780 1837 589 */ … … 1902 654 _unregister_taxonomy( 'wptests_tax' ); 1903 655 } 1904 1905 /** Helpers **********************************************************/1906 1907 public function _pre_insert_term_callback() {1908 return new WP_Error( 'custom_error' );1909 }1910 656 }
Note: See TracChangeset
for help on using the changeset viewer.