| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Json; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use JsonException; |
| 7 |
use JsonSerializable; |
| 8 |
use MediaWiki\Json\FormatJson; |
| 9 |
use MediaWiki\Json\JsonCodec; |
| 10 |
use MediaWiki\Json\JsonConstants; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\User\UserIdentityValue; |
| 13 |
use MediaWikiUnitTestCase; |
| 14 |
use Psr\Container\ContainerInterface; |
| 15 |
use Wikimedia\Assert\PreconditionException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class JsonCodecTest extends MediaWikiUnitTestCase { |
| 22 |
|
| 23 |
|
| 24 |
private static function getServices(): ContainerInterface { |
| 25 |
static $services = null; |
| 26 |
if ( !$services ) { |
| 27 |
$services = new class implements ContainerInterface { |
| 28 |
private $storage = []; |
| 29 |
|
| 30 |
public function get( $id ) { |
| 31 |
return $this->storage[$id]; |
| 32 |
} |
| 33 |
|
| 34 |
public function has( $id ): bool { |
| 35 |
return isset( $this->storage[$id] ); |
| 36 |
} |
| 37 |
|
| 38 |
public function set( $id, $value ) { |
| 39 |
$this->storage[$id] = $value; |
| 40 |
} |
| 41 |
}; |
| 42 |
$factory = new ManagedObjectFactory(); |
| 43 |
$factory->create( "a", 1 ); |
| 44 |
$factory->create( "b", 2 ); |
| 45 |
$services->set( 'ManagedObjectFactory', $factory ); |
| 46 |
} |
| 47 |
return $services; |
| 48 |
} |
| 49 |
|
| 50 |
private function getCodec(): JsonCodec { |
| 51 |
return new JsonCodec( self::getServices() ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function provideSimpleTypes() { |
| 55 |
yield 'Integer' => [ 1, json_encode( 1 ) ]; |
| 56 |
yield 'Boolean' => [ true, json_encode( true ) ]; |
| 57 |
yield 'Null' => [ null, json_encode( null ) ]; |
| 58 |
yield 'Array' => [ [ 1, 2, 3 ], json_encode( [ 1, 2, 3 ] ) ]; |
| 59 |
yield 'Assoc array' => [ [ 'a' => 'b' ], json_encode( [ 'a' => 'b' ] ) ]; |
| 60 |
|
| 61 |
$object = (object)[ 'c' => 'd' ]; |
| 62 |
yield 'Object' => [ (array)$object, json_encode( $object ) ]; |
| 63 |
} |
| 64 |
|
| 65 |
public static function provideInvalidJsonData() { |
| 66 |
yield 'Bad string' => [ 'bad string' ]; |
| 67 |
yield 'No deserialization metadata' => [ [ 'test' => 'test' ] ]; |
| 68 |
yield 'Deserialization metadata, but class not exist' => [ [ |
| 69 |
JsonConstants::TYPE_ANNOTATION => 'BadClassNotExist' |
| 70 |
] ]; |
| 71 |
yield 'Deserialization metadata, but class is not JsonDeserializable' => [ [ |
| 72 |
JsonConstants::TYPE_ANNOTATION => Title::class |
| 73 |
] ]; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function testSimpleTypesDeserialize( $value, string $serialization ) { |
| 80 |
$this->assertSame( $value, $this->getCodec()->deserialize( $serialization ) ); |
| 81 |
} |
| 82 |
|
| 83 |
public function testActualClassInstanceIsNotJsonObject() { |
| 84 |
|
| 85 |
$object = UserIdentityValue::newAnonymous( '' ); |
| 86 |
|
| 87 |
|
| 88 |
$array = [ $object ]; |
| 89 |
$this->assertSame( $array, $this->getCodec()->deserialize( $array ) ); |
| 90 |
|
| 91 |
$this->expectException( InvalidArgumentException::class ); |
| 92 |
$this->getCodec()->deserialize( $object ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function testInvalidJsonDataForClassExpectation( $jsonData, $simpleJsonData = null ) { |
| 100 |
$jsonData = $simpleJsonData ?? $jsonData; |
| 101 |
$this->expectException( JsonException::class ); |
| 102 |
$this->getCodec()->deserialize( $jsonData, JsonDeserializableSuperClass::class ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testExpectedClassMustBeDeserializable() { |
| 106 |
$this->expectException( PreconditionException::class ); |
| 107 |
$this->getCodec()->deserialize( '{}', self::class ); |
| 108 |
} |
| 109 |
|
| 110 |
public function testUnexpectedClassDeserialized() { |
| 111 |
$this->expectException( JsonException::class ); |
| 112 |
$superClassInstance = new JsonDeserializableSuperClass( 'Godzilla' ); |
| 113 |
$this->getCodec()->deserialize( |
| 114 |
$superClassInstance->jsonSerialize(), |
| 115 |
JsonDeserializableSubClass::class |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
public function testExpectedClassDeserialized() { |
| 120 |
$subClassInstance = new JsonDeserializableSubClass( 'Godzilla', 'But we are ready!' ); |
| 121 |
$this->assertNotNull( $this->getCodec()->deserialize( |
| 122 |
$subClassInstance->jsonSerialize(), |
| 123 |
JsonDeserializableSuperClass::class |
| 124 |
) ); |
| 125 |
$this->assertNotNull( $this->getCodec()->deserialize( |
| 126 |
$subClassInstance->jsonSerialize(), |
| 127 |
JsonDeserializableSubClass::class |
| 128 |
) ); |
| 129 |
} |
| 130 |
|
| 131 |
public function testRoundTripSuperClass() { |
| 132 |
$superClassInstance = new JsonDeserializableSuperClass( 'Super Value' ); |
| 133 |
$json = $superClassInstance->jsonSerialize(); |
| 134 |
$superClassDeserialized = $this->getCodec()->deserialize( $json ); |
| 135 |
$this->assertInstanceOf( JsonDeserializableSuperClass::class, $superClassInstance ); |
| 136 |
$this->assertSame( $superClassInstance->getSuperClassField(), $superClassDeserialized->getSuperClassField() ); |
| 137 |
} |
| 138 |
|
| 139 |
public function testRoundTripSuperClassObject() { |
| 140 |
$superClassInstance = new JsonDeserializableSuperClass( 'Super Value' ); |
| 141 |
$json = (object)$superClassInstance->jsonSerialize(); |
| 142 |
$superClassDeserialized = $this->getCodec()->deserialize( $json ); |
| 143 |
$this->assertInstanceOf( JsonDeserializableSuperClass::class, $superClassInstance ); |
| 144 |
$this->assertInstanceOf( JsonDeserializableSuperClass::class, $superClassDeserialized ); |
| 145 |
$this->assertSame( $superClassInstance->getSuperClassField(), $superClassDeserialized->getSuperClassField() ); |
| 146 |
} |
| 147 |
|
| 148 |
public function testRoundTripSubClass() { |
| 149 |
$subClassInstance = new JsonDeserializableSubClass( 'Super Value', 'Sub Value' ); |
| 150 |
$json = $subClassInstance->jsonSerialize(); |
| 151 |
$superClassDeserialized = $this->getCodec()->deserialize( $json ); |
| 152 |
$this->assertInstanceOf( JsonDeserializableSubClass::class, $subClassInstance ); |
| 153 |
$this->assertSame( $subClassInstance->getSuperClassField(), $superClassDeserialized->getSuperClassField() ); |
| 154 |
$this->assertSame( $subClassInstance->getSubClassField(), $superClassDeserialized->getSubClassField() ); |
| 155 |
} |
| 156 |
|
| 157 |
public function testRoundTripSubClassNested() { |
| 158 |
$subClassInstance1 = new JsonDeserializableSubClass( 'Super Value', 'Sub Value' ); |
| 159 |
$superClassInstance1 = new JsonDeserializableSuperClass( 'XYZ' ); |
| 160 |
$superClassInstance2 = new JsonDeserializableSuperClass( |
| 161 |
|
| 162 |
[ $superClassInstance1 ] |
| 163 |
); |
| 164 |
$subClassInstance2 = new JsonDeserializableSubClass( |
| 165 |
$subClassInstance1, |
| 166 |
|
| 167 |
(object)[ 'a' => $superClassInstance1 ] |
| 168 |
); |
| 169 |
$json = $this->getCodec()->serialize( $subClassInstance2 ); |
| 170 |
$deserialized = $this->getCodec()->deserialize( $json ); |
| 171 |
$this->assertEquals( $subClassInstance2, $deserialized ); |
| 172 |
} |
| 173 |
|
| 174 |
public function testArrayRoundTrip() { |
| 175 |
$array = [ |
| 176 |
new JsonDeserializableSuperClass( 'Super Value' ), |
| 177 |
new JsonDeserializableSubClass( 'Super Value', 'Sub Value' ), |
| 178 |
42 |
| 179 |
]; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
$serialized = FormatJson::encode( $array ); |
| 187 |
$deserialized = $this->getCodec()->deserializeArray( FormatJson::decode( $serialized ) ); |
| 188 |
$this->assertArrayEquals( $array, $deserialized ); |
| 189 |
} |
| 190 |
|
| 191 |
public static function provideValidateSerializable() { |
| 192 |
$classInstance = new class() { |
| 193 |
}; |
| 194 |
$serializableClass = new class() implements JsonSerializable { |
| 195 |
public function jsonSerialize(): array { |
| 196 |
return []; |
| 197 |
} |
| 198 |
}; |
| 199 |
$badSerializable = new class() implements JsonSerializable { |
| 200 |
public function jsonSerialize(): \stdClass { |
| 201 |
return (object)[]; |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
yield 'Number' => [ 1, true, null ]; |
| 206 |
yield 'Null' => [ null, true, null ]; |
| 207 |
yield 'Class' => [ $classInstance, false, '$' ]; |
| 208 |
yield 'Empty array' => [ [], true, null ]; |
| 209 |
yield 'Empty stdClass' => [ (object)[], true, null ]; |
| 210 |
yield 'Non-empty array' => [ [ 1, 2, 3 ], true, null ]; |
| 211 |
yield 'Non-empty map' => [ [ 'a' => 'b' ], true, null ]; |
| 212 |
yield 'Nested stdClass' => [ [ 'a' => [ 'b' => (object)[] ] ], true, null ]; |
| 213 |
yield 'Nested, serializable' => [ [ 'a' => [ 'b' => [ 'c' => 'd' ] ] ], true, null ]; |
| 214 |
yield 'Nested, serializable, with null' => [ [ 'a' => [ 'b' => null ] ], true, null ]; |
| 215 |
yield 'Nested, serializable, with stdClass' => [ [ 'a' => (object)[ 'b' => [ 'c' => 'd' ] ] ], true, null ]; |
| 216 |
yield 'Nested, serializable, with stdClass, with null' => [ [ 'a' => (object)[ 'b' => null ] ], true, null ]; |
| 217 |
yield 'Nested, non-serializable' => [ [ 'a' => [ 'b' => $classInstance ] ], true, '$.a.b' ]; |
| 218 |
yield 'Nested, non-serializable, in array' => [ [ 'a' => [ 1, 2, $classInstance ] ], true, '$.a.2' ]; |
| 219 |
yield 'Nested, non-serializable, in stdClass' => [ [ 'a' => (object)[ 1, 2, $classInstance ] ], true, '$.a.2' ]; |
| 220 |
yield 'Nested, non-serializable, in JsonDeserializable' => [ new JsonDeserializableSuperClass( $classInstance ), true, '$.super_class_field' ]; |
| 221 |
yield 'JsonDeserializable instance' => [ new JsonDeserializableSuperClass( 'Test' ), true, null ]; |
| 222 |
yield 'JsonDeserializable instance, in array' => |
| 223 |
[ [ new JsonDeserializableSuperClass( 'Test' ) ], true, null ]; |
| 224 |
yield 'JsonDeserializable instance, in stdClass' => |
| 225 |
[ (object)[ new JsonDeserializableSuperClass( 'Test' ) ], true, null ]; |
| 226 |
yield 'JsonDeserializable instance, in JsonCodecable' => |
| 227 |
[ new SampleContainerObject( new JsonDeserializableSuperClass( 'Test' ) ), true, null ]; |
| 228 |
yield 'JsonSerializable instance' => [ $serializableClass, false, null ]; |
| 229 |
yield 'JsonSerializable instance, in array' => [ [ $serializableClass ], false, null ]; |
| 230 |
yield 'JsonSerializable instance, in stdClass' => [ (object)[ $serializableClass ], false, null ]; |
| 231 |
yield 'JsonSerializable instance, in JsonCodecable' => [ new SampleContainerObject( $serializableClass ), false, null ]; |
| 232 |
yield 'JsonSerializable instance, expect deserialize' => [ $serializableClass, true, '$' ]; |
| 233 |
yield 'JsonSerializable instance, in array, expect deserialize' => [ [ $serializableClass ], true, '$.0' ]; |
| 234 |
yield 'JsonSerializable instance, in stdClass, expect deserialize' => |
| 235 |
[ (object)[ $serializableClass ], true, '$.0' ]; |
| 236 |
yield 'Bad JsonSerializable instance' => [ $badSerializable, false, '$' ]; |
| 237 |
|
| 238 |
yield 'JsonCodecable instance' => [ new SampleObject( 'a' ), true, null ]; |
| 239 |
yield 'JsonCodecable instance, in array' => |
| 240 |
[ [ new SampleObject( '123' ) ], true, null ]; |
| 241 |
yield 'JsonCodecable instance, in stdClass' => |
| 242 |
[ (object)[ new SampleObject( 'Test' ) ], true, null ]; |
| 243 |
yield 'JsonCodecable instance, in JsonCodecable' => [ |
| 244 |
new SampleContainerObject( new SampleObject( '123' ) ), true, null |
| 245 |
]; |
| 246 |
|
| 247 |
$factory = self::getServices()->get( 'ManagedObjectFactory' ); |
| 248 |
yield 'Managed instance' => [ $factory->lookup( 'a' ), true, null ]; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function testValidateSerializable( $value, bool $expectDeserialize, ?string $expected ) { |
| 257 |
$actual = $this->getCodec() |
| 258 |
->detectNonSerializableData( $value, $expectDeserialize ); |
| 259 |
|
| 260 |
if ( $actual !== null ) { |
| 261 |
$actual = preg_replace( '/:.*$/', '', $actual ); |
| 262 |
} |
| 263 |
$this->assertSame( $expected, $actual ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function testValidateSerializable2( $value, bool $expectDeserialize, ?string $result ) { |
| 272 |
if ( $result !== null || !$expectDeserialize ) { |
| 273 |
$this->assertTrue( true ); |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
$json = $this->getCodec()->serialize( $value ); |
| 279 |
$newValue = $this->getCodec()->deserialize( $json ); |
| 280 |
$this->assertEquals( $value, $newValue ); |
| 281 |
} |
| 282 |
|
| 283 |
public static function provideSerializeThrowsOnFailure() { |
| 284 |
$classInstance = new class() { |
| 285 |
}; |
| 286 |
yield 'non-serializable class' => [ $classInstance ]; |
| 287 |
yield 'invalid UTF-8' => [ |
| 288 |
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcb\x48\xcd\xc9\xc9\x57\x28\xcf\x2f" |
| 289 |
. "\xca\x49\x01\x00\x85\x11\x4a\x0d\x0b\x00\x00\x00" |
| 290 |
]; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public function testSerializeThrowsOnFailure( $value ) { |
| 298 |
$this->expectException( JsonException::class ); |
| 299 |
$this->getCodec()->serialize( $value ); |
| 300 |
} |
| 301 |
|
| 302 |
public static function provideSerializeSuccess() { |
| 303 |
$serializableInstance = new class() implements JsonSerializable { |
| 304 |
public function jsonSerialize(): array { |
| 305 |
return [ 'c' => 'd' ]; |
| 306 |
} |
| 307 |
}; |
| 308 |
yield 'array' => [ [ 'a' => 'b' ], '{"a":"b"}' ]; |
| 309 |
yield 'JsonSerializable' => [ |
| 310 |
$serializableInstance, |
| 311 |
json_encode( [ "c" => "d", "_type_" => get_class( $serializableInstance ), "_complex_" => true ], JSON_UNESCAPED_SLASHES ) |
| 312 |
]; |
| 313 |
yield 'JsonCodecable' => [ new SampleObject( 'a' ), json_encode( [ |
| 314 |
'property' => 'a', |
| 315 |
'_type_' => SampleObject::class, |
| 316 |
'_complex_' => true, |
| 317 |
] ) ]; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public function testSerializeSuccess( $value, string $expected ) { |
| 325 |
$this->assertSame( $expected, $this->getCodec()->serialize( $value ) ); |
| 326 |
} |
| 327 |
|
| 328 |
public function testManagedObjects() { |
| 329 |
$codec = $this->getCodec(); |
| 330 |
$factory = self::getServices()->get( 'ManagedObjectFactory' ); |
| 331 |
$a = $factory->lookup( 'a' ); |
| 332 |
$s = $codec->serialize( $a ); |
| 333 |
$v = $codec->deserialize( $s ); |
| 334 |
|
| 335 |
$this->assertSame( $a, $v ); |
| 336 |
} |
| 337 |
|
| 338 |
public function testCodecableAliases() { |
| 339 |
$codec = $this->getCodec(); |
| 340 |
|
| 341 |
|
| 342 |
$json = '{"property":"alias!","_type_":"MediaWiki\\\\Tests\\\\Json\\\\SampleObjectAlias","_complex_":true}'; |
| 343 |
$v = $codec->deserialize( $json, SampleObject::class ); |
| 344 |
$this->assertInstanceOf( SampleObject::class, $v ); |
| 345 |
} |
| 346 |
|
| 347 |
public function testJsonDeserializableAliases() { |
| 348 |
$codec = $this->getCodec(); |
| 349 |
|
| 350 |
|
| 351 |
$json = '{"super_class_field":1,"sub_class_field":"2","_type_":"MediaWiki\\\\Tests\\\\Json\\\\JsonDeserializableSubClassAlias","_complex_":true}'; |
| 352 |
$v = $codec->deserialize( $json, JsonDeserializableSubClass::class ); |
| 353 |
$this->assertInstanceOf( JsonDeserializableSubClass::class, $v ); |
| 354 |
} |
| 355 |
} |