Merge "Run structure tests on extensions"
[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( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() );
78 $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
79 }
80
81 private function getMockTitle() {
82 return $this->getMockBuilder( 'Title' )
83 ->disableOriginalConstructor()
84 ->getMock();
85 }
86
87 private function getMockUser() {
88 return $this->getMockBuilder( 'User' )
89 ->disableOriginalConstructor()
90 ->getMock();
91 }
92 private function getMockParserOptions() {
93 return $this->getMockBuilder( 'ParserOptions' )
94 ->disableOriginalConstructor()
95 ->getMock();
96 }
97
98 public static function provideDataAndParserText() {
99 return array(
100 array(
101 (object)array(),
102 '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr></tbody></table>'
103 ),
104 array(
105 (object)array( 'foo' ),
106 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
107 ),
108 array(
109 (object)array( 'foo', 'bar' ),
110 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
111 "\n" .
112 '<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
113 ),
114 array(
115 (object)array( 'baz' => 'foo', 'bar' ),
116 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
117 "\n" .
118 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
119 ),
120 array(
121 (object)array( 'baz' => 1000, 'bar' ),
122 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</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( '<script>alert("evil!")</script>'),
128 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;&lt;script&gt;alert(&quot;evil!&quot;)&lt;/script&gt;&quot;</td></tr></tbody></table>',
129 ),
130 );
131 }
132
133 /**
134 * @dataProvider provideDataAndParserText
135 */
136 public function testFillParserOutput( $data, $expected ) {
137 $obj = new JsonContent( FormatJson::encode( $data ) );
138 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
139 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
140 $this->assertEquals( $expected, $parserOutput->getText() );
141 }
142 }