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