Make WordPress Core

Changeset 35185


Ignore:
Timestamp:
10/15/2015 03:47:36 AM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: in Tests_Term, create fixtures for posts.

See #30017, #33968.

File:
1 edited

Legend:

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

    r35032 r35185  
    55 */
    66class Tests_Term extends WP_UnitTestCase {
    7         var $taxonomy = 'category';
     7        protected $taxonomy = 'category';
     8        protected static $post_ids = array();
     9
     10        public static function setUpBeforeClass() {
     11                parent::setUpBeforeClass();
     12
     13                $factory = new WP_UnitTest_Factory();
     14
     15                self::$post_ids = $factory->post->create_many( 5 );
     16
     17                self::commit_transaction();
     18        }
     19
     20        public static function tearDownAfterClass() {
     21                parent::tearDownAfterClass();
     22
     23                array_map( 'wp_delete_post', self::$post_ids );
     24
     25                self::commit_transaction();
     26        }
    827
    928        /**
     
    5473        function test_wp_count_terms() {
    5574                $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
    56                 // the terms inserted in setUp aren't attached to any posts, so should return 0
    57                 // this previously returned 2
    58                 $this->assertEquals( 0, $count );
     75                // there are 5 posts, all Uncategorized
     76                $this->assertEquals( 1, $count );
    5977        }
    6078
     
    7189
    7290                // Create a post.
    73                 $post_id = $this->factory->post->create();
     91                $post_id = self::$post_ids[0];
    7492
    7593                /*
     
    148166
    149167        function test_set_object_terms_by_id() {
    150                 $ids = $this->factory->post->create_many(5);
     168                $ids = self::$post_ids;
    151169
    152170                $terms = array();
     
    159177
    160178                foreach ($ids as $id) {
    161                                 $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
    162                                 // should return three term taxonomy ids
    163                                 $this->assertEquals( 3, count($tt) );
     179                        $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
     180                        // should return three term taxonomy ids
     181                        $this->assertEquals( 3, count($tt) );
    164182                }
    165183
     
    178196
    179197        function test_set_object_terms_by_name() {
    180                 $ids = $this->factory->post->create_many(5);
     198                $ids = self::$post_ids;
    181199
    182200                $terms = array(
    183                                 rand_str(),
    184                                 rand_str(),
    185                                 rand_str());
     201                        rand_str(),
     202                        rand_str(),
     203                        rand_str()
     204                );
    186205
    187206                foreach ($ids as $id) {
    188                                 $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
    189                                 // should return three term taxonomy ids
    190                                 $this->assertEquals( 3, count($tt) );
    191                                 // remember which term has which term_id
    192                                 for ($i=0; $i<3; $i++) {
    193                                         $term = get_term_by('name', $terms[$i], $this->taxonomy);
    194                                         $term_id[$terms[$i]] = intval($term->term_id);
    195                                 }
     207                        $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
     208                        // should return three term taxonomy ids
     209                        $this->assertEquals( 3, count($tt) );
     210                        // remember which term has which term_id
     211                        for ($i=0; $i<3; $i++) {
     212                                $term = get_term_by('name', $terms[$i], $this->taxonomy);
     213                                $term_id[$terms[$i]] = intval($term->term_id);
     214                        }
    196215                }
    197216
     
    210229
    211230        function test_set_object_terms_invalid() {
    212                 $post_id = $this->factory->post->create();
    213 
    214231                // bogus taxonomy
    215                 $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() );
     232                $result = wp_set_object_terms( self::$post_ids[0], array(rand_str()), rand_str() );
    216233                $this->assertTrue( is_wp_error($result) );
    217234        }
     
    219236        public function test_wp_set_object_terms_append_true() {
    220237                register_taxonomy( 'wptests_tax', 'post' );
    221                 $p = $this->factory->post->create();
     238                $p = self::$post_ids[0];
    222239                $t1 = $this->factory->term->create( array(
    223240                        'taxonomy' => 'wptests_tax',
     
    240257        public function test_wp_set_object_terms_append_false() {
    241258                register_taxonomy( 'wptests_tax', 'post' );
    242                 $p = $this->factory->post->create();
     259                $p = self::$post_ids[0];
    243260                $t1 = $this->factory->term->create( array(
    244261                        'taxonomy' => 'wptests_tax',
     
    261278        public function test_wp_set_object_terms_append_default_to_false() {
    262279                register_taxonomy( 'wptests_tax', 'post' );
    263                 $p = $this->factory->post->create();
     280                $p = self::$post_ids[0];
    264281                $t1 = $this->factory->term->create( array(
    265282                        'taxonomy' => 'wptests_tax',
     
    283300                // set some terms on an object; then change them while leaving one intact
    284301
    285                 $post_id = $this->factory->post->create();
     302                $post_id = self::$post_ids[0];
    286303
    287304                // first set: 3 terms
     
    327344                // set some terms on an object; then change them while leaving one intact
    328345
    329                 $post_id = $this->factory->post->create();
     346                $post_id = self::$post_ids[0];
    330347
    331348                $terms_1 = array('foo', 'bar', 'baz');
     
    357374         */
    358375        function test_wp_add_remove_object_terms() {
    359                 $posts = $this->factory->post->create_many( 5 );
     376                $posts = self::$post_ids;
    360377                $tags = $this->factory->tag->create_many( 5 );
    361378
     
    431448         */
    432449        function test_wp_set_post_categories() {
    433                 $post_id = $this->factory->post->create();
     450                $post_id = self::$post_ids[0];
    434451                $post = get_post( $post_id );
    435452
     
    487504         */
    488505        function test_object_term_cache() {
    489                 $post_id = $this->factory->post->create();
     506                $post_id = self::$post_ids[0];
    490507
    491508                $terms_1 = array('foo', 'bar', 'baz');
     
    516533         */
    517534        function test_object_term_cache_when_term_changes() {
    518                 $post_id = $this->factory->post->create();
    519                 $tag_id = $this->factory->tag->create( array( 'description' => 'My Amazing Tag' ) );
     535                $post_id = self::$post_ids[0];
     536                $tag_id = $this->factory->tag->create( array(
     537                        'name' => 'Amaze Tag',
     538                        'description' => 'My Amazing Tag'
     539                ) );
    520540
    521541                $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
     
    542562         */
    543563        public function test_get_the_terms_should_not_cache_wp_term_objects() {
    544                 $p = $this->factory->post->create();
     564                $p = self::$post_ids[0];
    545565                register_taxonomy( 'wptests_tax', 'post' );
    546566                $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     
    561581         */
    562582        public function test_get_the_terms_should_return_wp_term_objects_from_cache() {
    563                 $p = $this->factory->post->create();
     583                $p = self::$post_ids[0];
    564584                register_taxonomy( 'wptests_tax', 'post' );
    565585                $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     
    581601        public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
    582602                register_taxonomy( 'wptests_tax', 'post' );
    583                 $p = $this->factory->post->create();
     603                $p = self::$post_ids[0];
    584604                wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    585605
     
    594614        public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
    595615                register_taxonomy( 'wptests_tax', 'post' );
    596                 $p = $this->factory->post->create();
     616                $p = self::$post_ids[0];
    597617                wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
    598618
Note: See TracChangeset for help on using the changeset viewer.