Skip to content

Commit 9da33ad

Browse files
Tests: Expand wp_parse_id_list() unit tests.
Includes: * Moving pre-existing `wp_parse_id_list()` tests to their own file. * Merging new and pre-existing `wp_parse_slug_list()` tests. * Using named data provider in `wp_parse_list()` tests. Follow-up to [25170], [40044], [44546], [57284], [57725]. Props pbearne, mukesh27, SergeyBiryukov. Fixes #60218. See #60217, #59647. git-svn-id: https://develop.svn.wordpress.org/trunk@57737 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0366ad8 commit 9da33ad

4 files changed

Lines changed: 204 additions & 64 deletions

File tree

tests/phpunit/tests/functions.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -726,41 +726,6 @@ public function test_canonical_charset() {
726726
update_option( 'blog_charset', $orig_blog_charset );
727727
}
728728

729-
/**
730-
* @dataProvider data_wp_parse_id_list
731-
*/
732-
public function test_wp_parse_id_list( $expected, $actual ) {
733-
$this->assertSame( $expected, array_values( wp_parse_id_list( $actual ) ) );
734-
}
735-
736-
public function data_wp_parse_id_list() {
737-
return array(
738-
array( array( 1, 2, 3, 4 ), '1,2,3,4' ),
739-
array( array( 1, 2, 3, 4 ), '1, 2,,3,4' ),
740-
array( array( 1, 2, 3, 4 ), '1,2,2,3,4' ),
741-
array( array( 1, 2, 3, 4 ), array( '1', '2', '3', '4', '3' ) ),
742-
array( array( 1, 2, 3, 4 ), array( 1, '2', 3, '4' ) ),
743-
array( array( 1, 2, 3, 4 ), '-1,2,-3,4' ),
744-
array( array( 1, 2, 3, 4 ), array( -1, 2, '-3', '4' ) ),
745-
);
746-
}
747-
748-
/**
749-
* @dataProvider data_wp_parse_slug_list
750-
*/
751-
public function test_wp_parse_slug_list( $expected, $actual ) {
752-
$this->assertSame( $expected, array_values( wp_parse_slug_list( $actual ) ) );
753-
}
754-
755-
public function data_wp_parse_slug_list() {
756-
return array(
757-
array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
758-
array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple, banana,,carrot,dog' ),
759-
array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple banana carrot dog' ),
760-
array( array( 'apple', 'banana-carrot', 'd-o-g' ), array( 'apple ', 'banana carrot', 'd o g' ) ),
761-
);
762-
}
763-
764729
/**
765730
* @dataProvider data_device_can_upload
766731
*/
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/**
4+
* Tests for the wp_parse_id_list() function.
5+
*
6+
* @group functions
7+
*
8+
* @covers ::wp_parse_id_list
9+
*/
10+
class Tests_Functions_wpParseIdList extends WP_UnitTestCase {
11+
12+
/**
13+
* @ticket 22074
14+
* @ticket 60218
15+
*
16+
* @dataProvider data_wp_parse_id_list
17+
* @dataProvider data_unexpected_input
18+
*/
19+
public function test_wp_parse_id_list( $input_list, $expected ) {
20+
$this->assertSameSets( $expected, wp_parse_id_list( $input_list ) );
21+
}
22+
23+
/**
24+
* Data provider.
25+
*
26+
* @return array[]
27+
*/
28+
public function data_wp_parse_id_list() {
29+
return array(
30+
'regular' => array(
31+
'input_list' => '1,2,3,4',
32+
'expected' => array( 1, 2, 3, 4 ),
33+
),
34+
'double comma' => array(
35+
'input_list' => '1, 2,,3,4',
36+
'expected' => array( 1, 2, 3, 4 ),
37+
),
38+
'duplicate id in a string' => array(
39+
'input_list' => '1,2,2,3,4',
40+
'expected' => array( 1, 2, 3, 4 ),
41+
),
42+
'duplicate id in an array' => array(
43+
'input_list' => array( '1', '2', '3', '4', '3' ),
44+
'expected' => array( 1, 2, 3, 4 ),
45+
),
46+
'mixed type' => array(
47+
'input_list' => array( 1, '2', 3, '4' ),
48+
'expected' => array( 1, 2, 3, 4 ),
49+
),
50+
'negative ids in a string' => array(
51+
'input_list' => '-1,2,-3,4',
52+
'expected' => array( 1, 2, 3, 4 ),
53+
),
54+
'negative ids in an array' => array(
55+
'input_list' => array( -1, 2, '-3', '4' ),
56+
'expected' => array( 1, 2, 3, 4 ),
57+
),
58+
);
59+
}
60+
61+
/**
62+
* Data provider.
63+
*
64+
* @return array[]
65+
*/
66+
public function data_unexpected_input() {
67+
return array(
68+
'string with commas' => array(
69+
'input_list' => '1,2,string with spaces',
70+
'expected' => array( 1, 2, 0 ),
71+
),
72+
'array' => array(
73+
'input_list' => array( '1', 2, 'string with spaces' ),
74+
'expected' => array( 1, 2, 0 ),
75+
),
76+
'string with spaces' => array(
77+
'input_list' => '1 2 string with spaces',
78+
'expected' => array( 1, 2, 0 ),
79+
),
80+
'array with spaces' => array(
81+
'input_list' => array( '1 2 string with spaces' ),
82+
'expected' => array( 1 ),
83+
),
84+
'string with html' => array(
85+
'input_list' => '1 2 string <strong>with</strong> <h1>HEADING</h1>',
86+
'expected' => array( 1, 2, 0 ),
87+
),
88+
'array with html' => array(
89+
'input_list' => array( '1', 2, 'string <strong>with</strong> <h1>HEADING</h1>' ),
90+
'expected' => array( 1, 2, 0 ),
91+
),
92+
'array with null' => array(
93+
'input_list' => array( 1, 2, null ),
94+
'expected' => array( 1, 2 ),
95+
),
96+
'array with false' => array(
97+
'input_list' => array( 1, 2, false ),
98+
'expected' => array( 1, 2, 0 ),
99+
),
100+
);
101+
}
102+
}

