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