d4151a5c24f7834f737917b840c168b5d7ce768d
4 * @author Adam Shorland
7 class JsonContentTest
extends MediaWikiLangTestCase
{
9 public static function provideValidConstruction() {
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' ) ),
18 * @dataProvider provideValidConstruction
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() );
26 public static function provideDataToEncode() {
29 array( array( 'foo' ) ),
30 array( array( 'foo', 'bar' ) ),
31 array( array( 'baz' => 'foo', 'bar' ) ),
32 array( array( 'baz' => 1000, 'bar' ) ),
37 * @dataProvider provideDataToEncode
39 public function testBeautifyUsesFormatJson( $data ) {
40 $obj = new JsonContent( FormatJson
::encode( $data ) );
41 $this->assertEquals( FormatJson
::encode( $data, true ), $obj->beautifyJSON() );
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()
70 public static function provideDataAndParserText() {
74 '<table class="mw-json"><tbody></tbody></table>'
78 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr></tbody></table>'
81 array( 'foo', 'bar' ),
82 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
84 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
87 array( 'baz' => 'foo', 'bar' ),
88 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
90 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
93 array( 'baz' => 1000, 'bar' ),
94 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
96 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
99 array( '<script>alert("evil!")</script>'),
100 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"<script>alert("evil!")</script>"</td></tr></tbody></table>',
106 * @dataProvider provideDataAndParserText
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() );