tests/phpunit/tests/functions/wpParseList.php

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ class Tests_Functions_wpParseList extends WP_UnitTestCase {
1111

1212
/**
1313
* @ticket 43977
14+
*
1415
* @dataProvider data_wp_parse_list
1516
*/
16-
public function test_wp_parse_list( $expected, $actual ) {
17-
$this->assertSame( $expected, array_values( wp_parse_list( $actual ) ) );
17+
public function test_wp_parse_list( $input_list, $expected ) {
18+
$this->assertSameSets( $expected, wp_parse_list( $input_list ) );
1819
}
1920

2021
/**
@@ -24,17 +25,50 @@ public function test_wp_parse_list( $expected, $actual ) {
2425
*/
2526
public function data_wp_parse_list() {
2627
return array(
27-
array( array( '1', '2', '3', '4' ), '1,2,3,4' ),
28-
array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
29-
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana' ),
30-
array( array( '1', '2', 'apple', 'banana' ), '1, 2,apple,banana' ),
31-
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,,banana' ),
32-
array( array( '1', '2', 'apple', 'banana' ), ',1,2,apple,banana' ),
33-
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana,' ),
34-
array( array( '1', '2', 'apple', 'banana' ), '1,2 ,apple,banana' ),
35-
array( array(), '' ),
36-
array( array(), ',' ),
37-
array( array(), ',,' ),
28+
'ids only' => array(
29+
'input_list' => '1,2,3,4',
30+
'expected' => array( '1', '2', '3', '4' ),
31+
),
32+
'slugs only' => array(
33+
'input_list' => 'apple,banana,carrot,dog',
34+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
35+
),
36+
'ids and slugs' => array(
37+
'input_list' => '1,2,apple,banana',
38+
'expected' => array( '1', '2', 'apple', 'banana' ),
39+
),
40+
'space after comma' => array(
41+
'input_list' => '1, 2,apple,banana',
42+
'expected' => array( '1', '2', 'apple', 'banana' ),
43+
),
44+
'double comma' => array(
45+
'input_list' => '1,2,apple,,banana',
46+
'expected' => array( '1', '2', 'apple', 'banana' ),
47+
),
48+
'leading comma' => array(
49+
'input_list' => ',1,2,apple,banana',
50+
'expected' => array( '1', '2', 'apple', 'banana' ),
51+
),
52+
'trailing comma' => array(
53+
'input_list' => '1,2,apple,banana,',
54+
'expected' => array( '1', '2', 'apple', 'banana' ),
55+
),
56+
'space before comma' => array(
57+
'input_list' => '1,2 ,apple,banana',
58+
'expected' => array( '1', '2', 'apple', 'banana' ),
59+
),
60+
'empty string' => array(
61+
'input_list' => '',
62+
'expected' => array(),
63+
),
64+
'comma only' => array(
65+
'input_list' => ',',
66+
'expected' => array(),
67+
),
68+
'double comma only' => array(
69+
'input_list' => ',,',
70+
'expected' => array(),
71+
),
3872
);
3973
}
4074
}

tests/phpunit/tests/functions/wpParseSlugList.php

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,88 @@
1010
class Tests_Functions_WpParseSlugList extends WP_UnitTestCase {
1111

1212
/**
13+
* @ticket 35582
1314
* @ticket 60217
1415
*
1516
* @dataProvider data_wp_parse_slug_list
17+
* @dataProvider data_unexpected_input
1618
*/
1719
public function test_wp_parse_slug_list( $input_list, $expected ) {
18-
1920
$this->assertSameSets( $expected, wp_parse_slug_list( $input_list ) );
2021
}
2122

2223
/**
23-
* Data provider for test_wp_parse_slug_list().
24+
* Data provider.
2425
*
2526
* @return array[]
2627
*/
2728
public function data_wp_parse_slug_list() {
2829
return array(
29-
'simple' => array(
30+
'regular' => array(
31+
'input_list' => 'apple,banana,carrot,dog',
32+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
33+
),
34+
'double comma' => array(
35+
'input_list' => 'apple, banana,,carrot,dog',
36+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
37+
),
38+
'duplicate slug in a string' => array(
39+
'input_list' => 'apple,banana,carrot,carrot,dog',
40+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
41+
),
42+
'duplicate slug in an array' => array(
43+
'input_list' => array( 'apple', 'banana', 'carrot', 'carrot', 'dog' ),
44+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
45+
),
46+
'string with spaces' => array(
47+
'input_list' => 'apple banana carrot dog',
48+
'expected' => array( 'apple', 'banana', 'carrot', 'dog' ),
49+
),
50+
'array with spaces' => array(
51+
'input_list' => array( 'apple ', 'banana carrot', 'd o g' ),
52+
'expected' => array( 'apple', 'banana-carrot', 'd-o-g' ),
53+
),
54+
);
55+
}
56+
57+
/**
58+
* Data provider.
59+
*
60+
* @return array[]
61+
*/
62+
public function data_unexpected_input() {
63+
return array(
64+
'string with commas' => array(
65+
'input_list' => '1,2,string with spaces',
66+
'expected' => array( '1', '2', 'string', 'with', 'spaces' ),
67+
),
68+
'array' => array(
3069
'input_list' => array( '1', 2, 'string with spaces' ),
3170
'expected' => array( '1', '2', 'string-with-spaces' ),
3271
),
33-
'simple_with_comma' => array(
34-
'input_list' => '1,2,string with spaces',
72+
'string with spaces' => array(
73+
'input_list' => '1 2 string with spaces',
3574
'expected' => array( '1', '2', 'string', 'with', 'spaces' ),
3675
),
37-
'array_with_spaces' => array(
76+
'array with spaces' => array(
3877
'input_list' => array( '1 2 string with spaces' ),
3978
'expected' => array( '1-2-string-with-spaces' ),
4079
),
41-
'simple_with_spaces' => array(
42-
'input_list' => '1 2 string with spaces',
43-
'expected' => array( '1', '2', 'string', 'with', 'spaces' ),
80+
'string with html' => array(
81+
'input_list' => '1 2 string <strong>with</strong> <h1>HEADING</h1>',
82+
'expected' => array( '1', '2', 'string', 'with', 'heading' ),
4483
),
45-
'array_html' => array(
84+
'array with html' => array(
4685
'input_list' => array( '1', 2, 'string <strong>with</strong> <h1>HEADING</h1>' ),
4786
'expected' => array( '1', '2', 'string-with-heading' ),
4887
),
49-
'simple_html_spaces' => array(
50-
'input_list' => '1 2 string <strong>with</strong> <h1>HEADING</h1>',
51-
'expected' => array( '1', '2', 'string', 'with', 'heading' ),
88+
'array with null' => array(
89+
'input_list' => array( 1, 2, null ),
90+
'expected' => array( '1', '2' ),
5291
),
53-
'dup_id' => array(
54-
'input_list' => '1 1 string string',
55-
'expected' => array( '1', 'string' ),
92+
'array with false' => array(
93+
'input_list' => array( 1, 2, false ),
94+
'expected' => array( '1', '2', '' ),
5695
),
5796
);
5897
}

0 commit comments

Comments
 (0)