4 * @author Adam Shorland
7 class JsonContentTest
extends MediaWikiLangTestCase
{
9 protected function setUp() {
12 $this->setMwGlobals( 'wgWellFormedXml', true );
15 public static function provideValidConstruction() {
17 array( 'foo', false, null ),
18 array( '[]', true, array() ),
19 array( '{}', true, (object)array() ),
20 array( '""', true, '' ),
21 array( '"0"', true, '0' ),
22 array( '"bar"', true, 'bar' ),
23 array( '0', true, '0' ),
24 array( '{ "0": "bar" }', true, (object)array( 'bar' ) ),
29 * @dataProvider provideValidConstruction
31 public function testIsValid( $text, $isValid, $expected ) {
32 $obj = new JsonContent( $text, CONTENT_MODEL_JSON
);
33 $this->assertEquals( $isValid, $obj->isValid() );
34 $this->assertEquals( $expected, $obj->getData()->getValue() );
37 public static function provideDataToEncode() {
40 // Round-trip empty array
45 // Round-trip empty object
50 // Round-trip empty array/object (nested)
51 '{ "foo": {}, "bar": [] }',
52 "{\n \"foo\": {},\n \"bar\": []\n}",
56 "{\n \"foo\": \"bar\"\n}",
60 "{\n \"foo\": 1000\n}",
63 '{ "foo": 1000, "0": "bar" }',
64 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
70 * @dataProvider provideDataToEncode
72 public function testBeautifyJson( $input, $beautified ) {
73 $obj = new JsonContent( $input );
74 $this->assertEquals( $beautified, $obj->beautifyJSON() );
78 * @dataProvider provideDataToEncode
80 public function testPreSaveTransform( $input, $transformed ) {
81 $obj = new JsonContent( $input );
82 $newObj = $obj->preSaveTransform(
83 $this->getMockTitle(),
85 $this->getMockParserOptions()
87 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
90 private function getMockTitle() {
91 return $this->getMockBuilder( 'Title' )
92 ->disableOriginalConstructor()
96 private function getMockUser() {
97 return $this->getMockBuilder( 'User' )
98 ->disableOriginalConstructor()
101 private function getMockParserOptions() {
102 return $this->getMockBuilder( 'ParserOptions' )
103 ->disableOriginalConstructor()
107 public static function provideDataAndParserText() {
111 '<table class="mw-json"><tbody><tr><td>' .
112 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
113 . '</tbody></table></td></tr></tbody></table>'
117 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
121 (object)array( 'foo' ),
122 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
126 (object)array( 'foo', 'bar' ),
127 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
128 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
131 (object)array( 'baz' => 'foo', 'bar' ),
132 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
133 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
136 (object)array( 'baz' => 1000, 'bar' ),
137 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
138 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
141 (object)array( '<script>alert("evil!")</script>' ),
142 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
143 '<script>alert("evil!")</script>"' .
144 '</td></tr></tbody></table>',
150 * @dataProvider provideDataAndParserText
152 public function testFillParserOutput( $data, $expected ) {
153 $obj = new JsonContent( FormatJson
::encode( $data ) );
154 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
155 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
156 $this->assertEquals( $expected, $parserOutput->getText() );