| | 601 | |
| | 602 | /** |
| | 603 | * @ticket 9547 |
| | 604 | */ |
| | 605 | function test_get_the_terms_by_order() { |
| | 606 | // Set up a sortable taxonomy. The only way to add a value to the term_order column is via a sortable tax. |
| | 607 | register_taxonomy( 'sortable-taxonomy', 'post', array( |
| | 608 | 'sort' => true, |
| | 609 | ) ); |
| | 610 | |
| | 611 | $post_id = $this->factory->post->create(); |
| | 612 | |
| | 613 | // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering |
| | 614 | $term1_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'M Term' ) ); |
| | 615 | $term2_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'A Term' ) ); |
| | 616 | $term3_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'Z Term' ) ); |
| | 617 | |
| | 618 | // Attach the terms to the post |
| | 619 | $tt_ids = wp_set_object_terms( |
| | 620 | $post_id, |
| | 621 | array( $term1_id, $term2_id, $term3_id ), |
| | 622 | 'sortable-taxonomy' |
| | 623 | ); |
| | 624 | |
| | 625 | // Verify order by term order |
| | 626 | $post_terms_by_term_order = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'term_order' ) ); |
| | 627 | $this->assertEquals( array( $term1_id, $term2_id, $term3_id ), wp_list_pluck( $post_terms_by_term_order, 'term_id' ) ); |
| | 628 | |
| | 629 | // Verify order by term slug |
| | 630 | $post_terms_by_term_slug = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'slug' ) ); |
| | 631 | $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_slug, 'term_id' ) ); |
| | 632 | |
| | 633 | // Verify order by term name |
| | 634 | $post_terms_by_term_name = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'name' ) ); |
| | 635 | $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_name, 'term_id' ) ); |
| | 636 | } |
| | 637 | |
| | 638 | /** |
| | 639 | * @ticket 9547 |
| | 640 | */ |
| | 641 | function test_get_the_terms_with_args() { |
| | 642 | $post_id = $this->factory->post->create(); |
| | 643 | |
| | 644 | // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering |
| | 645 | $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) ); |
| | 646 | $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) ); |
| | 647 | $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) ); |
| | 648 | |
| | 649 | // Attach the terms to the post |
| | 650 | $tt_ids = wp_set_object_terms( |
| | 651 | $post_id, |
| | 652 | array( $term1_id, $term2_id, $term3_id ), |
| | 653 | 'post_tag' |
| | 654 | ); |
| | 655 | |
| | 656 | // Make sure that the terms added are available when the args parameter is used |
| | 657 | $terms = get_the_terms( $post_id, 'post_tag', array( 'orderby' => 'name' ) ); |
| | 658 | $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $terms, 'term_id' ) ); |
| | 659 | |
| | 660 | /** |
| | 661 | * Verify that using the args parameter produces two different results when the args parameter is different. This |
| | 662 | * suggests that the cache key is working correctly by taking the args into account. |
| | 663 | */ |
| | 664 | $orderby_asc = get_the_terms( $post_id, 'post_tag', array( 'order' => 'ASC' ) ); |
| | 665 | $orderby_desc = get_the_terms( $post_id, 'post_tag', array( 'order' => 'DESC' ) ); |
| | 666 | $this->assertNotEquals( $orderby_asc, $orderby_desc ); |
| | 667 | } |
| | 668 | |
| | 669 | /** |
| | 670 | * @ticket 9547 |
| | 671 | */ |
| | 672 | function test_get_the_term_list_with_args() { |
| | 673 | $post_id = $this->factory->post->create(); |
| | 674 | |
| | 675 | // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering |
| | 676 | $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) ); |
| | 677 | $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) ); |
| | 678 | $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) ); |
| | 679 | |
| | 680 | // Attach the terms to the post |
| | 681 | $tt_ids = wp_set_object_terms( |
| | 682 | $post_id, |
| | 683 | array( $term1_id, $term2_id, $term3_id ), |
| | 684 | 'post_tag' |
| | 685 | ); |
| | 686 | |
| | 687 | // Verify that the term list returns its expected result |
| | 688 | $links = array(); |
| | 689 | foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) { |
| | 690 | $term = get_term( $term_id, 'post_tag' ); |
| | 691 | $term_link = get_term_link( $term_id, 'post_tag' ); |
| | 692 | $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>'; |
| | 693 | } |
| | 694 | $expected = join( '', $links ); |
| | 695 | |
| | 696 | $this->assertEquals( $expected, get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC', 'orderby' => 'name' ) ) ); |
| | 697 | |
| | 698 | /** |
| | 699 | * Verify that using the args parameter produces two different results when the args parameter is different. This |
| | 700 | * suggests that the cache key is working correctly by taking the args into account. |
| | 701 | */ |
| | 702 | $orderby_asc = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC' ) ); |
| | 703 | $orderby_desc = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'DESC' ) ); |
| | 704 | $this->assertNotEquals( $orderby_asc, $orderby_desc ); |
| | 705 | } |
| | 706 | |
| | 707 | /** |
| | 708 | * @ticket 9547 |
| | 709 | */ |
| | 710 | function test_the_terms_with_args() { |
| | 711 | $post_id = $this->factory->post->create(); |
| | 712 | |
| | 713 | // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering |
| | 714 | $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) ); |
| | 715 | $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) ); |
| | 716 | $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) ); |
| | 717 | |
| | 718 | // Attach the terms to the post |
| | 719 | $tt_ids = wp_set_object_terms( |
| | 720 | $post_id, |
| | 721 | array( $term1_id, $term2_id, $term3_id ), |
| | 722 | 'post_tag' |
| | 723 | ); |
| | 724 | |
| | 725 | // Prepare a string that represents the output of the_terms when setting orderby to name and order to ASC |
| | 726 | $links = array(); |
| | 727 | foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) { |
| | 728 | $term = get_term( $term_id, 'post_tag' ); |
| | 729 | $term_link = get_term_link( $term_id, 'post_tag' ); |
| | 730 | $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>'; |
| | 731 | } |
| | 732 | $expected1 = join( ', ', $links ); |
| | 733 | |
| | 734 | /** |
| | 735 | * Prepare a string that represents the output of the_terms when setting order to DESC; It is important to test |
| | 736 | * two variants to ensure that the cache keys are working properly. |
| | 737 | */ |
| | 738 | $links = array(); |
| | 739 | foreach ( array( $term3_id, $term1_id, $term2_id ) as $term_id ) { |
| | 740 | $term = get_term( $term_id, 'post_tag' ); |
| | 741 | $term_link = get_term_link( $term_id, 'post_tag' ); |
| | 742 | $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>'; |
| | 743 | } |
| | 744 | $expected2 = join( ', ', $links ); |
| | 745 | |
| | 746 | /** |
| | 747 | * At the end of the test, the_terms will run twice producing output. The output should be the concatenation of |
| | 748 | * the first and second test string. |
| | 749 | */ |
| | 750 | $this->expectOutputString( $expected1 . $expected2 ); |
| | 751 | |
| | 752 | the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'ASC', 'orderby' => 'name' ) ); |
| | 753 | the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'DESC' ) ); |
| | 754 | } |