Make WordPress Core

Ticket #9547: 9547-tests.diff

File 9547-tests.diff, 8.4 KB (added by tollmanz, 12 years ago)
  • tests/term.php

     
    6767                $this->assertNull( term_exists(rand_str()) );
    6868                $this->assertEquals( 0, term_exists(0) );
    6969                $this->assertEquals( 0, term_exists('') );
    70                 $this->assertEquals( 0, term_exists(NULL) );
     70                $this->assertEquals( 0, term_exists(null) );
    7171        }
    7272
    7373        /**
     
    196196
    197197                // get_the_terms() does prime the cache.
    198198                $terms = get_the_terms( $post_id, $this->taxonomy );
    199                 $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    200                 $this->assertInternalType( 'array', $cache );
     199                $cache1 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array() ) ), $this->taxonomy . '_relationships');
     200                $this->assertInternalType( 'array', $cache1 );
    201201
     202                // get_the_terms() does prime the cache differently when the args parameter has a value.
     203                $terms = get_the_terms( $post_id, $this->taxonomy, array( 'order' => 'DESC' ) );
     204                $cache2 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array( 'order' => 'DESC' ) ) ), $this->taxonomy . '_relationships');
     205                $this->assertInternalType( 'array', $cache2 );
     206
     207                // Verify that the two cache gets returned different results
     208                $this->assertNotEquals( $cache1, $cache2 );
     209
    202210                // Cache should be empty after a set.
    203211                $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    204212                $this->assertEquals( 2, count($tt_2) );
     
    457465                $this->assertEquals( $tag_id, $terms[0]->term_id );
    458466                $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
    459467        }
     468
     469        function test_get_the_terms_by_order() {
     470                // Set up a sortable taxonomy. The only way to add a value to the term_order column is via a sortable tax.
     471                register_taxonomy( 'sortable-taxonomy', 'post', array(
     472                        'sort' => true,
     473                ) );
     474
     475                $post_id = $this->factory->post->create();
     476
     477                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     478                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'M Term' ) );
     479                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'A Term' ) );
     480                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'Z Term' ) );
     481
     482                // Attach the terms to the post
     483                $tt_ids = wp_set_object_terms(
     484                        $post_id,
     485                        array( $term1_id, $term2_id, $term3_id ),
     486                        'sortable-taxonomy'
     487                );
     488
     489                // Verify get by term order
     490                $post_terms_by_term_order = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'term_order' ) );
     491                $this->assertEquals( array( $term1_id, $term2_id, $term3_id ), wp_list_pluck( $post_terms_by_term_order, 'term_id' ) );
     492
     493                // Verify get by term slug
     494                $post_terms_by_term_slug = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'slug' ) );
     495                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_slug, 'term_id' ) );
     496
     497                // Verify get by term name
     498                $post_terms_by_term_name = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'name' ) );
     499                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_name, 'term_id' ) );
     500        }
     501
     502        /**
     503         * @ticket 9547
     504         */
     505        function test_get_the_terms_with_args() {
     506                $post_id = $this->factory->post->create();
     507
     508                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     509                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     510                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     511                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     512
     513                // Attach the terms to the post
     514                $tt_ids = wp_set_object_terms(
     515                        $post_id,
     516                        array( $term1_id, $term2_id, $term3_id ),
     517                        'post_tag'
     518                );
     519
     520                // Make sure that the terms added are available when the args parameter is used
     521                $terms = get_the_terms( $post_id, 'post_tag', array( 'orderby' => 'name' ) );
     522                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $terms, 'term_id' ) );
     523
     524                /**
     525                 * Verify that using the args parameter produces two different results when the args parameter is different. This
     526                 * suggests that the cache key is working correctly by taking the args into account.
     527                 */
     528                $orderby_asc  = get_the_terms( $post_id, 'post_tag', array( 'order' => 'ASC' ) );
     529                $orderby_desc = get_the_terms( $post_id, 'post_tag', array( 'order' => 'DESC' ) );
     530                $this->assertFalse( in_array( $orderby_asc, $orderby_desc ) );
     531        }
     532
     533        /**
     534         * @ticket 9547
     535         */
     536        function test_get_the_term_list_with_args() {
     537                $post_id = $this->factory->post->create();
     538
     539                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     540                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     541                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     542                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     543
     544                // Attach the terms to the post
     545                $tt_ids = wp_set_object_terms(
     546                        $post_id,
     547                        array( $term1_id, $term2_id, $term3_id ),
     548                        'post_tag'
     549                );
     550
     551                // Verify that the term list returns its expected result
     552                $links = array();
     553                foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) {
     554                        $term      = get_term( $term_id, 'post_tag' );
     555                        $term_link = get_term_link( $term_id, 'post_tag' );
     556                        $links[]   = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     557                }
     558                $expected = join( '', $links );
     559
     560                $this->assertEquals( $expected, get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC', 'orderby' => 'name' ) ) );
     561
     562                /**
     563                 * Verify that using the args parameter produces two different results when the args parameter is different. This
     564                 * suggests that the cache key is working correctly by taking the args into account.
     565                 */
     566                $orderby_asc  = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC' ) );
     567                $orderby_desc = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'DESC' ) );
     568                $this->assertNotEquals( $orderby_asc, $orderby_desc );
     569        }
     570
     571        /**
     572         * @ticket 9547
     573         */
     574        function test_the_terms_with_args() {
     575                $post_id = $this->factory->post->create();
     576
     577                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     578                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     579                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     580                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     581
     582                // Attach the terms to the post
     583                $tt_ids = wp_set_object_terms(
     584                        $post_id,
     585                        array( $term1_id, $term2_id, $term3_id ),
     586                        'post_tag'
     587                );
     588
     589                // Prepare a string that represents the output of the_terms when setting orderby to name and order to ASC
     590                $links = array();
     591                foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) {
     592                        $term      = get_term( $term_id, 'post_tag' );
     593                        $term_link = get_term_link( $term_id, 'post_tag' );
     594                        $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     595                }
     596                $expected1 = join( ', ', $links );
     597
     598                /**
     599                 * Prepare a string that represents the output of the_terms when setting order to DESC; It is important to test
     600                 * two variants to ensure that the cache keys are working properly.
     601                 */
     602                $links = array();
     603                foreach ( array( $term3_id, $term1_id, $term2_id ) as $term_id ) {
     604                        $term      = get_term( $term_id, 'post_tag' );
     605                        $term_link = get_term_link( $term_id, 'post_tag' );
     606                        $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     607                }
     608                $expected2 = join( ', ', $links );
     609
     610                /**
     611                 * At the end of the test, the_terms will run twice producing output. The output should be the concatenation of
     612                 * the first and second test string.
     613                 */
     614                $this->expectOutputString( $expected1 . $expected2 );
     615
     616                the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'ASC', 'orderby' => 'name' ) );
     617                the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'DESC' ) );
     618        }
    460619}