Make WordPress Core

Ticket #35935: 35935.diff

File 35935.diff, 3.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 3455fb8..ea7db71 100644
    function get_terms( $args = array(), $deprecated = '' ) { 
    17191719                $terms = $_terms;
    17201720        }
    17211721
    1722         if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
    1723                 $terms = array_slice( $terms, $offset, $number, true );
     1722        // Hierarchical queries are not limited, so 'offset' and 'number' must be handled now.
     1723        if ( $number && is_array( $terms ) ) {
     1724                if ( $offset >= count( $terms ) ) {
     1725                        $terms = array();
     1726                } else {
     1727                        $terms = array_slice( $terms, $offset, $number, true );
     1728                }
    17241729        }
    17251730
    17261731        wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
  • tests/phpunit/tests/term/getTerms.php

    diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php
    index 870f6a2..2095434 100644
    class Tests_Term_getTerms extends WP_UnitTestCase { 
    20782078                $this->assertSame( array( $terms[0], $terms[1] ), array_keys( $found ) );
    20792079        }
    20802080
     2081        /**
     2082         * @ticket 35935
     2083         */
     2084        public function test_hierarchical_offset_0_with_number_greater_than_total_available_count() {
     2085                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2086
     2087                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     2088
     2089                $found = get_terms( 'wptests_tax', array(
     2090                        'number'     => 3,
     2091                        'offset'     => 0,
     2092                        'hide_empty' => false,
     2093                        'fields'     => 'ids',
     2094                ) );
     2095                $this->assertEqualSets( $terms, $found );
     2096        }
     2097
     2098        /**
     2099         * @ticket 35935
     2100         */
     2101        public function test_hierarchical_offset_plus_number() {
     2102                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2103
     2104                $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     2105
     2106                $found = get_terms( 'wptests_tax', array(
     2107                        'number'     => 1,
     2108                        'offset'     => 1,
     2109                        'hide_empty' => false,
     2110                        'fields'     => 'ids',
     2111                        'orderby'    => 'term_id',
     2112                        'order'      => 'ASC',
     2113                ) );
     2114                $this->assertEqualSets( array( $terms[1] ), $found );
     2115        }
     2116
     2117        /**
     2118         * @ticket 35935
     2119         */
     2120        public function test_hierarchical_offset_plus_number_exceeds_available_count() {
     2121                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2122
     2123                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     2124
     2125                $found = get_terms( 'wptests_tax', array(
     2126                        'number'     => 2,
     2127                        'offset'     => 1,
     2128                        'hide_empty' => false,
     2129                        'fields'     => 'ids',
     2130                        'orderby'    => 'term_id',
     2131                        'order'      => 'ASC',
     2132                ) );
     2133                $this->assertEqualSets( array( $terms[1] ), $found );
     2134        }
     2135
     2136        /**
     2137         * @ticket 35935
     2138         */
     2139        public function test_hierarchical_offset_exceeds_available_count() {
     2140                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2141
     2142                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     2143
     2144                $found = get_terms( 'wptests_tax', array(
     2145                        'number'     => 100,
     2146                        'offset'     => 3,
     2147                        'hide_empty' => false,
     2148                        'fields'     => 'ids',
     2149                        'orderby'    => 'term_id',
     2150                        'order'      => 'ASC',
     2151                ) );
     2152                $this->assertEqualSets( array(), $found );
     2153        }
     2154
    20812155        protected function create_hierarchical_terms_and_posts() {
    20822156                $terms = array();
    20832157