3 use MediaWiki\Title\ForeignTitle
;
4 use MediaWiki\Title\Title
;
5 use MediaWiki\User\User
;
8 * Test class for Import methods.
12 * @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
14 class ImportTest
extends MediaWikiLangTestCase
{
17 * @covers \WikiImporter
18 * @dataProvider getUnknownTagsXML
21 * @param string $title
23 public function testUnknownXMLTags( $xml, $text, $title ) {
24 $source = new ImportStringSource( $xml );
25 $services = $this->getServiceContainer();
27 $importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() );
29 $importer->doImport();
30 $title = Title
::newFromText( $title );
31 $this->assertTrue( $title->exists() );
34 $services->getWikiPageFactory()->newFromTitle( $title )->getContent()->getText(),
39 public function getUnknownTagsXML() {
43 <mediawiki xmlns
="http://www.mediawiki.org/xml/export-0.10/" xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi
:schemaLocation
="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version
="0.10" xml
:lang
="en">
44 <page unknown
="123" dontknow
="533">
45 <title
>TestImportPage
</title
>
46 <unknowntag
>Should be ignored
</unknowntag
>
48 <id unknown
="123" dontknow
="533">14</id
>
50 <id unknown
="123" dontknow
="533">15</id
>
51 <unknowntag
>Should be ignored
</unknowntag
>
52 <timestamp
>2016-01-03T11
:18:43Z
</timestamp
>
54 <unknowntag
>Should be ignored
</unknowntag
>
55 <username unknown
="123" dontknow
="533">Admin
</username
>
58 <model
>wikitext
</model
>
59 <format
>text
/x
-wiki
</format
>
60 <text xml
:space
="preserve" bytes
="0">noitazinagro tseb eht si ikiWaideM
</text
>
61 <sha1
>phoiac9h4m842xq45sp7s6u21eteeq1
</sha1
>
62 <unknowntag
>Should be ignored
</unknowntag
>
65 <unknowntag
>Should be ignored
</unknowntag
>
69 'noitazinagro tseb eht si ikiWaideM',
77 * @covers \WikiImporter::handlePage
78 * @dataProvider getRedirectXML
80 * @param string|null $redirectTitle
82 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
83 $source = new ImportStringSource( $xml );
86 $callback = static function ( Title
$title, ForeignTitle
$foreignTitle, $revCount,
87 $sRevCount, $pageInfo ) use ( &$redirect ) {
88 if ( array_key_exists( 'redirect', $pageInfo ) ) {
89 $redirect = $pageInfo['redirect'];
93 $importer = $this->getServiceContainer()
94 ->getWikiImporterFactory()
95 ->getWikiImporter( $source, $this->getTestSysop()->getAuthority() );
96 $importer->setPageOutCallback( $callback );
97 $importer->doImport();
99 $this->assertEquals( $redirectTitle, $redirect );
102 public function getRedirectXML() {
106 <mediawiki xmlns
="http://www.mediawiki.org/xml/export-0.10/" xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi
:schemaLocation
="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version
="0.10" xml
:lang
="en">
111 <redirect title
="Test22"/>
114 <timestamp
>2014-05-27T10
:00:00Z
</timestamp
>
116 <username
>Admin
</username
>
119 <comment
>Admin moved page
[[Test
]] to
[[Test22
]]</comment
>
120 <model
>wikitext
</model
>
121 <format
>text
/x
-wiki
</format
>
122 <text xml
:space
="preserve" bytes
="20">#REDIRECT [[Test22]]</text>
123 <sha1
>tq456o9x3abm7r9ozi6km8yrbbc56o6
</sha1
>
133 <mediawiki xmlns
="http://www.mediawiki.org/xml/export-0.9/" xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi
:schemaLocation
="http://www.mediawiki.org/xml/export-0.9/ http://www.mediawiki.org/xml/export-0.9.xsd" version
="0.9" xml
:lang
="en">
140 <timestamp
>2014-05-27T11
:00:00Z
</timestamp
>
142 <username
>Admin
</username
>
145 <text xml
:space
="preserve" bytes
="4">Abcd
</text
>
146 <sha1
>n7uomjq96szt60fy5w3x7ahf7q8m8rh
</sha1
>
147 <model
>wikitext
</model
>
148 <format
>text
/x
-wiki
</format
>
161 * @covers \WikiImporter::handleSiteInfo
162 * @dataProvider getSiteInfoXML
164 * @param array|null $namespaces
166 public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
167 $source = new ImportStringSource( $xml );
169 $importNamespaces = null;
170 $callback = static function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
171 $importNamespaces = $siteinfo['_namespaces'];
174 $importer = $this->getServiceContainer()
175 ->getWikiImporterFactory()
176 ->getWikiImporter( $source, $this->getTestSysop()->getAuthority() );
177 $importer->setSiteInfoCallback( $callback );
178 $importer->doImport();
180 $this->assertEquals( $importNamespaces, $namespaces );
183 public function getSiteInfoXML() {
187 <mediawiki xmlns
="http://www.mediawiki.org/xml/export-0.10/" xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi
:schemaLocation
="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version
="0.10" xml
:lang
="en">
190 <namespace key
="-2" case="first-letter">Media
</namespace>
191 <namespace key
="-1" case="first-letter">Special
</namespace>
192 <namespace key
="0" case="first-letter" />
193 <namespace key
="1" case="first-letter">Talk
</namespace>
194 <namespace key
="2" case="first-letter">User
</namespace>
195 <namespace key
="3" case="first-letter">User talk
</namespace>
196 <namespace key
="100" case="first-letter">Portal
</namespace>
197 <namespace key
="101" case="first-letter">Portal talk
</namespace>
211 '101' => 'Portal talk',
219 * @dataProvider provideUnknownUserHandling
220 * @covers \WikiImporter::setUsernamePrefix
221 * @covers \MediaWiki\User\ExternalUserNames::addPrefix
222 * @covers \MediaWiki\User\ExternalUserNames::applyPrefix
223 * @param bool $assign
224 * @param bool $create
226 public function testUnknownUserHandling( $assign, $create ) {
228 $this->setTemporaryHook(
229 'ImportHandleUnknownUser',
230 function ( $name ) use ( $assign, $create, &$hookId ) {
232 $this->fail( 'ImportHandleUnknownUser was called unexpectedly' );
235 $this->assertEquals( 'UserDoesNotExist', $name );
237 $user = User
::createNew( $name );
238 $this->assertNotNull( $user );
239 $hookId = $user->getId();
246 $user = $this->getTestUser()->getUser();
248 $n = ( $assign ?
1 : 0 ) +
( $create ?
2 : 0 );
249 $source = new ImportStringSource( <<<EOF
250 <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
252 <title>TestImportPage</title>
257 <timestamp>2016-01-01T0$n:00:00Z</timestamp>
259 <username>UserDoesNotExist</username>
262 <model>wikitext</model>
263 <format>text/x-wiki</format>
264 <text xml:space="preserve" bytes="3">foo</text>
265 <sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1>
269 <timestamp>2016-01-01T0$n:00:01Z</timestamp>
271 <username>{$user->getName()}</username>
272 <id>{$user->getId()}</id>
274 <model>wikitext</model>
275 <format>text/x-wiki</format>
276 <text xml:space="preserve" bytes="3">bar</text>
277 <sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1>
285 $services = $this->getServiceContainer();
286 $importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() );
288 $importer->setUsernamePrefix( 'Xxx', $assign );
289 $importer->doImport();
291 $db = $this->getDb();
292 $row = $services->getRevisionStore()->newSelectQueryBuilder( $db )
293 ->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ] )
294 ->caller( __METHOD__
)->fetchRow();
296 $assign && $create ?
'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
299 $this->assertSame( $assign && $create ?
$hookId : 0, (int)$row->rev_user
);
301 $row = $services->getRevisionStore()->newSelectQueryBuilder( $db )
302 ->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ] )
303 ->caller( __METHOD__
)->fetchRow();
304 $this->assertSame( ( $assign ?
'' : 'Xxx>' ) . $user->getName(), $row->rev_user_text
);
305 $this->assertSame( $assign ?
$user->getId() : 0, (int)$row->rev_user
);
308 public static function provideUnknownUserHandling() {
310 'no assign' => [ false, false ],
311 'assign, no create' => [ true, false ],
312 'assign, create' => [ true, true ],