wiki.techinc.nl/tests/phpunit/includes/content/JsonContentTest.php
Timo Tijhof 96771e3a65 test: Clean up data providers that should be static
Follows-up b36d883.

By far most data providers are static (and PHPUnit expects them
to be static and calls them that way).

Most of these classes already had their data providers static
but additional commits sloppily introduced non-static ones.

* ResourceLoaderWikiModuleTest, 8968d8787f.
* TitleTest, 545f1d3a73.
  Odd unused method 'dataTestIsValidMoveOperation' was introduced
  in 550b878e63.
* GlobalVarConfigTest, a3e18c3670.

Change-Id: I5da99f7cd3da68c550ae507ffe1f725d31e7666f
2014-09-18 12:52:44 -07:00

114 lines
3.5 KiB
PHP

<?php
/**
* @author Adam Shorland
* @covers JsonContent
*/
class JsonContentTest extends MediaWikiLangTestCase {
/**
* @dataProvider provideValidConstruction
*/
public function testValidConstruct( $text, $modelId, $isValid, $expected ) {
$obj = new JsonContent( $text, $modelId );
$this->assertEquals( $isValid, $obj->isValid() );
$this->assertEquals( $expected, $obj->getJsonData() );
}
public static function provideValidConstruction() {
return array(
array( 'foo', CONTENT_MODEL_JSON, false, null ),
array( FormatJson::encode( array() ), CONTENT_MODEL_JSON, true, array() ),
array( FormatJson::encode( array( 'foo' ) ), CONTENT_MODEL_JSON, true, array( 'foo' ) ),
);
}
/**
* @dataProvider provideDataToEncode
*/
public function testBeautifyUsesFormatJson( $data ) {
$obj = new JsonContent( FormatJson::encode( $data ) );
$this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() );
}
public static function provideDataToEncode() {
return array(
array( array() ),
array( array( 'foo' ) ),
array( array( 'foo', 'bar' ) ),
array( array( 'baz' => 'foo', 'bar' ) ),
array( array( 'baz' => 1000, 'bar' ) ),
);
}
/**
* @dataProvider provideDataToEncode
*/
public function testPreSaveTransform( $data ) {
$obj = new JsonContent( FormatJson::encode( $data ) );
$newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() );
$this->assertTrue( $newObj->equals( new JsonContent( FormatJson::encode( $data, true ) ) ) );
}
private function getMockTitle() {
return $this->getMockBuilder( 'Title' )
->disableOriginalConstructor()
->getMock();
}
private function getMockUser() {
return $this->getMockBuilder( 'User' )
->disableOriginalConstructor()
->getMock();
}
private function getMockParserOptions() {
return $this->getMockBuilder( 'ParserOptions' )
->disableOriginalConstructor()
->getMock();
}
/**
* @dataProvider provideDataAndParserText
*/
public function testFillParserOutput( $data, $expected ) {
$obj = new JsonContent( FormatJson::encode( $data ) );
$parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
$this->assertInstanceOf( 'ParserOutput', $parserOutput );
$this->assertEquals( $expected, $parserOutput->getText() );
}
public static function provideDataAndParserText() {
return array(
array(
array(),
'<table class="mw-json"><tbody></tbody></table>'
),
array(
array( 'foo' ),
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
),
array(
array( 'foo', 'bar' ),
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
"\n" .
'<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
),
array(
array( 'baz' => 'foo', 'bar' ),
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
"\n" .
'<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
),
array(
array( 'baz' => 1000, 'bar' ),
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
"\n" .
'<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
),
array(
array( '<script>alert("evil!")</script>'),
'<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>',
),
);
}
}