Merge "Add ability to pre-render thumbnails at upload time"
[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 /**
10 * @dataProvider provideValidConstruction
11 */
12 public function testValidConstruct( $text, $modelId, $isValid, $expected ) {
13 $obj = new JsonContent( $text, $modelId );
14 $this->assertEquals( $isValid, $obj->isValid() );
15 $this->assertEquals( $expected, $obj->getJsonData() );
16 }
17
18 public static function provideValidConstruction() {
19 return array(
20 array( 'foo', CONTENT_MODEL_JSON, false, null ),
21 array( FormatJson::encode( array() ), CONTENT_MODEL_JSON, true, array() ),
22 array( FormatJson::encode( array( 'foo' ) ), CONTENT_MODEL_JSON, true, array( 'foo' ) ),
23 );
24 }
25
26 /**
27 * @dataProvider provideDataToEncode
28 */
29 public function testBeautifyUsesFormatJson( $data ) {
30 $obj = new JsonContent( FormatJson::encode( $data ) );
31 $this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() );
32 }
33
34 public static function provideDataToEncode() {
35 return array(
36 array( array() ),
37 array( array( 'foo' ) ),
38 array( array( 'foo', 'bar' ) ),
39 array( array( 'baz' => 'foo', 'bar' ) ),
40 array( array( 'baz' => 1000, 'bar' ) ),
41 );
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 /**
71 * @dataProvider provideDataAndParserText
72 */
73 public function testFillParserOutput( $data, $expected ) {
74 $obj = new JsonContent( FormatJson::encode( $data ) );
75 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
76 $this->assertInstanceOf( 'ParserOutput', $parserOutput );
77 $this->assertEquals( $expected, $parserOutput->getText() );
78 }
79
80 public static function provideDataAndParserText() {
81 return array(
82 array(
83 array(),
84 '<table class="mw-json"><tbody></tbody></table>'
85 ),
86 array(
87 array( 'foo' ),
88 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
89 ),
90 array(
91 array( 'foo', 'bar' ),
92 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
93 "\n" .
94 '<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
95 ),
96 array(
97 array( 'baz' => 'foo', 'bar' ),
98 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
99 "\n" .
100 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
101 ),
102 array(
103 array( 'baz' => 1000, 'bar' ),
104 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
105 "\n" .
106 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
107 ),
108 array(
109 array( '<script>alert("evil!")</script>'),
110 '<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>',
111 ),
112 );
113 }
114 }