| 1 | <?php |
| 2 | |
| 3 | require_once( ABSPATH . '../tests/phpunit/data/classes/class-foo.php' ); |
| 4 | |
| 5 | /** |
| 6 | * Test maybe_unserialize. |
| 7 | * |
| 8 | * @group functions.php |
| 9 | */ |
| 10 | class Tests_Functions_MaybeUnserialize extends WP_UnitTestCase { |
| 11 | |
| 12 | /** |
| 13 | * Object list for testing against. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | var $object_list = array(); |
| 18 | |
| 19 | /** |
| 20 | * Array list for testing against. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | var $array_list = array(); |
| 25 | |
| 26 | /** |
| 27 | * Setup for tests. |
| 28 | */ |
| 29 | function setUp() { |
| 30 | parent::setUp(); |
| 31 | $this->array_list['foo'] = array( |
| 32 | 'name' => 'foo', |
| 33 | 'id' => 'f', |
| 34 | 'field1' => true, |
| 35 | 'field2' => true, |
| 36 | 'field3' => true, |
| 37 | 'field4' => array( |
| 38 | 'red', |
| 39 | ), |
| 40 | ); |
| 41 | $this->array_list['bar'] = array( |
| 42 | 'name' => 'bar', |
| 43 | 'id' => 'b', |
| 44 | 'field1' => true, |
| 45 | 'field2' => true, |
| 46 | 'field3' => false, |
| 47 | 'field4' => array( |
| 48 | 'green', |
| 49 | ), |
| 50 | ); |
| 51 | foreach ( $this->array_list as $key => $value ) { |
| 52 | $this->object_list[ $key ] = (object) $value; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Test unserialization default functionality. |
| 58 | */ |
| 59 | public function test_maybe_unserialize() { |
| 60 | |
| 61 | // Unserialization of array. |
| 62 | $serialized_array = serialize( $this->array_list ); |
| 63 | $unseralized_array = maybe_unserialize( $serialized_array ); |
| 64 | $this->assertEquals( $unseralized_array, $this->array_list ); |
| 65 | |
| 66 | // Unserialization of object. |
| 67 | $serialized_object = serialize( $this->object_list ); |
| 68 | $unseralized_object = maybe_unserialize( $serialized_object ); |
| 69 | $this->assertEquals( $unseralized_object, $this->object_list ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Test unserialization no classes allowed. |
| 74 | * |
| 75 | * @requires PHP 7.0 |
| 76 | */ |
| 77 | public function test_maybe_unserialize_php7_no_classes() { |
| 78 | $foo = new Foo(); |
| 79 | $serialized_foo = serialize( $foo ); |
| 80 | $unseralized_foo = maybe_unserialize( $serialized_foo, array( |
| 81 | 'allowed_classes' => false, |
| 82 | ) ); |
| 83 | $this->assertEquals( get_class( $unseralized_foo ), '__PHP_Incomplete_Class' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Test unserialization different classes allowed. |
| 88 | * |
| 89 | * @requires PHP 7.0 |
| 90 | */ |
| 91 | public function test_maybe_unserialize_php7_different_classes() { |
| 92 | $foo = new Foo(); |
| 93 | $serialized_foo = serialize( $foo ); |
| 94 | $unseralized_foo = maybe_unserialize( $serialized_foo, array( |
| 95 | 'allowed_classes' => array( |
| 96 | 'bar', |
| 97 | ), |
| 98 | ) ); |
| 99 | $this->assertEquals( get_class( $unseralized_foo ), '__PHP_Incomplete_Class' ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Test unserialization class this class implements not allowed. |
| 104 | * |
| 105 | * @requires PHP 7.0 |
| 106 | */ |
| 107 | public function test_maybe_unserialize_php7_implement_classes() { |
| 108 | $foo = new Foo(); |
| 109 | $serialized_foo = serialize( $foo ); |
| 110 | $unseralized_foo = maybe_unserialize( $serialized_foo, array( |
| 111 | 'allowed_classes' => array( |
| 112 | 'FooInterface', |
| 113 | ), |
| 114 | ) ); |
| 115 | $this->assertEquals( get_class( $unseralized_foo ), '__PHP_Incomplete_Class' ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Test unserialization class this class extends not allowed. |
| 120 | * |
| 121 | * @requires PHP 7.0 |
| 122 | */ |
| 123 | public function test_maybe_unserialize_php7_extend_classes() { |
| 124 | $foo = new Foo(); |
| 125 | $serialized_foo = serialize( $foo ); |
| 126 | $unseralized_foo = maybe_unserialize( $serialized_foo, array( |
| 127 | 'allowed_classes' => array( |
| 128 | 'FooBase', |
| 129 | ), |
| 130 | ) ); |
| 131 | $this->assertEquals( get_class( $unseralized_foo ), '__PHP_Incomplete_Class' ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Test unserialization with classes allowed. |
| 136 | * |
| 137 | * @requires PHP 7.0 |
| 138 | */ |
| 139 | public function test_maybe_unserialize_php7_allowed_classes() { |
| 140 | $foo = new Foo(); |
| 141 | $serialized_foo = serialize( $foo ); |
| 142 | $unseralized_foo = maybe_unserialize( $serialized_foo, array( |
| 143 | 'allowed_classes' => array( |
| 144 | 'Foo', |
| 145 | ), |
| 146 | ) ); |
| 147 | $this->assertEquals( get_class( $unseralized_foo ), 'Foo' ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Test unserialization with classes allowed. |
| 152 | * |
| 153 | * @requires PHP 7.0 |
| 154 | */ |
| 155 | public function test_maybe_unserialize_php7_filter_options() { |
| 156 | $foo = new Foo(); |
| 157 | $serialized_foo = serialize( $foo ); |
| 158 | add_filter( 'unserialization_options', array( $this, 'filter__maybe_unserialize_allowed_classes' ), 10, 3 ); |
| 159 | $unseralized_foo = maybe_unserialize( $serialized_foo ); |
| 160 | $this->assertEquals( get_class( $unseralized_foo ), 'Foo' ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Filter callback for maybe_unserialize options. |
| 165 | * |
| 166 | * @param array $options The options to be passed to the php unserialize(). |
| 167 | * @param string $original The contents to be unserialized. |
| 168 | * |
| 169 | * @return array The options to pass to the php unserialize(). |
| 170 | */ |
| 171 | public function filter__maybe_unserialize_allowed_classes( $options, $original ) { |
| 172 | $options = array( |
| 173 | 'allowed_classes' => array( |
| 174 | 'Foo', |
| 175 | ), |
| 176 | ); |
| 177 | return $options; |
| 178 | } |
| 179 | |
| 180 | } |