Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / import / ImportTest.php
blobf92f62d6e9db90cdaf698505ed2647acff2f76c1
1 <?php
3 use MediaWiki\Title\ForeignTitle;
4 use MediaWiki\Title\Title;
5 use MediaWiki\User\User;
7 /**
8 * Test class for Import methods.
10 * @group Database
12 * @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
14 class ImportTest extends MediaWikiLangTestCase {
16 /**
17 * @covers \WikiImporter
18 * @dataProvider getUnknownTagsXML
19 * @param string $xml
20 * @param string $text
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() );
33 $this->assertEquals(
34 $services->getWikiPageFactory()->newFromTitle( $title )->getContent()->getText(),
35 $text
39 public function getUnknownTagsXML() {
40 return [
42 <<< EOF
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>
47 <ns>0</ns>
48 <id unknown="123" dontknow="533">14</id>
49 <revision>
50 <id unknown="123" dontknow="533">15</id>
51 <unknowntag>Should be ignored</unknowntag>
52 <timestamp>2016-01-03T11:18:43Z</timestamp>
53 <contributor>
54 <unknowntag>Should be ignored</unknowntag>
55 <username unknown="123" dontknow="533">Admin</username>
56 <id>1</id>
57 </contributor>
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>
63 </revision>
64 </page>
65 <unknowntag>Should be ignored</unknowntag>
66 </mediawiki>
67 EOF
69 'noitazinagro tseb eht si ikiWaideM',
70 'TestImportPage'
73 // phpcs:enable
76 /**
77 * @covers \WikiImporter::handlePage
78 * @dataProvider getRedirectXML
79 * @param string $xml
80 * @param string|null $redirectTitle
82 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
83 $source = new ImportStringSource( $xml );
85 $redirect = null;
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() {
103 return [
105 <<< EOF
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">
107 <page>
108 <title>Test</title>
109 <ns>0</ns>
110 <id>21</id>
111 <redirect title="Test22"/>
112 <revision>
113 <id>20</id>
114 <timestamp>2014-05-27T10:00:00Z</timestamp>
115 <contributor>
116 <username>Admin</username>
117 <id>10</id>
118 </contributor>
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>
124 </revision>
125 </page>
126 </mediawiki>
129 'Test22'
132 <<< EOF
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">
134 <page>
135 <title>Test</title>
136 <ns>0</ns>
137 <id>42</id>
138 <revision>
139 <id>421</id>
140 <timestamp>2014-05-27T11:00:00Z</timestamp>
141 <contributor>
142 <username>Admin</username>
143 <id>10</id>
144 </contributor>
145 <text xml:space="preserve" bytes="4">Abcd</text>
146 <sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
147 <model>wikitext</model>
148 <format>text/x-wiki</format>
149 </revision>
150 </page>
151 </mediawiki>
154 null
157 // phpcs:enable
161 * @covers \WikiImporter::handleSiteInfo
162 * @dataProvider getSiteInfoXML
163 * @param string $xml
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() {
184 return [
186 <<< EOF
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">
188 <siteinfo>
189 <namespaces>
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>
198 </namespaces>
199 </siteinfo>
200 </mediawiki>
204 '-2' => 'Media',
205 '-1' => 'Special',
206 '0' => '',
207 '1' => 'Talk',
208 '2' => 'User',
209 '3' => 'User talk',
210 '100' => 'Portal',
211 '101' => 'Portal talk',
215 // phpcs:enable
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 ) {
227 $hookId = -99;
228 $this->setTemporaryHook(
229 'ImportHandleUnknownUser',
230 function ( $name ) use ( $assign, $create, &$hookId ) {
231 if ( !$assign ) {
232 $this->fail( 'ImportHandleUnknownUser was called unexpectedly' );
235 $this->assertEquals( 'UserDoesNotExist', $name );
236 if ( $create ) {
237 $user = User::createNew( $name );
238 $this->assertNotNull( $user );
239 $hookId = $user->getId();
240 return false;
242 return true;
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">
251 <page>
252 <title>TestImportPage</title>
253 <ns>0</ns>
254 <id>14</id>
255 <revision>
256 <id>15</id>
257 <timestamp>2016-01-01T0$n:00:00Z</timestamp>
258 <contributor>
259 <username>UserDoesNotExist</username>
260 <id>1</id>
261 </contributor>
262 <model>wikitext</model>
263 <format>text/x-wiki</format>
264 <text xml:space="preserve" bytes="3">foo</text>
265 <sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1>
266 </revision>
267 <revision>
268 <id>16</id>
269 <timestamp>2016-01-01T0$n:00:01Z</timestamp>
270 <contributor>
271 <username>{$user->getName()}</username>
272 <id>{$user->getId()}</id>
273 </contributor>
274 <model>wikitext</model>
275 <format>text/x-wiki</format>
276 <text xml:space="preserve" bytes="3">bar</text>
277 <sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1>
278 </revision>
279 </page>
280 </mediawiki>
283 // phpcs:enable
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();
295 $this->assertSame(
296 $assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
297 $row->rev_user_text
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() {
309 return [
310 'no assign' => [ false, false ],
311 'assign, no create' => [ true, false ],
312 'assign, create' => [ true, true ],