WordPress.org

Make WordPress Core

Ticket #29859: 29859.unit-tests.diff

File 29859.unit-tests.diff, 6.9 KB (added by technical_mastermind, 7 years ago)

Unit tests that I added.

  • tests/phpunit/tests/term/getTerms.php

     
    351351
    352352                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
    353353        }
     354
     355        function test_get_terms_fields_hierarchical() {
     356                // Setup a clean taxonomy, no surprises
     357                $tax = 'hierarchical_fields';
     358                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     359
     360                // Setup two parents, two children on the first parent, one grandchild on the first child
     361                $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) );
     362                $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) );
     363                $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) );
     364                $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) );
     365                $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) );
     366
     367                // Set parent 2 and child 1 to have a post in them (so they are NOT empty)
     368                $post_id = $this->factory->post->create();
     369                wp_set_post_terms( $post_id, $term_parent2, $tax, true );
     370                wp_set_post_terms( $post_id, $term_child1, $tax, true );
     371
     372                // check 'fields' => 'ids' when hide_empty is false
     373                $terms_ids_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'ids' ) );
     374                $this->assertEqualSets( array( $term_parent1, $term_parent2, $term_child1, $term_child2, $term_grandchild1 ), $terms_ids_1 );
     375
     376                // check 'fields' => 'ids' when hide_empty is true
     377                $terms_ids_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'ids' ) );
     378                $this->assertEqualSets( array( $term_parent1, $term_parent2, $term_child1 ), $terms_ids_2 );
     379
     380                // check 'fields' => 'ids' when hide_empty is true & hierarchical is false
     381                $terms_ids_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'ids', 'hierarchical' => false ) );
     382                $this->assertEqualSets( array( $term_parent2, $term_child1 ), $terms_ids_3 );
     383
     384                // check 'fields' => 'names' when hide_empty is false
     385                $terms_names_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'names' ) );
     386                $this->assertEqualSets( array( 'Parent 1', 'Parent 2', 'Child 1', 'Child 2', 'Grandchild 1' ), $terms_names_1 );
     387
     388                // check 'fields' => 'names' when hide_empty is true
     389                $terms_names_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'names' ) );
     390                $this->assertEqualSets( array( 'Parent 1', 'Parent 2', 'Child 1' ), $terms_names_2 );
     391
     392                // check 'fields' => 'names' when hide_empty is true & hierarchical is false
     393                $terms_names_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'names', 'hierarchical' => false ) );
     394                $this->assertEqualSets( array( 'Parent 2', 'Child 1' ), $terms_names_3 );
     395
     396                // check 'fields' => 'count' when hide_empty is false
     397                $terms_count_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'count' ) );
     398                $this->assertEquals( 5, $terms_count_1 );
     399
     400                // check 'fields' => 'count' when hide_empty is true
     401                $terms_count_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'count' ) );
     402                $this->assertEquals( 2, $terms_count_2 );
     403                /*
     404                 * NOTE: This should be the same as when hierarchical is false since get_terms() sets hierarchical to false
     405                 * when using 'fields' => 'count'. This is included for completeness.
     406                 */
     407
     408                // check 'fields' => 'count' when hide_empty is true & hierarchical is false
     409                $terms_count_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'count', 'hierarchical' => false ) );
     410                $this->assertEquals( 2, $terms_count_3 );
     411
     412                // check 'fields' => 'id=>parent' when hide_empty is false
     413                $terms_id_parent_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
     414                $this->assertEquals( array(
     415                        $term_parent1 => 0,
     416                        $term_parent2 => 0,
     417                        $term_child1 => $term_parent1,
     418                        $term_child2 => $term_parent1,
     419                        $term_grandchild1 => $term_child1,
     420                ), $terms_id_parent_1 );
     421
     422                // check 'fields' => 'id=>parent' when hide_empty is true
     423                $terms_id_parent_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>parent' ) );
     424                $this->assertEquals( array(
     425                        $term_parent1 => 0,
     426                        $term_parent2 => 0,
     427                        $term_child1 => $term_parent1,
     428                ), $terms_id_parent_2 );
     429
     430                // check 'fields' => 'id=>parent' when hide_empty is true & hierarchical is false
     431                $terms_id_parent_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>parent', 'hierarchical' => false ) );
     432                $this->assertEquals( array(
     433                        $term_parent2 => 0,
     434                        $term_child1 => $term_parent1,
     435                ), $terms_id_parent_3 );
     436
     437                // check 'fields' => 'id=>slug' when hide_empty is false
     438                $terms_id_slug_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'id=>slug' ) );
     439                $this->assertEquals( array(
     440                        $term_parent1 => 'parent-1',
     441                        $term_parent2 => 'parent-2',
     442                        $term_child1 => 'child-1',
     443                        $term_child2 => 'child-2',
     444                        $term_grandchild1 => 'grandchild-1',
     445                ), $terms_id_slug_1 );
     446
     447                // check 'fields' => 'id=>slug' when hide_empty is true
     448                $terms_id_slug_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>slug' ) );
     449                $this->assertEquals( array(
     450                        $term_parent1 => 'parent-1',
     451                        $term_parent2 => 'parent-2',
     452                        $term_child1 => 'child-1',
     453                ), $terms_id_slug_2 );
     454
     455                // check 'fields' => 'id=>slug' when hide_empty is true & hierarchical is false
     456                $terms_id_slug_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>slug', 'hierarchical' => false ) );
     457                $this->assertEquals( array(
     458                        $term_parent2 => 'parent-2',
     459                        $term_child1 => 'child-1',
     460                ), $terms_id_slug_3 );
     461
     462                // check 'fields' => 'id=>name' when hide_empty is false
     463                $terms_id_name_1 = get_terms( $tax, array( 'hide_empty' => false, 'fields' => 'id=>name' ) );
     464                $this->assertEquals( array(
     465                        $term_parent1 => 'Parent 1',
     466                        $term_parent2 => 'Parent 2',
     467                        $term_child1 => 'Child 1',
     468                        $term_child2 => 'Child 2',
     469                        $term_grandchild1 => 'Grandchild 1',
     470                ), $terms_id_name_1 );
     471
     472                // check 'fields' => 'id=>name' when hide_empty is true
     473                $terms_id_name_2 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>name' ) );
     474                $this->assertEquals( array(
     475                        $term_parent1 => 'Parent 1',
     476                        $term_parent2 => 'Parent 2',
     477                        $term_child1 => 'Child 1',
     478                ), $terms_id_name_2 );
     479
     480                // check 'fields' => 'id=>name' when hide_empty is true & hierarchical is false
     481                $terms_id_name_3 = get_terms( $tax, array( 'hide_empty' => true, 'fields' => 'id=>name', 'hierarchical' => false ) );
     482                $this->assertEquals( array(
     483                        $term_parent2 => 'Parent 2',
     484                        $term_child1 => 'Child 1',
     485                ), $terms_id_name_3 );
     486
     487        }
    354488}