2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class XmlParserTest extends WikiaBaseTest {
|
|
|
|
|
|
|
|
protected function setUp() {
|
2015-05-04 14:43:53 +00:00
|
|
|
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
|
2015-04-27 14:05:31 +00:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:01:53 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider errorHandlingDataProvider
|
|
|
|
*/
|
|
|
|
public function testErrorHandling( $markup, $expectedErrors ) {
|
|
|
|
$parser = $this->getMockBuilder( 'Wikia\PortableInfobox\Parser\XmlParser' )
|
2015-06-10 15:19:40 +00:00
|
|
|
->setMethods( [ 'logXmlParseError' ] )
|
|
|
|
->getMock();
|
2015-05-20 10:15:22 +00:00
|
|
|
|
2015-06-10 15:19:40 +00:00
|
|
|
$errors = [ ];
|
2015-05-20 10:15:22 +00:00
|
|
|
try {
|
2015-06-10 15:19:40 +00:00
|
|
|
$data = $parser->parseXmlString( $markup, $errors );
|
2015-05-20 10:15:22 +00:00
|
|
|
} catch ( \Wikia\PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
|
2015-06-12 13:43:37 +00:00
|
|
|
// parseXmlString should throw an exception, but we want to proceed in order to check parameters
|
|
|
|
// from logXmlParseError
|
2015-05-19 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:43:37 +00:00
|
|
|
$this->assertEquals( $expectedErrors, array_map(
|
|
|
|
function ( LibXMLError $error ) {
|
|
|
|
return [ 'level' => $error->level, 'code' => $error->code, 'msg' => trim( $error->message ) ];
|
|
|
|
},
|
|
|
|
$errors
|
|
|
|
) );
|
2015-05-19 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function errorHandlingDataProvider() {
|
2015-05-19 14:43:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error codes are defined on official xml API documentation:
|
|
|
|
* http://www.xmlsoft.org/html/libxml-xmlerror.html
|
|
|
|
*/
|
2015-05-19 14:01:53 +00:00
|
|
|
return [
|
2015-06-11 16:38:08 +00:00
|
|
|
[
|
|
|
|
'<data>d</dat/a>',
|
|
|
|
[
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 73, 'msg' => "expected '>'" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 76, 'msg' => "Opening and ending tag mismatch: data line 1 and dat" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 5, 'msg' => "Extra content at the end of the document" ],
|
|
|
|
]
|
|
|
|
],
|
2015-06-10 15:19:40 +00:00
|
|
|
[
|
|
|
|
'<data> x </data></data>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 5, 'msg' => "Extra content at the end of the document" ],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<data> > ddd < a ></data>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 68, 'msg' => "StartTag: invalid element name" ],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<data>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 77, 'msg' => "Premature end of data in tag data line 1" ],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<infobox><data source=caption></infobox>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 39, 'msg' => "AttValue: \" or ' expected" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 65, 'msg' => "attributes construct error" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 73, 'msg' => "Couldn't find end of Start Tag data line 1" ],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<infobox><data source="caption"></infobox>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 76, 'msg' => "Opening and ending tag mismatch: data line 1 and infobox" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 77, 'msg' => "Premature end of data in tag infobox line 1" ],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<infobox><data source="caption></data></infobox>',
|
2015-05-19 14:01:53 +00:00
|
|
|
[
|
2015-06-10 15:19:40 +00:00
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 38, 'msg' => "Unescaped '<' not allowed in attributes values" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 65, 'msg' => "attributes construct error" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 73, 'msg' => "Couldn't find end of Start Tag data line 1" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 76, 'msg' => "Opening and ending tag mismatch: infobox line 1 and data" ],
|
|
|
|
[ 'level' => LIBXML_ERR_FATAL, 'code' => 5, 'msg' => "Extra content at the end of the document" ]
|
2015-05-19 14:01:53 +00:00
|
|
|
]
|
2015-06-10 15:19:40 +00:00
|
|
|
]
|
2015-05-19 14:01:53 +00:00
|
|
|
];
|
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|