RevisionStore::newRevisionFromBatch should use Title::newFromRow
[lhc/web/wiklou.git] / tests / phpunit / includes / Revision / RevisionStoreDbTestBase.php
index b0b9ddf..4248650 100644 (file)
@@ -4,8 +4,10 @@ namespace MediaWiki\Tests\Revision;
 
 use CommentStoreComment;
 use Content;
+use ContentHandler;
 use Exception;
 use HashBagOStuff;
+use IDBAccessObject;
 use InvalidArgumentException;
 use Language;
 use MediaWiki\Linker\LinkTarget;
@@ -81,7 +83,6 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
                $this->setMwGlobals( [
                        'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
                        'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
-                       'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_NEW,
                ] );
        }
 
@@ -106,7 +107,7 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
         * @return WikiPage
         */
        protected function getTestPage( $pageTitle = null ) {
-               if ( !is_null( $pageTitle ) && $this->testPage ) {
+               if ( is_null( $pageTitle ) && $this->testPage ) {
                        return $this->testPage;
                }
 
@@ -1791,8 +1792,6 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
         * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
         */
        public function testGetKnownCurrentRevision_userNameChange() {
-               global $wgActorTableSchemaMigrationStage;
-
                $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
                $this->setService( 'MainWANObjectCache', $cache );
 
@@ -1811,11 +1810,9 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
                $this->db->update( 'user',
                        [ 'user_name' => $newUserName ],
                        [ 'user_id' => $rev->getUser()->getId() ] );
-               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
-                       $this->db->update( 'actor',
-                               [ 'actor_name' => $newUserName ],
-                               [ 'actor_user' => $rev->getUser()->getId() ] );
-               }
+               $this->db->update( 'actor',
+                       [ 'actor_name' => $newUserName ],
+                       [ 'actor_user' => $rev->getUser()->getId() ] );
 
                // Reload the revision and regrab the user name.
                $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
@@ -1864,8 +1861,6 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
         * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
         */
        public function testNewRevisionFromRow_userNameChange() {
-               global $wgActorTableSchemaMigrationStage;
-
                $page = $this->getTestPage();
                $text = __METHOD__;
                /** @var Revision $rev */
@@ -1895,11 +1890,9 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
                $this->db->update( 'user',
                        [ 'user_name' => $newUserName ],
                        [ 'user_id' => $record->getUser()->getId() ] );
-               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
-                       $this->db->update( 'actor',
-                               [ 'actor_name' => $newUserName ],
-                               [ 'actor_user' => $record->getUser()->getId() ] );
-               }
+               $this->db->update( 'actor',
+                       [ 'actor_name' => $newUserName ],
+                       [ 'actor_user' => $record->getUser()->getId() ] );
 
                // Reload the record, passing $fromCache as true to force fresh info from the db,
                // and regrab the user name
@@ -1963,23 +1956,110 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
                $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
        }
 
+       public function provideGetContentBlobsForBatchOptions() {
+               yield 'all slots' => [ null ];
+               yield 'no slots' => [ [] ];
+               yield 'main slot' => [ [ SlotRecord::MAIN ] ];
+       }
+
+       /**
+        * @dataProvider provideGetContentBlobsForBatchOptions
+        * @covers       \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        * @param array|null $slots
+        * @throws \MWException
+        */
+       public function testGetContentBlobsForBatch( $slots ) {
+               $page1 = $this->getTestPage();
+               $text = __METHOD__ . 'b-ä';
+               $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
+               /** @var Revision $rev1 */
+               $rev1 = $editStatus->getValue()['revision'];
+
+               $page2 = $this->getTestPage( $page1->getTitle()->getPrefixedText() . '_other' );
+               $editStatus = $this->editPage( $page2->getTitle()->getPrefixedDBkey(), $text . '2' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 2' );
+               /** @var Revision $rev2 */
+               $rev2 = $editStatus->getValue()['revision'];
+
+               $store = MediaWikiServices::getInstance()->getRevisionStore();
+               $result = $store->getContentBlobsForBatch( [ $rev1->getId(), $rev2->getId() ], $slots );
+               $this->assertTrue( $result->isGood() );
+               $this->assertEmpty( $result->getErrors() );
+
+               $rowSetsByRevId = $result->getValue();
+               $this->assertArrayHasKey( $rev1->getId(), $rowSetsByRevId );
+               $this->assertArrayHasKey( $rev2->getId(), $rowSetsByRevId );
+
+               $rev1rows = $rowSetsByRevId[$rev1->getId()];
+               $rev2rows = $rowSetsByRevId[$rev2->getId()];
+
+               if ( is_array( $slots ) && !in_array( SlotRecord::MAIN, $slots ) ) {
+                       $this->assertArrayNotHasKey( SlotRecord::MAIN, $rev1rows );
+                       $this->assertArrayNotHasKey( SlotRecord::MAIN, $rev2rows );
+               } else {
+                       $this->assertArrayHasKey( SlotRecord::MAIN, $rev1rows );
+                       $this->assertArrayHasKey( SlotRecord::MAIN, $rev2rows );
+
+                       $mainSlotRow1 = $rev1rows[ SlotRecord::MAIN ];
+                       $mainSlotRow2 = $rev2rows[ SlotRecord::MAIN ];
+
+                       if ( $mainSlotRow1->model_name ) {
+                               $this->assertSame( $rev1->getContentModel(), $mainSlotRow1->model_name );
+                               $this->assertSame( $rev2->getContentModel(), $mainSlotRow2->model_name );
+                       }
+
+                       $this->assertSame( $text . '1', $mainSlotRow1->blob_data );
+                       $this->assertSame( $text . '2', $mainSlotRow2->blob_data );
+               }
+       }
+
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        */
+       public function testGetContentBlobsForBatch_emptyBatch() {
+               $rows = new FakeResultWrapper( [] );
+               $result = MediaWikiServices::getInstance()->getRevisionStore()
+                       ->getContentBlobsForBatch( $rows );
+               $this->assertTrue( $result->isGood() );
+               $this->assertEmpty( $result->getValue() );
+               $this->assertEmpty( $result->getErrors() );
+       }
+
        public function provideNewRevisionsFromBatchOptions() {
                yield 'No preload slots or content, single page' => [
+                       [ 'comment' ],
                        null,
                        []
                ];
                yield 'Preload slots and content, single page' => [
+                       [ 'comment' ],
                        null,
                        [
                                'slots' => [ SlotRecord::MAIN ],
                                'content' => true
                        ]
                ];
+               yield 'Ask for no slots' => [
+                       [ 'comment' ],
+                       null,
+                       [ 'slots' => [] ]
+               ];
                yield 'No preload slots or content, multiple pages' => [
+                       [ 'comment' ],
                        'Other_Page',
                        []
                ];
                yield 'Preload slots and content, multiple pages' => [
+                       [ 'comment' ],
+                       'Other_Page',
+                       [
+                               'slots' => [ SlotRecord::MAIN ],
+                               'content' => true
+                       ]
+               ];
+               yield 'Preload slots and content, multiple pages, preload page fields' => [
+                       [ 'page', 'comment' ],
                        'Other_Page',
                        [
                                'slots' => [ SlotRecord::MAIN ],
@@ -1990,49 +2070,49 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
 
        /**
         * @dataProvider provideNewRevisionsFromBatchOptions
-        * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        * @covers       \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        * @param array|null $queryOptions options to provide to revisionToRow
         * @param string|null $otherPageTitle
         * @param array|null $options
         * @throws \MWException
         */
        public function testNewRevisionsFromBatch_preloadContent(
+               $queryOptions,
                $otherPageTitle = null,
                array $options = []
        ) {
                $page1 = $this->getTestPage();
                $text = __METHOD__ . 'b-ä';
+               $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
                /** @var Revision $rev1 */
-               $rev1 = $page1->doEditContent(
-                       new WikitextContent( $text . '1' ),
-                       __METHOD__ . 'b',
-                       0,
-                       false,
-                       $this->getTestUser()->getUser()
-               )->value['revision'];
+               $rev1 = $editStatus->getValue()['revision'];
+
                $page2 = $this->getTestPage( $otherPageTitle );
+               $editStatus = $this->editPage( $page2->getTitle()->getPrefixedDBkey(), $text . '2' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 2' );
                /** @var Revision $rev2 */
-               $rev2 = $page2->doEditContent(
-                       new WikitextContent( $text . '2' ),
-                       __METHOD__ . 'b',
-                       0,
-                       false,
-                       $this->getTestUser()->getUser()
-               )->value['revision'];
+               $rev2 = $editStatus->getValue()['revision'];
+
                $store = MediaWikiServices::getInstance()->getRevisionStore();
                $result = $store->newRevisionsFromBatch(
-                       [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev2 ) ],
+                       [
+                               $this->revisionToRow( $rev1, $queryOptions ),
+                               $this->revisionToRow( $rev2, $queryOptions )
+                       ],
                        $options
                );
                $this->assertTrue( $result->isGood() );
                $this->assertEmpty( $result->getErrors() );
+               /** @var RevisionRecord[] $records */
                $records = $result->getValue();
                $this->assertRevisionRecordMatchesRevision( $rev1, $records[$rev1->getId()] );
                $this->assertRevisionRecordMatchesRevision( $rev2, $records[$rev2->getId()] );
 
                $this->assertSame( $text . '1',
-                       $records[$rev1->getId()]->getContent( SlotRecord::MAIN )->serialize() );
+                       ContentHandler::getContentText( $records[$rev1->getId()]->getContent( SlotRecord::MAIN ) ) );
                $this->assertSame( $text . '2',
-                       $records[$rev2->getId()]->getContent( SlotRecord::MAIN )->serialize() );
+                       ContentHandler::getContentText( $records[$rev2->getId()]->getContent( SlotRecord::MAIN ) ) );
                $this->assertEquals( $page1->getTitle()->getDBkey(),
                        $records[$rev1->getId()]->getPageAsLinkTarget()->getDBkey() );
                $this->assertEquals( $page2->getTitle()->getDBkey(),
@@ -2043,9 +2123,10 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
         * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
         */
        public function testNewRevisionsFromBatch_emptyBatch() {
+               $rows = new FakeResultWrapper( [] );
                $result = MediaWikiServices::getInstance()->getRevisionStore()
                        ->newRevisionsFromBatch(
-                               [],
+                               $rows,
                                [
                                        'slots' => [ SlotRecord::MAIN ],
                                        'content' => true
@@ -2055,4 +2136,44 @@ abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
                $this->assertEmpty( $result->getValue() );
                $this->assertEmpty( $result->getErrors() );
        }
+
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        */
+       public function testNewRevisionsFromBatch_wrongTitle() {
+               $page1 = $this->getTestPage();
+               $text = __METHOD__ . 'b-ä';
+               $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
+               /** @var Revision $rev1 */
+               $rev1 = $editStatus->getValue()['revision'];
+
+               $this->setExpectedException( InvalidArgumentException::class );
+               MediaWikiServices::getInstance()->getRevisionStore()
+                       ->newRevisionsFromBatch(
+                               [ $this->revisionToRow( $rev1 ) ],
+                               [],
+                               IDBAccessObject::READ_NORMAL,
+                               $this->getTestPage( 'Title_Other_Then_The_One_Revision_Belongs_To' )->getTitle()
+                       );
+       }
+
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
+        */
+       public function testNewRevisionsFromBatch_DuplicateRows() {
+               $page1 = $this->getTestPage();
+               $text = __METHOD__ . 'b-ä';
+               $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
+               $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
+               /** @var Revision $rev1 */
+               $rev1 = $editStatus->getValue()['revision'];
+
+               $status = MediaWikiServices::getInstance()->getRevisionStore()
+                       ->newRevisionsFromBatch( [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev1 ) ] );
+
+               $this->assertFalse( $status->isGood() );
+               $this->assertTrue( $status->hasMessage( 'internalerror' ) );
+       }
+
 }