| | 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 | |
| | 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 | |
| | 469 | // Test 1: hide_empty = false |
| | 470 | $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false) ); |
| | 471 | |
| | 472 | // Expected: Montreal, Nepean, PEI, Toronto |
| | 473 | $this->assertEquals( 4, count($terms) ); |
| | 474 | $this->assertEqualSets( array($montreal, $nepean, $pei, $toronto), wp_list_pluck( $terms, 'term_id' ) ); |
| | 475 | |
| | 476 | |
| | 477 | // Test 2: hide_empty = true |
| | 478 | $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => true) ); |
| | 479 | |
| | 480 | // Expected: Montreal, Nepean |
| | 481 | $this->assertEquals( 2, count($terms) ); |
| | 482 | $this->assertEqualSets( array($montreal, $nepean), wp_list_pluck( $terms, 'term_id' ) ); |
| | 483 | |
| | 484 | |
| | 485 | // Test 3: child_of = Quebec |
| | 486 | $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false, 'child_of' => $quebec ) ); |
| | 487 | |
| | 488 | // Expected: Montreal |
| | 489 | $this->assertEquals( 1, count($terms) ); |
| | 490 | $this->assertEqualSets( array( $montreal ), wp_list_pluck( $terms, 'term_id' ) ); |
| | 491 | |
| | 492 | |
| | 493 | // Test 4: all |
| | 494 | $terms = get_terms( $tax, array( 'childless' => true, 'hide_empty' => false, 'get' => 'all' ) ); |
| | 495 | $terms_2 = get_terms( $tax, array( 'hide_empty' => false, 'get' => 'all' ) ); |
| | 496 | |
| | 497 | // Expected: both match |
| | 498 | $this->assertEqualSets( wp_list_pluck( $terms, 'term_id' ), wp_list_pluck( $terms_2, 'term_id' ) ); |
| | 499 | |
| | 500 | _unregister_taxonomy( $tax ); |
| | 501 | |
| | 502 | |
| | 503 | // If run on a flat heirarchy it should return everything. |
| | 504 | $flat_tax = 'countries'; |
| | 505 | register_taxonomy( $flat_tax, 'post', array( 'hierarchical' => false ) ); |
| | 506 | $australia = $this->factory->term->create( array( 'name' => 'Australia', 'taxonomy' => $flat_tax ) ); |
| | 507 | $china = $this->factory->term->create( array( 'name' => 'China', 'taxonomy' => $flat_tax ) ); |
| | 508 | $tanzania = $this->factory->term->create( array( 'name' => 'Tanzania', 'taxonomy' => $flat_tax ) ); |
| | 509 | |
| | 510 | $post4_id = $this->factory->post->create(); |
| | 511 | $post5_id = $this->factory->post->create(); |
| | 512 | |
| | 513 | $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) ); |
| | 514 | error_log( print_r( $terms )); |
| | 515 | wp_set_post_terms( $post4_id, $montreal, $flat_tax ); |
| | 516 | wp_set_post_terms( $post5_id, $ottawa, $flat_tax ); |
| | 517 | $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) ); |
| | 518 | error_log( print_r( $terms )); |
| | 519 | |
| | 520 | |
| | 521 | // Test 4: hide_empty = false |
| | 522 | $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) ); |
| | 523 | |
| | 524 | // Expected: Australia, Canada, Tanzania |
| | 525 | $this->assertEquals( 3, count($terms) ); |
| | 526 | $this->assertEqualSets( array($australia, $china, $tanzania), wp_list_pluck( $terms, 'term_id' ) ); |
| | 527 | |
| | 528 | |
| | 529 | // Test 5: hide_empty = true |
| | 530 | $terms = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => true) ); |
| | 531 | |
| | 532 | // Expected: Australia, Tanzania |
| | 533 | $this->assertEquals( 2, count($terms) ); |
| | 534 | $this->assertEqualSets( array($australia, $tanzania), wp_list_pluck( $terms, 'term_id' ) ); |
| | 535 | |
| | 536 | |
| | 537 | # Test 6: all should equal ('childless' => true, 'hide_empty' => false) |
| | 538 | $terms_1 = get_terms( $flat_tax, array( 'childless' => true, 'hide_empty' => false) ); |
| | 539 | $terms_2 = get_terms( $flat_tax, array( 'hide_empty' => false, 'get' => 'all' ) ); |
| | 540 | |
| | 541 | $this->assertEqualSets(wp_list_pluck( $terms_1, 'term_id' ), wp_list_pluck( $terms_2, 'term_id' )); |
| | 542 | |
| | 543 | _unregister_taxonomy( $flat_tax ); |
| | 544 | } |
| | 545 | |