Add `actor` table and code to start using it
[lhc/web/wiklou.git] / tests / phpunit / includes / import / ImportTest.php
index 53d91c6..3b91f5b 100644 (file)
@@ -37,7 +37,7 @@ class ImportTest extends MediaWikiLangTestCase {
        }
 
        public function getUnknownTagsXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
+               // phpcs:disable Generic.Files.LineLength
                return [
                        [
                                <<< EOF
@@ -71,7 +71,7 @@ EOF
                                'TestImportPage'
                        ]
                ];
-               // @codingStandardsIgnoreEnd
+               // phpcs:enable
        }
 
        /**
@@ -102,7 +102,7 @@ EOF
        }
 
        public function getRedirectXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
+               // phpcs:disable Generic.Files.LineLength
                return [
                        [
                                <<< EOF
@@ -157,7 +157,7 @@ EOF
                                null
                        ],
                ];
-               // @codingStandardsIgnoreEnd
+               // phpcs:enable
        }
 
        /**
@@ -185,7 +185,7 @@ EOF
        }
 
        public function getSiteInfoXML() {
-               // @codingStandardsIgnoreStart Generic.Files.LineLength
+               // phpcs:disable Generic.Files.LineLength
                return [
                        [
                                <<< EOF
@@ -217,7 +217,113 @@ EOF
                                ]
                        ],
                ];
-               // @codingStandardsIgnoreEnd
+               // 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
+               );
+               // 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 ],
+               ];
        }
 
 }