Skip to content

Commit 1eff3fb

Browse files
committed
Tests: Add tests for malformed post type query vars handling
1 parent 1641032 commit 1eff3fb

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

tests/phpunit/tests/query/parseQuery.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,68 @@ public function test_parse_query_attachment_id_nonscalar() {
234234
$this->assertEmpty( $q->query_vars['attachment_id'] );
235235
}
236236

237+
/**
238+
* Ensure non-scalar 'attachment' value is rejected and attachment flags are not set.
239+
*
240+
* @ticket 65123
241+
*/
242+
public function test_parse_query_attachment_nonscalar() {
243+
$q = new WP_Query();
244+
$q->parse_query(
245+
array(
246+
'attachment' => array( 'foo' => 'bar' ),
247+
)
248+
);
249+
250+
$this->assertEmpty( $q->query_vars['attachment'] );
251+
$this->assertFalse( $q->is_attachment );
252+
$this->assertFalse( $q->is_single );
253+
}
254+
255+
/**
256+
* Ensure a string 'attachment' value sets is_attachment and is_single flags.
257+
*
258+
* @ticket 65123
259+
*/
260+
public function test_parse_query_attachment_scalar() {
261+
$q = new WP_Query();
262+
$q->parse_query(
263+
array(
264+
'attachment' => 'my-image',
265+
)
266+
);
267+
268+
$this->assertSame( 'my-image', $q->query_vars['attachment'] );
269+
$this->assertTrue( $q->is_attachment );
270+
$this->assertTrue( $q->is_single );
271+
}
272+
273+
/**
274+
* Ensure non-scalar post type query var does not cause a fatal error.
275+
*
276+
* @ticket 65123
277+
*/
278+
public function test_parse_query_post_type_query_var_array() {
279+
register_post_type(
280+
'wptests_cpt',
281+
array(
282+
'public' => true,
283+
'query_var' => 'wptests_cpt',
284+
)
285+
);
286+
287+
$q = new WP_Query(
288+
array(
289+
'post_type' => 'wptests_cpt',
290+
'wptests_cpt' => array( 'foo' => 'bar' ),
291+
)
292+
);
293+
294+
unregister_post_type( 'wptests_cpt' );
295+
296+
$this->assertIsArray( $q->posts );
297+
}
298+
237299
/**
238300
* Tests that a fatal error is not thrown when a hierarchical taxonomy query var
239301
* passed to wp_basename() in ::parse_tax_query() is an array instead of a string.

0 commit comments

Comments
 (0)