Ticket #39080: is_serialized.patch
File is_serialized.patch, 6.2 KB (added by , 8 years ago) |
---|
-
tests/phpunit/tests/functions.php
161 161 $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' ); 162 162 } 163 163 164 function test_is_serialized() {165 $cases = array(166 serialize(null),167 serialize(true),168 serialize(false),169 serialize(-25),170 serialize(25),171 serialize(1.1),172 serialize('this string will be serialized'),173 serialize("a\nb"),174 serialize(array()),175 serialize(array(1,1,2,3,5,8,13)),176 serialize( (object)array('test' => true, '3', 4) )177 );178 foreach ( $cases as $case )179 $this->assertTrue( is_serialized($case), "Serialized data: $case" );180 181 $not_serialized = array(182 'a string',183 'garbage:a:0:garbage;',184 's:4:test;'185 );186 foreach ( $not_serialized as $case )187 $this->assertFalse( is_serialized($case), "Test data: $case" );188 }189 190 164 /** 191 * @ticket 17375192 */193 function test_no_new_serializable_types() {194 $this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );195 }196 197 /**198 * @dataProvider data_is_serialized_string199 */200 public function test_is_serialized_string( $value, $result ) {201 $this->assertSame( is_serialized_string( $value ), $result );202 }203 204 public function data_is_serialized_string() {205 return array(206 // Not a string.207 array( 0, false ),208 209 // Too short when trimmed.210 array( 's:3 ', false ),211 212 // Too short.213 array( 's:3', false ),214 215 // No colon in second position.216 array( 's!3:"foo";', false ),217 218 // No trailing semicolon.219 array( 's:3:"foo"', false ),220 221 // Wrong type.222 array( 'a:3:"foo";', false ),223 224 // No closing quote.225 array( 'a:3:"foo;', false ),226 227 // Wrong number of characters is close enough for is_serialized_string().228 array( 's:12:"foo";', true ),229 230 // Okay.231 array( 's:3:"foo";', true ),232 233 );234 }235 236 /**237 165 * @group add_query_arg 238 166 */ 239 167 function test_add_query_arg() { -
tests/phpunit/tests/functions/is-serialized.php
1 <?php 2 3 /** 4 * @group functions.php 5 */ 6 class Tests_Functions_Is_Serialized extends WP_UnitTestCase { 7 public function data_is_serialized() { 8 global $wp; 9 10 return array( 11 array( true, serialize( 'a string' ), 'pass a serialized string' ), 12 array( false, 'a string', 'pass a string' ), 13 array( 14 true, 15 serialize( array( 16 'start' => 1454803200, 17 'end' => 1455407999, 18 ) ), 19 'pass serialized array', 20 ), 21 array( 22 false, 23 array( 24 'start' => 1454803200, 25 'end' => 1455407999, 26 ), 27 'pass array', 28 ), 29 array( true, serialize( $wp ), 'pass a serialized object' ), 30 array( false, $wp, 'pass a object' ), 31 array( true, serialize( true ), 'pass a serialized bool true' ), 32 array( false, true, 'pass a bool' ), 33 array( true, serialize( 99 ), 'pass a serialized int' ), 34 array( false, 99, 'pass a int' ), 35 array( false, 's:3 ', 'Too short when trimmed.' ), 36 array( false, 's:3', 'Too short' ), 37 array( false, 's!3:"foo";', 'No colon in second position.' ), 38 array( false, 's:3:"foo"', 'No trailing semicolon.' ), 39 array( true, 'a:3:"foo";', 'Wrong type.' ), 40 array( true, 'a:3:"foo;', 'No closing quote.' ), 41 array( true, 's:12:"foo";', 'Wrong number of characters is close enough for is_serialized_string()' ), 42 array( true, 's:3:"foo";', 'Okay' ), 43 array( true, serialize( null ), 'null' ), 44 array( true, serialize(false), 'bool false' ), 45 array( true, serialize(-25), '-25' ), 46 array( true, serialize(1.1), '1.1' ), 47 array( true, serialize( "a\nb" ), '"a\nb"' ), 48 array( true, serialize( (object)array('test' => true, '3', 4) ) , 'null' ), 49 array( false, 'Test data: garbage:a:0:garbage;', ' garbage:a:0:garbage;' ), 50 array( false, 'Test data: a string', '' ), 51 array( false, 'Test data: s:4:test;', '' ), 52 array( false, 'C:16:"Serialized_Class":6:{a:0:{}}', 'class object' ), 53 ); 54 55 } 56 57 /** 58 * @dataProvider data_is_serialized 59 * @covers ::is_serialized 60 * 61 * @param $expected 62 * @param $data 63 * @param $message 64 */ 65 public function test_is_serialized( $expected, $data, $message ) { 66 $this->assertEquals( $expected, is_serialized( $data ), $message ); 67 } 68 } -
tests/phpunit/tests/functions/maybe-unserialize.php
1 <?php 2 3 /** 4 * @group functions.php 5 */ 6 class Tests_Functions_maybe_unserialize extends WP_UnitTestCase { 7 public function data_maybe_unserialize() { 8 global $wp; 9 10 return array( 11 array( 'a string', serialize( 'a string' ), 'pass a serialized string' ), 12 array( 'a string', 'a string', 'pass a string' ), 13 array( 14 array( 15 'start' => 1454803200, 16 'end' => 1455407999, 17 ), 18 serialize( array( 19 'start' => 1454803200, 20 'end' => 1455407999, 21 ) ), 22 'pass serialized array', 23 ), 24 array( 25 array( 26 'start' => 1454803200, 27 'end' => 1455407999, 28 ), 29 array( 30 'start' => 1454803200, 31 'end' => 1455407999, 32 ), 33 'pass array', 34 ), 35 array( $wp, serialize( $wp ), 'pass a serialized object' ), 36 array( $wp, $wp, 'pass a object' ), 37 array( true, serialize( true ), 'pass a serialized bool' ), 38 array( true, true, 'pass a bool' ), 39 array( 99, serialize( 99 ), 'pass a serialized int' ), 40 array( 99, 99, 'pass a int' ) 41 42 ); 43 44 } 45 46 /** 47 * @dataProvider data_maybe_unserialize 48 * @covers ::maybe_unserialize 49 * 50 * @param $expected 51 * @param $data 52 * @param $message 53 */ 54 public function test_maybe_unserialize_pass_serialized( $expected, $data, $message ) { 55 $this->assertEquals( $expected, maybe_unserialize( $data ), $message ); 56 } 57 }