*/ class ImportTest extends MediaWikiLangTestCase { /** * @covers \WikiImporter * @dataProvider getUnknownTagsXML * @param string $xml * @param string $text * @param string $title */ public function testUnknownXMLTags( $xml, $text, $title ) { $source = new ImportStringSource( $xml ); $services = $this->getServiceContainer(); $importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); $importer->doImport(); $title = Title::newFromText( $title ); $this->assertTrue( $title->exists() ); $this->assertEquals( $services->getWikiPageFactory()->newFromTitle( $title )->getContent()->getText(), $text ); } public function getUnknownTagsXML() { return [ [ <<< EOF TestImportPage Should be ignored 0 14 15 Should be ignored 2016-01-03T11:18:43Z Should be ignored Admin 1 wikitext text/x-wiki noitazinagro tseb eht si ikiWaideM phoiac9h4m842xq45sp7s6u21eteeq1 Should be ignored Should be ignored EOF , 'noitazinagro tseb eht si ikiWaideM', 'TestImportPage' ] ]; // phpcs:enable } /** * @covers \WikiImporter::handlePage * @dataProvider getRedirectXML * @param string $xml * @param string|null $redirectTitle */ public function testHandlePageContainsRedirect( $xml, $redirectTitle ) { $source = new ImportStringSource( $xml ); $redirect = null; $callback = static function ( Title $title, ForeignTitle $foreignTitle, $revCount, $sRevCount, $pageInfo ) use ( &$redirect ) { if ( array_key_exists( 'redirect', $pageInfo ) ) { $redirect = $pageInfo['redirect']; } }; $importer = $this->getServiceContainer() ->getWikiImporterFactory() ->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); $importer->setPageOutCallback( $callback ); $importer->doImport(); $this->assertEquals( $redirectTitle, $redirect ); } public function getRedirectXML() { return [ [ <<< 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' ], [ <<< EOF Test 0 42 421 2014-05-27T11:00:00Z Admin 10 Abcd n7uomjq96szt60fy5w3x7ahf7q8m8rh wikitext text/x-wiki EOF , null ], ]; // phpcs:enable } /** * @covers \WikiImporter::handleSiteInfo * @dataProvider getSiteInfoXML * @param string $xml * @param array|null $namespaces */ public function testSiteInfoContainsNamespaces( $xml, $namespaces ) { $source = new ImportStringSource( $xml ); $importNamespaces = null; $callback = static function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) { $importNamespaces = $siteinfo['_namespaces']; }; $importer = $this->getServiceContainer() ->getWikiImporterFactory() ->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); $importer->setSiteInfoCallback( $callback ); $importer->doImport(); $this->assertEquals( $importNamespaces, $namespaces ); } public function getSiteInfoXML() { return [ [ <<< EOF Media Special Talk User User talk Portal Portal talk EOF , [ '-2' => 'Media', '-1' => 'Special', '0' => '', '1' => 'Talk', '2' => 'User', '3' => 'User talk', '100' => 'Portal', '101' => 'Portal talk', ] ], ]; // phpcs:enable } /** * @dataProvider provideUnknownUserHandling * @covers \WikiImporter::setUsernamePrefix * @covers \MediaWiki\User\ExternalUserNames::addPrefix * @covers \MediaWiki\User\ExternalUserNames::applyPrefix * @param bool $assign * @param bool $create */ public function testUnknownUserHandling( $assign, $create ) { $hookId = -99; $this->setTemporaryHook( 'ImportHandleUnknownUser', function ( $name ) use ( $assign, $create, &$hookId ) { if ( !$assign ) { $this->fail( 'ImportHandleUnknownUser was called unexpectedly' ); } $this->assertEquals( 'UserDoesNotExist', $name ); if ( $create ) { $user = User::createNew( $name ); $this->assertNotNull( $user ); $hookId = $user->getId(); return false; } return true; } ); $user = $this->getTestUser()->getUser(); $n = ( $assign ? 1 : 0 ) + ( $create ? 2 : 0 ); $source = new ImportStringSource( << TestImportPage 0 14 15 2016-01-01T0$n:00:00Z UserDoesNotExist 1 wikitext text/x-wiki foo 1e6gpc3ehk0mu2jqu8cg42g009s796b 16 2016-01-01T0$n:00:01Z {$user->getName()} {$user->getId()} wikitext text/x-wiki bar bjhlo6dxh5wivnszm93u4b78fheiy4t EOF ); // phpcs:enable $services = $this->getServiceContainer(); $importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); $importer->setUsernamePrefix( 'Xxx', $assign ); $importer->doImport(); $db = $this->getDb(); $row = $services->getRevisionStore()->newSelectQueryBuilder( $db ) ->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ] ) ->caller( __METHOD__ )->fetchRow(); $this->assertSame( $assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist', $row->rev_user_text ); $this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user ); $row = $services->getRevisionStore()->newSelectQueryBuilder( $db ) ->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ] ) ->caller( __METHOD__ )->fetchRow(); $this->assertSame( ( $assign ? '' : 'Xxx>' ) . $user->getName(), $row->rev_user_text ); $this->assertSame( $assign ? $user->getId() : 0, (int)$row->rev_user ); } public static function provideUnknownUserHandling() { return [ 'no assign' => [ false, false ], 'assign, no create' => [ true, false ], 'assign, create' => [ true, true ], ]; } private function prepareImportLogEntries( $performer ) { $source = new ImportStringSource( << 1 2008-10-23T00:00:02Z Wikimedian 12345 content was: 'I think this was a silly edit' test test Silly page name a:1:{s:3:"foo";s:3:"bar";} 2 2008-10-23T00:00:01Z Wikimedian 12345 content was: 'I think this was a silly edit' test test Silly page name a:1:{s:3:"foo";O:8:"stdClass":0:{}} EOF ); $services = $this->getServiceContainer(); $importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $performer ); $importer->setUsernamePrefix( 'Xxx', true ); return $importer; } public function testLogEntryImportNoRightsChecksUnsafe() { $importer = $this->prepareImportLogEntries( new UltimateAuthority( $this->getTestUser()->getUserIdentity() ) ); $importer->doImport(); $result = $this->getDb()->newSelectQueryBuilder() ->from( 'logging' ) ->where( [ 'log_type' => 'test', 'log_action' => 'test', ] ) ->fetchRowCount(); $this->assertSame( 1, $result, "Only one, safe log entry imported" ); } public function testLogEntryImportNoRightsChecksSafe() { $scope = ExtensionRegistry::getInstance()->setAttributeForTest( 'LogParamsAllowedClasses', [ 'test/test' => [ stdClass::class ], ] ); $importer = $this->prepareImportLogEntries( new UltimateAuthority( $this->getTestUser()->getUserIdentity() ) ); $importer->doImport(); $result = $this->getDb()->newSelectQueryBuilder() ->from( 'logging' ) ->where( [ 'log_type' => 'test', 'log_action' => 'test', ] ) ->fetchRowCount(); $this->assertSame( 2, $result, "Both log entries imported" ); } public function testLogEntryImportUserNotAllowed() { RequestContext::getMain()->setOutput( $this->createMock( OutputPage::class ) ); $importer = $this->prepareImportLogEntries( $this->getTestSysop()->getAuthority() ); $importer->doImport(); $result = $this->getDb()->newSelectQueryBuilder() ->from( 'logging' ) ->where( [ 'log_type' => 'test', 'log_action' => 'test', ] ) ->fetchRowCount(); $this->assertSame( 0, $result, "No log entries imported" ); } public function testLogEntryImportUserAllowed() { $this->overrideConfigValue( MainConfigNames::GroupPermissions, [ 'logentryimport' => [ 'logentryimport' => true ], ] ); $performer = $this->getTestUser( 'logentryimport' ); RequestContext::getMain()->setOutput( $this->createMock( OutputPage::class ) ); RequestContext::getMain()->setUser( $performer->getUser() ); $importer = $this->prepareImportLogEntries( $performer->getAuthority() ); $importer->doImport(); $result = $this->getDb()->newSelectQueryBuilder() ->from( 'logging' ) ->where( [ 'log_type' => 'test', 'log_action' => 'test', ] ) ->fetchRowCount(); $this->assertSame( 1, $result, "Only one, safe log entry imported" ); } }