WordPress.org

Make WordPress Core

Ticket #9547: 9547.4.diff

File 9547.4.diff, 11.3 KB (added by simonwheatley, 6 years ago)

Amended end assertion of test_get_the_terms_with_args

  • src/wp-includes/category-template.php

     
    12381238 *
    12391239 * @since 2.5.0
    12401240 *
     1241 * @see wp_get_object_terms() for reference on the $args param
     1242 *
    12411243 * @param int|object $post Post ID or object.
    12421244 * @param string $taxonomy Taxonomy name.
     1245 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    12431246 * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure.
    12441247 */
    1245 function get_the_terms( $post, $taxonomy ) {
     1248function get_the_terms( $post, $taxonomy, $args = array() ) {
    12461249        if ( ! $post = get_post( $post ) )
    12471250                return false;
    12481251
    1249         $terms = get_object_term_cache( $post->ID, $taxonomy );
     1252        $key = "{$post->ID}:$taxonomy:" . md5( serialize( $args ) );
     1253        $group = "{$taxonomy}_relationships";
     1254        $terms = wp_cache_get( $key, $group );
     1255
    12501256        if ( false === $terms ) {
    1251                 $terms = wp_get_object_terms( $post->ID, $taxonomy );
    1252                 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
     1257                $terms = wp_get_object_terms( $post->ID, $taxonomy, $args );
     1258                wp_cache_add( $key, $terms, $group );
    12531259        }
    12541260
    12551261        /**
     
    12741280 *
    12751281 * @since 2.5.0
    12761282 *
     1283 * @see wp_get_object_terms() for reference on the $args param
     1284 *
    12771285 * @param int $id Post ID.
    12781286 * @param string $taxonomy Taxonomy name.
    12791287 * @param string $before Optional. Before list.
    12801288 * @param string $sep Optional. Separate items using this.
    12811289 * @param string $after Optional. After list.
     1290 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    12821291 * @return string|bool|WP_Error A list of terms on success, false or WP_Error on failure.
    12831292 */
    1284 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
    1285         $terms = get_the_terms( $id, $taxonomy );
     1293function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '', $args = array() ) {
     1294        $terms = get_the_terms( $id, $taxonomy, $args );
    12861295
    12871296        if ( is_wp_error( $terms ) )
    12881297                return $terms;
     
    13221331 * @param string $before Optional. Before list.
    13231332 * @param string $sep Optional. Separate items using this.
    13241333 * @param string $after Optional. After list.
     1334 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    13251335 * @return null|bool False on WordPress error. Returns null when displaying.
    13261336 */
    1327 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
    1328         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
     1337function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '', $args = array() ) {
     1338        $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after, $args );
    13291339
    13301340        if ( is_wp_error( $term_list ) )
    13311341                return false;
  • tests/phpunit/tests/term.php

     
    6969                $this->assertNull( term_exists(rand_str()) );
    7070                $this->assertEquals( 0, term_exists(0) );
    7171                $this->assertEquals( 0, term_exists('') );
    72                 $this->assertEquals( 0, term_exists(NULL) );
     72                $this->assertEquals( 0, term_exists(null) );
    7373        }
    7474
    7575        /**
     
    220220
    221221                // get_the_terms() does prime the cache.
    222222                $terms = get_the_terms( $post_id, $this->taxonomy );
    223                 $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    224                 $this->assertInternalType( 'array', $cache );
     223                $cache1 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array() ) ), $this->taxonomy . '_relationships');
     224                $this->assertInternalType( 'array', $cache1 );
    225225
     226                // get_the_terms() does prime the cache differently when the args parameter has a value.
     227                $terms = get_the_terms( $post_id, $this->taxonomy, array( 'order' => 'DESC' ) );
     228                $cache2 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array( 'order' => 'DESC' ) ) ), $this->taxonomy . '_relationships');
     229                $this->assertInternalType( 'array', $cache2 );
     230
     231                // Verify that the two cache gets returned different results
     232                $this->assertNotEquals( $cache1, $cache2 );
     233
    226234                // Cache should be empty after a set.
    227235                $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    228236                $this->assertEquals( 2, count($tt_2) );
     
    590598                $this->assertCount( 3, $terms2 );
    591599                $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );
    592600        }
     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        }
    593755}