Merge "Switch name to username in @author tags"
[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 array(
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' ) ),
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 array(
39 array(
40 // Round-trip empty array
41 '[]',
42 '[]',
43 ),
44 array(
45 // Round-trip empty object
46 '{}',
47 '{}',
48 ),
49 array(
50 // Round-trip empty array/object (nested)
51 '{ "foo": {}, "bar": [] }',
52 "{\n \"foo\": {},\n \"bar\": []\n}",
53 ),
54 array(
55 '{ "foo": "bar" }',
56 "{\n \"foo\": \"bar\"\n}",
57 ),
58 array(
59 '{ "foo": 1000 }',
60 "{\n \"foo\": 1000\n}",
61 ),
62 array(
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 array(
109 array(
110 array(),
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 array(
116 (object)array(),
117 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
118 '</tbody></table>'
119 ),
120 array(
121 (object)array( 'foo' ),
122 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
123 '</tbody></table>'
124 ),
125 array(
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>'
129 ),
130 array(
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>'
134 ),
135 array(
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>'
139 ),
140 array(
141 (object)array( '<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 }