X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/tests/phpunit/includes/content/JsonContentTest.php diff --git a/tests/phpunit/includes/content/JsonContentTest.php b/tests/phpunit/includes/content/JsonContentTest.php new file mode 100644 index 00000000..de8e371e --- /dev/null +++ b/tests/phpunit/includes/content/JsonContentTest.php @@ -0,0 +1,152 @@ +assertEquals( $isValid, $obj->isValid() ); + $this->assertEquals( $expected, $obj->getData()->getValue() ); + } + + public static function provideDataToEncode() { + return [ + [ + // Round-trip empty array + '[]', + '[]', + ], + [ + // Round-trip empty object + '{}', + '{}', + ], + [ + // Round-trip empty array/object (nested) + '{ "foo": {}, "bar": [] }', + "{\n \"foo\": {},\n \"bar\": []\n}", + ], + [ + '{ "foo": "bar" }', + "{\n \"foo\": \"bar\"\n}", + ], + [ + '{ "foo": 1000 }', + "{\n \"foo\": 1000\n}", + ], + [ + '{ "foo": 1000, "0": "bar" }', + "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}", + ], + ]; + } + + /** + * @dataProvider provideDataToEncode + */ + public function testBeautifyJson( $input, $beautified ) { + $obj = new JsonContent( $input ); + $this->assertEquals( $beautified, $obj->beautifyJSON() ); + } + + /** + * @dataProvider provideDataToEncode + */ + public function testPreSaveTransform( $input, $transformed ) { + $obj = new JsonContent( $input ); + $newObj = $obj->preSaveTransform( + $this->getMockTitle(), + $this->getMockUser(), + $this->getMockParserOptions() + ); + $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) ); + } + + 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(); + } + + public static function provideDataAndParserText() { + return [ + [ + [], + '
' . + '' + . '
Empty array
' + ], + [ + (object)[], + '' . + '
Empty object
' + ], + [ + (object)[ 'foo' ], + '' . + '
0"foo"
' + ], + [ + (object)[ 'foo', 'bar' ], + '' . + '
0"foo"
1"bar"
' + ], + [ + (object)[ 'baz' => 'foo', 'bar' ], + '' . + '
baz"foo"
0"bar"
' + ], + [ + (object)[ 'baz' => 1000, 'bar' ], + '' . + '
baz1000
0"bar"
' + ], + [ + (object)[ '' ], + '
0"' . + '<script>alert("evil!")</script>"' . + '
', + ], + ]; + } + + /** + * @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() ); + } +}