Skip to content

Commit bcf08b1

Browse files
committed
Tests: Expand coverage for wp_list_filter().
Cover lists of arrays, mixed arrays and objects, null-value matching, and case-insensitive operators. Verify the default `AND` operator, key preservation, and empty and invalid inputs. Developed in: #13718 Props mindctrl, huzaifaalmesbah. See #53987. git-svn-id: https://develop.svn.wordpress.org/trunk@65112 602fd350-edb4-49c9-b593-d223f7449a82
1 parent aca248c commit bcf08b1

1 file changed

Lines changed: 279 additions & 9 deletions

File tree

‎tests/phpunit/tests/functions/wpListFilter.php‎

Lines changed: 279 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
class Tests_Functions_wpListFilter extends WP_UnitTestCase {
1111

1212
/**
13+
* @ticket 53987
14+
*
1315
* @dataProvider data_wp_list_filter
1416
*
1517
* @param array $input_list An array of objects to filter.
@@ -22,32 +24,58 @@ public function test_wp_list_filter( $input_list, $args, $operator, $expected )
2224
$this->assertEqualSetsWithIndex( $expected, wp_list_filter( $input_list, $args, $operator ) );
2325
}
2426

27+
/**
28+
* Tests that the 'AND' operator is used when no operator is passed.
29+
*
30+
* @ticket 53987
31+
*/
32+
public function test_wp_list_filter_should_default_to_and_operator() {
33+
$input_list = array(
34+
(object) array(
35+
'foo' => 'bar',
36+
'bar' => 'baz',
37+
),
38+
(object) array( 'foo' => 'bar' ),
39+
);
40+
41+
$this->assertEqualSetsWithIndex(
42+
array( 0 => $input_list[0] ),
43+
wp_list_filter(
44+
$input_list,
45+
array(
46+
'foo' => 'bar',
47+
'bar' => 'baz',
48+
)
49+
)
50+
);
51+
}
52+
2553
/**
2654
* Data provider.
2755
*
2856
* @return array[]
2957
*/
3058
public static function data_wp_list_filter() {
3159
return array(
32-
'string instead of array' => array(
60+
'string instead of array' => array(
3361
'foo',
3462
array(),
3563
'AND',
3664
array(),
3765
),
38-
'object instead of array' => array(
66+
'object instead of array' => array(
3967
(object) array( 'foo' ),
4068
array(),
4169
'AND',
4270
array(),
4371
),
44-
'empty args' => array(
72+
'empty args' => array(
4573
array( 'foo', 'bar' ),
4674
array(),
4775
'AND',
4876
array( 'foo', 'bar' ),
4977
),
50-
'invalid operator' => array(
78+
'invalid operator' => array(
5179
array(
5280
(object) array( 'foo' => 'bar' ),
5381
(object) array( 'foo' => 'baz' ),
@@ -56,7 +84,7 @@ public static function data_wp_list_filter() {
5684
'XOR',
5785
array(),
5886
),
59-
'single argument to match' => array(
87+
'single argument to match' => array(
6088
array(
6189
(object) array(
6290
'foo' => 'bar',
@@ -94,7 +122,7 @@ public static function data_wp_list_filter() {
94122
),
95123
),
96124
),
97-
'all must match' => array(
125+
'all must match' => array(
98126
array(
99127
(object) array(
100128
'foo' => 'bar',
@@ -132,7 +160,7 @@ public static function data_wp_list_filter() {
132160
),
133161
),
134162
),
135-
'any must match' => array(
163+
'any must match' => array(
136164
array(
137165
(object) array(
138166
'foo' => 'bar',
@@ -179,7 +207,7 @@ public static function data_wp_list_filter() {
179207
),
180208
),
181209
),
182-
'none must match' => array(
210+
'none must match' => array(
183211
array(
184212
(object) array(
185213
'foo' => 'bar',
@@ -216,7 +244,7 @@ public static function data_wp_list_filter() {
216244
),
217245
),
218246
),
219-
'string to int comparison' => array(
247+
'string to int comparison' => array(
220248
array(
221249
(object) array(
222250
'foo' => '1',
@@ -230,6 +258,248 @@ public static function data_wp_list_filter() {
230258
),
231259
),
232260
),
261+
'bool to string comparison' => array(
262+
array(
263+
(object) array(
264+
'foo' => true,
265+
),
266+
),
267+
array( 'foo' => 'yes' ),
268+
'AND',
269+
array(
270+
0 => (object) array(
271+
'foo' => true,
272+
),
273+
),
274+
),
275+
'null instead of array' => array(
276+
null,
277+
array( 'foo' => 'bar' ),
278+
'AND',
279+
array(),
280+
),
281+
'empty list' => array(
282+
array(),
283+
array( 'foo' => 'bar' ),
284+
'AND',
285+
array(),
286+
),
287+
288+
/*
289+
* Empty arguments are checked before the operator is validated,
290+
* so the original list is returned even for an invalid operator.
291+
*/
292+
'empty args, invalid operator' => array(
293+
array( 'foo', 'bar' ),
294+
array(),
295+
'XOR',
296+
array( 'foo', 'bar' ),
297+
),
298+
299+
// The operator is uppercased before it is validated.
300+
'lowercase AND operator' => array(
301+
array(
302+
(object) array( 'foo' => 'bar' ),
303+
(object) array( 'foo' => 'baz' ),
304+
),
305+
array( 'foo' => 'bar' ),
306+
'and',
307+
array(
308+
0 => (object) array( 'foo' => 'bar' ),
309+
),
310+
),
311+
'mixed case OR operator' => array(
312+
array(
313+
(object) array( 'foo' => 'bar' ),
314+
(object) array( 'foo' => 'baz' ),
315+
),
316+
array( 'foo' => 'bar' ),
317+
'oR',
318+
array(
319+
0 => (object) array( 'foo' => 'bar' ),
320+
),
321+
),
322+
'lowercase NOT operator' => array(
323+
array(
324+
(object) array( 'foo' => 'bar' ),
325+
(object) array( 'foo' => 'baz' ),
326+
),
327+
array( 'foo' => 'bar' ),
328+
'not',
329+
array(
330+
1 => (object) array( 'foo' => 'baz' ),
331+
),
332+
),
333+
334+
// Lists of arrays are supported alongside lists of objects.
335+
'arrays: single argument to match' => array(
336+
array(
337+
array(
338+
'foo' => 'bar',
339+
'key' => 'foo',
340+
),
341+
array(
342+
'foo' => 'baz',
343+
'key' => 'value',
344+
),
345+
array(
346+
'foo' => 'bar',
347+
'key' => 'value',
348+
),
349+
),
350+
array( 'foo' => 'bar' ),
351+
'AND',
352+
array(
353+
0 => array(
354+
'foo' => 'bar',
355+
'key' => 'foo',
356+
),
357+
2 => array(
358+
'foo' => 'bar',
359+
'key' => 'value',
360+
),
361+
),
362+
),
363+
'arrays: all must match' => array(
364+
array(
365+
array(
366+
'foo' => 'bar',
367+
'bar' => 'baz',
368+
),
369+
array( 'foo' => 'bar' ),
370+
),
371+
array(
372+
'foo' => 'bar',
373+
'bar' => 'baz',
374+
),
375+
'AND',
376+
array(
377+
0 => array(
378+
'foo' => 'bar',
379+
'bar' => 'baz',
380+
),
381+
),
382+
),
383+
'arrays: any must match' => array(
384+
array(
385+
array( 'foo' => 'bar' ),
386+
array( 'bar' => 'baz' ),
387+
array( 'foo' => 'baz' ),
388+
),
389+
array(
390+
'foo' => 'bar',
391+
'bar' => 'baz',
392+
),
393+
'OR',
394+
array(
395+
0 => array( 'foo' => 'bar' ),
396+
1 => array( 'bar' => 'baz' ),
397+
),
398+
),
399+
'arrays: none must match' => array(
400+
array(
401+
array( 'foo' => 'bar' ),
402+
array( 'bar' => 'baz' ),
403+
array( 'foo' => 'baz' ),
404+
),
405+
array(
406+
'foo' => 'bar',
407+
'bar' => 'baz',
408+
),
409+
'NOT',
410+
array(
411+
2 => array( 'foo' => 'baz' ),
412+
),
413+
),
414+
'mixed arrays and objects' => array(
415+
array(
416+
array( 'foo' => 'bar' ),
417+
(object) array( 'foo' => 'bar' ),
418+
array( 'foo' => 'baz' ),
419+
(object) array( 'foo' => 'baz' ),
420+
),
421+
array( 'foo' => 'bar' ),
422+
'AND',
423+
array(
424+
0 => array( 'foo' => 'bar' ),
425+
1 => (object) array( 'foo' => 'bar' ),
426+
),
427+
),
428+
429+
/*
430+
* Values that are neither arrays nor objects can never match,
431+
* so they are only returned by the 'NOT' operator.
432+
*/
433+
'scalar values with AND' => array(
434+
array( 'a string', 42, null, array( 'foo' => 'bar' ) ),
435+
array( 'foo' => 'bar' ),
436+
'AND',
437+
array(
438+
3 => array( 'foo' => 'bar' ),
439+
),
440+
),
441+
'scalar values with NOT' => array(
442+
array( 'a string', 42, null, array( 'foo' => 'bar' ) ),
443+
array( 'foo' => 'bar' ),
444+
'NOT',
445+
array(
446+
0 => 'a string',
447+
1 => 42,
448+
2 => null,
449+
),
450+
),
451+
452+
/*
453+
* Array values are checked with array_key_exists(), so a null value
454+
* matches, while object properties are checked with isset(),
455+
* so a null property does not.
456+
*/
457+
'array with a null value matching null' => array(
458+
array( array( 'foo' => null ) ),
459+
array( 'foo' => null ),
460+
'AND',
461+
array(
462+
0 => array( 'foo' => null ),
463+
),
464+
),
465+
'object with a null property matching null' => array(
466+
array( (object) array( 'foo' => null ) ),
467+
array( 'foo' => null ),
468+
'AND',
469+
array(),
470+
),
471+
472+
'string keys are preserved' => array(
473+
array(
474+
'first' => (object) array( 'foo' => 'bar' ),
475+
'second' => (object) array( 'foo' => 'baz' ),
476+
'third' => (object) array( 'foo' => 'bar' ),
477+
),
478+
array( 'foo' => 'bar' ),
479+
'AND',
480+
array(
481+
'first' => (object) array( 'foo' => 'bar' ),
482+
'third' => (object) array( 'foo' => 'bar' ),
483+
),
484+
),
485+
'no matches with OR' => array(
486+
array(
487+
(object) array( 'foo' => 'baz' ),
488+
(object) array( 'foo' => 'qux' ),
489+
),
490+
array( 'foo' => 'bar' ),
491+
'OR',
492+
array(),
493+
),
494+
'all matches with NOT' => array(
495+
array(
496+
(object) array( 'foo' => 'bar' ),
497+
(object) array( 'foo' => 'bar' ),
498+
),
499+
array( 'foo' => 'bar' ),
500+
'NOT',
501+
array(),
502+
),
233503
);
234504
}
235505
}

0 commit comments

Comments
 (0)