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, (object)array() ),
19 array( '{ "0": "bar" }', true, (object)array( 'bar' ) ),
24 * @dataProvider provideValidConstruction
26 public function testIsValid( $text, $isValid, $expected ) {
27 $obj = new JsonContent( $text, CONTENT_MODEL_JSON
);
28 $this->assertEquals( $isValid, $obj->isValid() );
29 $this->assertEquals( $expected, $obj->getData()->getValue() );
32 public static function provideDataToEncode() {
35 // Round-trip empty array
40 // Round-trip empty object
45 // Round-trip empty array/object (nested)
46 '{ "foo": {}, "bar": [] }',
47 "{\n \"foo\": {},\n \"bar\": []\n}",
51 "{\n \"foo\": \"bar\"\n}",
55 "{\n \"foo\": 1000\n}",
58 '{ "foo": 1000, "0": "bar" }',
59 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
65 * @dataProvider provideDataToEncode
67 public function testBeautifyJson( $input, $beautified ) {
68 $obj = new JsonContent( $input );
69 $this->assertEquals( $beautified, $obj->beautifyJSON() );
73 * @dataProvider provideDataToEncode
75 public function testPreSaveTransform( $input, $transformed ) {
76 $obj = new JsonContent( $input );
77 $newObj = $obj->preSaveTransform(
78 $this->getMockTitle(),
80 $this->getMockParserOptions()
82 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
85 private function getMockTitle() {
86 return $this->getMockBuilder( 'Title' )
87 ->disableOriginalConstructor()
91 private function getMockUser() {
92 return $this->getMockBuilder( 'User' )
93 ->disableOriginalConstructor()
96 private function getMockParserOptions() {
97 return $this->getMockBuilder( 'ParserOptions' )
98 ->disableOriginalConstructor()
102 public static function provideDataAndParserText() {
106 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
110 (object)array( 'foo' ),
111 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
115 (object)array( 'foo', 'bar' ),
116 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
118 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
121 (object)array( 'baz' => 'foo', 'bar' ),
122 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
124 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
127 (object)array( 'baz' => 1000, 'bar' ),
128 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
130 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
133 (object)array( '<script>alert("evil!")</script>'),
134 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
135 '<script>alert("evil!")</script>"' .
136 '</td></tr></tbody></table>',
142 * @dataProvider provideDataAndParserText
144 public function testFillParserOutput( $data, $expected ) {
145 $obj = new JsonContent( FormatJson
::encode( $data ) );
146 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
147 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
148 $this->assertEquals( $expected, $parserOutput->getText() );