Changeset 28953 for trunk/tests/phpunit/tests/term.php
- Timestamp:
- 07/02/2014 01:49:41 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term.php
r28951 r28953 89 89 90 90 /** 91 * @ticket 21651 92 */ 93 function test_get_term_by_tt_id() { 94 $term1 = wp_insert_term( 'Foo', 'category' ); 95 $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' ); 96 $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); 97 } 98 99 /** 100 * @ticket 15919 101 */ 102 function test_wp_count_terms() { 103 $count = wp_count_terms( 'category', array( 'hide_empty' => true ) ); 104 // the terms inserted in setUp aren't attached to any posts, so should return 0 105 // this previously returned 2 106 $this->assertEquals( 0, $count ); 107 } 108 109 /** 91 110 * @ticket 26339 92 111 */ … … 108 127 // all terms should still be objects 109 128 return $terms; 129 } 130 131 function test_get_object_terms_by_slug() { 132 $post_id = $this->factory->post->create(); 133 134 $terms_1 = array('Foo', 'Bar', 'Baz'); 135 $terms_1_slugs = array('foo', 'bar', 'baz'); 136 137 // set the initial terms 138 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 139 $this->assertEquals( 3, count($tt_1) ); 140 141 // make sure they're correct 142 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't.term_id')); 143 $this->assertEquals( $terms_1_slugs, $terms ); 144 } 145 146 /** 147 * @ticket 11003 148 */ 149 function test_wp_get_object_terms_no_dupes() { 150 $post_id1 = $this->factory->post->create(); 151 $post_id2 = $this->factory->post->create(); 152 $cat_id = $this->factory->category->create(); 153 $cat_id2 = $this->factory->category->create(); 154 wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) ); 155 wp_set_post_categories( $post_id2, $cat_id ); 156 157 $terms = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category' ); 158 $this->assertCount( 2, $terms ); 159 $this->assertEquals( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) ); 160 161 $terms2 = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category', array( 162 'fields' => 'all_with_object_id' 163 ) ); 164 165 $this->assertCount( 3, $terms2 ); 166 $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) ); 167 } 168 169 /** 170 * @ticket 17646 171 */ 172 function test_get_object_terms_types() { 173 $post_id = $this->factory->post->create(); 174 $term = wp_insert_term( 'one', $this->taxonomy ); 175 wp_set_object_terms( $post_id, $term, $this->taxonomy ); 176 177 $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) ); 178 $term = array_shift( $terms ); 179 $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); 180 foreach ( $int_fields as $field ) 181 $this->assertInternalType( 'int', $term->$field, $field ); 182 183 $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'ids' ) ); 184 $term = array_shift( $terms ); 185 $this->assertInternalType( 'int', $term, 'term' ); 110 186 } 111 187 … … 260 336 } 261 337 262 function test_change_object_terms_by_name() { 263 // set some terms on an object; then change them while leaving one intact 264 265 $post_id = $this->factory->post->create(); 266 267 $terms_1 = array('foo', 'bar', 'baz'); 268 $terms_2 = array('bar', 'bing'); 269 270 // set the initial terms 271 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 272 $this->assertEquals( 3, count($tt_1) ); 273 274 // make sure they're correct 275 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 276 $this->assertEquals( $terms_1, $terms ); 277 278 // change the terms 279 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 280 $this->assertEquals( 2, count($tt_2) ); 281 282 // make sure they're correct 283 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 284 $this->assertEquals( $terms_2, $terms ); 285 286 // make sure the tt id for 'bar' matches 287 $this->assertEquals( $tt_1[1], $tt_2[0] ); 288 289 } 290 291 /** 292 * @ticket 22560 293 */ 294 function test_object_term_cache() { 295 $post_id = $this->factory->post->create(); 296 297 $terms_1 = array('foo', 'bar', 'baz'); 298 $terms_2 = array('bar', 'bing'); 299 300 // Cache should be empty after a set. 301 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 302 $this->assertEquals( 3, count($tt_1) ); 303 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 304 305 // wp_get_object_terms() does not prime the cache. 306 wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') ); 307 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 308 309 // get_the_terms() does prime the cache. 310 $terms = get_the_terms( $post_id, $this->taxonomy ); 311 $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships'); 312 $this->assertInternalType( 'array', $cache ); 313 314 // Cache should be empty after a set. 315 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 316 $this->assertEquals( 2, count($tt_2) ); 317 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 338 function test_set_object_terms_invalid() { 339 $post_id = $this->factory->post->create(); 340 341 // bogus taxonomy 342 $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() ); 343 $this->assertTrue( is_wp_error($result) ); 318 344 } 319 345 … … 362 388 } 363 389 364 function test_get_object_terms_by_slug() { 365 $post_id = $this->factory->post->create(); 366 367 $terms_1 = array('Foo', 'Bar', 'Baz'); 368 $terms_1_slugs = array('foo', 'bar', 'baz'); 390 function test_change_object_terms_by_name() { 391 // set some terms on an object; then change them while leaving one intact 392 393 $post_id = $this->factory->post->create(); 394 395 $terms_1 = array('foo', 'bar', 'baz'); 396 $terms_2 = array('bar', 'bing'); 369 397 370 398 // set the initial terms … … 373 401 374 402 // make sure they're correct 375 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't.term_id')); 376 $this->assertEquals( $terms_1_slugs, $terms ); 377 } 378 379 function test_set_object_terms_invalid() { 380 $post_id = $this->factory->post->create(); 381 382 // bogus taxonomy 383 $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() ); 384 $this->assertTrue( is_wp_error($result) ); 403 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 404 $this->assertEquals( $terms_1, $terms ); 405 406 // change the terms 407 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 408 $this->assertEquals( 2, count($tt_2) ); 409 410 // make sure they're correct 411 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id')); 412 $this->assertEquals( $terms_2, $terms ); 413 414 // make sure the tt id for 'bar' matches 415 $this->assertEquals( $tt_1[1], $tt_2[0] ); 416 385 417 } 386 418 … … 457 489 $this->assertNull( term_exists($t) ); 458 490 $this->assertEquals( $initial_count, wp_count_terms('category') ); 491 } 492 493 /** 494 * @ticket 16550 495 */ 496 function test_wp_set_post_categories() { 497 $post_id = $this->factory->post->create(); 498 $post = get_post( $post_id ); 499 500 $this->assertInternalType( 'array', $post->post_category ); 501 $this->assertEquals( 1, count( $post->post_category ) ); 502 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 503 $term1 = wp_insert_term( 'Foo', 'category' ); 504 $term2 = wp_insert_term( 'Bar', 'category' ); 505 $term3 = wp_insert_term( 'Baz', 'category' ); 506 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) ); 507 $this->assertEquals( 2, count( $post->post_category ) ); 508 $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category ); 509 510 wp_set_post_categories( $post_id, $term3['term_id'], true ); 511 $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category ); 512 513 $term4 = wp_insert_term( 'Burrito', 'category' ); 514 wp_set_post_categories( $post_id, $term4['term_id'] ); 515 $this->assertEquals( array( $term4['term_id'] ), $post->post_category ); 516 517 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true ); 518 $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category ); 519 520 wp_set_post_categories( $post_id, array(), true ); 521 $this->assertEquals( 1, count( $post->post_category ) ); 522 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 523 524 wp_set_post_categories( $post_id, array() ); 525 $this->assertEquals( 1, count( $post->post_category ) ); 526 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 459 527 } 460 528 … … 550 618 551 619 /** 552 * @ticket 17646553 */554 function test_get_object_terms_types() {555 $post_id = $this->factory->post->create();556 $term = wp_insert_term( 'one', $this->taxonomy );557 wp_set_object_terms( $post_id, $term, $this->taxonomy );558 559 $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) );560 $term = array_shift( $terms );561 $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );562 foreach ( $int_fields as $field )563 $this->assertInternalType( 'int', $term->$field, $field );564 565 $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'ids' ) );566 $term = array_shift( $terms );567 $this->assertInternalType( 'int', $term, 'term' );568 }569 570 /**571 620 * @ticket 25852 572 621 */ … … 589 638 590 639 /** 640 * @ticket 22560 641 */ 642 function test_object_term_cache() { 643 $post_id = $this->factory->post->create(); 644 645 $terms_1 = array('foo', 'bar', 'baz'); 646 $terms_2 = array('bar', 'bing'); 647 648 // Cache should be empty after a set. 649 $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); 650 $this->assertEquals( 3, count($tt_1) ); 651 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 652 653 // wp_get_object_terms() does not prime the cache. 654 wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') ); 655 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 656 657 // get_the_terms() does prime the cache. 658 $terms = get_the_terms( $post_id, $this->taxonomy ); 659 $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships'); 660 $this->assertInternalType( 'array', $cache ); 661 662 // Cache should be empty after a set. 663 $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy ); 664 $this->assertEquals( 2, count($tt_2) ); 665 $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') ); 666 } 667 668 /** 591 669 * @ticket 24189 592 670 */ … … 613 691 $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); 614 692 } 615 616 function test_wp_set_post_categories() {617 $post_id = $this->factory->post->create();618 $post = get_post( $post_id );619 620 $this->assertInternalType( 'array', $post->post_category );621 $this->assertEquals( 1, count( $post->post_category ) );622 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );623 $term1 = wp_insert_term( 'Foo', 'category' );624 $term2 = wp_insert_term( 'Bar', 'category' );625 $term3 = wp_insert_term( 'Baz', 'category' );626 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );627 $this->assertEquals( 2, count( $post->post_category ) );628 $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );629 630 wp_set_post_categories( $post_id, $term3['term_id'], true );631 $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );632 633 $term4 = wp_insert_term( 'Burrito', 'category' );634 wp_set_post_categories( $post_id, $term4['term_id'] );635 $this->assertEquals( array( $term4['term_id'] ), $post->post_category );636 637 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );638 $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );639 640 wp_set_post_categories( $post_id, array(), true );641 $this->assertEquals( 1, count( $post->post_category ) );642 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );643 644 wp_set_post_categories( $post_id, array() );645 $this->assertEquals( 1, count( $post->post_category ) );646 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );647 }648 649 function test_get_term_by_tt_id() {650 $term1 = wp_insert_term( 'Foo', 'category' );651 $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );652 $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );653 }654 655 function test_wp_count_terms() {656 $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );657 // the terms inserted in setUp aren't attached to any posts, so should return 0658 // this previously returned 2659 $this->assertEquals( 0, $count );660 }661 662 function test_wp_get_object_terms_no_dupes() {663 $post_id1 = $this->factory->post->create();664 $post_id2 = $this->factory->post->create();665 $cat_id = $this->factory->category->create();666 $cat_id2 = $this->factory->category->create();667 wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );668 wp_set_post_categories( $post_id2, $cat_id );669 670 $terms = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category' );671 $this->assertCount( 2, $terms );672 $this->assertEquals( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) );673 674 $terms2 = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category', array(675 'fields' => 'all_with_object_id'676 ) );677 678 $this->assertCount( 3, $terms2 );679 $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );680 }681 693 }
Note: See TracChangeset
for help on using the changeset viewer.