Make WordPress Core


Ignore:
Timestamp:
06/06/2016 09:22:43 PM (10 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.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • 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.