MediaWiki
REL1_24
|
00001 <?php 00002 00007 class JsonContentTest extends MediaWikiLangTestCase { 00008 00012 public function testValidConstruct( $text, $modelId, $isValid, $expected ) { 00013 $obj = new JsonContent( $text, $modelId ); 00014 $this->assertEquals( $isValid, $obj->isValid() ); 00015 $this->assertEquals( $expected, $obj->getJsonData() ); 00016 } 00017 00018 public static function provideValidConstruction() { 00019 return array( 00020 array( 'foo', CONTENT_MODEL_JSON, false, null ), 00021 array( FormatJson::encode( array() ), CONTENT_MODEL_JSON, true, array() ), 00022 array( FormatJson::encode( array( 'foo' ) ), CONTENT_MODEL_JSON, true, array( 'foo' ) ), 00023 ); 00024 } 00025 00029 public function testBeautifyUsesFormatJson( $data ) { 00030 $obj = new JsonContent( FormatJson::encode( $data ) ); 00031 $this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() ); 00032 } 00033 00034 public static function provideDataToEncode() { 00035 return array( 00036 array( array() ), 00037 array( array( 'foo' ) ), 00038 array( array( 'foo', 'bar' ) ), 00039 array( array( 'baz' => 'foo', 'bar' ) ), 00040 array( array( 'baz' => 1000, 'bar' ) ), 00041 ); 00042 } 00043 00047 public function testPreSaveTransform( $data ) { 00048 $obj = new JsonContent( FormatJson::encode( $data ) ); 00049 $newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() ); 00050 $this->assertTrue( $newObj->equals( new JsonContent( FormatJson::encode( $data, true ) ) ) ); 00051 } 00052 00053 private function getMockTitle() { 00054 return $this->getMockBuilder( 'Title' ) 00055 ->disableOriginalConstructor() 00056 ->getMock(); 00057 } 00058 00059 private function getMockUser() { 00060 return $this->getMockBuilder( 'User' ) 00061 ->disableOriginalConstructor() 00062 ->getMock(); 00063 } 00064 private function getMockParserOptions() { 00065 return $this->getMockBuilder( 'ParserOptions' ) 00066 ->disableOriginalConstructor() 00067 ->getMock(); 00068 } 00069 00073 public function testFillParserOutput( $data, $expected ) { 00074 $obj = new JsonContent( FormatJson::encode( $data ) ); 00075 $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true ); 00076 $this->assertInstanceOf( 'ParserOutput', $parserOutput ); 00077 $this->assertEquals( $expected, $parserOutput->getText() ); 00078 } 00079 00080 public static function provideDataAndParserText() { 00081 return array( 00082 array( 00083 array(), 00084 '<table class="mw-json"><tbody></tbody></table>' 00085 ), 00086 array( 00087 array( 'foo' ), 00088 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr></tbody></table>' 00089 ), 00090 array( 00091 array( 'foo', 'bar' ), 00092 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' . 00093 "\n" . 00094 '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>' 00095 ), 00096 array( 00097 array( 'baz' => 'foo', 'bar' ), 00098 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' . 00099 "\n" . 00100 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>' 00101 ), 00102 array( 00103 array( 'baz' => 1000, 'bar' ), 00104 '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' . 00105 "\n" . 00106 '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>' 00107 ), 00108 array( 00109 array( '<script>alert("evil!")</script>'), 00110 '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"<script>alert("evil!")</script>"</td></tr></tbody></table>', 00111 ), 00112 ); 00113 } 00114 }