4 * @author Adam Shorland
7 class JsonContentTest
extends MediaWikiLangTestCase
{
10 * @dataProvider provideValidConstruction
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() );
18 public static function provideValidConstruction() {
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' ) ),
27 * @dataProvider provideDataToEncode
29 public function testBeautifyUsesFormatJson( $data ) {
30 $obj = new JsonContent( FormatJson
::encode( $data ) );
31 $this->assertEquals( FormatJson
::encode( $data, true ), $obj->beautifyJSON() );
34 public static function provideDataToEncode() {
37 array( array( 'foo' ) ),
38 array( array( 'foo', 'bar' ) ),
39 array( array( 'baz' => 'foo', 'bar' ) ),
40 array( array( 'baz' => 1000, 'bar' ) ),
45 * @dataProvider provideDataToEncode
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 ) ) ) );
53 private function getMockTitle() {
54 return $this->getMockBuilder( 'Title' )
55 ->disableOriginalConstructor()
59 private function getMockUser() {
60 return $this->getMockBuilder( 'User' )
61 ->disableOriginalConstructor()
64 private function getMockParserOptions() {
65 return $this->getMockBuilder( 'ParserOptions' )
66 ->disableOriginalConstructor()
71 * @dataProvider provideDataAndParserText
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() );
80 public static function provideDataAndParserText() {
84 '<table class="mw-json"><tbody></tbody></table>'
88 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr></tbody></table>'
91 array( 'foo', 'bar' ),
92 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
94 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
97 array( 'baz' => 'foo', 'bar' ),
98 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
100 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
103 array( 'baz' => 1000, 'bar' ),
104 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
106 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
109 array( '<script>alert("evil!")</script>'),
110 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"<script>alert("evil!")</script>"</td></tr></tbody></table>',