| 354 | |
| 355 | /** |
| 356 | * @ticket 29859 |
| 357 | */ |
| 358 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() { |
| 359 | // Set up a clean taxonomy. |
| 360 | $tax = 'hierarchical_fields'; |
| 361 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 362 | |
| 363 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 364 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 365 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 366 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 367 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 368 | |
| 369 | $found = get_terms( $tax, array( |
| 370 | 'hide_empty' => false, |
| 371 | 'fields' => 'ids', |
| 372 | ) ); |
| 373 | |
| 374 | $expected = array( |
| 375 | $term_parent1, |
| 376 | $term_parent2, |
| 377 | $term_child1, |
| 378 | $term_child2, |
| 379 | $term_grandchild1, |
| 380 | ); |
| 381 | |
| 382 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 383 | |
| 384 | $this->assertEqualSets( $expected, $found ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @ticket 29859 |
| 389 | */ |
| 390 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids() { |
| 391 | // Set up a clean taxonomy. |
| 392 | $tax = 'hierarchical_fields'; |
| 393 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 394 | |
| 395 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 396 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 397 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 398 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 399 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 400 | |
| 401 | // Set parent 2 and child 1 to have a post in them (so they are NOT empty) |
| 402 | $post_id = $this->factory->post->create(); |
| 403 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 404 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 405 | |
| 406 | $found = get_terms( $tax, array( |
| 407 | 'hide_empty' => true, |
| 408 | 'fields' => 'ids', |
| 409 | ) ); |
| 410 | |
| 411 | $expected = array( |
| 412 | $term_parent1, |
| 413 | $term_parent2, |
| 414 | $term_child1, |
| 415 | ); |
| 416 | |
| 417 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 418 | |
| 419 | $this->assertEqualSets( $expected, $found ); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * @ticket 29859 |
| 424 | */ |
| 425 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids_hierarchical_false() { |
| 426 | // Set up a clean taxonomy. |
| 427 | $tax = 'hierarchical_fields'; |
| 428 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 429 | |
| 430 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 431 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 432 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 433 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 434 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 435 | |
| 436 | // Set parent 2 and child 1 to have a post in them (so they are NOT empty) |
| 437 | $post_id = $this->factory->post->create(); |
| 438 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 439 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 440 | |
| 441 | $found = get_terms( $tax, array( |
| 442 | 'hide_empty' => true, |
| 443 | 'fields' => 'ids', |
| 444 | 'hierarchical' => false, |
| 445 | ) ); |
| 446 | |
| 447 | $expected = array( |
| 448 | $term_parent2, |
| 449 | $term_child1, |
| 450 | ); |
| 451 | |
| 452 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 453 | |
| 454 | $this->assertEqualSets( $expected, $found ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @ticket 29859 |
| 459 | */ |
| 460 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_names() { |
| 461 | // Set up a clean taxonomy. |
| 462 | $tax = 'hierarchical_fields'; |
| 463 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 464 | |
| 465 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 466 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 467 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 468 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 469 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 470 | |
| 471 | $found = get_terms( $tax, array( |
| 472 | 'hide_empty' => false, |
| 473 | 'fields' => 'names', |
| 474 | ) ); |
| 475 | |
| 476 | $expected = array( |
| 477 | 'Parent 1', |
| 478 | 'Parent 2', |
| 479 | 'Child 1', |
| 480 | 'Child 2', |
| 481 | 'Grandchild 1', |
| 482 | ); |
| 483 | |
| 484 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 485 | |
| 486 | $this->assertEqualSets( $expected, $found ); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * @ticket 29859 |
| 491 | */ |
| 492 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names() { |
| 493 | // Set up a clean taxonomy. |
| 494 | $tax = 'hierarchical_fields'; |
| 495 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 496 | |
| 497 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 498 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 499 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 500 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 501 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 502 | |
| 503 | $post_id = $this->factory->post->create(); |
| 504 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 505 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 506 | |
| 507 | $found = get_terms( $tax, array( |
| 508 | 'hide_empty' => true, |
| 509 | 'fields' => 'names', |
| 510 | ) ); |
| 511 | |
| 512 | $expected = array( |
| 513 | 'Parent 1', |
| 514 | 'Parent 2', |
| 515 | 'Child 1', |
| 516 | ); |
| 517 | |
| 518 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 519 | |
| 520 | $this->assertEqualSets( $expected, $found ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @ticket 29859 |
| 525 | */ |
| 526 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names_hierarchical_false() { |
| 527 | // Set up a clean taxonomy. |
| 528 | $tax = 'hierarchical_fields'; |
| 529 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 530 | |
| 531 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 532 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 533 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 534 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 535 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 536 | |
| 537 | $post_id = $this->factory->post->create(); |
| 538 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 539 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 540 | |
| 541 | $found = get_terms( $tax, array( |
| 542 | 'hide_empty' => true, |
| 543 | 'fields' => 'names', |
| 544 | 'hierarchical' => false, |
| 545 | ) ); |
| 546 | |
| 547 | $expected = array( |
| 548 | 'Parent 2', |
| 549 | 'Child 1', |
| 550 | ); |
| 551 | |
| 552 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 553 | |
| 554 | $this->assertEqualSets( $expected, $found ); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @ticket 29859 |
| 559 | */ |
| 560 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_count() { |
| 561 | // Set up a clean taxonomy. |
| 562 | $tax = 'hierarchical_fields'; |
| 563 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 564 | |
| 565 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 566 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 567 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 568 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 569 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 570 | |
| 571 | $found = get_terms( $tax, array( |
| 572 | 'hide_empty' => false, |
| 573 | 'fields' => 'count', |
| 574 | ) ); |
| 575 | |
| 576 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 577 | |
| 578 | $this->assertEquals( 5, $found ); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @ticket 29859 |
| 583 | */ |
| 584 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count() { |
| 585 | // Set up a clean taxonomy. |
| 586 | $tax = 'hierarchical_fields'; |
| 587 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 588 | |
| 589 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 590 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 591 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 592 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 593 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 594 | |
| 595 | $post_id = $this->factory->post->create(); |
| 596 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 597 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 598 | |
| 599 | $found = get_terms( $tax, array( |
| 600 | 'hide_empty' => true, |
| 601 | 'fields' => 'count', |
| 602 | ) ); |
| 603 | |
| 604 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 605 | |
| 606 | // When using 'fields=count', 'hierarchical' is forced to false. |
| 607 | $this->assertEquals( 2, $found ); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * @ticket 29859 |
| 612 | */ |
| 613 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count_hierarchical_false() { |
| 614 | // Set up a clean taxonomy. |
| 615 | $tax = 'hierarchical_fields'; |
| 616 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 617 | |
| 618 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 619 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 620 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 621 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 622 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 623 | |
| 624 | $post_id = $this->factory->post->create(); |
| 625 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 626 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 627 | |
| 628 | $found = get_terms( $tax, array( |
| 629 | 'hide_empty' => true, |
| 630 | 'fields' => 'count', |
| 631 | 'hierarchical' => false, |
| 632 | ) ); |
| 633 | |
| 634 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 635 | |
| 636 | $this->assertEquals( 2, $found ); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * @ticket 29859 |
| 641 | */ |
| 642 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idparent() { |
| 643 | // Set up a clean taxonomy. |
| 644 | $tax = 'hierarchical_fields'; |
| 645 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 646 | |
| 647 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 648 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 649 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 650 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 651 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 652 | |
| 653 | $found = get_terms( $tax, array( |
| 654 | 'hide_empty' => false, |
| 655 | 'fields' => 'id=>parent', |
| 656 | ) ); |
| 657 | |
| 658 | $expected = array( |
| 659 | $term_parent1 => 0, |
| 660 | $term_parent2 => 0, |
| 661 | $term_child1 => $term_parent1, |
| 662 | $term_child2 => $term_parent1, |
| 663 | $term_grandchild1 => $term_child1, |
| 664 | ); |
| 665 | |
| 666 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 667 | |
| 668 | $this->assertEqualSets( $expected, $found ); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * @ticket 29859 |
| 673 | */ |
| 674 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent() { |
| 675 | // Set up a clean taxonomy. |
| 676 | $tax = 'hierarchical_fields'; |
| 677 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 678 | |
| 679 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 680 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 681 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 682 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 683 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 684 | |
| 685 | $post_id = $this->factory->post->create(); |
| 686 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 687 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 688 | |
| 689 | $found = get_terms( $tax, array( |
| 690 | 'hide_empty' => true, |
| 691 | 'fields' => 'id=>parent', |
| 692 | ) ); |
| 693 | |
| 694 | $expected = array( |
| 695 | $term_parent1 => 0, |
| 696 | $term_parent2 => 0, |
| 697 | $term_child1 => $term_parent1, |
| 698 | ); |
| 699 | |
| 700 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 701 | |
| 702 | $this->assertEqualSets( $expected, $found ); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * @ticket 29859 |
| 707 | */ |
| 708 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent_hierarchical_false() { |
| 709 | // Set up a clean taxonomy. |
| 710 | $tax = 'hierarchical_fields'; |
| 711 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 712 | |
| 713 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 714 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 715 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 716 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 717 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 718 | |
| 719 | $post_id = $this->factory->post->create(); |
| 720 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 721 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 722 | |
| 723 | $found = get_terms( $tax, array( |
| 724 | 'hide_empty' => true, |
| 725 | 'fields' => 'id=>parent', |
| 726 | 'hierarchical' => false, |
| 727 | ) ); |
| 728 | |
| 729 | $expected = array( |
| 730 | $term_parent2 => 0, |
| 731 | $term_child1 => $term_parent1, |
| 732 | ); |
| 733 | |
| 734 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 735 | |
| 736 | $this->assertEqualSets( $expected, $found ); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * @ticket 29859 |
| 741 | */ |
| 742 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idslug() { |
| 743 | // Set up a clean taxonomy. |
| 744 | $tax = 'hierarchical_fields'; |
| 745 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 746 | |
| 747 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 748 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 749 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 750 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 751 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 752 | |
| 753 | $found = get_terms( $tax, array( |
| 754 | 'hide_empty' => false, |
| 755 | 'fields' => 'id=>slug', |
| 756 | ) ); |
| 757 | |
| 758 | $expected = array( |
| 759 | $term_parent1 => 'parent-1', |
| 760 | $term_parent2 => 'parent-2', |
| 761 | $term_child1 => 'child-1', |
| 762 | $term_child2 => 'child-2', |
| 763 | $term_grandchild1 => 'grandchild-1', |
| 764 | ); |
| 765 | |
| 766 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 767 | |
| 768 | $this->assertEqualSets( $expected, $found ); |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * @ticket 29859 |
| 773 | */ |
| 774 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug() { |
| 775 | // Set up a clean taxonomy. |
| 776 | $tax = 'hierarchical_fields'; |
| 777 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 778 | |
| 779 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 780 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 781 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 782 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 783 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 784 | |
| 785 | $post_id = $this->factory->post->create(); |
| 786 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 787 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 788 | |
| 789 | $found = get_terms( $tax, array( |
| 790 | 'hide_empty' => true, |
| 791 | 'fields' => 'id=>slug', |
| 792 | ) ); |
| 793 | |
| 794 | $expected = array( |
| 795 | $term_parent1 => 'parent-1', |
| 796 | $term_parent2 => 'parent-2', |
| 797 | $term_child1 => 'child-1', |
| 798 | ); |
| 799 | |
| 800 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 801 | |
| 802 | $this->assertEqualSets( $expected, $found ); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @ticket 29859 |
| 807 | */ |
| 808 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug_hierarchical_false() { |
| 809 | // Set up a clean taxonomy. |
| 810 | $tax = 'hierarchical_fields'; |
| 811 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 812 | |
| 813 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 814 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 815 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 816 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 817 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 818 | |
| 819 | $post_id = $this->factory->post->create(); |
| 820 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 821 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 822 | |
| 823 | $found = get_terms( $tax, array( |
| 824 | 'hide_empty' => true, |
| 825 | 'fields' => 'id=>slug', |
| 826 | 'hierarchical' => false, |
| 827 | ) ); |
| 828 | |
| 829 | $expected = array( |
| 830 | $term_parent2 => 'parent-2', |
| 831 | $term_child1 => 'child-1', |
| 832 | ); |
| 833 | |
| 834 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 835 | |
| 836 | $this->assertEqualSets( $expected, $found ); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * @ticket 29859 |
| 841 | */ |
| 842 | public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idname() { |
| 843 | // Set up a clean taxonomy. |
| 844 | $tax = 'hierarchical_fields'; |
| 845 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 846 | |
| 847 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 848 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 849 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 850 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 851 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 852 | |
| 853 | $found = get_terms( $tax, array( |
| 854 | 'hide_empty' => false, |
| 855 | 'fields' => 'id=>name', |
| 856 | ) ); |
| 857 | |
| 858 | $expected = array( |
| 859 | $term_parent1 => 'Parent 1', |
| 860 | $term_parent2 => 'Parent 2', |
| 861 | $term_child1 => 'Child 1', |
| 862 | $term_child2 => 'Child 2', |
| 863 | $term_grandchild1 => 'Grandchild 1', |
| 864 | ); |
| 865 | |
| 866 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 867 | |
| 868 | $this->assertEqualSets( $expected, $found ); |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * @ticket 29859 |
| 873 | */ |
| 874 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname() { |
| 875 | // Set up a clean taxonomy. |
| 876 | $tax = 'hierarchical_fields'; |
| 877 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 878 | |
| 879 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 880 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 881 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 882 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 883 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 884 | |
| 885 | $post_id = $this->factory->post->create(); |
| 886 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 887 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 888 | |
| 889 | $found = get_terms( $tax, array( |
| 890 | 'hide_empty' => true, |
| 891 | 'fields' => 'id=>name', |
| 892 | ) ); |
| 893 | |
| 894 | $expected = array( |
| 895 | $term_parent1 => 'Parent 1', |
| 896 | $term_parent2 => 'Parent 2', |
| 897 | $term_child1 => 'Child 1', |
| 898 | ); |
| 899 | |
| 900 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 901 | |
| 902 | $this->assertEqualSets( $expected, $found ); |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * @ticket 29859 |
| 907 | */ |
| 908 | public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname_hierarchical_false() { |
| 909 | // Set up a clean taxonomy. |
| 910 | $tax = 'hierarchical_fields'; |
| 911 | register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); |
| 912 | |
| 913 | $term_parent1 = $this->factory->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => $tax ) ); |
| 914 | $term_parent2 = $this->factory->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => $tax ) ); |
| 915 | $term_child1 = $this->factory->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 916 | $term_child2 = $this->factory->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => $tax, 'parent' => $term_parent1 ) ); |
| 917 | $term_grandchild1 = $this->factory->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => $tax, 'parent' => $term_child1 ) ); |
| 918 | |
| 919 | $post_id = $this->factory->post->create(); |
| 920 | wp_set_post_terms( $post_id, $term_parent2, $tax, true ); |
| 921 | wp_set_post_terms( $post_id, $term_child1, $tax, true ); |
| 922 | |
| 923 | $found = get_terms( $tax, array( |
| 924 | 'hide_empty' => true, |
| 925 | 'fields' => 'id=>name', |
| 926 | 'hierarchical' => false, |
| 927 | ) ); |
| 928 | |
| 929 | $expected = array( |
| 930 | $term_parent2 => 'Parent 2', |
| 931 | $term_child1 => 'Child 1', |
| 932 | ); |
| 933 | |
| 934 | _unregister_taxonomy( 'hierarchical_fields' ); |
| 935 | |
| 936 | $this->assertEqualSets( $expected, $found ); |
| 937 | } |