Make WordPress Core


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

Move and tests to their own files.

File:
1 copied

Legend:

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

    r31811 r31812  
    44 * @group taxonomy
    55 */
    6 class Tests_Term extends WP_UnitTestCase {
    7     var $taxonomy = 'category';
    8 
    9     function setUp() {
     6class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
     7    public function setUp() {
    108        parent::setUp();
    119
     
    1816    }
    1917
    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
    3022        // a new unused term
    3123        $term = rand_str();
    3224        $this->assertNull( term_exists($term) );
    3325
    34         $initial_count = wp_count_terms( $this->taxonomy );
    35 
    36         $t = wp_insert_term( $term, $this->taxonomy );
     26        $initial_count = wp_count_terms( $taxonomy );
     27
     28        $t = wp_insert_term( $term, $taxonomy );
    3729        $this->assertInternalType( 'array', $t );
    3830        $this->assertFalse( is_wp_error($t) );
    3931        $this->assertTrue( $t['term_id'] > 0 );
    4032        $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 ) );
    4234
    4335        // make sure the term exists
     
    4739        // now delete it
    4840        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 ) );
    5042        remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
    5143        $this->assertNull( term_exists($term) );
    5244        $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 ) );
    5446    }
    5547
     
    628620    }
    629621
    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 29614
    693      */
    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 5809
    767      */
    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 5809
    793      */
    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 30780
    823      */
    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 30780
    852      */
    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 5809
    890      */
    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 5809
    928      */
    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 these
    1190          * caches, but we should ensure that they're being cleared for
    1191          * compatibility with third-party addons. Prime the caches
    1192          * 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 29911
    1212      */
    1213     public function test_wp_delete_term_should_invalidate_cache_for_child_terms() {
    1214         register_taxonomy( 'wptests_tax', 'post', array(
    1215             'hierarchical' => true,
    1216         ) );
    1217 
    1218         $parent = $this->factory->term->create( array(
    1219             'taxonomy' => 'wptests_tax',
    1220         ) );
    1221 
    1222         $child = $this->factory->term->create( array(
    1223             'taxonomy' => 'wptests_tax',
    1224             'parent' => $parent,
    1225             'slug' => 'foo',
    1226         ) );
    1227 
    1228         // Prime the cache.
    1229         $child_term = get_term( $child, 'wptests_tax' );
    1230         $this->assertSame( $parent, $child_term->parent );
    1231 
    1232         wp_delete_term( $parent, 'wptests_tax' );
    1233         $child_term = get_term( $child, 'wptests_tax' );
    1234         $this->assertSame( 0, $child_term->parent );
    1235     }
    1236 
    1237     /**
    1238      * @ticket 5381
    1239      */
    1240     function test_is_term_type() {
    1241         // insert a term
    1242         $term = rand_str();
    1243         $t = wp_insert_term( $term, $this->taxonomy );
    1244         $this->assertInternalType( 'array', $t );
    1245         $term_obj = get_term_by('name', $term, $this->taxonomy);
    1246         $this->assertEquals( $t['term_id'], term_exists($term_obj->slug) );
    1247 
    1248         // clean up
    1249         $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
    1250     }
    1251 
    1252     /**
    1253      * @ticket 21651
    1254      */
    1255     function test_get_term_by_tt_id() {
    1256         $term1 = wp_insert_term( 'Foo', 'category' );
    1257         $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
    1258         $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
    1259     }
    1260 
    1261     /**
    1262      * @ticket 15919
    1263      */
    1264     function test_wp_count_terms() {
    1265         $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
    1266         // the terms inserted in setUp aren't attached to any posts, so should return 0
    1267         // this previously returned 2
    1268         $this->assertEquals( 0, $count );
    1269     }
    1270 
    1271     /**
    1272      * @ticket 26570
    1273      */
    1274     function test_set_object_terms() {
    1275         $non_hier = rand_str( 10 );
    1276         $hier     = rand_str( 10 );
    1277 
    1278         // Register taxonomies
    1279         register_taxonomy( $non_hier, array() );
    1280         register_taxonomy( $hier, array( 'hierarchical' => true ) );
    1281 
    1282         // Create a post.
    1283         $post_id = $this->factory->post->create();
    1284 
    1285         /*
    1286          * Set a single term (non-hierarchical) by ID.
    1287          */
    1288         $tag = wp_insert_term( 'Foo', $non_hier );
    1289         $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
    1290 
    1291         wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
    1292         $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
    1293 
    1294         /*
    1295          * Set a single term (non-hierarchical) by slug.
    1296          */
    1297         $tag = wp_insert_term( 'Bar', $non_hier );
    1298         $tag = get_term( $tag['term_id'], $non_hier );
    1299 
    1300         $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) );
    1301 
    1302         wp_set_object_terms( $post_id, $tag->slug, $non_hier );
    1303         $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) );
    1304 
    1305         /*
    1306          * Set a single term (hierarchical) by ID.
    1307          */
    1308         $cat = wp_insert_term( 'Baz', $hier );
    1309         $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) );
    1310 
    1311         wp_set_object_terms( $post_id, $cat['term_id'], $hier );
    1312         $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) );
    1313 
    1314         /*
    1315          * Set a single term (hierarchical) by slug.
    1316          */
    1317         $cat = wp_insert_term( 'Qux', $hier );
    1318         $cat = get_term( $cat['term_id'], $hier );
    1319 
    1320         $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
    1321 
    1322         wp_set_object_terms( $post_id, $cat->slug, $hier );
    1323         $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
    1324 
    1325         /*
    1326          * Set an array of term IDs (non-hierarchical) by ID.
    1327          */
    1328         $tag1 = wp_insert_term( '_tag1', $non_hier );
    1329         $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
    1330 
    1331         $tag2 = wp_insert_term( '_tag2', $non_hier );
    1332         $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
    1333 
    1334         $tag3 = wp_insert_term( '_tag3', $non_hier );
    1335         $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
    1336 
    1337         wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
    1338         $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
    1339 
    1340         /*
    1341          * Set an array of term slugs (hierarchical) by slug.
    1342          */
    1343         $cat1 = wp_insert_term( '_cat1', $hier );
    1344         $cat1 = get_term( $cat1['term_id'], $hier );
    1345         $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
    1346 
    1347         $cat2 = wp_insert_term( '_cat2', $hier );
    1348         $cat2 = get_term( $cat2['term_id'], $hier );
    1349         $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
    1350 
    1351         $cat3 = wp_insert_term( '_cat3', $hier );
    1352         $cat3 = get_term( $cat3['term_id'], $hier );
    1353         $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
    1354 
    1355         wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
    1356         $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
    1357     }
    1358 
    1359     function test_set_object_terms_by_id() {
    1360         $ids = $this->factory->post->create_many(5);
    1361 
    1362         $terms = array();
    1363         for ($i=0; $i<3; $i++ ) {
    1364             $term = rand_str();
    1365             $result = wp_insert_term( $term, $this->taxonomy );
    1366             $this->assertInternalType( 'array', $result );
    1367             $term_id[$term] = $result['term_id'];
    1368         }
    1369 
    1370         foreach ($ids as $id) {
    1371                 $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
    1372                 // should return three term taxonomy ids
    1373                 $this->assertEquals( 3, count($tt) );
    1374         }
    1375 
    1376         // each term should be associated with every post
    1377         foreach ($term_id as $term=>$id) {
    1378             $actual = get_objects_in_term($id, $this->taxonomy);
    1379             $this->assertEquals( $ids, array_map('intval', $actual) );
    1380         }
    1381 
    1382         // each term should have a count of 5
    1383         foreach (array_keys($term_id) as $term) {
    1384             $t = get_term_by('name', $term, $this->taxonomy);
    1385             $this->assertEquals( 5, $t->count );
    1386         }
    1387     }
    1388 
    1389     function test_set_object_terms_by_name() {
    1390         $ids = $this->factory->post->create_many(5);
    1391 
    1392         $terms = array(
    1393                 rand_str(),
    1394                 rand_str(),
    1395                 rand_str());
    1396 
    1397         foreach ($ids as $id) {
    1398                 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
    1399                 // should return three term taxonomy ids
    1400                 $this->assertEquals( 3, count($tt) );
    1401                 // remember which term has which term_id
    1402                 for ($i=0; $i<3; $i++) {
    1403                     $term = get_term_by('name', $terms[$i], $this->taxonomy);
    1404                     $term_id[$terms[$i]] = intval($term->term_id);
    1405                 }
    1406         }
    1407 
    1408         // each term should be associated with every post
    1409         foreach ($term_id as $term=>$id) {
    1410             $actual = get_objects_in_term($id, $this->taxonomy);
    1411             $this->assertEquals( $ids, array_map('intval', $actual) );
    1412         }
    1413 
    1414         // each term should have a count of 5
    1415         foreach ($terms as $term) {
    1416             $t = get_term_by('name', $term, $this->taxonomy);
    1417             $this->assertEquals( 5, $t->count );
    1418         }
    1419     }
    1420 
    1421     function test_set_object_terms_invalid() {
    1422         $post_id = $this->factory->post->create();
    1423 
    1424         // bogus taxonomy
    1425         $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() );
    1426         $this->assertTrue( is_wp_error($result) );
    1427     }
    1428 
    1429     public function test_wp_set_object_terms_append_true() {
    1430         register_taxonomy( 'wptests_tax', 'post' );
    1431         $p = $this->factory->post->create();
    1432         $t1 = $this->factory->term->create( array(
    1433             'taxonomy' => 'wptests_tax',
    1434         ) );
    1435         $t2 = $this->factory->term->create( array(
    1436             'taxonomy' => 'wptests_tax',
    1437         ) );
    1438 
    1439         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1440         $this->assertNotEmpty( $added1 );
    1441         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1442 
    1443         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', true );
    1444         $this->assertNotEmpty( $added2 );
    1445         $this->assertEqualSets( array( $t1, $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1446 
    1447         _unregister_taxonomy( 'wptests_tax' );
    1448     }
    1449 
    1450     public function test_wp_set_object_terms_append_false() {
    1451         register_taxonomy( 'wptests_tax', 'post' );
    1452         $p = $this->factory->post->create();
    1453         $t1 = $this->factory->term->create( array(
    1454             'taxonomy' => 'wptests_tax',
    1455         ) );
    1456         $t2 = $this->factory->term->create( array(
    1457             'taxonomy' => 'wptests_tax',
    1458         ) );
    1459 
    1460         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1461         $this->assertNotEmpty( $added1 );
    1462         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1463 
    1464         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', false );
    1465         $this->assertNotEmpty( $added2 );
    1466         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1467 
    1468         _unregister_taxonomy( 'wptests_tax' );
    1469     }
    1470 
    1471     public function test_wp_set_object_terms_append_default_to_false() {
    1472         register_taxonomy( 'wptests_tax', 'post' );
    1473         $p = $this->factory->post->create();
    1474         $t1 = $this->factory->term->create( array(
    1475             'taxonomy' => 'wptests_tax',
    1476         ) );
    1477         $t2 = $this->factory->term->create( array(
    1478             'taxonomy' => 'wptests_tax',
    1479         ) );
    1480 
    1481         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    1482         $this->assertNotEmpty( $added1 );
    1483         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1484 
    1485         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax' );
    1486         $this->assertNotEmpty( $added2 );
    1487         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    1488 
    1489         _unregister_taxonomy( 'wptests_tax' );
    1490     }
    1491 
    1492     function test_change_object_terms_by_id() {
    1493         // set some terms on an object; then change them while leaving one intact
    1494 
    1495         $post_id = $this->factory->post->create();
    1496 
    1497         // first set: 3 terms
    1498         $terms_1 = array();
    1499         for ($i=0; $i<3; $i++ ) {
    1500             $term = rand_str();
    1501             $result = wp_insert_term( $term, $this->taxonomy );
    1502             $this->assertInternalType( 'array', $result );
    1503             $terms_1[$i] = $result['term_id'];
    1504         }
    1505 
    1506         // second set: one of the original terms, plus one new term
    1507         $terms_2 = array();
    1508         $terms_2[0] = $terms_1[1];
    1509 
    1510         $term = rand_str();
    1511         $result = wp_insert_term( $term, $this->taxonomy );
    1512         $terms_2[1] = $result['term_id'];
    1513 
    1514 
    1515         // set the initial terms
    1516         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1517         $this->assertEquals( 3, count($tt_1) );
    1518 
    1519         // make sure they're correct
    1520         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    1521         $this->assertEquals( $terms_1, $terms );
    1522 
    1523         // change the terms
    1524         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1525         $this->assertEquals( 2, count($tt_2) );
    1526 
    1527         // make sure they're correct
    1528         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    1529         $this->assertEquals( $terms_2, $terms );
    1530 
    1531         // make sure the tt id for 'bar' matches
    1532         $this->assertEquals( $tt_1[1], $tt_2[0] );
    1533 
    1534     }
    1535 
    1536     function test_change_object_terms_by_name() {
    1537         // set some terms on an object; then change them while leaving one intact
    1538 
    1539         $post_id = $this->factory->post->create();
    1540 
    1541         $terms_1 = array('foo', 'bar', 'baz');
    1542         $terms_2 = array('bar', 'bing');
    1543 
    1544         // set the initial terms
    1545         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1546         $this->assertEquals( 3, count($tt_1) );
    1547 
    1548         // make sure they're correct
    1549         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    1550         $this->assertEquals( $terms_1, $terms );
    1551 
    1552         // change the terms
    1553         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1554         $this->assertEquals( 2, count($tt_2) );
    1555 
    1556         // make sure they're correct
    1557         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    1558         $this->assertEquals( $terms_2, $terms );
    1559 
    1560         // make sure the tt id for 'bar' matches
    1561         $this->assertEquals( $tt_1[1], $tt_2[0] );
    1562 
    1563     }
    1564 
    1565     /**
    1566      * @ticket 15475
    1567      */
    1568     function test_wp_add_remove_object_terms() {
    1569         $posts = $this->factory->post->create_many( 5 );
    1570         $tags = $this->factory->tag->create_many( 5 );
    1571 
    1572         $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
    1573         $this->assertEquals( 1, count( $tt ) );
    1574         $this->assertEquals( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
    1575 
    1576         $three_tags = array( $tags[0], $tags[1], $tags[2] );
    1577         $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
    1578         $this->assertEquals( 3, count( $tt ) );
    1579         $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
    1580 
    1581         $this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
    1582         $this->assertFalse( wp_remove_object_terms( $posts[0], $tags[0], 'post_tag' ) );
    1583         $this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
    1584         $this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
    1585         $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
    1586 
    1587         foreach ( $tags as $term_id )
    1588             $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
    1589 
    1590         foreach ( $posts as $post_id )
    1591             $this->assertTrue( (bool) wp_delete_post( $post_id, true ) );
    1592     }
    1593 
    1594     /**
    1595      * @group category.php
    1596      */
    1597     function test_term_is_ancestor_of( ) {
    1598         $term = rand_str();
    1599         $term2 = rand_str();
    1600 
    1601         $t = wp_insert_term( $term, 'category' );
    1602         $this->assertInternalType( 'array', $t );
    1603         $t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
    1604         $this->assertInternalType( 'array', $t2 );
    1605         if ( function_exists( 'term_is_ancestor_of' ) ) {
    1606             $this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
    1607             $this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
    1608         }
    1609         $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id']) );
    1610         $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id']) );
    1611 
    1612         wp_delete_term($t['term_id'], 'category');
    1613         wp_delete_term($t2['term_id'], 'category');
    1614     }
    1615 
    1616     function test_wp_insert_delete_category() {
    1617         $term = rand_str();
    1618         $this->assertNull( category_exists( $term ) );
    1619 
    1620         $initial_count = wp_count_terms( 'category' );
    1621 
    1622         $t = wp_insert_category( array( 'cat_name' => $term ) );
    1623         $this->assertTrue( is_numeric($t) );
    1624         $this->assertFalse( is_wp_error($t) );
    1625         $this->assertTrue( $t > 0 );
    1626         $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
    1627 
    1628         // make sure the term exists
    1629         $this->assertTrue( term_exists($term) > 0 );
    1630         $this->assertTrue( term_exists($t) > 0 );
    1631 
    1632         // now delete it
    1633         $this->assertTrue( wp_delete_category($t) );
    1634         $this->assertNull( term_exists($term) );
    1635         $this->assertNull( term_exists($t) );
    1636         $this->assertEquals( $initial_count, wp_count_terms('category') );
    1637     }
    1638 
    1639     /**
    1640      * @ticket 16550
    1641      */
    1642     function test_wp_set_post_categories() {
    1643         $post_id = $this->factory->post->create();
    1644         $post = get_post( $post_id );
    1645 
    1646         $this->assertInternalType( 'array', $post->post_category );
    1647         $this->assertEquals( 1, count( $post->post_category ) );
    1648         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1649         $term1 = wp_insert_term( 'Foo', 'category' );
    1650         $term2 = wp_insert_term( 'Bar', 'category' );
    1651         $term3 = wp_insert_term( 'Baz', 'category' );
    1652         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
    1653         $this->assertEquals( 2, count( $post->post_category ) );
    1654         $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );
    1655 
    1656         wp_set_post_categories( $post_id, $term3['term_id'], true );
    1657         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );
    1658 
    1659         $term4 = wp_insert_term( 'Burrito', 'category' );
    1660         wp_set_post_categories( $post_id, $term4['term_id'] );
    1661         $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
    1662 
    1663         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
    1664         $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
    1665 
    1666         wp_set_post_categories( $post_id, array(), true );
    1667         $this->assertEquals( 1, count( $post->post_category ) );
    1668         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1669 
    1670         wp_set_post_categories( $post_id, array() );
    1671         $this->assertEquals( 1, count( $post->post_category ) );
    1672         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    1673     }
    1674 
    1675     function test_wp_unique_term_slug() {
    1676         // set up test data
    1677         $a = wp_insert_term( 'parent', $this->taxonomy );
    1678         $this->assertInternalType( 'array', $a );
    1679         $b = wp_insert_term( 'child',  $this->taxonomy, array( 'parent' => $a['term_id'] ) );
    1680         $this->assertInternalType( 'array', $b );
    1681         $c = wp_insert_term( 'neighbor', $this->taxonomy );
    1682         $this->assertInternalType( 'array', $c );
    1683         $d = wp_insert_term( 'pet',  $this->taxonomy, array( 'parent' => $c['term_id'] )  );
    1684         $this->assertInternalType( 'array', $c );
    1685 
    1686         $a_term = get_term( $a['term_id'], $this->taxonomy );
    1687         $b_term = get_term( $b['term_id'], $this->taxonomy );
    1688         $c_term = get_term( $c['term_id'], $this->taxonomy );
    1689         $d_term = get_term( $d['term_id'], $this->taxonomy );
    1690 
    1691         // a unique slug gets unchanged
    1692         $this->assertEquals( 'unique-term', wp_unique_term_slug( 'unique-term', $c_term ) );
    1693 
    1694         // a non-hierarchicial dupe gets suffixed with "-#"
    1695         $this->assertEquals( 'parent-2', wp_unique_term_slug( 'parent', $c_term ) );
    1696 
    1697         // a hierarchical dupe initially gets suffixed with its parent term
    1698         $this->assertEquals( 'child-neighbor', wp_unique_term_slug( 'child', $d_term ) );
    1699 
    1700         // a hierarchical dupe whose parent already contains the {term}-{parent term}
    1701         // term gets suffixed with parent term name and then '-#'
    1702         $e = wp_insert_term( 'child-neighbor', $this->taxonomy, array( 'parent' => $c['term_id'] ) );
    1703         $this->assertEquals( 'child-neighbor-2', wp_unique_term_slug( 'child', $d_term ) );
    1704 
    1705         $f = wp_insert_term( 'foo', $this->taxonomy );
    1706         $this->assertInternalType( 'array', $f );
    1707         $f_term = get_term( $f['term_id'], $this->taxonomy );
    1708         $this->assertEquals( 'foo', $f_term->slug );
    1709         $this->assertEquals( 'foo', wp_unique_term_slug(  'foo', $f_term ) );
    1710 
    1711         $g = wp_insert_term( 'foo',  $this->taxonomy );
    1712         $this->assertInstanceOf( 'WP_Error', $g );
    1713 
    1714         // clean up
    1715         foreach ( array( $a, $b, $c, $d, $e, $f ) as $t )
    1716             $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) );
    1717     }
    1718 
    1719     /**
    1720      * @ticket 25852
    1721      */
    1722     function test_sanitize_term_field() {
    1723         $term = wp_insert_term( 'foo', $this->taxonomy );
    1724 
    1725         $this->assertEquals( 0, sanitize_term_field( 'parent',  0, $term['term_id'], $this->taxonomy, 'raw' ) );
    1726         $this->assertEquals( 1, sanitize_term_field( 'parent',  1, $term['term_id'], $this->taxonomy, 'raw' ) );
    1727         $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
    1728         $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
    1729     }
    1730 
    1731     private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) {
    1732         $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array(
    1733             'fields' => 'ids'
    1734         ) );
    1735 
    1736         $this->assertEquals( $expected_term_ids, $assigned_term_ids );
    1737     }
    1738 
    1739     /**
    1740      * @ticket 22560
    1741      */
    1742     function test_object_term_cache() {
    1743         $post_id = $this->factory->post->create();
    1744 
    1745         $terms_1 = array('foo', 'bar', 'baz');
    1746         $terms_2 = array('bar', 'bing');
    1747 
    1748         // Cache should be empty after a set.
    1749         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    1750         $this->assertEquals( 3, count($tt_1) );
    1751         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1752 
    1753         // wp_get_object_terms() does not prime the cache.
    1754         wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') );
    1755         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1756 
    1757         // get_the_terms() does prime the cache.
    1758         $terms = get_the_terms( $post_id, $this->taxonomy );
    1759         $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    1760         $this->assertInternalType( 'array', $cache );
    1761 
    1762         // Cache should be empty after a set.
    1763         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    1764         $this->assertEquals( 2, count($tt_2) );
    1765         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    1766     }
    1767 
    1768     /**
    1769      * @ticket 24189
    1770      */
    1771     function test_object_term_cache_when_term_changes() {
    1772         $post_id = $this->factory->post->create();
    1773         $tag_id = $this->factory->tag->create( array( 'description' => 'My Amazing Tag' ) );
    1774 
    1775         $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
    1776 
    1777         $terms = get_the_terms( $post_id, 'post_tag' );
    1778         $this->assertEquals( $tag_id, $terms[0]->term_id );
    1779         $this->assertEquals( 'My Amazing Tag', $terms[0]->description );
    1780 
    1781         $_updated = wp_update_term( $tag_id, 'post_tag', array(
    1782             'description' => 'This description is even more amazing!'
    1783         ) );
    1784 
    1785         $_new_term = get_term( $tag_id, 'post_tag' );
    1786         $this->assertEquals( $tag_id, $_new_term->term_id );
    1787         $this->assertEquals( 'This description is even more amazing!', $_new_term->description );
    1788 
    1789         $terms = get_the_terms( $post_id, 'post_tag' );
    1790         $this->assertEquals( $tag_id, $terms[0]->term_id );
    1791         $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
    1792     }
    1793 
    1794     /**
    1795      * @ticket 31086
    1796      */
    1797     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
    1798         register_taxonomy( 'wptests_tax', 'post' );
    1799         $p = $this->factory->post->create();
    1800         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    1801 
    1802         $found = get_the_terms( $p, 'wptests_tax' );
    1803 
    1804         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    1805     }
    1806 
    1807     /**
    1808      * @ticket 31086
    1809      */
    1810     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
    1811         register_taxonomy( 'wptests_tax', 'post' );
    1812         $p = $this->factory->post->create();
    1813         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    1814 
    1815         // Prime cache.
    1816         update_object_term_cache( array( $p ), array( 'post' ) );
    1817 
    1818         $found = get_the_terms( $p, 'wptests_tax' );
    1819 
    1820         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    1821     }
    1822 
    1823     /**
    1824      * @ticket 19205
    1825      */
    1826     function test_orphan_category() {
    1827         $cat_id1 = $this->factory->category->create();
    1828 
    1829         wp_delete_category( $cat_id1 );
    1830 
    1831         $cat_id2 = $this->factory->category->create( array( 'parent' => $cat_id1 ) );
    1832         $this->assertWPError( $cat_id2 );
    1833     }
    1834 
    1835     /**
    1836      * @ticket 30780
    1837      */
    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 30780
    1869      */
    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 
    1905622    /** 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    }
    1906632
    1907633    public function _pre_insert_term_callback() {
Note: See TracChangeset for help on using the changeset viewer.