Make WordPress Core


Ignore:
Timestamp:
08/13/2022 12:09:41 PM (21 months 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.