setMwGlobals( 'wgWellFormedXml', true ); } public static function provideValidConstruction() { return array( array( 'foo', false, null ), array( '[]', true, array() ), array( '{}', true, (object)array() ), array( '""', true, '' ), array( '"0"', true, '0' ), array( '"bar"', true, 'bar' ), array( '0', true, '0' ), array( '{ "0": "bar" }', true, (object)array( 'bar' ) ), ); } /** * @dataProvider provideValidConstruction */ public function testIsValid( $text, $isValid, $expected ) { $obj = new JsonContent( $text, CONTENT_MODEL_JSON ); $this->assertEquals( $isValid, $obj->isValid() ); $this->assertEquals( $expected, $obj->getData()->getValue() ); } public static function provideDataToEncode() { return array( 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}", ), ); } /** * @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 array( array( array(), '
' . '' . '
Empty array
' ), array( (object)array(), '' . '
Empty object
' ), array( (object)array( 'foo' ), '' . '
0"foo"
' ), array( (object)array( 'foo', 'bar' ), '' . '
0"foo"
1"bar"
' ), array( (object)array( 'baz' => 'foo', 'bar' ), '' . '
baz"foo"
0"bar"
' ), array( (object)array( 'baz' => 1000, 'bar' ), '' . '
baz1000
0"bar"
' ), array( (object)array( ''), '
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() ); } }