2014-08-08 16:41:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Adam Shorland
|
2014-09-15 08:17:29 +00:00
|
|
|
* @covers JsonContent
|
2014-08-08 16:41:26 +00:00
|
|
|
*/
|
2014-09-15 08:17:29 +00:00
|
|
|
class JsonContentTest extends MediaWikiLangTestCase {
|
2014-08-08 16:41:26 +00:00
|
|
|
|
2014-12-03 02:10:50 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgWellFormedXml', true );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideValidConstruction() {
|
2014-08-08 16:41:26 +00:00
|
|
|
return array(
|
2014-12-03 02:10:50 +00:00
|
|
|
array( 'foo', false, null ),
|
2015-01-16 03:25:15 +00:00
|
|
|
array( '[]', true, array() ),
|
2014-12-03 02:10:50 +00:00
|
|
|
array( '{}', true, (object)array() ),
|
2015-01-16 03:25:15 +00:00
|
|
|
array( '""', true, '' ),
|
|
|
|
|
array( '"0"', true, '0' ),
|
|
|
|
|
array( '"bar"', true, 'bar' ),
|
|
|
|
|
array( '0', true, '0' ),
|
2014-12-03 02:10:50 +00:00
|
|
|
array( '{ "0": "bar" }', true, (object)array( 'bar' ) ),
|
2014-08-08 16:41:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-03 01:52:00 +00:00
|
|
|
* @dataProvider provideValidConstruction
|
2014-08-08 16:41:26 +00:00
|
|
|
*/
|
2014-12-03 02:10:50 +00:00
|
|
|
public function testIsValid( $text, $isValid, $expected ) {
|
|
|
|
|
$obj = new JsonContent( $text, CONTENT_MODEL_JSON );
|
2014-12-03 01:52:00 +00:00
|
|
|
$this->assertEquals( $isValid, $obj->isValid() );
|
2014-12-03 02:10:50 +00:00
|
|
|
$this->assertEquals( $expected, $obj->getData()->getValue() );
|
2014-08-08 16:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideDataToEncode() {
|
2014-08-08 16:41:26 +00:00
|
|
|
return array(
|
2014-12-03 02:10:50 +00:00
|
|
|
array(
|
|
|
|
|
// Round-trip empty array
|
|
|
|
|
'[]',
|
|
|
|
|
'[]',
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
// Round-trip empty object
|
|
|
|
|
'{}',
|
|
|
|
|
'{}',
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
// Round-trip empty array/object (nested)
|
|
|
|
|
'{ "foo": {}, "bar": [] }',
|
|
|
|
|
"{\n \"foo\": {},\n \"bar\": []\n}",
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
'{ "foo": "bar" }',
|
|
|
|
|
"{\n \"foo\": \"bar\"\n}",
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
'{ "foo": 1000 }',
|
|
|
|
|
"{\n \"foo\": 1000\n}",
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
'{ "foo": 1000, "0": "bar" }',
|
|
|
|
|
"{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
|
|
|
|
|
),
|
2014-08-08 16:41:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:52:00 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDataToEncode
|
|
|
|
|
*/
|
2014-12-03 02:10:50 +00:00
|
|
|
public function testBeautifyJson( $input, $beautified ) {
|
|
|
|
|
$obj = new JsonContent( $input );
|
|
|
|
|
$this->assertEquals( $beautified, $obj->beautifyJSON() );
|
2014-12-03 01:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:41:26 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDataToEncode
|
|
|
|
|
*/
|
2014-12-03 02:10:50 +00:00
|
|
|
public function testPreSaveTransform( $input, $transformed ) {
|
|
|
|
|
$obj = new JsonContent( $input );
|
2014-12-18 13:13:07 +00:00
|
|
|
$newObj = $obj->preSaveTransform(
|
|
|
|
|
$this->getMockTitle(),
|
|
|
|
|
$this->getMockUser(),
|
|
|
|
|
$this->getMockParserOptions()
|
|
|
|
|
);
|
2014-12-03 02:10:50 +00:00
|
|
|
$this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
|
2014-08-08 16:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideDataAndParserText() {
|
2014-08-08 16:41:26 +00:00
|
|
|
return array(
|
2015-01-16 03:25:15 +00:00
|
|
|
array(
|
|
|
|
|
array(),
|
|
|
|
|
'<table class="mw-json"><tbody><tr><td>' .
|
|
|
|
|
'<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
|
|
|
|
|
. '</tbody></table></td></tr></tbody></table>'
|
|
|
|
|
),
|
2014-08-08 16:41:26 +00:00
|
|
|
array(
|
2014-12-03 02:10:50 +00:00
|
|
|
(object)array(),
|
2014-12-18 13:13:07 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
|
|
|
|
|
'</tbody></table>'
|
2014-08-08 16:41:26 +00:00
|
|
|
),
|
|
|
|
|
array(
|
2014-12-03 02:10:50 +00:00
|
|
|
(object)array( 'foo' ),
|
2015-01-16 03:25:15 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
|
2014-12-18 13:13:07 +00:00
|
|
|
'</tbody></table>'
|
2014-08-08 16:41:26 +00:00
|
|
|
),
|
|
|
|
|
array(
|
2014-12-03 02:10:50 +00:00
|
|
|
(object)array( 'foo', 'bar' ),
|
2015-01-16 03:25:15 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
|
|
|
|
|
'<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
|
2014-08-08 16:41:26 +00:00
|
|
|
),
|
|
|
|
|
array(
|
2014-12-03 02:10:50 +00:00
|
|
|
(object)array( 'baz' => 'foo', 'bar' ),
|
2015-01-16 03:25:15 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
|
|
|
|
|
'<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
|
2014-08-08 16:41:26 +00:00
|
|
|
),
|
|
|
|
|
array(
|
2014-12-03 02:10:50 +00:00
|
|
|
(object)array( 'baz' => 1000, 'bar' ),
|
2014-08-08 16:41:26 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
|
2015-01-16 03:25:15 +00:00
|
|
|
'<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
|
2014-08-08 16:41:26 +00:00
|
|
|
),
|
2014-08-09 10:51:11 +00:00
|
|
|
array(
|
2015-06-16 19:06:19 +00:00
|
|
|
(object)array( '<script>alert("evil!")</script>' ),
|
2015-01-16 03:25:15 +00:00
|
|
|
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
|
|
|
|
|
'<script>alert("evil!")</script>"' .
|
2014-12-18 13:13:07 +00:00
|
|
|
'</td></tr></tbody></table>',
|
2014-08-09 10:51:11 +00:00
|
|
|
),
|
2014-08-08 16:41:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
2014-12-03 01:52:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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() );
|
|
|
|
|
}
|
2014-08-13 17:59:03 +00:00
|
|
|
}
|