7 class JsonContentTest extends MediaWikiLangTestCase {
9 public static function provideValidConstruction() {
11 [ 'foo', false, null ],
13 [ '{}', true, (object)[] ],
16 [ '"bar"', true, 'bar' ],
18 [ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
23 * @dataProvider provideValidConstruction
25 public function testIsValid( $text, $isValid, $expected ) {
26 $obj = new JsonContent( $text, CONTENT_MODEL_JSON );
27 $this->assertEquals( $isValid, $obj->isValid() );
28 $this->assertEquals( $expected, $obj->getData()->getValue() );
31 public static function provideDataToEncode() {
34 // Round-trip empty array
39 // Round-trip empty object
44 // Round-trip empty array/object (nested)
45 '{ "foo": {}, "bar": [] }',
46 "{\n \"foo\": {},\n \"bar\": []\n}",
50 "{\n \"foo\": \"bar\"\n}",
54 "{\n \"foo\": 1000\n}",
57 '{ "foo": 1000, "0": "bar" }',
58 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
64 * @dataProvider provideDataToEncode
66 public function testBeautifyJson( $input, $beautified ) {
67 $obj = new JsonContent( $input );
68 $this->assertEquals( $beautified, $obj->beautifyJSON() );
72 * @dataProvider provideDataToEncode
74 public function testPreSaveTransform( $input, $transformed ) {
75 $obj = new JsonContent( $input );
76 $newObj = $obj->preSaveTransform(
77 $this->getMockTitle(),
79 $this->getMockParserOptions()
81 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
84 private function getMockTitle() {
85 return $this->getMockBuilder( 'Title' )
86 ->disableOriginalConstructor()
90 private function getMockUser() {
91 return $this->getMockBuilder( 'User' )
92 ->disableOriginalConstructor()
95 private function getMockParserOptions() {
96 return $this->getMockBuilder( 'ParserOptions' )
97 ->disableOriginalConstructor()
101 public static function provideDataAndParserText() {
105 '<table class="mw-json"><tbody><tr><td>' .
106 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
107 . '</tbody></table></td></tr></tbody></table>'
111 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
116 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
120 (object)[ 'foo', 'bar' ],
121 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
122 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
125 (object)[ 'baz' => 'foo', 'bar' ],
126 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
127 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
130 (object)[ 'baz' => 1000, 'bar' ],
131 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
132 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
135 (object)[ '<script>alert("evil!")</script>' ],
136 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
137 '<script>alert("evil!")</script>"' .
138 '</td></tr></tbody></table>',
144 * @dataProvider provideDataAndParserText
146 public function testFillParserOutput( $data, $expected ) {
147 $obj = new JsonContent( FormatJson::encode( $data ) );
148 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
149 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
150 $this->assertEquals( $expected, $parserOutput->getText() );