Make WordPress Core

Changeset 29845


Ignore:
Timestamp:
10/06/2014 10:04:11 PM (12 years ago)
Author:
boonebgorges
Message:

In get_terms(), select term taxonomy count for all values of 'fields'.

Not having the count caused queries with 'fields' values of 'id=>name' and
'id=>slug' to return incorrect results when querying a hierarchical taxonomy
with 'hide_empty=true'.

Includes unit tests for get_terms() when using various combinations of 'fields',
'hide_empty', and 'hierarchical' arguments.

Props technical_mastermind.
Fixes #29859.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r29549 r29845  
    15131513                        break;
    15141514                case 'id=>name':
    1515                         $selects = array( 't.term_id', 't.name' );
     1515                        $selects = array( 't.term_id', 't.name', 'tt.count' );
    15161516                        break;
    15171517                case 'id=>slug':
    1518                         $selects = array( 't.term_id', 't.slug' );
     1518                        $selects = array( 't.term_id', 't.slug', 'tt.count' );
    15191519                        break;
    15201520        }
  • trunk/tests/phpunit/tests/term/getTerms.php

    r27837 r29845  
    352352                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
    353353        }
     354
     355        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
     356                // Set up a clean taxonomy.
     357                $tax = 'hierarchical_fields';
     358                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     359
     360                $terms = $this->create_hierarchical_terms_and_posts();
     361
     362                $found = get_terms( $tax, array(
     363                        'hide_empty' => false,
     364                        'fields' => 'ids',
     365                ) );
     366
     367                $expected = array(
     368                        $terms['parent1'],
     369                        $terms['parent2'],
     370                        $terms['child1'],
     371                        $terms['child2'],
     372                        $terms['grandchild1'],
     373                );
     374
     375                _unregister_taxonomy( 'hierarchical_fields' );
     376
     377                $this->assertEqualSets( $expected, $found );
     378        }
     379
     380        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids() {
     381                // Set up a clean taxonomy.
     382                $tax = 'hierarchical_fields';
     383                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     384
     385                $terms = $this->create_hierarchical_terms_and_posts();
     386
     387                $found = get_terms( $tax, array(
     388                        'hide_empty' => true,
     389                        'fields' => 'ids',
     390                ) );
     391
     392                $expected = array(
     393                        $terms['parent1'],
     394                        $terms['parent2'],
     395                        $terms['child1'],
     396                );
     397
     398                _unregister_taxonomy( 'hierarchical_fields' );
     399
     400                $this->assertEqualSets( $expected, $found );
     401        }
     402
     403        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids_hierarchical_false() {
     404                // Set up a clean taxonomy.
     405                $tax = 'hierarchical_fields';
     406                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     407
     408                $terms = $this->create_hierarchical_terms_and_posts();
     409
     410                $found = get_terms( $tax, array(
     411                        'hide_empty' => true,
     412                        'fields' => 'ids',
     413                        'hierarchical' => false,
     414                ) );
     415
     416                $expected = array(
     417                        $terms['parent2'],
     418                        $terms['child1'],
     419                );
     420
     421                _unregister_taxonomy( 'hierarchical_fields' );
     422
     423                $this->assertEqualSets( $expected, $found );
     424        }
     425
     426        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_names() {
     427                // Set up a clean taxonomy.
     428                $tax = 'hierarchical_fields';
     429                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     430
     431                $terms = $this->create_hierarchical_terms_and_posts();
     432
     433                $found = get_terms( $tax, array(
     434                        'hide_empty' => false,
     435                        'fields' => 'names',
     436                ) );
     437
     438                $expected = array(
     439                        'Parent 1',
     440                        'Parent 2',
     441                        'Child 1',
     442                        'Child 2',
     443                        'Grandchild 1',
     444                );
     445
     446                _unregister_taxonomy( 'hierarchical_fields' );
     447
     448                $this->assertEqualSets( $expected, $found );
     449        }
     450
     451        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names() {
     452                // Set up a clean taxonomy.
     453                $tax = 'hierarchical_fields';
     454                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     455
     456                $terms = $this->create_hierarchical_terms_and_posts();
     457
     458                $found = get_terms( $tax, array(
     459                        'hide_empty' => true,
     460                        'fields' => 'names',
     461                ) );
     462
     463                $expected = array(
     464                        'Parent 1',
     465                        'Parent 2',
     466                        'Child 1',
     467                );
     468
     469                _unregister_taxonomy( 'hierarchical_fields' );
     470
     471                $this->assertEqualSets( $expected, $found );
     472        }
     473
     474        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names_hierarchical_false() {
     475                // Set up a clean taxonomy.
     476                $tax = 'hierarchical_fields';
     477                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     478
     479                $terms = $this->create_hierarchical_terms_and_posts();
     480
     481                $found = get_terms( $tax, array(
     482                        'hide_empty' => true,
     483                        'fields' => 'names',
     484                        'hierarchical' => false,
     485                ) );
     486
     487                $expected = array(
     488                        'Parent 2',
     489                        'Child 1',
     490                );
     491
     492                _unregister_taxonomy( 'hierarchical_fields' );
     493
     494                $this->assertEqualSets( $expected, $found );
     495        }
     496
     497        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_count() {
     498                // Set up a clean taxonomy.
     499                $tax = 'hierarchical_fields';
     500                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     501
     502                $terms = $this->create_hierarchical_terms_and_posts();
     503
     504                $found = get_terms( $tax, array(
     505                        'hide_empty' => false,
     506                        'fields' => 'count',
     507                ) );
     508
     509                _unregister_taxonomy( 'hierarchical_fields' );
     510
     511                $this->assertEquals( 5, $found );
     512        }
     513
     514        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count() {
     515                // Set up a clean taxonomy.
     516                $tax = 'hierarchical_fields';
     517                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     518
     519                $terms = $this->create_hierarchical_terms_and_posts();
     520
     521                $found = get_terms( $tax, array(
     522                        'hide_empty' => true,
     523                        'fields' => 'count',
     524                ) );
     525
     526                _unregister_taxonomy( 'hierarchical_fields' );
     527
     528                // When using 'fields=count', 'hierarchical' is forced to false.
     529                $this->assertEquals( 2, $found );
     530        }
     531
     532        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count_hierarchical_false() {
     533                // Set up a clean taxonomy.
     534                $tax = 'hierarchical_fields';
     535                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     536
     537                $terms = $this->create_hierarchical_terms_and_posts();
     538
     539                $found = get_terms( $tax, array(
     540                        'hide_empty' => true,
     541                        'fields' => 'count',
     542                        'hierarchical' => false,
     543                ) );
     544
     545                _unregister_taxonomy( 'hierarchical_fields' );
     546
     547                $this->assertEquals( 2, $found );
     548        }
     549
     550        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idparent() {
     551                // Set up a clean taxonomy.
     552                $tax = 'hierarchical_fields';
     553                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     554
     555                $terms = $this->create_hierarchical_terms_and_posts();
     556
     557                $found = get_terms( $tax, array(
     558                        'hide_empty' => false,
     559                        'fields' => 'id=>parent',
     560                ) );
     561
     562                $expected = array(
     563                        $terms['parent1'] => 0,
     564                        $terms['parent2'] => 0,
     565                        $terms['child1'] => $terms['parent1'],
     566                        $terms['child2'] => $terms['parent1'],
     567                        $terms['grandchild1'] => $terms['child1'],
     568                );
     569
     570                _unregister_taxonomy( 'hierarchical_fields' );
     571
     572                $this->assertEqualSets( $expected, $found );
     573        }
     574
     575        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent() {
     576                // Set up a clean taxonomy.
     577                $tax = 'hierarchical_fields';
     578                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     579
     580                $terms = $this->create_hierarchical_terms_and_posts();
     581
     582                $found = get_terms( $tax, array(
     583                        'hide_empty' => true,
     584                        'fields' => 'id=>parent',
     585                ) );
     586
     587                $expected = array(
     588                        $terms['parent1'] => 0,
     589                        $terms['parent2'] => 0,
     590                        $terms['child1'] => $terms['parent1'],
     591                );
     592
     593                _unregister_taxonomy( 'hierarchical_fields' );
     594
     595                $this->assertEqualSets( $expected, $found );
     596        }
     597
     598        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent_hierarchical_false() {
     599                // Set up a clean taxonomy.
     600                $tax = 'hierarchical_fields';
     601                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     602
     603                $terms = $this->create_hierarchical_terms_and_posts();
     604
     605                $found = get_terms( $tax, array(
     606                        'hide_empty' => true,
     607                        'fields' => 'id=>parent',
     608                        'hierarchical' => false,
     609                ) );
     610
     611                $expected = array(
     612                        $terms['parent2'] => 0,
     613                        $terms['child2'] => $terms['parent1'],
     614                );
     615
     616                _unregister_taxonomy( 'hierarchical_fields' );
     617
     618                $this->assertEqualSets( $expected, $found );
     619        }
     620
     621        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idslug() {
     622                // Set up a clean taxonomy.
     623                $tax = 'hierarchical_fields';
     624                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     625
     626                $terms = $this->create_hierarchical_terms_and_posts();
     627
     628                $found = get_terms( $tax, array(
     629                        'hide_empty' => false,
     630                        'fields' => 'id=>slug',
     631                ) );
     632
     633                $expected = array(
     634                        $terms['parent1'] => 'parent-1',
     635                        $terms['parent2'] => 'parent-2',
     636                        $terms['child1'] => 'child-1',
     637                        $terms['child2'] => 'child-2',
     638                        $terms['grandchild1'] => 'grandchild-1',
     639                );
     640
     641                _unregister_taxonomy( 'hierarchical_fields' );
     642
     643                $this->assertEqualSets( $expected, $found );
     644        }
     645
     646        /**
     647         * @ticket 29859
     648         */
     649        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug() {
     650                // Set up a clean taxonomy.
     651                $tax = 'hierarchical_fields';
     652                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     653
     654                $terms = $this->create_hierarchical_terms_and_posts();
     655
     656                $found = get_terms( $tax, array(
     657                        'hide_empty' => true,
     658                        'fields' => 'id=>slug',
     659                ) );
     660
     661                $expected = array(
     662                        $terms['parent1'] => 'parent-1',
     663                        $terms['parent2'] => 'parent-2',
     664                        $terms['child1'] => 'child-1',
     665                );
     666
     667                _unregister_taxonomy( 'hierarchical_fields' );
     668
     669                $this->assertEqualSets( $expected, $found );
     670        }
     671
     672        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug_hierarchical_false() {
     673                // Set up a clean taxonomy.
     674                $tax = 'hierarchical_fields';
     675                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     676
     677                $terms = $this->create_hierarchical_terms_and_posts();
     678
     679                $found = get_terms( $tax, array(
     680                        'hide_empty' => true,
     681                        'fields' => 'id=>slug',
     682                        'hierarchical' => false,
     683                ) );
     684
     685                $expected = array(
     686                        $terms['parent2'] => 'parent-2',
     687                        $terms['child1'] => 'child-1',
     688                );
     689
     690                _unregister_taxonomy( 'hierarchical_fields' );
     691
     692                $this->assertEqualSets( $expected, $found );
     693        }
     694
     695        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idname() {
     696                // Set up a clean taxonomy.
     697                $tax = 'hierarchical_fields';
     698                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     699
     700                $terms = $this->create_hierarchical_terms_and_posts();
     701
     702                $found = get_terms( $tax, array(
     703                        'hide_empty' => false,
     704                        'fields' => 'id=>name',
     705                ) );
     706
     707                $expected = array(
     708                        $terms['parent1'] => 'Parent 1',
     709                        $terms['parent2'] => 'Parent 2',
     710                        $terms['child1'] => 'Child 1',
     711                        $terms['child2'] => 'Child 2',
     712                        $terms['grandchild1'] => 'Grandchild 1',
     713                );
     714
     715                _unregister_taxonomy( 'hierarchical_fields' );
     716
     717                $this->assertEqualSets( $expected, $found );
     718        }
     719
     720        /**
     721         * @ticket 29859
     722         */
     723        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname() {
     724                // Set up a clean taxonomy.
     725                $tax = 'hierarchical_fields';
     726                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     727
     728                $terms = $this->create_hierarchical_terms_and_posts();
     729
     730                $found = get_terms( $tax, array(
     731                        'hide_empty' => true,
     732                        'fields' => 'id=>name',
     733                ) );
     734
     735                $expected = array(
     736                        $terms['parent1'] => 'Parent 1',
     737                        $terms['parent2'] => 'Parent 2',
     738                        $terms['child1'] => 'Child 1',
     739                );
     740
     741                _unregister_taxonomy( 'hierarchical_fields' );
     742
     743                $this->assertEqualSets( $expected, $found );
     744        }
     745
     746        public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname_hierarchical_false() {
     747                // Set up a clean taxonomy.
     748                $tax = 'hierarchical_fields';
     749                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     750
     751                $terms = $this->create_hierarchical_terms_and_posts();
     752
     753                $found = get_terms( $tax, array(
     754                        'hide_empty' => true,
     755                        'fields' => 'id=>name',
     756                        'hierarchical' => false,
     757                ) );
     758
     759                $expected = array(
     760                        $terms['parent2'] => 'Parent 2',
     761                        $terms['child1'] => 'Child 1',
     762                );
     763
     764                _unregister_taxonomy( 'hierarchical_fields' );
     765
     766                $this->assertEqualSets( $expected, $found );
     767        }
     768
     769        protected function create_hierarchical_terms_and_posts() {
     770
     771                $terms = array();
     772
     773                $terms['parent1'] = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => 'hierarchical_fields' ) );
     774                $terms['parent2'] = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => 'hierarchical_fields' ) );
     775                $terms['child1'] = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ) );
     776                $terms['child2'] = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ) );
     777                $terms['grandchild1'] = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['child1'] ) );
     778
     779                $post_id = $this->factory->post->create();
     780                wp_set_post_terms( $post_id, $terms['parent2'], 'hierarchical_fields', true );
     781                wp_set_post_terms( $post_id, $terms['child1'], 'hierarchical_fields', true );
     782
     783                return $terms;
     784        }
    354785}
Note: See TracChangeset for help on using the changeset viewer.