Skip to content

Commit d4efa6d

Browse files
committed
Tests: Cover home query flags during REST requests.
Verify `WP_Query::parse_query()` suppresses `is_home` for the main query during REST requests while allowing secondary queries to retain it. Isolate the tests in separate processes to define `REST_REQUEST`. Developed in: #14797 Props irozum. See #65819, #50213. git-svn-id: https://develop.svn.wordpress.org/trunk@65108 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d1bd61e commit d4efa6d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

‎tests/phpunit/tests/query/parseQuery.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,49 @@ public function test_parse_query_hierarchical_taxonomy_query_var_array() {
242242

243243
$this->assertIsArray( $q->posts );
244244
}
245+
246+
/**
247+
* Tests that a secondary query can be `is_home` while serving a REST request.
248+
*
249+
* Runs in a separate process because constants cannot be redefined.
250+
*
251+
* @ticket 50213
252+
*
253+
* @covers WP_Query::parse_query
254+
*
255+
* @runInSeparateProcess
256+
* @preserveGlobalState disabled
257+
*/
258+
public function test_secondary_query_during_rest_request_can_be_home() {
259+
define( 'REST_REQUEST', true );
260+
261+
$query = new WP_Query();
262+
$query->parse_query( array() );
263+
264+
$this->assertFalse( $query->is_main_query(), 'Precondition: the query must not be the main query.' );
265+
$this->assertTrue( $query->is_home );
266+
}
267+
268+
/**
269+
* Tests that the main query is not `is_home` while serving a REST request.
270+
*
271+
* Runs in a separate process because constants cannot be redefined.
272+
*
273+
* @ticket 50213
274+
*
275+
* @covers WP_Query::parse_query
276+
*
277+
* @runInSeparateProcess
278+
* @preserveGlobalState disabled
279+
*/
280+
public function test_main_query_during_rest_request_is_not_home() {
281+
define( 'REST_REQUEST', true );
282+
283+
$query = new WP_Query();
284+
$GLOBALS['wp_the_query'] = $query;
285+
$query->parse_query( array() );
286+
287+
$this->assertTrue( $query->is_main_query(), 'Precondition: the query must be the main query.' );
288+
$this->assertFalse( $query->is_home );
289+
}
245290
}

0 commit comments

Comments
 (0)