X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FRevisionDbTestBase.php;h=94f8fba2509ede883afbd88674a93d550f24913b;hp=2fdf59019ee97d49dfb97670055d050d7c227017;hb=0c77841534d9139b0042696fe015a15d3e349ef5;hpb=c9be16a35596ff369bb5f700c20ae8dc2f7def48 diff --git a/tests/phpunit/includes/RevisionDbTestBase.php b/tests/phpunit/includes/RevisionDbTestBase.php index 2fdf59019e..94f8fba250 100644 --- a/tests/phpunit/includes/RevisionDbTestBase.php +++ b/tests/phpunit/includes/RevisionDbTestBase.php @@ -1,4 +1,8 @@ resetNamespaces(); + if ( !$this->testPage ) { /** * We have to create a new page for each subclass as the page creation may result @@ -102,6 +107,14 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $props['text'] = 'Lorem Ipsum'; } + if ( !isset( $props['user_text'] ) ) { + $props['user_text'] = 'Tester'; + } + + if ( !isset( $props['user'] ) ) { + $props['user'] = 0; + } + if ( !isset( $props['comment'] ) ) { $props['comment'] = 'just a test'; } @@ -110,6 +123,10 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $props['page'] = $this->testPage->getId(); } + if ( !isset( $props['content_model'] ) ) { + $props['content_model'] = CONTENT_MODEL_WIKITEXT; + } + $rev = new Revision( $props ); $dbw = wfGetDB( DB_MASTER ); @@ -202,14 +219,23 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $revId = $rev->insertOn( wfGetDB( DB_MASTER ) ); $this->assertInternalType( 'integer', $revId ); - $this->assertInternalType( 'integer', $rev->getTextId() ); $this->assertSame( $revId, $rev->getId() ); + // getTextId() must be an int! + $this->assertInternalType( 'integer', $rev->getTextId() ); + + $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord::RAW ); + + // we currently only support storage in the text table + $textId = MediaWikiServices::getInstance() + ->getBlobStore() + ->getTextIdFromAddress( $mainSlot->getAddress() ); + $this->assertSelect( 'text', [ 'old_id', 'old_text' ], - "old_id = {$rev->getTextId()}", - [ [ strval( $rev->getTextId() ), 'Revision Text' ] ] + "old_id = $textId", + [ [ strval( $textId ), 'Revision Text' ] ] ); $this->assertSelect( 'revision', @@ -228,7 +254,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { [ [ strval( $rev->getId() ), strval( $this->testPage->getId() ), - strval( $rev->getTextId() ), + strval( $textId ), '0', '0', '0', @@ -246,11 +272,12 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { // If an ExternalStore is set don't use it. $this->setMwGlobals( 'wgDefaultExternalStore', false ); $this->setExpectedException( - MWException::class, - "Cannot insert revision: page ID must be nonzero" + IncompleteRevisionException::class, + "rev_page field must not be 0!" ); - $rev = new Revision( [] ); + $title = Title::newFromText( 'Nonexistant-' . __METHOD__ ); + $rev = new Revision( [], 0, $title ); $rev->insertOn( wfGetDB( DB_MASTER ) ); } @@ -321,12 +348,42 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { return $f + [ 'ar_namespace', 'ar_title' ]; }, ]; + yield [ + function ( $f ) { + unset( $f['ar_text'] ); + return $f; + }, + ]; yield [ function ( $f ) { unset( $f['ar_text_id'] ); return $f; }, ]; + yield [ + function ( $f ) { + unset( $f['ar_page_id'] ); + return $f; + }, + ]; + yield [ + function ( $f ) { + unset( $f['ar_parent_id'] ); + return $f; + }, + ]; + yield [ + function ( $f ) { + unset( $f['ar_rev_id'] ); + return $f; + }, + ]; + yield [ + function ( $f ) { + unset( $f['ar_sha1'] ); + return $f; + }, + ]; } /** @@ -334,6 +391,17 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { * @covers Revision::newFromArchiveRow */ public function testNewFromArchiveRow( $selectModifier ) { + $services = MediaWikiServices::getInstance(); + + $store = new RevisionStore( + $services->getDBLoadBalancer(), + $services->getService( '_SqlBlobStore' ), + $services->getMainWANObjectCache() + ); + + $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() ); + $this->setService( 'RevisionStore', $store ); + $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum', @@ -354,6 +422,8 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $row = $res->fetchObject(); $res->free(); + // MCR migration note: $row is now required to contain ar_title and ar_namespace. + // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow $rev = Revision::newFromArchiveRow( $row ); $this->assertRevEquals( $orig, $rev ); @@ -382,7 +452,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $row = $res->fetchObject(); $res->free(); - $rev = Revision::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] ); + $rev = Revision::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] ); $this->assertNotEquals( $orig->getComment(), $rev->getComment() ); $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() ); @@ -426,7 +496,8 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { * @covers Revision::newFromPageId */ public function testNewFromPageIdWithNotLatestId() { - $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ); + $content = new WikitextContent( __METHOD__ ); + $this->testPage->doEditContent( $content, __METHOD__ ); $rev = Revision::newFromPageId( $this->testPage->getId(), $this->testPage->getRevision()->getPrevious()->getId() @@ -447,6 +518,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ); $id = $this->testPage->getRevision()->getId(); + $this->hideDeprecated( 'Revision::fetchRevision' ); $res = Revision::fetchRevision( $this->testPage->getTitle() ); # note: order is unspecified @@ -455,8 +527,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $rows[$row->rev_id] = $row; } - $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' ); - $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id ); + $this->assertEmpty( $rows, 'expected empty set' ); } /** @@ -541,6 +612,10 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'new null revision should have a different id from the original revision' ); $this->assertEquals( $orig->getTextId(), $rev->getTextId(), 'new null revision should have the same text id as the original revision' ); + $this->assertEquals( $orig->getSha1(), $rev->getSha1(), + 'new null revision should have the same SHA1 as the original revision' ); + $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ), + 'new null revision should have the same content as the original revision' ); $this->assertEquals( __METHOD__, $rev->getContent()->getNativeData() ); } @@ -560,7 +635,10 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $row = $res->fetchObject(); $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex ); - $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp ); + $this->assertEquals( + $orig->getTimestamp(), + wfTimestamp( TS_MW, $row->ipc_rev_timestamp ) + ); } public static function provideUserWasLastToEdit() { @@ -571,6 +649,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { } /** + * @covers Revision::userWasLastToEdit * @dataProvider provideUserWasLastToEdit */ public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) { @@ -603,7 +682,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'user' => $userA->getId(), 'text' => 'zero', 'content_model' => CONTENT_MODEL_WIKITEXT, - 'summary' => 'edit zero' + 'comment' => 'edit zero' ] ); $revisions[0]->insertOn( $dbw ); @@ -615,7 +694,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'user' => $userA->getId(), 'text' => 'one', 'content_model' => CONTENT_MODEL_WIKITEXT, - 'summary' => 'edit one' + 'comment' => 'edit one' ] ); $revisions[1]->insertOn( $dbw ); @@ -626,7 +705,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'user' => $userB->getId(), 'text' => 'two', 'content_model' => CONTENT_MODEL_WIKITEXT, - 'summary' => 'edit two' + 'comment' => 'edit two' ] ); $revisions[2]->insertOn( $dbw ); @@ -637,7 +716,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'user' => $userA->getId(), 'text' => 'three', 'content_model' => CONTENT_MODEL_WIKITEXT, - 'summary' => 'edit three' + 'comment' => 'edit three' ] ); $revisions[3]->insertOn( $dbw ); @@ -648,13 +727,24 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'user' => $userA->getId(), 'text' => 'zero', 'content_model' => CONTENT_MODEL_WIKITEXT, - 'summary' => 'edit four' + 'comment' => 'edit four' ] ); $revisions[4]->insertOn( $dbw ); // test it --------------------------------- $since = $revisions[$sinceIdx]->getTimestamp(); + $allRows = iterator_to_array( $dbw->select( + 'revision', + [ 'rev_id', 'rev_timestamp', 'rev_user' ], + [ + 'rev_page' => $page->getId(), + //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) ) + ], + __METHOD__, + [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] + ) ); + $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since ); $this->assertEquals( $expectedLast, $wasLast ); @@ -802,12 +892,16 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { 'text_id' => 123456789, // not in the test DB ] ); + MediaWiki\suppressWarnings(); // bad text_id will trigger a warning. + $this->assertNull( $rev->getContent(), "getContent() should return null if the revision's text blob could not be loaded." ); // NOTE: check this twice, once for lazy initialization, and once with the cached value. $this->assertNull( $rev->getContent(), "getContent() should return null if the revision's text blob could not be loaded." ); + + MediaWiki\suppressWarnings( 'end' ); } public function provideGetSize() { @@ -901,6 +995,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { */ public function testLoadFromId() { $rev = $this->testPage->getRevision(); + $this->hideDeprecated( 'Revision::loadFromId' ); $this->assertRevEquals( $rev, Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() ) @@ -1023,7 +1118,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $rev[1] = $this->testPage->getLatest(); $this->assertSame( - [ $rev[1] => strval( $textLength ) ], + [ $rev[1] => $textLength ], Revision::getParentLengths( wfGetDB( DB_MASTER ), [ $rev[1] ] @@ -1046,7 +1141,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $rev[2] = $this->testPage->getLatest(); $this->assertSame( - [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ], + [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ], Revision::getParentLengths( wfGetDB( DB_MASTER ), [ $rev[1], $rev[2] ] @@ -1077,14 +1172,6 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { ); } - /** - * @covers Revision::getTitle - */ - public function testGetTitle_forBadRevision() { - $rev = new Revision( [] ); - $this->assertNull( $rev->getTitle() ); - } - /** * @covers Revision::isMinor */ @@ -1260,14 +1347,21 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $rev = $this->testPage->getRevision(); // Clear any previous cache for the revision during creation - $key = $cache->makeGlobalKey( 'revision', $db->getDomainID(), $rev->getPage(), $rev->getId() ); + $key = $cache->makeGlobalKey( 'revision-row-1.29', + $db->getDomainID(), + $rev->getPage(), + $rev->getId() + ); $cache->delete( $key, WANObjectCache::HOLDOFF_NONE ); $this->assertFalse( $cache->get( $key ) ); // Get the new revision and make sure it is in the cache and correct $newRev = Revision::newKnownCurrent( $db, $rev->getPage(), $rev->getId() ); $this->assertRevEquals( $rev, $newRev ); - $this->assertRevEquals( $rev, $cache->get( $key ) ); + + $cachedRow = $cache->get( $key ); + $this->assertNotFalse( $cachedRow ); + $this->assertEquals( $rev->getId(), $cachedRow->rev_id ); } public function provideUserCanBitfield() { @@ -1326,6 +1420,16 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { $expected, Revision::userCanBitfield( $bitField, $field, $user, $title ) ); + + // Fallback to $wgUser + $this->setMwGlobals( + 'wgUser', + $user + ); + $this->assertSame( + $expected, + Revision::userCanBitfield( $bitField, $field, null, $title ) + ); } public function provideUserCan() { @@ -1364,7 +1468,7 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase { ] ); $user = $this->getTestUser( $userGroups )->getUser(); - $revision = new Revision( [ 'deleted' => $bitField ] ); + $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage->getTitle() ); $this->assertSame( $expected,