WordPress.org

Make WordPress Core

Opened 4 days ago

Closed 4 days ago

#54822 closed defect (bug) (fixed)

Incorrectly accessing posts fetched by WP_Query in query PHPUnit tests

Reported by: david.binda Owned by: SergeyBiryukov
Milestone: 6.0 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords:
Focuses: Cc:

Description

Similarly to #53280, the usage of WP_Query::get_posts() method after the WP_Query object was initialised with arguments passed to it is incorrect in tests/phpunit/tests/query/invalidQueries.php and tests/phpunit/tests/query/taxQuery.php PHPUnit tests, as it triggers just one more SQL query.

For instance: https://core.trac.wordpress.org/browser/trunk/tests/phpunit/tests/query/invalidQueries.php?rev=51568#L94 or https://core.trac.wordpress.org/browser/trunk/tests/phpunit/tests/query/taxQuery.php?rev=52389#L1027

Instead, the posts should be read via WP_Query::posts property.

Attachments (1)

54822.diff (2.2 KB) - added by david.binda 4 days ago.

Download all attachments as: .zip

Change History (3)

@david.binda
4 days ago

#1 @SergeyBiryukov
4 days ago

  • Milestone changed from Awaiting Review to 6.0

#2 @SergeyBiryukov
4 days ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 52577:

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.

Note: See TracTickets for help on using tickets.