| 1 | <?php |
| 2 | /** |
| 3 | * Test functions against maybe_unserialize. |
| 4 | * @group functions.php |
| 5 | */ |
| 6 | |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | |
| 12 | class Tests_Functions_maybe_unserialize extends WP_UnitTestCase { |
| 13 | |
| 14 | //Passing an array should return the same array back |
| 15 | public function test_maybe_unserialize_array() { |
| 16 | $expected = array( |
| 17 | 'start' => 'here', |
| 18 | 'end' => 'here', |
| 19 | ); |
| 20 | |
| 21 | $this->assertEquals( $expected, maybe_unserialize( $expected ) ); |
| 22 | } |
| 23 | |
| 24 | //Tests normal string unserialization |
| 25 | public function test_maybe_unserialize_string() { |
| 26 | $this->assertEquals( 'string', maybe_unserialize( 'string' ) ); |
| 27 | |
| 28 | $this->assertEquals( '[string]', maybe_unserialize( '[string]' ) ); |
| 29 | |
| 30 | $this->assertEquals( '{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}', maybe_unserialize( '{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) ); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | //Passing an int should return the same int |
| 35 | public function test_maybe_unserialize_int() { |
| 36 | |
| 37 | $this->assertEquals( 100, maybe_unserialize( 100 ) ); |
| 38 | } |
| 39 | |
| 40 | //Passing null should return null |
| 41 | public function test_maybe_unserialize_null() { |
| 42 | |
| 43 | $this->assertEquals( null, maybe_unserialize( null ) ); |
| 44 | } |
| 45 | |
| 46 | //Passing null should return null |
| 47 | public function test_maybe_unserialize_object() { |
| 48 | $object = new stdClass(); |
| 49 | |
| 50 | $this->assertEquals( $object, maybe_unserialize( $object ) ); |
| 51 | } |
| 52 | |
| 53 | //Passing a well formatted seriliazed string, should return an array |
| 54 | public function test_maybe_unserialize_to_array() { |
| 55 | $expected = array( |
| 56 | 'start' => 'here', |
| 57 | 'end' => 'here', |
| 58 | ); |
| 59 | |
| 60 | $this->assertEquals( $expected, maybe_unserialize( 'a:2:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) ); |
| 61 | } |
| 62 | |
| 63 | //Passing a baddly formatted seriliazed string, should return false |
| 64 | public function test_maybe_unserialize_bad_serialized_string() { |
| 65 | |
| 66 | // a:3 not a:2 as it should be |
| 67 | $this->assertFalse( maybe_unserialize( 'a:3:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) ); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | //Passing a well formatted seriliazed string with spaces before and after, should return false |
| 72 | public function test_maybe_unserialize_to_array_untrimmed() { |
| 73 | |
| 74 | $this->assertFalse( maybe_unserialize( ' a:2:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";} ' ) ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | } |
| 79 | No newline at end of file |