Make WordPress Core

Ticket #29839: 20141116_03-getTerms.diff

File 20141116_03-getTerms.diff, 5.3 KB (added by theMikeD, 11 years ago)
  • tests/phpunit/tests/term/getTerms.php

     
    424424                $this->assertEquals( array( $t1, $t3 ), $found );
    425425        }
    426426
     427        /**
     428         * @ticket 29839
     429         */
     430        function test_get_terms_childless() {
     431                $tax = 'location';
     432                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     433                /*
     434                Canada
     435                        Ontario
     436                                Ottawa
     437                                        Nepean
     438                                Toronto
     439                        Quebec
     440                                Montreal
     441                        PEI
     442                */
     443                // Level 1
     444                $canada = $this->factory->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) );
     445
     446                // Level 2
     447                $ontario = $this->factory->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) );
     448                $quebec  = $this->factory->term->create( array( 'name' => 'Quebec',  'parent' => $canada, 'taxonomy' => $tax ) );
     449                $pei     = $this->factory->term->create( array( 'name' => 'PEI', '    parent' => $canada, 'taxonomy' => $tax ) );
     450
     451                // Level 3
     452                $toronto=  $this->factory->term->create( array( 'name' => 'Toronto',  'parent' => $ontario, 'taxonomy' => $tax ) );
     453                $ottawa=   $this->factory->term->create( array( 'name' => 'Ottawa',   'parent' => $ontario, 'taxonomy' => $tax ) );
     454                $montreal= $this->factory->term->create( array( 'name' => 'Montreal', 'parent' => $quebec,  'taxonomy' => $tax ) );
     455
     456                // Level 4
     457                $nepean = $this->factory->term->create( array( 'name' => 'Nepean', 'parent' => $ottawa, 'taxonomy' => $tax ) );
     458
     459                $post1_id = $this->factory->post->create();
     460                $post2_id = $this->factory->post->create();
     461                $post3_id = $this->factory->post->create();
     462
     463                wp_set_post_terms( $post1_id, $nepean, $tax );
     464                wp_set_post_terms( $post2_id, $montreal, $tax );
     465                wp_set_post_terms( $post3_id, $ottawa, $tax );
     466
     467
     468                // Test 1: hide_empty = false
     469                $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false) );
     470
     471                // Expected: Montreal, Nepean, PEI, Toronto
     472                $this->assertEquals( 4, count($terms) );
     473                $this->assertEqualSets( array($montreal, $nepean, $pei, $toronto), wp_list_pluck( $terms, 'term_id' ) );
     474
     475
     476                // Test 2: hide_empty = true
     477                $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => true) );
     478
     479                // Expected: Montreal, Nepean
     480                $this->assertEquals( 2, count($terms) );
     481                $this->assertEqualSets( array($montreal, $nepean), wp_list_pluck( $terms, 'term_id' ) );
     482
     483
     484                // Test 3: child_of = Quebec
     485                $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false, 'child_of' => $quebec ) );
     486
     487                // Expected: Montreal
     488                $this->assertEquals( 1, count($terms) );
     489                $this->assertEqualSets( array( $montreal ), wp_list_pluck( $terms, 'term_id' ) );
     490
     491
     492                // Test 4: all
     493                $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false, 'get' => 'all' ) );
     494                $terms_2 = get_terms( $tax, array( 'hide_empty' => false, 'get' => 'all' ) );
     495
     496                // Expected: both match
     497                $this->assertEqualSets( wp_list_pluck( $terms, 'term_id' ), wp_list_pluck( $terms_2, 'term_id' ) );
     498
     499                _unregister_taxonomy( $tax );
     500
     501
     502                // If run on a flat heirarchy it should return everything.
     503                $flat_tax = 'countries';
     504                register_taxonomy( $flat_tax, 'post', array( 'hierarchical' => false ) );
     505                $australia = $this->factory->term->create( array( 'name' => 'Australia', 'taxonomy' => $flat_tax ) );
     506                $china  =    $this->factory->term->create( array( 'name' => 'China',     'taxonomy' => $flat_tax ) );
     507                $tanzania =  $this->factory->term->create( array( 'name' => 'Tanzania',  'taxonomy' => $flat_tax ) );
     508
     509                // Note that wp_set_post_terms() expects parameter 2 to be a string when the taxonomy is flat
     510                //  so lets redefine the term IDs created above to be the term objects
     511                $australia = get_term( $australia, $flat_tax );
     512                $china  =    get_term( $china, $flat_tax );
     513                $tanzania =  get_term( $tanzania, $flat_tax );
     514
     515                $post4_id = $this->factory->post->create();
     516                $post5_id = $this->factory->post->create();
     517
     518                // Note that wp_set_post_terms() expects parameter 2 to be a string when the taxonomy is flat
     519                wp_set_post_terms( $post4_id, $australia->name, $flat_tax );
     520                wp_set_post_terms( $post5_id, $tanzania->name,  $flat_tax );
     521
     522
     523                // Test 5: hide_empty = false
     524                $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) );
     525
     526                // Expected: Australia, China, Tanzania
     527                $this->assertEquals( 3, count($terms) );
     528                $this->assertEqualSets( array($australia->term_id, $china->term_id, $tanzania->term_id), wp_list_pluck( $terms, 'term_id' ) );
     529
     530
     531                // Test 5: hide_empty = true
     532                $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => true) );
     533
     534                // Expected: Australia, Tanzania
     535                $this->assertEquals( 2, count($terms) );
     536                $this->assertEqualSets( array($australia->term_id, $tanzania->term_id), wp_list_pluck( $terms, 'term_id' ) );
     537
     538
     539                # Test 6: all should equal ('childless' => true, 'hide_empty' => false)
     540                $terms_1 = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) );
     541                $terms_2 = get_terms( $flat_tax, array( 'hide_empty' => false, 'get' => 'all' ) );
     542
     543                $this->assertEqualSets(wp_list_pluck( $terms_1, 'term_id' ), wp_list_pluck( $terms_2, 'term_id' ));
     544
     545                _unregister_taxonomy( $flat_tax );
     546        }
     547
    427548        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
    428549                // Set up a clean taxonomy.
    429550                $tax = 'hierarchical_fields';