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