Make WordPress Core


Ignore:
Timestamp:
06/06/2016 09:08:23 PM (10 years ago)
Author:
boonebgorges
Message:

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

See #37009.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/4.4/tests/phpunit/tests/term/wpSetObjectTerms.php

    r37641 r37642  
    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 22560
    496      */
    497     function test_object_term_cache() {
    498         $post_id = self::$post_ids[0];
    499 
    500         $terms_1 = array('foo', 'bar', 'baz');
    501         $terms_2 = array('bar', 'bing');
    502 
    503         // Cache should be empty after a set.
    504         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
    505         $this->assertEquals( 3, count($tt_1) );
    506         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    507 
    508         // wp_get_object_terms() does not prime the cache.
    509         wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') );
    510         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    511 
    512         // get_the_terms() does prime the cache.
    513         $terms = get_the_terms( $post_id, $this->taxonomy );
    514         $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    515         $this->assertInternalType( 'array', $cache );
    516 
    517         // Cache should be empty after a set.
    518         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    519         $this->assertEquals( 2, count($tt_2) );
    520         $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
    521     }
    522 
    523     /**
    524      * @ticket 24189
    525      */
    526     function test_object_term_cache_when_term_changes() {
    527         $post_id = self::$post_ids[0];
    528         $tag_id = self::factory()->tag->create( array(
    529             'name' => 'Amaze Tag',
    530             'description' => 'My Amazing Tag'
    531         ) );
    532 
    533         $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
    534 
    535         $terms = get_the_terms( $post_id, 'post_tag' );
    536         $this->assertEquals( $tag_id, $terms[0]->term_id );
    537         $this->assertEquals( 'My Amazing Tag', $terms[0]->description );
    538 
    539         $_updated = wp_update_term( $tag_id, 'post_tag', array(
    540             'description' => 'This description is even more amazing!'
    541         ) );
    542 
    543         $_new_term = get_term( $tag_id, 'post_tag' );
    544         $this->assertEquals( $tag_id, $_new_term->term_id );
    545         $this->assertEquals( 'This description is even more amazing!', $_new_term->description );
    546 
    547         $terms = get_the_terms( $post_id, 'post_tag' );
    548         $this->assertEquals( $tag_id, $terms[0]->term_id );
    549         $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
    550     }
    551 
    552     /**
    553      * @ticket 34262
    554      */
    555     public function test_get_the_terms_should_not_cache_wp_term_objects() {
    556         $p = self::$post_ids[0];
    557         register_taxonomy( 'wptests_tax', 'post' );
    558         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    559         wp_set_object_terms( $p, $t, 'wptests_tax' );
    560 
    561         // Prime the cache.
    562         $terms = get_the_terms( $p, 'wptests_tax' );
    563 
    564         $cached = get_object_term_cache( $p, 'wptests_tax' );
    565 
    566         $this->assertNotEmpty( $cached );
    567         $this->assertSame( $t, (int) $cached[0]->term_id );
    568         $this->assertNotInstanceOf( 'WP_Term', $cached[0] );
    569     }
    570 
    571     /**
    572      * @ticket 34262
    573      */
    574     public function test_get_the_terms_should_return_wp_term_objects_from_cache() {
    575         $p = self::$post_ids[0];
    576         register_taxonomy( 'wptests_tax', 'post' );
    577         $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    578         wp_set_object_terms( $p, $t, 'wptests_tax' );
    579 
    580         // Prime the cache.
    581         get_the_terms( $p, 'wptests_tax' );
    582 
    583         $cached = get_the_terms( $p, 'wptests_tax' );
    584 
    585         $this->assertNotEmpty( $cached );
    586         $this->assertSame( $t, (int) $cached[0]->term_id );
    587         $this->assertInstanceOf( 'WP_Term', $cached[0] );
    588     }
    589 
    590     /**
    591      * @ticket 31086
    592      */
    593     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
    594         register_taxonomy( 'wptests_tax', 'post' );
    595         $p = self::$post_ids[0];
    596         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    597 
    598         $found = get_the_terms( $p, 'wptests_tax' );
    599 
    600         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    601     }
    602 
    603     /**
    604      * @ticket 31086
    605      */
    606     public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
    607         register_taxonomy( 'wptests_tax', 'post' );
    608         $p = self::$post_ids[0];
    609         wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    610 
    611         // Prime cache.
    612         update_object_term_cache( array( $p ), array( 'post' ) );
    613 
    614         $found = get_the_terms( $p, 'wptests_tax' );
    615 
    616         $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) );
    617     }
    618 
    619     /**
    620      * @ticket 35180
    621      * @ticket 28922
    622      */
    623     public function test_get_the_terms_should_return_results_ordered_by_name_when_pulling_from_cache() {
    624         register_taxonomy( 'wptests_tax', 'post' );
    625         $p = self::$post_ids[0];
    626 
    627         $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'fff' ) );
    628         $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'aaa' ) );
    629         $t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'zzz' ) );
    630 
    631         wp_set_object_terms( $p, array( $t1, $t2, $t3 ), 'wptests_tax' );
    632         update_object_term_cache( $p, 'post' );
    633 
    634         $found = get_the_terms( $p, 'wptests_tax' );
    635 
    636         $this->assertSame( array( $t2, $t1, $t3 ), wp_list_pluck( $found, 'term_id' ) );
    637     }
    638 
    639     /**
    640      * @ticket 19205
    641      */
    642     function test_orphan_category() {
    643         $cat_id1 = self::factory()->category->create();
    644 
    645         wp_delete_category( $cat_id1 );
    646 
    647         $cat_id2 = self::factory()->category->create( array( 'parent' => $cat_id1 ) );
    648         $this->assertWPError( $cat_id2 );
    649     }
    650 
    651     /**
    652      * @ticket 34723
    653      */
    654     function test_get_the_terms_should_return_wp_error_when_taxonomy_is_unregistered() {
    655         $p = self::$post_ids[0];
    656         $terms = get_the_terms( $p, 'this-taxonomy-does-not-exist' );
    657         $this->assertTrue( is_wp_error( $terms ) );
    658     }
    659312}
Note: See TracChangeset for help on using the changeset viewer.