Make WordPress Core

Changeset 53890


Ignore:
Timestamp:
08/13/2022 12:09:41 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to serialization tests.

There were two sets of tests for is_serialized():

  • One in the functions.php file, based on the same file name in core.
  • One in a separate class in the functions directory.

To avoid confusion and make it easier to decide where new tests should go in the future, the existing tests are now combined in the latter location.

Includes:

  • Moving is_serialized() and maybe_serialize() tests into their own classes.
  • Using named data providers to make test output more descriptive.
  • Combining test cases and removing duplicates.

Follow-up to [278/tests], [279/tests], [328/tests], [32631], [45754], [47452], [49382], [53886], [53889].

See #55652.

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

Legend:

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

    r53461 r53890  
    372372
    373373        return $formats;
    374     }
    375 
    376     /**
    377      * @dataProvider data_is_not_serialized
    378      */
    379     public function test_maybe_serialize( $value ) {
    380         if ( is_array( $value ) || is_object( $value ) ) {
    381             $expected = serialize( $value );
    382         } else {
    383             $expected = $value;
    384         }
    385 
    386         $this->assertSame( $expected, maybe_serialize( $value ) );
    387     }
    388 
    389     /**
    390      * @dataProvider data_is_serialized
    391      */
    392     public function test_maybe_serialize_with_double_serialization( $value ) {
    393         $expected = serialize( $value );
    394 
    395         $this->assertSame( $expected, maybe_serialize( $value ) );
    396     }
    397 
    398     /**
    399      * @dataProvider data_is_serialized
    400      * @dataProvider data_is_not_serialized
    401      */
    402     public function test_maybe_unserialize( $value, $is_serialized ) {
    403         if ( $is_serialized ) {
    404             $expected = unserialize( trim( $value ) );
    405         } else {
    406             $expected = $value;
    407         }
    408 
    409         if ( is_object( $expected ) ) {
    410             $this->assertEquals( $expected, maybe_unserialize( $value ) );
    411         } else {
    412             $this->assertSame( $expected, maybe_unserialize( $value ) );
    413         }
    414     }
    415 
    416     /**
    417      * @dataProvider data_is_serialized
    418      * @dataProvider data_is_not_serialized
    419      */
    420     public function test_is_serialized( $value, $expected ) {
    421         $this->assertSame( $expected, is_serialized( $value ) );
    422     }
    423 
    424     /**
    425      * @dataProvider data_serialize_deserialize_objects
    426      */
    427     public function test_deserialize_request_utility_filtered_iterator_objects( $value ) {
    428         $serialized = maybe_serialize( $value );
    429         if ( get_class( $value ) === 'Requests_Utility_FilteredIterator' ) {
    430             $new_value = unserialize( $serialized );
    431             $property  = ( new ReflectionClass( 'Requests_Utility_FilteredIterator' ) )->getProperty( 'callback' );
    432             $property->setAccessible( true );
    433             $callback_value = $property->getValue( $new_value );
    434             $this->assertSame( null, $callback_value );
    435         } else {
    436             $this->assertSame( $value->count(), unserialize( $serialized )->count() );
    437         }
    438     }
    439 
    440     public function data_serialize_deserialize_objects() {
    441         return array(
    442             array( new Requests_Utility_FilteredIterator( array( 1 ), 'md5' ) ),
    443             array( new Requests_Utility_FilteredIterator( array( 1, 2 ), 'sha1' ) ),
    444             array( new ArrayIterator( array( 1, 2, 3 ) ) ),
    445         );
    446     }
    447 
    448     public function data_is_serialized() {
    449         return array(
    450             array( serialize( null ), true ),
    451             array( serialize( true ), true ),
    452             array( serialize( false ), true ),
    453             array( serialize( -25 ), true ),
    454             array( serialize( 25 ), true ),
    455             array( serialize( 1.1 ), true ),
    456             array( serialize( 'this string will be serialized' ), true ),
    457             array( serialize( "a\nb" ), true ),
    458             array( serialize( array() ), true ),
    459             array( serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ), true ),
    460             array(
    461                 serialize(
    462                     (object) array(
    463                         'test' => true,
    464                         '3',
    465                         4,
    466                     )
    467                 ),
    468                 true,
    469             ),
    470             array( '   s:25:"this string is serialized";   ', true ),
    471         );
    472     }
    473 
    474     public function data_is_not_serialized() {
    475         return array(
    476             array( null, false ),
    477             array( true, false ),
    478             array( false, false ),
    479             array( -25, false ),
    480             array( 25, false ),
    481             array( 1.1, false ),
    482             array( 'this string will be serialized', false ),
    483             array( "a\nb", false ),
    484             array( array(), false ),
    485             array( array( 1, 1, 2, 3, 5, 8, 13 ), false ),
    486             array(
    487                 (object) array(
    488                     'test' => true,
    489                     '3',
    490                     4,
    491                 ),
    492                 false,
    493             ),
    494             array( 'a string', false ),
    495             array( 'garbage:a:0:garbage;', false ),
    496             array( 's:4:test;', false ),
    497         );
    498     }
    499 
    500     /**
    501      * @ticket 46570
    502      * @dataProvider data_is_serialized_should_return_true_for_large_floats
    503      */
    504     public function test_is_serialized_should_return_true_for_large_floats( $value ) {
    505         $this->assertTrue( is_serialized( $value ) );
    506     }
    507 
    508     public function data_is_serialized_should_return_true_for_large_floats() {
    509         return array(
    510             array( serialize( 1.7976931348623157E+308 ) ),
    511             array( serialize( array( 1.7976931348623157E+308, 1.23e50 ) ) ),
    512         );
    513     }
    514 
    515     /**
    516      * @ticket 17375
    517      */
    518     public function test_no_new_serializable_types() {
    519         $this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
    520374    }
    521375
  • trunk/tests/phpunit/tests/functions/isSerialized.php

    r53886 r53890  
    1212
    1313    /**
    14      * Run tests on `is_serialized()`.
    15      *
    1614     * @dataProvider data_is_serialized
     15     * @dataProvider data_is_not_serialized
    1716     *
    1817     * @param mixed $data     Data value to test.
    1918     * @param bool  $expected Expected function result.
    2019     */
    21     public function test_is_serialized_string( $data, $expected ) {
     20    public function test_is_serialized( $data, $expected ) {
    2221        $this->assertSame( $expected, is_serialized( $data ) );
    2322    }
    2423
    2524    /**
    26      * Data provider method for testing `is_serialized()`.
     25     * Data provider for `test_is_serialized()`.
    2726     *
    2827     * @return array
     
    3029    public function data_is_serialized() {
    3130        return array(
    32             'an array'                             => array(
     31            'serialized empty array'            => array(
     32                'data'     => serialize( array() ),
     33                'expected' => true,
     34            ),
     35            'serialized non-empty array'        => array(
     36                'data'     => serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ),
     37                'expected' => true,
     38            ),
     39            'serialized empty object'           => array(
     40                'data'     => serialize( new stdClass() ),
     41                'expected' => true,
     42            ),
     43            'serialized non-empty object'       => array(
     44                'data'     => serialize(
     45                    (object) array(
     46                        'test' => true,
     47                        '1',
     48                        2,
     49                    )
     50                ),
     51                'expected' => true,
     52            ),
     53            'serialized null'                   => array(
     54                'data'     => serialize( null ),
     55                'expected' => true,
     56            ),
     57            'serialized boolean true'           => array(
     58                'data'     => serialize( true ),
     59                'expected' => true,
     60            ),
     61            'serialized boolean false'          => array(
     62                'data'     => serialize( false ),
     63                'expected' => true,
     64            ),
     65            'serialized integer -1'             => array(
     66                'data'     => serialize( -1 ),
     67                'expected' => true,
     68            ),
     69            'serialized integer 1'              => array(
     70                'data'     => serialize( -1 ),
     71                'expected' => true,
     72            ),
     73            'serialized float 1.1'              => array(
     74                'data'     => serialize( 1.1 ),
     75                'expected' => true,
     76            ),
     77            'serialized string'                 => array(
     78                'data'     => serialize( 'this string will be serialized' ),
     79                'expected' => true,
     80            ),
     81            'serialized string with line break' => array(
     82                'data'     => serialize( "a\nb" ),
     83                'expected' => true,
     84            ),
     85            'serialized string with leading and trailing spaces' => array(
     86                'data'     => '   s:25:"this string is serialized";   ',
     87                'expected' => true,
     88            ),
     89            'serialized enum'                   => array(
     90                'data'     => 'E:7:"Foo:bar";',
     91                'expected' => true,
     92            ),
     93        );
     94    }
     95
     96    /**
     97     * Data provider for `test_is_serialized()`.
     98     *
     99     * @return array
     100     */
     101    public function data_is_not_serialized() {
     102        return array(
     103            'an empty array'                             => array(
    33104                'data'     => array(),
    34105                'expected' => false,
    35106            ),
    36             'an object'                            => array(
     107            'a non-empty array'                          => array(
     108                'data'     => array( 1, 1, 2, 3, 5, 8, 13 ),
     109                'expected' => false,
     110            ),
     111            'an empty object'                            => array(
    37112                'data'     => new stdClass(),
    38113                'expected' => false,
    39114            ),
    40             'a boolean false'                      => array(
     115            'a non-empty object'                         => array(
     116                'data'     => (object) array(
     117                    'test' => true,
     118                    '1',
     119                    2,
     120                ),
     121                'expected' => false,
     122            ),
     123            'null'                                       => array(
     124                'data'     => null,
     125                'expected' => false,
     126            ),
     127            'a boolean true'                             => array(
     128                'data'     => true,
     129                'expected' => false,
     130            ),
     131            'a boolean false'                            => array(
    41132                'data'     => false,
    42133                'expected' => false,
    43134            ),
    44             'a boolean true'                       => array(
    45                 'data'     => true,
    46                 'expected' => false,
    47             ),
    48             'an integer 0'                         => array(
     135            'an integer -1'                              => array(
     136                'data'     => -1,
     137                'expected' => false,
     138            ),
     139            'an integer 0'                               => array(
    49140                'data'     => 0,
    50141                'expected' => false,
    51142            ),
    52             'an integer 1'                         => array(
     143            'an integer 1'                               => array(
    53144                'data'     => 1,
    54145                'expected' => false,
    55146            ),
    56             'a float 0.0'                          => array(
     147            'a float 0.0'                                => array(
    57148                'data'     => 0.0,
    58149                'expected' => false,
    59150            ),
    60             'a float 1.0'                          => array(
    61                 'data'     => 1.0,
    62                 'expected' => false,
    63             ),
    64             'string that is too short'             => array(
     151            'a float 1.1'                                => array(
     152                'data'     => 1.1,
     153                'expected' => false,
     154            ),
     155            'a string'                                   => array(
     156                'data'     => 'a string',
     157                'expected' => false,
     158            ),
     159            'a string with line break'                   => array(
     160                'data'     => "a\nb",
     161                'expected' => false,
     162            ),
     163            'a string with leading and trailing garbage' => array(
     164                'data'     => 'garbage:a:0:garbage;',
     165                'expected' => false,
     166            ),
     167            'a string with missing double quotes'        => array(
     168                'data'     => 's:4:test;',
     169                'expected' => false,
     170            ),
     171            'a string that is too short'                 => array(
    65172                'data'     => 's:3',
    66173                'expected' => false,
    67174            ),
    68             'not a colon in second position'       => array(
     175            'not a colon in second position'             => array(
    69176                'data'     => 's!3:"foo";',
    70177                'expected' => false,
    71178            ),
    72             'no trailing semicolon (strict check)' => array(
     179            'no trailing semicolon (strict check)'       => array(
    73180                'data'     => 's:3:"foo"',
    74181                'expected' => false,
    75182            ),
    76             'valid serialized null'                => array(
    77                 'data'     => 'N;',
    78                 'expected' => true,
    79             ),
    80             'valid serialized Enum'                => array(
    81                 'data'     => 'E:7:"Foo:bar";',
    82                 'expected' => true,
    83             ),
    84183        );
    85184    }
     185
     186    /**
     187     * @ticket 46570
     188     * @dataProvider data_is_serialized_should_return_true_for_large_floats
     189     */
     190    public function test_is_serialized_should_return_true_for_large_floats( $value ) {
     191        $this->assertTrue( is_serialized( $value ) );
     192    }
     193
     194    public function data_is_serialized_should_return_true_for_large_floats() {
     195        return array(
     196            array( serialize( 1.7976931348623157E+308 ) ),
     197            array( serialize( array( 1.7976931348623157E+308, 1.23e50 ) ) ),
     198        );
     199    }
     200
     201    /**
     202     * @ticket 17375
     203     */
     204    public function test_no_new_serializable_types() {
     205        $this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
     206    }
    86207}
  • trunk/tests/phpunit/tests/functions/isSerializedString.php

    r53889 r53890  
    3232                'expected' => false,
    3333            ),
    34             'a class'                                 => array(
     34            'an object'                               => array(
    3535                'data'     => new stdClass(),
    3636                'expected' => false,
Note: See TracChangeset for help on using the changeset viewer.