Merge "jquery.client: Recognize IE12 correctly"
[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 public static function provideValidConstruction() {
10 return array(
11 array( 'foo', CONTENT_MODEL_JSON, false, null ),
12 array( FormatJson::encode( array() ), CONTENT_MODEL_JSON, true, array() ),
13 array( FormatJson::encode( array( 'foo' ) ), CONTENT_MODEL_JSON, true, array( 'foo' ) ),
14 );
15 }
16
17 /**
18 * @dataProvider provideValidConstruction
19 */
20 public function testValidConstruct( $text, $modelId, $isValid, $expected ) {
21 $obj = new JsonContent( $text, $modelId );
22 $this->assertEquals( $isValid, $obj->isValid() );
23 $this->assertEquals( $expected, $obj->getJsonData() );
24 }
25
26 public static function provideDataToEncode() {
27 return array(
28 array( array() ),
29 array( array( 'foo' ) ),
30 array( array( 'foo', 'bar' ) ),
31 array( array( 'baz' => 'foo', 'bar' ) ),
32 array( array( 'baz' => 1000, 'bar' ) ),
33 );
34 }
35
36 /**
37 * @dataProvider provideDataToEncode
38 */
39 public function testBeautifyUsesFormatJson( $data ) {
40 $obj = new JsonContent( FormatJson::encode( $data ) );
41 $this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() );
42 }
43
44 /**
45 * @dataProvider provideDataToEncode
46 */
47 public function testPreSaveTransform( $data ) {
48 $obj = new JsonContent( FormatJson::encode( $data ) );
49 $newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() );
50 $this->assertTrue( $newObj->equals( new JsonContent( FormatJson::encode( $data, true ) ) ) );
51 }
52
53 private function getMockTitle() {
54 return $this->getMockBuilder( 'Title' )
55 ->disableOriginalConstructor()
56 ->getMock();
57 }
58
59 private function getMockUser() {
60 return $this->getMockBuilder( 'User' )
61 ->disableOriginalConstructor()
62 ->getMock();
63 }
64 private function getMockParserOptions() {
65 return $this->getMockBuilder( 'ParserOptions' )
66 ->disableOriginalConstructor()
67 ->getMock();
68 }
69
70 public static function provideDataAndParserText() {
71 return array(
72 array(
73 array(),
74 '<table class="mw-json"><tbody></tbody></table>'
75 ),
76 array(
77 array( 'foo' ),
78 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
79 ),
80 array(
81 array( 'foo', 'bar' ),
82 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
83 "\n" .
84 '<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
85 ),
86 array(
87 array( 'baz' => 'foo', 'bar' ),
88 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
89 "\n" .
90 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
91 ),
92 array(
93 array( 'baz' => 1000, 'bar' ),
94 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
95 "\n" .
96 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
97 ),
98 array(
99 array( '<script>alert("evil!")</script>'),
100 '<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>',
101 ),
102 );
103 }
104
105 /**
106 * @dataProvider provideDataAndParserText
107 */
108 public function testFillParserOutput( $data, $expected ) {
109 $obj = new JsonContent( FormatJson::encode( $data ) );
110 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
111 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
112 $this->assertEquals( $expected, $parserOutput->getText() );
113 }
114 }