Make WordPress Core

Changeset 57737


Ignore:
Timestamp:
02/28/2024 06:09:38 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk/tests/phpunit/tests
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions.php

    r57524 r57737  
    725725
    726726                update_option( 'blog_charset', $orig_blog_charset );
    727         }
    728 
    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                 );
    762727        }
    763728
  • trunk/tests/phpunit/tests/functions/wpParseList.php

    r57735 r57737  
    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
     
    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        }
  • trunk/tests/phpunit/tests/functions/wpParseSlugList.php

    r57735 r57737  
    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[]
     
    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                );
Note: See TracChangeset for help on using the changeset viewer.