WordPress.org

Make WordPress Core

Changeset 37644


Ignore:
Timestamp:
06/06/2016 09:22:43 PM (4 years ago)
Author:
boonebgorges
Message:

Tests: Move wp_set_object_terms() tests to their own file.

This is a redo of [37642], this time not done in the not right place.

See #37009.

Location:
trunk/tests/phpunit/tests
Files:
1 edited
1 copied

Legend:

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

    r37480 r37644  
    6767        // there are 5 posts, all Uncategorized
    6868        $this->assertEquals( 1, $count );
    69     }
    70 
    71     /**
    72      * @ticket 26570
    73      */
    74     function test_set_object_terms() {
    75         $non_hier = rand_str( 10 );
    76         $hier     = rand_str( 10 );
    77 
    78         // Register taxonomies
    79         register_taxonomy( $non_hier, array() );
    80         register_taxonomy( $hier, array( 'hierarchical' => true ) );
    81 
    82         // Create a post.
    83         $post_id = self::$post_ids[0];
    84 
    85         /*
    86          * Set a single term (non-hierarchical) by ID.
    87          */
    88         $tag = wp_insert_term( 'Foo', $non_hier );
    89         $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
    90 
    91         wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
    92         $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
    93 
    94         /*
    95          * Set a single term (non-hierarchical) by slug.
    96          */
    97         $tag = wp_insert_term( 'Bar', $non_hier );
    98         $tag = get_term( $tag['term_id'], $non_hier );
    99 
    100         $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) );
    101 
    102         wp_set_object_terms( $post_id, $tag->slug, $non_hier );
    103         $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) );
    104 
    105         /*
    106          * Set a single term (hierarchical) by ID.
    107          */
    108         $cat = wp_insert_term( 'Baz', $hier );
    109         $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) );
    110 
    111         wp_set_object_terms( $post_id, $cat['term_id'], $hier );
    112         $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) );
    113 
    114         /*
    115          * Set a single term (hierarchical) by slug.
    116          */
    117         $cat = wp_insert_term( 'Qux', $hier );
    118         $cat = get_term( $cat['term_id'], $hier );
    119 
    120         $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
    121 
    122         wp_set_object_terms( $post_id, $cat->slug, $hier );
    123         $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
    124 
    125         /*
    126          * Set an array of term IDs (non-hierarchical) by ID.
    127          */
    128         $tag1 = wp_insert_term( '_tag1', $non_hier );
    129         $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
    130 
    131         $tag2 = wp_insert_term( '_tag2', $non_hier );
    132         $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
    133 
    134         $tag3 = wp_insert_term( '_tag3', $non_hier );
    135         $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
    136 
    137         wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
    138         $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
    139 
    140         /*
    141          * Set an array of term slugs (hierarchical) by slug.
    142          */
    143         $cat1 = wp_insert_term( '_cat1', $hier );
    144         $cat1 = get_term( $cat1['term_id'], $hier );
    145         $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
    146 
    147         $cat2 = wp_insert_term( '_cat2', $hier );
    148         $cat2 = get_term( $cat2['term_id'], $hier );
    149         $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
    150 
    151         $cat3 = wp_insert_term( '_cat3', $hier );
    152         $cat3 = get_term( $cat3['term_id'], $hier );
    153         $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
    154 
    155         wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
    156         $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
    157     }
    158 
    159     function test_set_object_terms_by_id() {
    160         $ids = self::$post_ids;
    161 
    162         $terms = array();
    163         for ($i=0; $i<3; $i++ ) {
    164             $term = rand_str();
    165             $result = wp_insert_term( $term, $this->taxonomy );
    166             $this->assertInternalType( 'array', $result );
    167             $term_id[$term] = $result['term_id'];
    168         }
    169 
    170         foreach ($ids as $id) {
    171             $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
    172             // should return three term taxonomy ids
    173             $this->assertEquals( 3, count($tt) );
    174         }
    175 
    176         // each term should be associated with every post
    177         foreach ($term_id as $term=>$id) {
    178             $actual = get_objects_in_term($id, $this->taxonomy);
    179             $this->assertEquals( $ids, array_map('intval', $actual) );
    180         }
    181 
    182         // each term should have a count of 5
    183         foreach (array_keys($term_id) as $term) {
    184             $t = get_term_by('name', $term, $this->taxonomy);
    185             $this->assertEquals( 5, $t->count );
    186         }
    187     }
    188 
    189     function test_set_object_terms_by_name() {
    190         $ids = self::$post_ids;
    191 
    192         $terms = array(
    193             rand_str(),
    194             rand_str(),
    195             rand_str()
    196         );
    197 
    198         foreach ($ids as $id) {
    199             $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
    200             // should return three term taxonomy ids
    201             $this->assertEquals( 3, count($tt) );
    202             // remember which term has which term_id
    203             for ($i=0; $i<3; $i++) {
    204                 $term = get_term_by('name', $terms[$i], $this->taxonomy);
    205                 $term_id[$terms[$i]] = intval($term->term_id);
    206             }
    207         }
    208 
    209         // each term should be associated with every post
    210         foreach ($term_id as $term=>$id) {
    211             $actual = get_objects_in_term($id, $this->taxonomy);
    212             $this->assertEquals( $ids, array_map('intval', $actual) );
    213         }
    214 
    215         // each term should have a count of 5
    216         foreach ($terms as $term) {
    217             $t = get_term_by('name', $term, $this->taxonomy);
    218             $this->assertEquals( 5, $t->count );
    219         }
    220     }
    221 
    222     function test_set_object_terms_invalid() {
    223         // bogus taxonomy
    224         $result = wp_set_object_terms( self::$post_ids[0], array(rand_str()), rand_str() );
    225         $this->assertTrue( is_wp_error($result) );
    226     }
    227 
    228     public function test_wp_set_object_terms_append_true() {
    229         register_taxonomy( 'wptests_tax', 'post' );
    230         $p = self::$post_ids[0];
    231         $t1 = self::factory()->term->create( array(
    232             'taxonomy' => 'wptests_tax',
    233         ) );
    234         $t2 = self::factory()->term->create( array(
    235             'taxonomy' => 'wptests_tax',
    236         ) );
    237 
    238         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    239         $this->assertNotEmpty( $added1 );
    240         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    241 
    242         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', true );
    243         $this->assertNotEmpty( $added2 );
    244         $this->assertEqualSets( array( $t1, $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    245 
    246         _unregister_taxonomy( 'wptests_tax' );
    247     }
    248 
    249     public function test_wp_set_object_terms_append_false() {
    250         register_taxonomy( 'wptests_tax', 'post' );
    251         $p = self::$post_ids[0];
    252         $t1 = self::factory()->term->create( array(
    253             'taxonomy' => 'wptests_tax',
    254         ) );
    255         $t2 = self::factory()->term->create( array(
    256             'taxonomy' => 'wptests_tax',
    257         ) );
    258 
    259         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    260         $this->assertNotEmpty( $added1 );
    261         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    262 
    263         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', false );
    264         $this->assertNotEmpty( $added2 );
    265         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    266 
    267         _unregister_taxonomy( 'wptests_tax' );
    268     }
    269 
    270     public function test_wp_set_object_terms_append_default_to_false() {
    271         register_taxonomy( 'wptests_tax', 'post' );
    272         $p = self::$post_ids[0];
    273         $t1 = self::factory()->term->create( array(
    274             'taxonomy' => 'wptests_tax',
    275         ) );
    276         $t2 = self::factory()->term->create( array(
    277             'taxonomy' => 'wptests_tax',
    278         ) );
    279 
    280         $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
    281         $this->assertNotEmpty( $added1 );
    282         $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    283 
    284         $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax' );
    285         $this->assertNotEmpty( $added2 );
    286         $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
    287 
    288         _unregister_taxonomy( 'wptests_tax' );
    289     }
    290 
    291     function test_change_object_terms_by_id() {
    292         // set some terms on an object; then change them while leaving one intact
    293 
    294         $post_id = self::$post_ids[0];
    295 
    296         // first set: 3 terms
    297         $terms_1 = array();
    298         for ($i=0; $i<3; $i++ ) {
    299             $term = rand_str();
    300             $result = wp_insert_term( $term, $this->taxonomy );
    301             $this->assertInternalType( 'array', $result );
    302             $terms_1[$i] = $result['term_id'];
    303         }
    304 
    305         // second set: one of the original terms, plus one new term
    306         $terms_2 = array();
    307         $terms_2[0] = $terms_1[1];
    308 
    309         $term = rand_str();
    310         $result = wp_insert_term( $term, $this->taxonomy );
    311         $terms_2[1] = $result['term_id'];
    312 
    313 
    314         // set the initial terms
    315         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    316         $this->assertEquals( 3, count($tt_1) );
    317 
    318         // make sure they're correct
    319         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    320         $this->assertEquals( $terms_1, $terms );
    321 
    322         // change the terms
    323         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    324         $this->assertEquals( 2, count($tt_2) );
    325 
    326         // make sure they're correct
    327         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
    328         $this->assertEquals( $terms_2, $terms );
    329 
    330         // make sure the tt id for 'bar' matches
    331         $this->assertEquals( $tt_1[1], $tt_2[0] );
    332 
    333     }
    334 
    335     function test_change_object_terms_by_name() {
    336         // set some terms on an object; then change them while leaving one intact
    337 
    338         $post_id = self::$post_ids[0];
    339 
    340         $terms_1 = array('foo', 'bar', 'baz');
    341         $terms_2 = array('bar', 'bing');
    342 
    343         // set the initial terms
    344         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    345         $this->assertEquals( 3, count($tt_1) );
    346 
    347         // make sure they're correct
    348         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    349         $this->assertEquals( $terms_1, $terms );
    350 
    351         // change the terms
    352         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    353         $this->assertEquals( 2, count($tt_2) );
    354 
    355         // make sure they're correct
    356         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
    357         $this->assertEquals( $terms_2, $terms );
    358 
    359         // make sure the tt id for 'bar' matches
    360         $this->assertEquals( $tt_1[1], $tt_2[0] );
    361 
    36269    }
    36370
  • trunk/tests/phpunit/tests/term/wpSetObjectTerms.php

    r37643 r37644  
    44 * @group taxonomy
    55 */
    6 class Tests_Term extends WP_UnitTestCase {
     6class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
    77    protected $taxonomy = 'category';
    88    protected static $post_ids = array();
     
    1616            wp_delete_post( $post_id, true );
    1717        }
    18     }
    19 
    20     /**
    21      * @ticket 29911
    22      */
    23     public function test_wp_delete_term_should_invalidate_cache_for_child_terms() {
    24         register_taxonomy( 'wptests_tax', 'post', array(
    25             'hierarchical' => true,
    26         ) );
    27 
    28         $parent = self::factory()->term->create( array(
    29             'taxonomy' => 'wptests_tax',
    30         ) );
    31 
    32         $child = self::factory()->term->create( array(
    33             'taxonomy' => 'wptests_tax',
    34             'parent' => $parent,
    35             'slug' => 'foo',
    36         ) );
    37 
    38         // Prime the cache.
    39         $child_term = get_term( $child, 'wptests_tax' );
    40         $this->assertSame( $parent, $child_term->parent );
    41 
    42         wp_delete_term( $parent, 'wptests_tax' );
    43         $child_term = get_term( $child, 'wptests_tax' );
    44         $this->assertSame( 0, $child_term->parent );
    45     }
    46 
    47     /**
    48      * @ticket 5381
    49      */
    50     function test_is_term_type() {
    51         // insert a term
    52         $term = rand_str();
    53         $t = wp_insert_term( $term, $this->taxonomy );
    54         $this->assertInternalType( 'array', $t );
    55         $term_obj = get_term_by('name', $term, $this->taxonomy);
    56         $this->assertEquals( $t['term_id'], term_exists($term_obj->slug) );
    57 
    58         // clean up
    59         $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
    60     }
    61 
    62     /**
    63      * @ticket 15919
    64      */
    65     function test_wp_count_terms() {
    66         $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
    67         // there are 5 posts, all Uncategorized
    68         $this->assertEquals( 1, $count );
    6918    }
    7019
     
    361310
    362311    }
    363 
    364     /**
    365      * @ticket 15475
    366      */
    367     function test_wp_add_remove_object_terms() {
    368         $posts = self::$post_ids;
    369         $tags = self::factory()->tag->create_many( 5 );
    370 
    371         $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
    372         $this->assertEquals( 1, count( $tt ) );
    373         $this->assertEquals( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
    374 
    375         $three_tags = array( $tags[0], $tags[1], $tags[2] );
    376         $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
    377         $this->assertEquals( 3, count( $tt ) );
    378         $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
    379 
    380         $this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
    381         $this->assertFalse( wp_remove_object_terms( $posts[0], $tags[0], 'post_tag' ) );
    382         $this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
    383         $this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
    384         $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
    385 
    386         foreach ( $tags as $term_id )
    387             $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
    388 
    389         foreach ( $posts as $post_id )
    390             $this->assertTrue( (bool) wp_delete_post( $post_id ) );
    391     }
    392 
    393     /**
    394      * @group category.php
    395      */
    396     function test_term_is_ancestor_of( ) {
    397         $term = rand_str();
    398         $term2 = rand_str();
    399 
    400         $t = wp_insert_term( $term, 'category' );
    401         $this->assertInternalType( 'array', $t );
    402         $t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
    403         $this->assertInternalType( 'array', $t2 );
    404         if ( function_exists( 'term_is_ancestor_of' ) ) {
    405             $this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
    406             $this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
    407         }
    408         $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id']) );
    409         $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id']) );
    410 
    411         wp_delete_term($t['term_id'], 'category');
    412         wp_delete_term($t2['term_id'], 'category');
    413     }
    414 
    415     function test_wp_insert_delete_category() {
    416         $term = rand_str();
    417         $this->assertNull( category_exists( $term ) );
    418 
    419         $initial_count = wp_count_terms( 'category' );
    420 
    421         $t = wp_insert_category( array( 'cat_name' => $term ) );
    422         $this->assertTrue( is_numeric($t) );
    423         $this->assertNotWPError( $t );
    424         $this->assertTrue( $t > 0 );
    425         $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
    426 
    427         // make sure the term exists
    428         $this->assertTrue( term_exists($term) > 0 );
    429         $this->assertTrue( term_exists($t) > 0 );
    430 
    431         // now delete it
    432         $this->assertTrue( wp_delete_category($t) );
    433         $this->assertNull( term_exists($term) );
    434         $this->assertNull( term_exists($t) );
    435         $this->assertEquals( $initial_count, wp_count_terms('category') );
    436     }
    437 
    438     /**
    439      * @ticket 16550
    440      */
    441     function test_wp_set_post_categories() {
    442         $post_id = self::$post_ids[0];
    443         $post = get_post( $post_id );
    444 
    445         $this->assertInternalType( 'array', $post->post_category );
    446         $this->assertEquals( 1, count( $post->post_category ) );
    447         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    448         $term1 = wp_insert_term( 'Foo', 'category' );
    449         $term2 = wp_insert_term( 'Bar', 'category' );
    450         $term3 = wp_insert_term( 'Baz', 'category' );
    451         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
    452         $this->assertEquals( 2, count( $post->post_category ) );
    453         $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );
    454 
    455         wp_set_post_categories( $post_id, $term3['term_id'], true );
    456         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );
    457 
    458         $term4 = wp_insert_term( 'Burrito', 'category' );
    459         wp_set_post_categories( $post_id, $term4['term_id'] );
    460         $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
    461 
    462         wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
    463         $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
    464 
    465         wp_set_post_categories( $post_id, array(), true );
    466         $this->assertEquals( 1, count( $post->post_category ) );
    467         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    468 
    469         wp_set_post_categories( $post_id, array() );
    470         $this->assertEquals( 1, count( $post->post_category ) );
    471         $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    472     }
    473 
    474     /**
    475      * @ticket 25852
    476      */
    477     function test_sanitize_term_field() {
    478         $term = wp_insert_term( 'foo', $this->taxonomy );
    479 
    480         $this->assertEquals( 0, sanitize_term_field( 'parent',  0, $term['term_id'], $this->taxonomy, 'raw' ) );
    481         $this->assertEquals( 1, sanitize_term_field( 'parent',  1, $term['term_id'], $this->taxonomy, 'raw' ) );
    482         $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
    483         $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
    484     }
    485 
    486     private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) {
    487         $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array(
    488             'fields' => 'ids'
    489         ) );
    490 
    491         $this->assertEquals( $expected_term_ids, $assigned_term_ids );
    492     }
    493 
    494     /**
    495      * @ticket 19205
    496      */
    497     function test_orphan_category() {
    498         $cat_id1 = self::factory()->category->create();
    499 
    500         wp_delete_category( $cat_id1 );
    501 
    502         $cat_id2 = self::factory()->category->create( array( 'parent' => $cat_id1 ) );
    503         $this->assertWPError( $cat_id2 );
    504     }
    505312}
Note: See TracChangeset for help on using the changeset viewer.