Make WordPress Core


Ignore:
Timestamp:
04/30/2018 09:07:16 PM (9 years ago)
Author:
boonebgorges
Message:

Taxonomy: Ensure that invalid term objects are discarded in WP_Term_Query.

The get_term() mapping may result in term objects that are null or
WP_Error when plugins use get_term or a related filter. Since null
and error objects are not valid results for a term query, we discard
them.

Props GM_Alex.
See #42691.

File:
1 edited

Legend:

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

    r42343 r43049  
    615615                $this->assertSame( array(), $q->terms );
    616616        }
     617
     618        /**
     619         * @ticket 42691
     620         */
     621        public function test_null_term_object_should_be_discarded() {
     622                register_taxonomy( 'wptests_tax', 'post' );
     623
     624                $terms = self::factory()->term->create_many( 3, array(
     625                        'taxonomy' => 'wptests_tax',
     626                ) );
     627
     628                $this->term_id = $terms[1];
     629
     630                add_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     631                $found = get_terms( array(
     632                        'taxonomy'   => 'wptests_tax',
     633                        'hide_empty' => false,
     634                ) );
     635                remove_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     636
     637                $expected = array( $terms[0], $terms[2] );
     638
     639                $this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
     640        }
     641
     642        public function filter_term_to_null( $term ) {
     643                if ( $this->term_id === $term->term_id ) {
     644                        return null;
     645                }
     646
     647                return $term;
     648        }
     649
     650        /**
     651         * @ticket 42691
     652         */
     653        public function test_error_term_object_should_be_discarded() {
     654                register_taxonomy( 'wptests_tax', 'post' );
     655
     656                $terms = self::factory()->term->create_many( 3, array(
     657                        'taxonomy' => 'wptests_tax',
     658                ) );
     659
     660                $this->term_id = $terms[1];
     661
     662                add_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
     663                $found = get_terms( array(
     664                        'taxonomy'   => 'wptests_tax',
     665                        'hide_empty' => false,
     666                ) );
     667                remove_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
     668
     669                $expected = array( $terms[0], $terms[2] );
     670
     671                $this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
     672        }
     673
     674        public function filter_term_to_wp_error( $term ) {
     675                if ( $this->term_id === $term->term_id ) {
     676                        return new WP_Error( 'foo' );
     677                }
     678
     679                return $term;
     680        }
    617681}
Note: See TracChangeset for help on using the changeset viewer.