Make WordPress Core

Ticket #29839: 20141115_01-getTerms.diff

File 20141115_01-getTerms.diff, 2.9 KB (added by theMikeD, 11 years ago)

unit test for #29839

  • 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_children_only() {
     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
     460                $post1_id = $this->factory->post->create();
     461                $post2_id = $this->factory->post->create();
     462                $post3_id = $this->factory->post->create();
     463
     464                wp_set_post_terms( $post1_id, $nepean, $tax );
     465                wp_set_post_terms( $post2_id, $montreal, $tax );
     466                wp_set_post_terms( $post3_id, $ottawa, $tax );
     467
     468                // Test 1: hide_empty = false
     469                $terms = get_terms( $tax, array( 'children_only' => 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( 'children_only' => 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( 'children_only' => 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                _unregister_taxonomy( $tax );
     492        }
     493
    427494        public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
    428495                // Set up a clean taxonomy.
    429496                $tax = 'hierarchical_fields';