Make WordPress Core


Ignore:
Timestamp:
01/17/2015 08:38:25 PM (11 years ago)
Author:
boonebgorges
Message:

Add unit tests for 'orderby' and 'order' param of wp_get_object_terms().

File:
1 edited

Legend:

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

    r31230 r31233  
    8686    }
    8787
     88    public function test_orderby_name() {
     89        $p = $this->factory->post->create();
     90
     91        $t1 = $this->factory->term->create( array(
     92            'taxonomy' => $this->taxonomy,
     93            'name' => 'AAA',
     94        ) );
     95        $t2 = $this->factory->term->create( array(
     96            'taxonomy' => $this->taxonomy,
     97            'name' => 'ZZZ',
     98        ) );
     99        $t3 = $this->factory->term->create( array(
     100            'taxonomy' => $this->taxonomy,
     101            'name' => 'JJJ',
     102        ) );
     103
     104        wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     105
     106        $found = wp_get_object_terms( $p, $this->taxonomy, array(
     107            'orderby' => 'name',
     108            'fields' => 'ids',
     109        ) );
     110
     111        $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     112    }
     113
     114    public function test_orderby_count() {
     115        $posts = $this->factory->post->create_many( 3 );
     116
     117        $t1 = $this->factory->term->create( array(
     118            'taxonomy' => $this->taxonomy,
     119            'name' => 'AAA',
     120        ) );
     121        $t2 = $this->factory->term->create( array(
     122            'taxonomy' => $this->taxonomy,
     123            'name' => 'ZZZ',
     124        ) );
     125        $t3 = $this->factory->term->create( array(
     126            'taxonomy' => $this->taxonomy,
     127            'name' => 'JJJ',
     128        ) );
     129
     130        wp_set_object_terms( $posts[0], array( $t3, $t2, $t1 ), $this->taxonomy );
     131        wp_set_object_terms( $posts[1], array( $t3, $t1 ), $this->taxonomy );
     132        wp_set_object_terms( $posts[2], array( $t3 ), $this->taxonomy );
     133
     134        $found = wp_get_object_terms( $posts[0], $this->taxonomy, array(
     135            'orderby' => 'count',
     136            'fields' => 'ids',
     137        ) );
     138
     139        $this->assertEquals( array( $t2, $t1, $t3 ), $found );
     140    }
     141
     142    public function test_orderby_slug() {
     143        $p = $this->factory->post->create();
     144
     145        $t1 = $this->factory->term->create( array(
     146            'taxonomy' => $this->taxonomy,
     147            'slug' => 'aaa',
     148        ) );
     149        $t2 = $this->factory->term->create( array(
     150            'taxonomy' => $this->taxonomy,
     151            'slug' => 'zzz',
     152        ) );
     153        $t3 = $this->factory->term->create( array(
     154            'taxonomy' => $this->taxonomy,
     155            'slug' => 'jjj',
     156        ) );
     157
     158        wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     159
     160        $found = wp_get_object_terms( $p, $this->taxonomy, array(
     161            'orderby' => 'slug',
     162            'fields' => 'ids',
     163        ) );
     164
     165        $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     166    }
     167
     168    public function test_orderby_term_group() {
     169        $p = $this->factory->post->create();
     170
     171        $t1 = $this->factory->term->create( array(
     172            'taxonomy' => $this->taxonomy,
     173        ) );
     174        $t2 = $this->factory->term->create( array(
     175            'taxonomy' => $this->taxonomy,
     176        ) );
     177        $t3 = $this->factory->term->create( array(
     178            'taxonomy' => $this->taxonomy,
     179        ) );
     180
     181        // No great way to do this in the API.
     182        global $wpdb;
     183        $wpdb->update( $wpdb->terms, array( 'term_group' => 1 ), array( 'term_id' => $t1 ) );
     184        $wpdb->update( $wpdb->terms, array( 'term_group' => 3 ), array( 'term_id' => $t2 ) );
     185        $wpdb->update( $wpdb->terms, array( 'term_group' => 2 ), array( 'term_id' => $t3 ) );
     186
     187        wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     188
     189        $found = wp_get_object_terms( $p, $this->taxonomy, array(
     190            'orderby' => 'term_group',
     191            'fields' => 'ids',
     192        ) );
     193
     194        $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     195    }
     196
     197    public function test_orderby_term_order() {
     198        $p = $this->factory->post->create();
     199
     200        $t1 = $this->factory->term->create( array(
     201            'taxonomy' => $this->taxonomy,
     202        ) );
     203        $t2 = $this->factory->term->create( array(
     204            'taxonomy' => $this->taxonomy,
     205        ) );
     206        $t3 = $this->factory->term->create( array(
     207            'taxonomy' => $this->taxonomy,
     208        ) );
     209
     210        $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     211
     212        // No great way to do this in the API.
     213        $term_1 = get_term( $t1, $this->taxonomy );
     214        $term_2 = get_term( $t2, $this->taxonomy );
     215        $term_3 = get_term( $t3, $this->taxonomy );
     216
     217        global $wpdb;
     218        $wpdb->update( $wpdb->term_relationships, array( 'term_order' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id, 'object_id' => $p ) );
     219        $wpdb->update( $wpdb->term_relationships, array( 'term_order' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id, 'object_id' => $p ) );
     220        $wpdb->update( $wpdb->term_relationships, array( 'term_order' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id, 'object_id' => $p ) );
     221
     222        $found = wp_get_object_terms( $p, $this->taxonomy, array(
     223            'orderby' => 'term_order',
     224            'fields' => 'ids',
     225        ) );
     226
     227        $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     228    }
     229
     230    public function test_order_desc() {
     231        $p = $this->factory->post->create();
     232
     233        $t1 = $this->factory->term->create( array(
     234            'taxonomy' => $this->taxonomy,
     235            'name' => 'AAA',
     236        ) );
     237        $t2 = $this->factory->term->create( array(
     238            'taxonomy' => $this->taxonomy,
     239            'name' => 'ZZZ',
     240        ) );
     241        $t3 = $this->factory->term->create( array(
     242            'taxonomy' => $this->taxonomy,
     243            'name' => 'JJJ',
     244        ) );
     245
     246        wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     247
     248        $found = wp_get_object_terms( $p, $this->taxonomy, array(
     249            'orderby' => 'name',
     250            'order' => 'DESC',
     251            'fields' => 'ids',
     252        ) );
     253
     254        $this->assertEquals( array( $t2, $t3, $t1 ), $found );
     255    }
     256
    88257    public function filter_get_object_terms( $terms ) {
    89258        $term_ids = wp_list_pluck( $terms, 'term_id' );
Note: See TracChangeset for help on using the changeset viewer.