Merge "Message: Don't include Title objects in the serialization (part 1)"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionDbTestBase.php
index c760b41..cfff088 100644 (file)
@@ -1,8 +1,10 @@
 <?php
 use MediaWiki\MediaWikiServices;
-use MediaWiki\Storage\RevisionStore;
-use MediaWiki\Storage\IncompleteRevisionException;
-use MediaWiki\Storage\RevisionRecord;
+use MediaWiki\Revision\MutableRevisionRecord;
+use MediaWiki\Revision\RevisionStore;
+use MediaWiki\Revision\IncompleteRevisionException;
+use MediaWiki\Revision\RevisionRecord;
+use MediaWiki\Revision\SlotRecord;
 
 /**
  * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
@@ -85,11 +87,12 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                        ]
                );
 
-               $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
-               $this->setMwGlobals(
-                       'wgMultiContentRevisionSchemaMigrationStage',
-                       $this->getMcrMigrationStage()
-               );
+               $this->setMwGlobals( [
+                       'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
+                       'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
+                       'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
+                       'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD,
+               ] );
 
                $this->overrideMwServices();
 
@@ -102,6 +105,30 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                }
        }
 
+       /**
+        * @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();
 
        private function makeRevisionWithProps( $props = null ) {
@@ -232,7 +259,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                // getTextId() must be an int!
                $this->assertInternalType( 'integer', $rev->getTextId() );
 
-               $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord::RAW );
+               $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
 
                // we currently only support storage in the text table
                $textId = MediaWikiServices::getInstance()
@@ -1546,4 +1573,66 @@ 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' => SlotRecord::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() );
+       }
+
+       public function provideGetRevisionText() {
+               yield [
+                       [ 'text' ]
+               ];
+       }
+
+       /**
+        * @dataProvider provideGetRevisionText
+        * @covers Revision::getRevisionText
+        */
+       public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
+               $rev = $this->testPage->getRevisionRecord();
+
+               $queryInfo = Revision::getQueryInfo( $queryInfoOptions );
+               $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ?? [] );
+               $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ?? [] );
+               $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ?? [] );
+
+               $conds = [ 'rev_id' => $rev->getId() ];
+               $row = $this->db->selectRow(
+                       $queryInfo['tables'],
+                       $queryInfo['fields'],
+                       $conds,
+                       __METHOD__,
+                       [],
+                       $queryInfo['joins']
+               );
+
+               $expected = $rev->getContent( SlotRecord::MAIN )->serialize();
+
+               $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
+               $this->assertSame( $expected, Revision::getRevisionText( $row ) );
+       }
+
 }