Make WordPress Core

Changeset 33182


Ignore:
Timestamp:
07/12/2015 08:12:19 PM (10 years ago)
Author:
jorbin
Message:

Add Unit Tests for get_objects_in_term()

Props rodrigosprimo.
Fixes #32946.

File:
1 edited

Legend:

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

    r32709 r33182  
    252252    }
    253253
     254    public function test_get_objects_in_term_should_return_invalid_taxonomy_error() {
     255        $terms = get_objects_in_term( 1, 'invalid_taxonomy' );
     256        $this->assertInstanceOf( 'WP_Error', $terms );
     257        $this->assertEquals( 'Invalid taxonomy', $terms->get_error_message() );
     258    }
     259
     260    public function test_get_objects_in_term_should_return_empty_array() {
     261        $this->assertEquals( array(), get_objects_in_term( 1, 'post_tag' ) );
     262    }
     263
     264    public function test_get_objects_in_term_should_return_objects_ids() {
     265        $tag_id = $this->factory->tag->create();
     266        $cat_id = $this->factory->category->create();
     267        $posts_with_tag = array();
     268        $posts_with_category = array();
     269
     270        for ( $i = 0; $i < 3; $i++ ) {
     271            $post_id = $this->factory->post->create();
     272            wp_set_post_tags( $post_id, array( $tag_id ) );
     273            $posts_with_tag[] = $post_id;
     274        }
     275
     276        for ( $i = 0; $i < 3; $i++ ) {
     277            $post_id = $this->factory->post->create();
     278            wp_set_post_categories( $post_id, array( $cat_id ) );
     279            $posts_with_category[] = $post_id;
     280        }
     281
     282        for ( $i = 0; $i < 3; $i++ ) {
     283            $this->factory->post->create();
     284        }
     285
     286        $posts_with_terms = array_merge( $posts_with_tag, $posts_with_category );
     287
     288        $this->assertEquals( $posts_with_tag, get_objects_in_term( $tag_id, 'post_tag' ) );
     289        $this->assertEquals( $posts_with_category, get_objects_in_term( $cat_id, 'category' ) );
     290        $this->assertEquals( $posts_with_terms, get_objects_in_term( array( $tag_id, $cat_id ), array( 'post_tag', 'category' ) ) );
     291        $this->assertEquals( array_reverse( $posts_with_tag ), get_objects_in_term( $tag_id, 'post_tag', array( 'order' => 'desc' ) ) );
     292    }
     293
    254294    /**
    255295     * @ticket 25706
Note: See TracChangeset for help on using the changeset viewer.