Make WordPress Core

Changeset 52577


Ignore:
Timestamp:
01/14/2022 05:50:52 PM (12 days ago)
Author:
SergeyBiryukov
Message:

Tests: Avoid duplicate queries in some WP_Query tests.

When passing args to the WP_Query::__construct() method, it internally executes the WP_Query::get_posts() method and stores the result in the WP_Query::posts property.

When calling WP_Query::get_posts() again, the same SQL query gets executed, and the result is again stored in the WP_Query::posts property.

Thus, the correct usage is to read the posts already stored in the WP_Query::posts property.

Follow-up to [1062/tests], [1065/tests], [1066/tests], [1070/tests], [29805], [31286], [49900], [51144].

Props david.binda.
Fixes #54822.

Location:
trunk/tests/phpunit/tests/query
Files:
2 edited

Legend:

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

    r51568 r52577  
    9292
    9393        $query = new WP_Query( array( 'post_type' => 'unregistered_cpt' ) );
    94         $posts = $query->get_posts();
    9594
    9695        $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
    9796        $this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
    98         $this->assertCount( 0, $posts );
     97        $this->assertCount( 0, $query->posts );
    9998    }
    10099
     
    112111            )
    113112        );
    114         $posts = $query->get_posts();
    115113
    116114        $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
    117         $this->assertCount( 1, $posts, 'the valid `page` post type should still return one post' );
     115        $this->assertCount( 1, $query->posts, 'the valid `page` post type should still return one post' );
    118116    }
    119117
     
    144142            )
    145143        );
    146         $posts = $query->get_posts();
    147144
    148145        // Only the published page should be returned.
    149         $this->assertCount( 1, $posts );
     146        $this->assertCount( 1, $query->posts );
    150147    }
    151148
     
    159156            )
    160157        );
    161         $posts = $query->get_posts();
    162158
    163159        // Only the published post should be returned.
    164         $this->assertCount( 1, $posts );
     160        $this->assertCount( 1, $query->posts );
    165161    }
    166162}
  • trunk/tests/phpunit/tests/query/taxQuery.php

    r52389 r52577  
    10251025        );
    10261026
    1027         $posts = $query->get_posts();
    1028         $this->assertCount( 0, $posts );
     1027        $this->assertCount( 0, $query->posts );
    10291028    }
    10301029
     
    10601059        );
    10611060
    1062         $posts = $query->get_posts();
    1063         $this->assertCount( 0, $posts );
     1061        $this->assertCount( 0, $query->posts );
    10641062    }
    10651063
Note: See TracChangeset for help on using the changeset viewer.