Merge "Remove fix for a 5.3 problem"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / JsonContentTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 * @covers JsonContent
6 */
7 class JsonContentTest extends MediaWikiLangTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11
12 $this->setMwGlobals( 'wgWellFormedXml', true );
13 }
14
15 public static function provideValidConstruction() {
16 return [
17 [ 'foo', false, null ],
18 [ '[]', true, [] ],
19 [ '{}', true, (object)[] ],
20 [ '""', true, '' ],
21 [ '"0"', true, '0' ],
22 [ '"bar"', true, 'bar' ],
23 [ '0', true, '0' ],
24 [ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
25 ];
26 }
27
28 /**
29 * @dataProvider provideValidConstruction
30 */
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() );
35 }
36
37 public static function provideDataToEncode() {
38 return [
39 [
40 // Round-trip empty array
41 '[]',
42 '[]',
43 ],
44 [
45 // Round-trip empty object
46 '{}',
47 '{}',
48 ],
49 [
50 // Round-trip empty array/object (nested)
51 '{ "foo": {}, "bar": [] }',
52 "{\n \"foo\": {},\n \"bar\": []\n}",
53 ],
54 [
55 '{ "foo": "bar" }',
56 "{\n \"foo\": \"bar\"\n}",
57 ],
58 [
59 '{ "foo": 1000 }',
60 "{\n \"foo\": 1000\n}",
61 ],
62 [
63 '{ "foo": 1000, "0": "bar" }',
64 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
65 ],
66 ];
67 }
68
69 /**
70 * @dataProvider provideDataToEncode
71 */
72 public function testBeautifyJson( $input, $beautified ) {
73 $obj = new JsonContent( $input );
74 $this->assertEquals( $beautified, $obj->beautifyJSON() );
75 }
76
77 /**
78 * @dataProvider provideDataToEncode
79 */
80 public function testPreSaveTransform( $input, $transformed ) {
81 $obj = new JsonContent( $input );
82 $newObj = $obj->preSaveTransform(
83 $this->getMockTitle(),
84 $this->getMockUser(),
85 $this->getMockParserOptions()
86 );
87 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
88 }
89
90 private function getMockTitle() {
91 return $this->getMockBuilder( 'Title' )
92 ->disableOriginalConstructor()
93 ->getMock();
94 }
95
96 private function getMockUser() {
97 return $this->getMockBuilder( 'User' )
98 ->disableOriginalConstructor()
99 ->getMock();
100 }
101 private function getMockParserOptions() {
102 return $this->getMockBuilder( 'ParserOptions' )
103 ->disableOriginalConstructor()
104 ->getMock();
105 }
106
107 public static function provideDataAndParserText() {
108 return [
109 [
110 [],
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>'
114 ],
115 [
116 (object)[],
117 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
118 '</tbody></table>'
119 ],
120 [
121 (object)[ 'foo' ],
122 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
123 '</tbody></table>'
124 ],
125 [
126 (object)[ '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>'
129 ],
130 [
131 (object)[ '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>'
134 ],
135 [
136 (object)[ '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>'
139 ],
140 [
141 (object)[ '<script>alert("evil!")</script>' ],
142 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
143 '&lt;script>alert("evil!")&lt;/script>"' .
144 '</td></tr></tbody></table>',
145 ],
146 ];
147 }
148
149 /**
150 * @dataProvider provideDataAndParserText
151 */
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() );
157 }
158 }