Merge "resourceloader: Configure eslint to disallow $ and require inside startup"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionDbTestBase.php
index e17f855..8bf87a2 100644 (file)
@@ -1,8 +1,10 @@
 <?php
 use MediaWiki\MediaWikiServices;
+use MediaWiki\Storage\MutableRevisionRecord;
 use MediaWiki\Storage\RevisionStore;
 use MediaWiki\Storage\IncompleteRevisionException;
 use MediaWiki\Storage\RevisionRecord;
+use MediaWiki\Storage\SlotRecord;
 
 /**
  * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
@@ -58,8 +60,6 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
        abstract protected function getMcrTablesToReset();
 
        protected function setUp() {
-               global $wgContLang;
-
                $this->tablesUsed += $this->getMcrTablesToReset();
 
                parent::setUp();
@@ -87,15 +87,12 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                        ]
                );
 
-               $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
-               $this->setMwGlobals(
-                       'wgMultiContentRevisionSchemaMigrationStage',
-                       $this->getMcrMigrationStage()
-               );
-
-               MWNamespace::clearCaches();
-               // Reset namespace cache
-               $wgContLang->resetNamespaces();
+               $this->setMwGlobals( [
+                       'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
+                       'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
+                       'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
+                       'wgActorTableSchemaMigrationStage' => MIGRATION_OLD,
+               ] );
 
                $this->overrideMwServices();
 
@@ -108,14 +105,28 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                }
        }
 
-       protected function tearDown() {
-               global $wgContLang;
-
-               parent::tearDown();
-
-               MWNamespace::clearCaches();
-               // Reset namespace cache
-               $wgContLang->resetNamespaces();
+       /**
+        * @param string $model
+        * @return Title
+        */
+       protected function getMockTitle() {
+               $mock = $this->getMockBuilder( Title::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $mock->expects( $this->any() )
+                       ->method( 'getNamespace' )
+                       ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
+               $mock->expects( $this->any() )
+                       ->method( 'getPrefixedText' )
+                       ->will( $this->returnValue( __CLASS__ ) );
+               $mock->expects( $this->any() )
+                       ->method( 'getDBkey' )
+                       ->will( $this->returnValue( __CLASS__ ) );
+               $mock->expects( $this->any() )
+                       ->method( 'getArticleID' )
+                       ->will( $this->returnValue( 23 ) );
+
+               return $mock;
        }
 
        abstract protected function getContentHandlerUseDB();
@@ -1402,7 +1413,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
         */
        public function testNewKnownCurrent() {
                // Setup the services
-               $this->resetGlobalServices();
+               $this->overrideMwServices();
                $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
                $this->setService( 'MainWANObjectCache', $cache );
                $db = wfGetDB( DB_MASTER );
@@ -1468,14 +1479,14 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                        Revision::DELETED_TEXT,
                        Revision::DELETED_TEXT,
                        [ 'sysop' ],
-                       Title::newFromText( __METHOD__ ),
+                       __METHOD__,
                        true,
                ];
                yield [
                        Revision::DELETED_TEXT,
                        Revision::DELETED_TEXT,
                        [],
-                       Title::newFromText( __METHOD__ ),
+                       __METHOD__,
                        false,
                ];
        }
@@ -1485,6 +1496,8 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
         * @covers Revision::userCanBitfield
         */
        public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
+               $title = Title::newFromText( $title );
+
                $this->setMwGlobals(
                        'wgGroupPermissions',
                        [
@@ -1560,4 +1573,32 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                );
        }
 
+       public function provideGetTextId() {
+               yield [ [], null ];
+
+               $slot = new SlotRecord( (object)[
+                       'slot_revision_id' => 42,
+                       'slot_content_id' => 1,
+                       'content_address' => 'tt:789',
+                       'model_name' => CONTENT_MODEL_WIKITEXT,
+                       'role_name' => 'main',
+                       'slot_origin' => 1,
+               ], new WikitextContent( 'Test' ) );
+
+               $rec = new MutableRevisionRecord( $this->testPage->getTitle() );
+               $rec->setId( 42 );
+               $rec->setSlot( $slot );
+
+               yield [ $rec, 789 ];
+       }
+
+       /**
+        * @dataProvider provideGetTextId
+        * @covers Revision::getTextId()
+        */
+       public function testGetTextId( $spec, $expected ) {
+               $rev = new Revision( $spec, 0, $this->testPage->getTitle() );
+               $this->assertSame( $expected, $rev->getTextId() );
+       }
+
 }