| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Content\JsonContent; |
| 6 |
use MediaWikiUnitTestCase; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class JsonContentTest extends MediaWikiUnitTestCase { |
| 15 |
|
| 16 |
public static function provideValidConstruction() { |
| 17 |
return [ |
| 18 |
[ 'foo', false, null ], |
| 19 |
[ '[]', true, [] ], |
| 20 |
[ '{}', true, (object)[] ], |
| 21 |
[ '""', true, '' ], |
| 22 |
[ '"0"', true, '0' ], |
| 23 |
[ '"bar"', true, 'bar' ], |
| 24 |
[ '0', true, '0' ], |
| 25 |
[ '{ "0": "bar" }', true, (object)[ 'bar' ] ], |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function testIsValid( $text, $isValid, $expected ) { |
| 33 |
$obj = new JsonContent( $text, CONTENT_MODEL_JSON ); |
| 34 |
$this->assertEquals( $isValid, $obj->isValid() ); |
| 35 |
$this->assertEquals( $expected, $obj->getData()->getValue() ); |
| 36 |
} |
| 37 |
|
| 38 |
public static function provideDataToEncode() { |
| 39 |
return [ |
| 40 |
[ |
| 41 |
|
| 42 |
'[]', |
| 43 |
'[]', |
| 44 |
], |
| 45 |
[ |
| 46 |
|
| 47 |
'{}', |
| 48 |
'{}', |
| 49 |
], |
| 50 |
[ |
| 51 |
|
| 52 |
'{ "foo": {}, "bar": [] }', |
| 53 |
"{\n\t\"foo\": {},\n\t\"bar\": []\n}", |
| 54 |
], |
| 55 |
[ |
| 56 |
'{ "foo": "bar" }', |
| 57 |
"{\n\t\"foo\": \"bar\"\n}", |
| 58 |
], |
| 59 |
[ |
| 60 |
'{ "foo": 1000 }', |
| 61 |
"{\n\t\"foo\": 1000\n}", |
| 62 |
], |
| 63 |
[ |
| 64 |
'{ "foo": 1000, "0": "bar" }', |
| 65 |
"{\n\t\"foo\": 1000,\n\t\"0\": \"bar\"\n}", |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testBeautifyJson( $input, $beautified ) { |
| 74 |
$obj = new JsonContent( $input ); |
| 75 |
$this->assertEquals( $beautified, $obj->beautifyJSON() ); |
| 76 |
} |
| 77 |
} |