WordPress.org

Make WordPress Core

Ticket #47591: terms-pre-query-tests.diff

File terms-pre-query-tests.diff, 1.0 KB (added by tlovett1, 10 months ago)

Unit test

  • tests/phpunit/tests/term/query.php

     
    737737
    738738                return $term;
    739739        }
     740
     741        /**
     742         * @ticket 47591
     743         */
     744        public function test_terms_pre_query_filter_should_bypass_database_query() {
     745                global $wpdb;
     746
     747                add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 3 );
     748
     749                $num_queries = $wpdb->num_queries;
     750                $q           = new WP_Term_Query(
     751                        array(
     752                                'fields' => 'ID',
     753                        )
     754                );
     755
     756                remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 3 );
     757
     758                // Make sure no queries were executed.
     759                $this->assertSame( $num_queries, $wpdb->num_queries );
     760
     761                // We manually inserted a non-existing term and overrode the results with it.
     762                $this->assertSame( array( 555 ), $q->results );
     763        }
     764
     765        public static function filter_terms_pre_query( $terms, $query, $args ) {
     766                return array( 555 );
     767        }
    740768}