Make WordPress Core


Ignore:
Timestamp:
02/24/2016 07:12:45 PM (10 years ago)
Author:
boonebgorges
Message:

Improve 'offset' calculation when querying for hierarchical terms.

When querying for terms in hierarchical taxonomies, get_terms() initially
queries for all matching terms, and then trims the located results based on the
$number and $offset arguments passed to the function. See #8832. However,
a flaw in the original logic meant that results were failing to be trimmed
properly in cases where $offset exceeds the total number of matching terms;
in these cases, we should force an empty array.

Props danielbachhuber.
Fixes #35935.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTerms.php

    r36614 r36691  
    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();
Note: See TracChangeset for help on using the changeset viewer.