Add `actor` table and code to start using it
[lhc/web/wiklou.git] / tests / phpunit / includes / import / ImportTest.php
index f4aac23..3b91f5b 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+use MediaWiki\MediaWikiServices;
 
 /**
  * Test class for Import methods.
@@ -25,7 +26,7 @@ class ImportTest extends MediaWikiLangTestCase {
 
                $importer = new WikiImporter(
                        $source,
-                       ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
+                       MediaWikiServices::getInstance()->getMainConfig()
                );
 
                $importer->doImport();
@@ -36,9 +37,9 @@ class ImportTest extends MediaWikiLangTestCase {
        }
 
        public function getUnknownTagsXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
-               return array(
-                       array(
+               // phpcs:disable Generic.Files.LineLength
+               return [
+                       [
                                <<< EOF
 <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">
   <page unknown="123" dontknow="533">
@@ -68,9 +69,9 @@ EOF
                                ,
                                'noitazinagro tseb eht si ikiWaideM',
                                'TestImportPage'
-                       )
-               );
-               // @codingStandardsIgnoreEnd
+                       ]
+               ];
+               // phpcs:enable
        }
 
        /**
@@ -92,7 +93,7 @@ EOF
 
                $importer = new WikiImporter(
                        $source,
-                       ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
+                       MediaWikiServices::getInstance()->getMainConfig()
                );
                $importer->setPageOutCallback( $callback );
                $importer->doImport();
@@ -101,9 +102,9 @@ EOF
        }
 
        public function getRedirectXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
-               return array(
-                       array(
+               // phpcs:disable Generic.Files.LineLength
+               return [
+                       [
                                <<< EOF
 <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">
        <page>
@@ -129,8 +130,8 @@ EOF
 EOF
                        ,
                                'Test22'
-                       ),
-                       array(
+                       ],
+                       [
                                <<< EOF
 <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">
        <page>
@@ -154,9 +155,9 @@ EOF
 EOF
                        ,
                                null
-                       ),
-               );
-               // @codingStandardsIgnoreEnd
+                       ],
+               ];
+               // phpcs:enable
        }
 
        /**
@@ -175,7 +176,7 @@ EOF
 
                $importer = new WikiImporter(
                        $source,
-                       ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
+                       MediaWikiServices::getInstance()->getMainConfig()
                );
                $importer->setSiteInfoCallback( $callback );
                $importer->doImport();
@@ -184,9 +185,9 @@ EOF
        }
 
        public function getSiteInfoXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
-               return array(
-                       array(
+               // phpcs:disable Generic.Files.LineLength
+               return [
+                       [
                                <<< EOF
 <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">
   <siteinfo>
@@ -204,7 +205,7 @@ EOF
 </mediawiki>
 EOF
                        ,
-                               array(
+                               [
                                        '-2' => 'Media',
                                        '-1' => 'Special',
                                        '0' => '',
@@ -213,10 +214,116 @@ EOF
                                        '3' => 'User talk',
                                        '100' => 'Portal',
                                        '101' => 'Portal talk',
-                               )
-                       ),
+                               ]
+                       ],
+               ];
+               // phpcs:enable
+       }
+
+       /**
+        * @dataProvider provideUnknownUserHandling
+        * @param bool $assign
+        * @param bool $create
+        */
+       public function testUnknownUserHandling( $assign, $create ) {
+               $hookId = -99;
+               $this->setMwGlobals( 'wgHooks', [
+                       '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 );
+
+               // phpcs:disable Generic.Files.LineLength
+               $source = $this->getDataSource( <<<EOF
+<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">
+  <page>
+    <title>TestImportPage</title>
+    <ns>0</ns>
+    <id>14</id>
+    <revision>
+      <id>15</id>
+      <timestamp>2016-01-01T0$n:00:00Z</timestamp>
+      <contributor>
+        <username>UserDoesNotExist</username>
+        <id>1</id>
+      </contributor>
+      <model>wikitext</model>
+      <format>text/x-wiki</format>
+      <text xml:space="preserve" bytes="3">foo</text>
+      <sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1>
+    </revision>
+    <revision>
+      <id>16</id>
+      <timestamp>2016-01-01T0$n:00:01Z</timestamp>
+      <contributor>
+        <username>{$user->getName()}</username>
+        <id>{$user->getId()}</id>
+      </contributor>
+      <model>wikitext</model>
+      <format>text/x-wiki</format>
+      <text xml:space="preserve" bytes="3">bar</text>
+      <sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1>
+    </revision>
+  </page>
+</mediawiki>
+EOF
                );
-               // @codingStandardsIgnoreEnd
+               // phpcs:enable
+
+               $importer = new WikiImporter( $source, MediaWikiServices::getInstance()->getMainConfig() );
+               $importer->setUsernamePrefix( 'Xxx', $assign );
+               $importer->doImport();
+
+               $db = wfGetDB( DB_MASTER );
+               $revQuery = Revision::getQueryInfo();
+
+               $row = $db->selectRow(
+                       $revQuery['tables'],
+                       $revQuery['fields'],
+                       [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ],
+                       __METHOD__,
+                       [],
+                       $revQuery['joins']
+               );
+               $this->assertSame(
+                       $assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
+                       $row->rev_user_text
+               );
+               $this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user );
+
+               $row = $db->selectRow(
+                       $revQuery['tables'],
+                       $revQuery['fields'],
+                       [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ],
+                       __METHOD__,
+                       [],
+                       $revQuery['joins']
+               );
+               $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 ],
+               ];
        }
 
 }