Merge "Use getHtmlCode() instead of getCode() to set the lang attribute"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / JsonContentTest.php
1 <?php
2
3 /**
4 * @author Adam Shorland
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 array(
17 array( 'foo', false, null ),
18 array( '{}', true, (object)array() ),
19 array( '{ "0": "bar" }', true, (object)array( 'bar' ) ),
20 );
21 }
22
23 /**
24 * @dataProvider provideValidConstruction
25 */
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() );
30 }
31
32 public static function provideDataToEncode() {
33 return array(
34 array(
35 // Round-trip empty array
36 '[]',
37 '[]',
38 ),
39 array(
40 // Round-trip empty object
41 '{}',
42 '{}',
43 ),
44 array(
45 // Round-trip empty array/object (nested)
46 '{ "foo": {}, "bar": [] }',
47 "{\n \"foo\": {},\n \"bar\": []\n}",
48 ),
49 array(
50 '{ "foo": "bar" }',
51 "{\n \"foo\": \"bar\"\n}",
52 ),
53 array(
54 '{ "foo": 1000 }',
55 "{\n \"foo\": 1000\n}",
56 ),
57 array(
58 '{ "foo": 1000, "0": "bar" }',
59 "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
60 ),
61 );
62 }
63
64 /**
65 * @dataProvider provideDataToEncode
66 */
67 public function testBeautifyJson( $input, $beautified ) {
68 $obj = new JsonContent( $input );
69 $this->assertEquals( $beautified, $obj->beautifyJSON() );
70 }
71
72 /**
73 * @dataProvider provideDataToEncode
74 */
75 public function testPreSaveTransform( $input, $transformed ) {
76 $obj = new JsonContent( $input );
77 $newObj = $obj->preSaveTransform(
78 $this->getMockTitle(),
79 $this->getMockUser(),
80 $this->getMockParserOptions()
81 );
82 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
83 }
84
85 private function getMockTitle() {
86 return $this->getMockBuilder( 'Title' )
87 ->disableOriginalConstructor()
88 ->getMock();
89 }
90
91 private function getMockUser() {
92 return $this->getMockBuilder( 'User' )
93 ->disableOriginalConstructor()
94 ->getMock();
95 }
96 private function getMockParserOptions() {
97 return $this->getMockBuilder( 'ParserOptions' )
98 ->disableOriginalConstructor()
99 ->getMock();
100 }
101
102 public static function provideDataAndParserText() {
103 return array(
104 array(
105 (object)array(),
106 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
107 '</tbody></table>'
108 ),
109 array(
110 (object)array( 'foo' ),
111 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
112 '</tbody></table>'
113 ),
114 array(
115 (object)array( 'foo', 'bar' ),
116 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
117 "\n" .
118 '<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
119 ),
120 array(
121 (object)array( 'baz' => 'foo', 'bar' ),
122 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
123 "\n" .
124 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
125 ),
126 array(
127 (object)array( 'baz' => 1000, 'bar' ),
128 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
129 "\n" .
130 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
131 ),
132 array(
133 (object)array( '<script>alert("evil!")</script>'),
134 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;' .
135 '&lt;script&gt;alert(&quot;evil!&quot;)&lt;/script&gt;&quot;' .
136 '</td></tr></tbody></table>',
137 ),
138 );
139 }
140
141 /**
142 * @dataProvider provideDataAndParserText
143 */
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() );
149 }
150 }