Make WordPress Core


Ignore:
Timestamp:
06/14/2022 12:40:29 PM (2 years ago)
Author:
spacedmonkey
Message:

REST API: Avoid duplicated query in post collections.

Avoid duplicated query when retrieving empty posts collections by adding a check if the page is more than 1.

Props furi3r, gdetassigny, TimothyBlynJacobs, spacedmonkey.
Fixes #55677.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r52389 r53498  
    3030    private $test_file2;
    3131
     32    /**
     33     * @var array The recorded posts query clauses.
     34     */
     35    protected $posts_clauses;
     36
    3237    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    3338        self::$superadmin_id  = $factory->user->create(
     
    8691        $this->test_file2 = get_temp_dir() . 'codeispoetry.png';
    8792        copy( $orig_file2, $this->test_file2 );
     93
     94        add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     95        add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
     96    }
     97
     98    public function wpSetUpBeforeRequest( $result ) {
     99        $this->posts_clauses = array();
     100        return $result;
     101    }
     102
     103    public function save_posts_clauses( $clauses ) {
     104        $this->posts_clauses[] = $clauses;
     105        return $clauses;
    88106    }
    89107
     
    618636        $this->assertCount( 1, $data );
    619637        $this->assertSame( $id2, $data[0]['id'] );
     638    }
     639
     640    /**
     641     * @ticket 55677
     642     */
     643    public function test_get_items_avoid_duplicated_count_query_if_no_items() {
     644        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     645        $request->set_param( 'media_type', 'video' );
     646
     647        $response = rest_get_server()->dispatch( $request );
     648
     649        $this->assertCount( 1, $this->posts_clauses );
     650
     651        $headers = $response->get_headers();
     652
     653        $this->assertSame( 0, $headers['X-WP-Total'] );
     654        $this->assertSame( 0, $headers['X-WP-TotalPages'] );
     655    }
     656
     657    /**
     658     * @ticket 55677
     659     */
     660    public function test_get_items_with_empty_page_runs_count_query_after() {
     661        $this->factory->attachment->create_object(
     662            $this->test_file,
     663            0,
     664            array(
     665                'post_date'      => '2022-06-12T00:00:00Z',
     666                'post_mime_type' => 'image/jpeg',
     667                'post_excerpt'   => 'A sample caption',
     668            )
     669        );
     670
     671        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     672        $request->set_param( 'media_type', 'image' );
     673        $request->set_param( 'page', 2 );
     674
     675        $response = rest_get_server()->dispatch( $request );
     676
     677        $this->assertCount( 2, $this->posts_clauses );
     678
     679        $this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 );
    620680    }
    621681
Note: See TracChangeset for help on using the changeset viewer.