Make WordPress Core

Ticket #35476: 35476.3.diff

File 35476.3.diff, 3.2 KB (added by welcher, 10 years ago)

Adding some unit tests

  • src/wp-includes/query.php

     
    35313531                }
    35323532
    35333533                if ( 'ids' == $q['fields'] ) {
     3534
     3535                        /**
     3536                         * Filter the query results when 'fields' specified in a query.
     3537                         *
     3538                         * Plugins can bypass a 'fields' query by returning
     3539                         * an array of posts to be returned from the query.
     3540                         *
     3541                         * @since 4.5.0
     3542                         *
     3543                         * @param array    $posts   The filtered array of posts.
     3544                         * @param array    $request The complete SQL query.
     3545                         * @param string   $fields  The query 'fields' value.
     3546                         * @param WP_Query &$this   The WP_Query instance (passed by reference).
     3547                         */
     3548                        $this->posts = apply_filters_ref_array( 'fields_the_posts', array( false, $q['fields'], $this->request, &$this ) );
     3549
     3550                        if ( false !== $this->posts ) {
     3551                                return $this->posts;
     3552                        }
     3553
    35343554                        $this->posts = $wpdb->get_col( $this->request );
    35353555                        $this->posts = array_map( 'intval', $this->posts );
    35363556                        $this->post_count = count( $this->posts );
     
    35403560                }
    35413561
    35423562                if ( 'id=>parent' == $q['fields'] ) {
     3563
     3564                        /** This filter is documented in wp-includes/query.php */
     3565                        $this->posts = apply_filters_ref_array( 'fields_the_posts', array( false, $q['fields'], $this->request, &$this ) );
     3566
     3567                        if ( false !== $this->posts ) {
     3568                                return $this->posts;
     3569                        }
     3570
    35433571                        $this->posts = $wpdb->get_results( $this->request );
    35443572                        $this->post_count = count( $this->posts );
    35453573                        $this->set_found_posts( $q, $limits );
  • tests/phpunit/tests/query.php

     
    492492
    493493                $this->assertContains( 'LIMIT 5, 5', $q->request );
    494494        }
     495
     496
     497        /**
     498         * @ticket 35476
     499         *
     500         * @dataProvider data_filter_query_results_with_fields
     501         */
     502        public function test_filter_query_results_with_fields( $query_params ) {
     503
     504                // The array we're expecting back from the filter.
     505                $filtered_post_ids_array = array( 1001, 1002, 1003 );
     506
     507
     508                // Create some posts.
     509                $this->factory()->post->create_many( 10 );
     510
     511                add_filter( 'fields_the_posts', array( $this, 'filter_fields_the_posts' ) );
     512
     513                $query = new WP_Query( $query_params );
     514
     515                remove_filter( 'fields_the_posts', array( $this, 'filter_fields_the_posts' ) );
     516
     517
     518                $this->assertSame( $filtered_post_ids_array, $query->posts );
     519        }
     520
     521
     522        /**
     523         * Data provider for test_filter_query_results_with_fields().
     524         *
     525         * Passes the three available options for the $notify parameter and the expected email
     526         * emails sent status as a bool.
     527         *
     528         * @return array {
     529         *     @type array {
     530         *         @type array $query_params The arguments that will be passed to WP_Query
     531         *
     532         *     }
     533         * }
     534         */
     535        function data_filter_query_results_with_fields() {
     536                return array(
     537                        array(
     538                                array(
     539                                        'fields' => 'ids',
     540                                ),
     541                        ),
     542                        array(
     543                                array(
     544                                        'fields' => 'id=>parent',
     545                                ),
     546                        ),
     547                );
     548        }
     549
     550
     551        /**
     552         * Filter callback for test_filter_query_results_with_fields.
     553         *
     554         * @return array
     555         */
     556        public function filter_fields_the_posts() {
     557                return array( 1001, 1002, 1003 );
     558        }
    495559}