Merge "Use camel case for variable names in Article.php"
[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 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 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 // var_dump( $parserOutput->getText(), "\n" );
78 $this->assertEquals( $expected, $parserOutput->getText() );
79 }
80
81 public function provideDataAndParserText() {
82 return array(
83 array(
84 array(),
85 '<table class="mw-json"><tbody></tbody></table>'
86 ),
87 array(
88 array( 'foo' ),
89 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
90 ),
91 array(
92 array( 'foo', 'bar' ),
93 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
94 "\n" .
95 '<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
96 ),
97 array(
98 array( 'baz' => 'foo', 'bar' ),
99 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
100 "\n" .
101 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
102 ),
103 array(
104 array( 'baz' => 1000, 'bar' ),
105 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
106 "\n" .
107 '<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
108 ),
109 array(
110 array( '<script>alert("evil!")</script>'),
111 '<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>',
112 ),
113 );
114 }
115 }