| | 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 | // expected: Montreal, Nepean, PEI, Toronto |
| | 470 | $terms = get_terms( $tax, array( 'children_only' => true, 'hide_empty' => false) ); |
| | 471 | |
| | 472 | |
| | 473 | // Should be 4 |
| | 474 | $this->assertEquals( 4, count($terms) ); |
| | 475 | |
| | 476 | // Item 3 should be PEI |
| | 477 | $term = $terms[2]; |
| | 478 | $this->assertEquals( 'PEI', $term->name ); |
| | 479 | |
| | 480 | |
| | 481 | // Test 2: hide_empty = true |
| | 482 | // expected: Montreal, Nepean |
| | 483 | $terms = get_terms( $tax, array( 'children_only' => true, 'hide_empty' => true) ); |
| | 484 | |
| | 485 | // Should be 2 |
| | 486 | $this->assertEquals( 2, count($terms) ); |
| | 487 | |
| | 488 | // Item 2 should be Nepean |
| | 489 | $term = $terms[1]; |
| | 490 | $this->assertEquals( 'Nepean', $term->name ); |
| | 491 | |
| | 492 | |
| | 493 | // Test 3: child_of = Quebec |
| | 494 | // expected: Montreal |
| | 495 | $terms = get_terms( $tax, array( 'children_only' => true, 'hide_empty' => false, 'child_of' => $quebec ) ); |
| | 496 | |
| | 497 | // Should be 1 |
| | 498 | $this->assertEquals( 1, count($terms) ); |
| | 499 | |
| | 500 | // Item 1 should be Montreal |
| | 501 | $term = $terms[0]; |
| | 502 | $this->assertEquals( 'Montreal', $term->name ); |
| | 503 | |
| | 504 | _unregister_taxonomy( $tax ); |
| | 505 | } |
| | 506 | |