From 38037ebb2c51bdfe508819182fcc2b3cba340055 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Fri, 12 Aug 2022 09:24:23 +0000 Subject: [PATCH] Formatting: Add support for Enums in `is_serialized()`. This changeset adds support for Enums in `is_serialized()`. It also adds new unit tests for this function. Props ayeshrajans, konradyoast, peterwilsoncc, costdev, dennisatyoast, mukesh27. Fixes #53299. git-svn-id: https://develop.svn.wordpress.org/trunk@53886 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 4 +- .../phpunit/tests/functions/isSerialized.php | 86 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/functions/isSerialized.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index cf2d7ad146..3fb8df3344 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -657,10 +657,11 @@ function maybe_unserialize( $data ) { /** * Check value to find if it was serialized. * - * If $data is not an string, then returned value will always be false. + * If $data is not a string, then returned value will always be false. * Serialized data is always a string. * * @since 2.0.5 + * @since 6.1.0 Added Enum support. * * @param string $data Value to check to see if was serialized. * @param bool $strict Optional. Whether to be strict about the end of the string. Default true. @@ -714,6 +715,7 @@ function is_serialized( $data, $strict = true ) { // Or else fall through. case 'a': case 'O': + case 'E': return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); case 'b': case 'i': diff --git a/tests/phpunit/tests/functions/isSerialized.php b/tests/phpunit/tests/functions/isSerialized.php new file mode 100644 index 0000000000..64835085e3 --- /dev/null +++ b/tests/phpunit/tests/functions/isSerialized.php @@ -0,0 +1,86 @@ +assertSame( $expected, is_serialized( $data ) ); + } + + /** + * Data provider method for testing `is_serialized()`. + * + * @return array + */ + public function data_is_serialized() { + return array( + 'an array' => array( + 'data' => array(), + 'expected' => false, + ), + 'an object' => array( + 'data' => new stdClass(), + 'expected' => false, + ), + 'a boolean false' => array( + 'data' => false, + 'expected' => false, + ), + 'a boolean true' => array( + 'data' => true, + 'expected' => false, + ), + 'an integer 0' => array( + 'data' => 0, + 'expected' => false, + ), + 'an integer 1' => array( + 'data' => 1, + 'expected' => false, + ), + 'a float 0.0' => array( + 'data' => 0.0, + 'expected' => false, + ), + 'a float 1.0' => array( + 'data' => 1.0, + 'expected' => false, + ), + 'string that is too short' => array( + 'data' => 's:3', + 'expected' => false, + ), + 'not a colon in second position' => array( + 'data' => 's!3:"foo";', + 'expected' => false, + ), + 'no trailing semicolon (strict check)' => array( + 'data' => 's:3:"foo"', + 'expected' => false, + ), + 'valid serialized null' => array( + 'data' => 'N;', + 'expected' => true, + ), + 'valid serialized Enum' => array( + 'data' => 'E:7:"Foo:bar";', + 'expected' => true, + ), + ); + } +}