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.

git-svn-id: https://develop.svn.wordpress.org/trunk@53890 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-08-13 12:09:41 +00:00
parent c45ea397da
commit d1e22dbad8
4 changed files with 419 additions and 198 deletions

View File

@@ -373,152 +373,6 @@ class Tests_Functions extends WP_UnitTestCase {
return $formats;
}
/**
* @dataProvider data_is_not_serialized
*/
public function test_maybe_serialize( $value ) {
if ( is_array( $value ) || is_object( $value ) ) {
$expected = serialize( $value );
} else {
$expected = $value;
}
$this->assertSame( $expected, maybe_serialize( $value ) );
}
/**
* @dataProvider data_is_serialized
*/
public function test_maybe_serialize_with_double_serialization( $value ) {
$expected = serialize( $value );
$this->assertSame( $expected, maybe_serialize( $value ) );
}
/**
* @dataProvider data_is_serialized
* @dataProvider data_is_not_serialized
*/
public function test_maybe_unserialize( $value, $is_serialized ) {
if ( $is_serialized ) {
$expected = unserialize( trim( $value ) );
} else {
$expected = $value;
}
if ( is_object( $expected ) ) {
$this->assertEquals( $expected, maybe_unserialize( $value ) );
} else {
$this->assertSame( $expected, maybe_unserialize( $value ) );
}
}
/**
* @dataProvider data_is_serialized
* @dataProvider data_is_not_serialized
*/
public function test_is_serialized( $value, $expected ) {
$this->assertSame( $expected, is_serialized( $value ) );
}
/**
* @dataProvider data_serialize_deserialize_objects
*/
public function test_deserialize_request_utility_filtered_iterator_objects( $value ) {
$serialized = maybe_serialize( $value );
if ( get_class( $value ) === 'Requests_Utility_FilteredIterator' ) {
$new_value = unserialize( $serialized );
$property = ( new ReflectionClass( 'Requests_Utility_FilteredIterator' ) )->getProperty( 'callback' );
$property->setAccessible( true );
$callback_value = $property->getValue( $new_value );
$this->assertSame( null, $callback_value );
} else {
$this->assertSame( $value->count(), unserialize( $serialized )->count() );
}
}
public function data_serialize_deserialize_objects() {
return array(
array( new Requests_Utility_FilteredIterator( array( 1 ), 'md5' ) ),
array( new Requests_Utility_FilteredIterator( array( 1, 2 ), 'sha1' ) ),
array( new ArrayIterator( array( 1, 2, 3 ) ) ),
);
}
public function data_is_serialized() {
return array(
array( serialize( null ), true ),
array( serialize( true ), true ),
array( serialize( false ), true ),
array( serialize( -25 ), true ),
array( serialize( 25 ), true ),
array( serialize( 1.1 ), true ),
array( serialize( 'this string will be serialized' ), true ),
array( serialize( "a\nb" ), true ),
array( serialize( array() ), true ),
array( serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ), true ),
array(
serialize(
(object) array(
'test' => true,
'3',
4,
)
),
true,
),
array( ' s:25:"this string is serialized"; ', true ),
);
}
public function data_is_not_serialized() {
return array(
array( null, false ),
array( true, false ),
array( false, false ),
array( -25, false ),
array( 25, false ),
array( 1.1, false ),
array( 'this string will be serialized', false ),
array( "a\nb", false ),
array( array(), false ),
array( array( 1, 1, 2, 3, 5, 8, 13 ), false ),
array(
(object) array(
'test' => true,
'3',
4,
),
false,
),
array( 'a string', false ),
array( 'garbage:a:0:garbage;', false ),
array( 's:4:test;', false ),
);
}
/**
* @ticket 46570
* @dataProvider data_is_serialized_should_return_true_for_large_floats
*/
public function test_is_serialized_should_return_true_for_large_floats( $value ) {
$this->assertTrue( is_serialized( $value ) );
}
public function data_is_serialized_should_return_true_for_large_floats() {
return array(
array( serialize( 1.7976931348623157E+308 ) ),
array( serialize( array( 1.7976931348623157E+308, 1.23e50 ) ) ),
);
}
/**
* @ticket 17375
*/
public function test_no_new_serializable_types() {
$this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
}
/**
* @group add_query_arg
*/