*/
class ImportTest extends MediaWikiLangTestCase {
private function getInputStreamSource( $xml ) {
$file = 'data:application/xml,' . $xml;
$status = ImportStreamSource::newFromFile( $file );
if ( !$status->isGood() ) {
throw new MWException( "Cannot create InputStreamSource." );
}
return $status->value;
}
/**
* @covers WikiImporter::handlePage
* @dataProvider getRedirectXML
* @param string $xml
* @param string|null $redirectTitle
*/
public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
$source = $this->getInputStreamSource( $xml );
$redirect = null;
$callback = function ( Title $title, ForeignTitle $foreignTitle, $revCount,
$sRevCount, $pageInfo ) use ( &$redirect ) {
if ( array_key_exists( 'redirect', $pageInfo ) ) {
$redirect = $pageInfo['redirect'];
}
};
$importer = new WikiImporter( $source, ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
$importer->setPageOutCallback( $callback );
$importer->doImport();
$this->assertEquals( $redirectTitle, $redirect );
}
public function getRedirectXML() {
return array(
array(
<<< EOF
Test
0
21
20
2014-05-27T10:00:00Z
Admin
10
Admin moved page [[Test]] to [[Test22]]
wikitext
text/x-wiki
#REDIRECT [[Test22]]
tq456o9x3abm7r9ozi6km8yrbbc56o6
EOF
,
'Test22'
),
array(
<<< EOF
Test
0
42
421
2014-05-27T11:00:00Z
Admin
10
Abcd
n7uomjq96szt60fy5w3x7ahf7q8m8rh
wikitext
text/x-wiki
EOF
,
null
),
);
}
/**
* @covers WikiImporter::handleSiteInfo
* @dataProvider getSiteInfoXML
* @param string $xml
* @param array|null $namespaces
*/
public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
$source = $this->getInputStreamSource( $xml );
$importNamespaces = null;
$callback = function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
$importNamespaces = $siteinfo['_namespaces'];
};
$importer = new WikiImporter( $source, ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
$importer->setSiteInfoCallback( $callback );
$importer->doImport();
$this->assertEquals( $importNamespaces, $namespaces );
}
public function getSiteInfoXML() {
return array(
array(
<<< EOF
Media
Special
Talk
User
User talk
Portal
Portal talk
EOF
,
array(
'-2' => 'Media',
'-1' => 'Special',
'0' => '',
'1' => 'Talk',
'2' => 'User',
'3' => 'User talk',
'100' => 'Portal',
'101' => 'Portal talk',
)
),
);
}
}