123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665 |
- <?php
- namespace MediaWiki\Tests\Storage;
- use CommentStoreComment;
- use Content;
- use Exception;
- use HashBagOStuff;
- use InvalidArgumentException;
- use Language;
- use MediaWiki\Linker\LinkTarget;
- use MediaWiki\MediaWikiServices;
- use MediaWiki\Storage\BlobStoreFactory;
- use MediaWiki\Storage\IncompleteRevisionException;
- use MediaWiki\Storage\MutableRevisionRecord;
- use MediaWiki\Storage\RevisionRecord;
- use MediaWiki\Storage\RevisionStore;
- use MediaWiki\Storage\SlotRecord;
- use MediaWiki\Storage\SqlBlobStore;
- use MediaWiki\User\UserIdentityValue;
- use MediaWikiTestCase;
- use PHPUnit_Framework_MockObject_MockObject;
- use Revision;
- use TestUserRegistry;
- use Title;
- use WANObjectCache;
- use Wikimedia\Rdbms\Database;
- use Wikimedia\Rdbms\DatabaseSqlite;
- use Wikimedia\Rdbms\FakeResultWrapper;
- use Wikimedia\Rdbms\LoadBalancer;
- use Wikimedia\Rdbms\TransactionProfiler;
- use WikiPage;
- use WikitextContent;
- /**
- * @group Database
- * @group RevisionStore
- */
- abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
- /**
- * @var Title
- */
- private $testPageTitle;
- /**
- * @var WikiPage
- */
- private $testPage;
- /**
- * @return int
- */
- abstract protected function getMcrMigrationStage();
- /**
- * @return bool
- */
- protected function getContentHandlerUseDB() {
- return true;
- }
- /**
- * @return string[]
- */
- abstract protected function getMcrTablesToReset();
- public function needsDB() {
- return true;
- }
- public function setUp() {
- parent::setUp();
- $this->tablesUsed[] = 'archive';
- $this->tablesUsed[] = 'page';
- $this->tablesUsed[] = 'revision';
- $this->tablesUsed[] = 'comment';
- $this->tablesUsed += $this->getMcrTablesToReset();
- $this->setMwGlobals( [
- 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
- 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
- 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
- 'wgActorTableSchemaMigrationStage' => MIGRATION_OLD,
- ] );
- $this->overrideMwServices();
- }
- protected function addCoreDBData() {
- // Blank out. This would fail with a modified schema, and we don't need it.
- }
- /**
- * @return Title
- */
- protected function getTestPageTitle() {
- if ( $this->testPageTitle ) {
- return $this->testPageTitle;
- }
- $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
- return $this->testPageTitle;
- }
- /**
- * @return WikiPage
- */
- protected function getTestPage() {
- if ( $this->testPage ) {
- return $this->testPage;
- }
- $title = $this->getTestPageTitle();
- $this->testPage = WikiPage::factory( $title );
- if ( !$this->testPage->exists() ) {
- // Make sure we don't write to the live db.
- $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER ) );
- $user = static::getTestSysop()->getUser();
- $this->testPage->doEditContent(
- new WikitextContent( 'UTContent-' . __CLASS__ ),
- 'UTPageSummary-' . __CLASS__,
- EDIT_NEW | EDIT_SUPPRESS_RC,
- false,
- $user
- );
- }
- return $this->testPage;
- }
- /**
- * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
- */
- private function getLoadBalancerMock( array $server ) {
- $lb = $this->getMockBuilder( LoadBalancer::class )
- ->setMethods( [ 'reallyOpenConnection' ] )
- ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
- ->getMock();
- $lb->method( 'reallyOpenConnection' )->willReturnCallback(
- function ( array $server, $dbNameOverride ) {
- return $this->getDatabaseMock( $server );
- }
- );
- return $lb;
- }
- /**
- * @return Database|PHPUnit_Framework_MockObject_MockObject
- */
- private function getDatabaseMock( array $params ) {
- $db = $this->getMockBuilder( DatabaseSqlite::class )
- ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
- ->setConstructorArgs( [ $params ] )
- ->getMock();
- $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
- $db->method( 'isOpen' )->willReturn( true );
- return $db;
- }
- public function provideDomainCheck() {
- yield [ false, 'test', '' ];
- yield [ 'test', 'test', '' ];
- yield [ false, 'test', 'foo_' ];
- yield [ 'test-foo_', 'test', 'foo_' ];
- yield [ false, 'dash-test', '' ];
- yield [ 'dash-test', 'dash-test', '' ];
- yield [ false, 'underscore_test', 'foo_' ];
- yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
- }
- /**
- * @dataProvider provideDomainCheck
- * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
- */
- public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
- $this->setMwGlobals(
- [
- 'wgDBname' => $dbName,
- 'wgDBprefix' => $dbPrefix,
- ]
- );
- $loadBalancer = $this->getLoadBalancerMock(
- [
- 'host' => '*dummy*',
- 'dbDirectory' => '*dummy*',
- 'user' => 'test',
- 'password' => 'test',
- 'flags' => 0,
- 'variables' => [],
- 'schema' => '',
- 'cliMode' => true,
- 'agent' => '',
- 'load' => 100,
- 'profiler' => null,
- 'trxProfiler' => new TransactionProfiler(),
- 'connLogger' => new \Psr\Log\NullLogger(),
- 'queryLogger' => new \Psr\Log\NullLogger(),
- 'errorLogger' => function () {
- },
- 'deprecationLogger' => function () {
- },
- 'type' => 'test',
- 'dbname' => $dbName,
- 'tablePrefix' => $dbPrefix,
- ]
- );
- $db = $loadBalancer->getConnection( DB_REPLICA );
- /** @var SqlBlobStore $blobStore */
- $blobStore = $this->getMockBuilder( SqlBlobStore::class )
- ->disableOriginalConstructor()
- ->getMock();
- $store = new RevisionStore(
- $loadBalancer,
- $blobStore,
- new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
- MediaWikiServices::getInstance()->getCommentStore(),
- MediaWikiServices::getInstance()->getContentModelStore(),
- MediaWikiServices::getInstance()->getSlotRoleStore(),
- $this->getMcrMigrationStage(),
- MediaWikiServices::getInstance()->getActorMigration(),
- $wikiId
- );
- $count = $store->countRevisionsByPageId( $db, 0 );
- // Dummy check to make PhpUnit happy. We are really only interested in
- // countRevisionsByPageId not failing due to the DB domain check.
- $this->assertSame( 0, $count );
- }
- protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
- $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
- $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
- $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
- $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
- }
- protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
- $this->assertEquals(
- $r1->getPageAsLinkTarget()->getNamespace(),
- $r2->getPageAsLinkTarget()->getNamespace()
- );
- $this->assertEquals(
- $r1->getPageAsLinkTarget()->getText(),
- $r2->getPageAsLinkTarget()->getText()
- );
- if ( $r1->getParentId() ) {
- $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
- }
- $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
- $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
- $this->assertEquals( $r1->getComment(), $r2->getComment() );
- $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
- $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
- $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
- $this->assertEquals( $r1->getSize(), $r2->getSize() );
- $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
- $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
- $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
- $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
- foreach ( $r1->getSlotRoles() as $role ) {
- $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
- $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
- }
- foreach ( [
- RevisionRecord::DELETED_TEXT,
- RevisionRecord::DELETED_COMMENT,
- RevisionRecord::DELETED_USER,
- RevisionRecord::DELETED_RESTRICTED,
- ] as $field ) {
- $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
- }
- }
- protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
- $this->assertSame( $s1->getRole(), $s2->getRole() );
- $this->assertSame( $s1->getModel(), $s2->getModel() );
- $this->assertSame( $s1->getFormat(), $s2->getFormat() );
- $this->assertSame( $s1->getSha1(), $s2->getSha1() );
- $this->assertSame( $s1->getSize(), $s2->getSize() );
- $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
- $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
- $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
- }
- protected function assertRevisionCompleteness( RevisionRecord $r ) {
- $this->assertTrue( $r->hasSlot( 'main' ) );
- $this->assertInstanceOf( SlotRecord::class, $r->getSlot( 'main' ) );
- $this->assertInstanceOf( Content::class, $r->getContent( 'main' ) );
- foreach ( $r->getSlotRoles() as $role ) {
- $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
- }
- }
- protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
- $this->assertTrue( $slot->hasAddress() );
- $this->assertSame( $r->getId(), $slot->getRevision() );
- $this->assertInstanceOf( Content::class, $slot->getContent() );
- }
- /**
- * @param mixed[] $details
- *
- * @return RevisionRecord
- */
- private function getRevisionRecordFromDetailsArray( $details = [] ) {
- // Convert some values that can't be provided by dataProviders
- if ( isset( $details['user'] ) && $details['user'] === true ) {
- $details['user'] = $this->getTestUser()->getUser();
- }
- if ( isset( $details['page'] ) && $details['page'] === true ) {
- $details['page'] = $this->getTestPage()->getId();
- }
- if ( isset( $details['parent'] ) && $details['parent'] === true ) {
- $details['parent'] = $this->getTestPage()->getLatest();
- }
- // Create the RevisionRecord with any available data
- $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
- isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
- isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
- isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
- isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
- isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
- isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
- isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
- isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
- isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
- isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
- isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
- if ( isset( $details['content'] ) ) {
- foreach ( $details['content'] as $role => $content ) {
- $rev->setContent( $role, $content );
- }
- }
- return $rev;
- }
- public function provideInsertRevisionOn_successes() {
- yield 'Bare minimum revision insertion' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'page' => true,
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- 'user' => true,
- ],
- ];
- yield 'Detailed revision insertion' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'parent' => true,
- 'page' => true,
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- 'user' => true,
- 'minor' => true,
- 'visibility' => RevisionRecord::DELETED_RESTRICTED,
- ],
- ];
- }
- protected function getRandomCommentStoreComment() {
- return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
- }
- /**
- * @dataProvider provideInsertRevisionOn_successes
- * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
- * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
- * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
- */
- public function testInsertRevisionOn_successes(
- array $revDetails = []
- ) {
- $title = $this->getTestPageTitle();
- $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
- $this->overrideMwServices();
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
- // is the new revision correct?
- $this->assertRevisionCompleteness( $return );
- $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
- $this->assertRevisionRecordsEqual( $rev, $return );
- // can we load it from the store?
- $loaded = $store->getRevisionById( $return->getId() );
- $this->assertRevisionCompleteness( $loaded );
- $this->assertRevisionRecordsEqual( $return, $loaded );
- // can we find it directly in the database?
- $this->assertRevisionExistsInDatabase( $return );
- }
- protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
- $row = $this->revisionToRow( new Revision( $rev ), [] );
- // unset nulled fields
- unset( $row->rev_content_model );
- unset( $row->rev_content_format );
- // unset fake fields
- unset( $row->rev_comment_text );
- unset( $row->rev_comment_data );
- unset( $row->rev_comment_cid );
- unset( $row->rev_comment_id );
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $queryInfo = $store->getQueryInfo( [ 'user' ] );
- $row = get_object_vars( $row );
- $this->assertSelect(
- $queryInfo['tables'],
- array_keys( $row ),
- [ 'rev_id' => $rev->getId() ],
- [ array_values( $row ) ],
- [],
- $queryInfo['joins']
- );
- }
- /**
- * @param SlotRecord $a
- * @param SlotRecord $b
- */
- protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
- // Assert that the same blob address has been used.
- $this->assertSame( $a->getAddress(), $b->getAddress() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
- */
- public function testInsertRevisionOn_blobAddressExists() {
- $title = $this->getTestPageTitle();
- $revDetails = [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'parent' => true,
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- 'user' => true,
- ];
- $this->overrideMwServices();
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- // Insert the first revision
- $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
- $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
- $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
- $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
- // Insert a second revision inheriting the same blob address
- $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) );
- $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
- $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
- $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
- $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
- $firstMainSlot = $firstReturn->getSlot( 'main' );
- $secondMainSlot = $secondReturn->getSlot( 'main' );
- $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
- // And that different revisions have been created.
- $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
- // Make sure the slot rows reference the correct revision
- $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
- $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
- }
- public function provideInsertRevisionOn_failures() {
- yield 'no slot' => [
- [
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- 'user' => true,
- ],
- new InvalidArgumentException( 'main slot must be provided' )
- ];
- yield 'no main slot' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- 'user' => true,
- ],
- new InvalidArgumentException( 'main slot must be provided' )
- ];
- yield 'no timestamp' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'comment' => $this->getRandomCommentStoreComment(),
- 'user' => true,
- ],
- new IncompleteRevisionException( 'timestamp field must not be NULL!' )
- ];
- yield 'no comment' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'timestamp' => '20171117010101',
- 'user' => true,
- ],
- new IncompleteRevisionException( 'comment must not be NULL!' )
- ];
- yield 'no user' => [
- [
- 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
- 'comment' => $this->getRandomCommentStoreComment(),
- 'timestamp' => '20171117010101',
- ],
- new IncompleteRevisionException( 'user must not be NULL!' )
- ];
- }
- /**
- * @dataProvider provideInsertRevisionOn_failures
- * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
- */
- public function testInsertRevisionOn_failures(
- array $revDetails = [],
- Exception $exception
- ) {
- $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $this->setExpectedException(
- get_class( $exception ),
- $exception->getMessage(),
- $exception->getCode()
- );
- $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
- }
- public function provideNewNullRevision() {
- yield [
- Title::newFromText( 'UTPage_notAutoCreated' ),
- [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
- CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
- true,
- ];
- yield [
- Title::newFromText( 'UTPage_notAutoCreated' ),
- [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
- CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
- false,
- ];
- }
- /**
- * @dataProvider provideNewNullRevision
- * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
- * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
- */
- public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
- $this->overrideMwServices();
- $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
- $page = WikiPage::factory( $title );
- if ( !$page->exists() ) {
- $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
- }
- $revDetails['page'] = $page->getId();
- $revDetails['timestamp'] = wfTimestampNow();
- $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
- $revDetails['user'] = $user;
- $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $dbw = wfGetDB( DB_MASTER );
- $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
- $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
- $record = $store->newNullRevision(
- wfGetDB( DB_MASTER ),
- $title,
- $comment,
- $minor,
- $user
- );
- $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
- $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
- $this->assertEquals( $comment, $record->getComment() );
- $this->assertEquals( $minor, $record->isMinor() );
- $this->assertEquals( $user->getName(), $record->getUser()->getName() );
- $this->assertEquals( $baseRev->getId(), $record->getParentId() );
- $this->assertArrayEquals(
- $baseRev->getSlotRoles(),
- $record->getSlotRoles()
- );
- foreach ( $baseRev->getSlotRoles() as $role ) {
- $parentSlot = $baseRev->getSlot( $role );
- $slot = $record->getSlot( $role );
- $this->assertTrue( $slot->isInherited(), 'isInherited' );
- $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
- $this->assertSameSlotContent( $parentSlot, $slot );
- }
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
- */
- public function testNewNullRevision_nonExistingTitle() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $record = $store->newNullRevision(
- wfGetDB( DB_MASTER ),
- Title::newFromText( __METHOD__ . '.iDontExist!' ),
- CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
- false,
- TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
- );
- $this->assertNull( $record );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
- */
- public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
- $page = $this->getTestPage();
- $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revisionRecord = $store->getRevisionById( $rev->getId() );
- $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
- $this->assertGreaterThan( 0, $result );
- $this->assertSame(
- $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
- $result
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
- */
- public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
- // This assumes that sysops are auto patrolled
- $sysop = $this->getTestSysop()->getUser();
- $page = $this->getTestPage();
- $status = $page->doEditContent(
- new WikitextContent( __METHOD__ ),
- __METHOD__,
- 0,
- false,
- $sysop
- );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revisionRecord = $store->getRevisionById( $rev->getId() );
- $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
- $this->assertSame( 0, $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
- */
- public function testGetRecentChange() {
- $page = $this->getTestPage();
- $content = new WikitextContent( __METHOD__ );
- $status = $page->doEditContent( $content, __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revRecord = $store->getRevisionById( $rev->getId() );
- $recentChange = $store->getRecentChange( $revRecord );
- $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
- $this->assertEquals( $rev->getRecentChange(), $recentChange );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
- */
- public function testGetRevisionById() {
- $page = $this->getTestPage();
- $content = new WikitextContent( __METHOD__ );
- $status = $page->doEditContent( $content, __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revRecord = $store->getRevisionById( $rev->getId() );
- $this->assertSame( $rev->getId(), $revRecord->getId() );
- $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
- $this->assertSame( __METHOD__, $revRecord->getComment()->text );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
- */
- public function testGetRevisionByTitle() {
- $page = $this->getTestPage();
- $content = new WikitextContent( __METHOD__ );
- $status = $page->doEditContent( $content, __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revRecord = $store->getRevisionByTitle( $page->getTitle() );
- $this->assertSame( $rev->getId(), $revRecord->getId() );
- $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
- $this->assertSame( __METHOD__, $revRecord->getComment()->text );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
- */
- public function testGetRevisionByPageId() {
- $page = $this->getTestPage();
- $content = new WikitextContent( __METHOD__ );
- $status = $page->doEditContent( $content, __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revRecord = $store->getRevisionByPageId( $page->getId() );
- $this->assertSame( $rev->getId(), $revRecord->getId() );
- $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
- $this->assertSame( __METHOD__, $revRecord->getComment()->text );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
- */
- public function testGetRevisionByTimestamp() {
- // Make sure there is 1 second between the last revision and the rev we create...
- // Otherwise we might not get the correct revision and the test may fail...
- // :(
- $page = $this->getTestPage();
- sleep( 1 );
- $content = new WikitextContent( __METHOD__ );
- $status = $page->doEditContent( $content, __METHOD__ );
- /** @var Revision $rev */
- $rev = $status->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $revRecord = $store->getRevisionByTimestamp(
- $page->getTitle(),
- $rev->getTimestamp()
- );
- $this->assertSame( $rev->getId(), $revRecord->getId() );
- $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
- $this->assertSame( __METHOD__, $revRecord->getComment()->text );
- }
- protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
- // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
- $page = WikiPage::factory( $rev->getTitle() );
- $fields = [
- 'rev_id' => (string)$rev->getId(),
- 'rev_page' => (string)$rev->getPage(),
- 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
- 'rev_user_text' => (string)$rev->getUserText(),
- 'rev_user' => (string)$rev->getUser(),
- 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
- 'rev_deleted' => (string)$rev->getVisibility(),
- 'rev_len' => (string)$rev->getSize(),
- 'rev_parent_id' => (string)$rev->getParentId(),
- 'rev_sha1' => (string)$rev->getSha1(),
- ];
- if ( in_array( 'page', $options ) ) {
- $fields += [
- 'page_namespace' => (string)$page->getTitle()->getNamespace(),
- 'page_title' => $page->getTitle()->getDBkey(),
- 'page_id' => (string)$page->getId(),
- 'page_latest' => (string)$page->getLatest(),
- 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
- 'page_len' => (string)$page->getContent()->getSize(),
- ];
- }
- if ( in_array( 'user', $options ) ) {
- $fields += [
- 'user_name' => (string)$rev->getUserText(),
- ];
- }
- if ( in_array( 'comment', $options ) ) {
- $fields += [
- 'rev_comment_text' => $rev->getComment(),
- 'rev_comment_data' => null,
- 'rev_comment_cid' => null,
- ];
- }
- if ( $rev->getId() ) {
- $fields += [
- 'rev_id' => (string)$rev->getId(),
- ];
- }
- return (object)$fields;
- }
- protected function assertRevisionRecordMatchesRevision(
- Revision $rev,
- RevisionRecord $record
- ) {
- $this->assertSame( $rev->getId(), $record->getId() );
- $this->assertSame( $rev->getPage(), $record->getPageId() );
- $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
- $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
- $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
- $this->assertSame( $rev->isMinor(), $record->isMinor() );
- $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
- $this->assertSame( $rev->getSize(), $record->getSize() );
- /**
- * @note As of MW 1.31, the database schema allows the parent ID to be
- * NULL to indicate that it is unknown.
- */
- $expectedParent = $rev->getParentId();
- if ( $expectedParent === null ) {
- $expectedParent = 0;
- }
- $this->assertSame( $expectedParent, $record->getParentId() );
- $this->assertSame( $rev->getSha1(), $record->getSha1() );
- $this->assertSame( $rev->getComment(), $record->getComment()->text );
- $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
- $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
- $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
- $revRec = $rev->getRevisionRecord();
- $revMain = $revRec->getSlot( 'main' );
- $recMain = $record->getSlot( 'main' );
- $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
- $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
- $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
- if ( $revMain->hasOrigin() ) {
- $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
- }
- if ( $revMain->hasAddress() ) {
- $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
- }
- if ( $revMain->hasContentId() ) {
- $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
- }
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
- * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
- */
- public function testNewRevisionFromRow_getQueryInfo() {
- $page = $this->getTestPage();
- $text = __METHOD__ . 'a-ä';
- /** @var Revision $rev */
- $rev = $page->doEditContent(
- new WikitextContent( $text ),
- __METHOD__ . 'a'
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $info = $store->getQueryInfo();
- $row = $this->db->selectRow(
- $info['tables'],
- $info['fields'],
- [ 'rev_id' => $rev->getId() ],
- __METHOD__,
- [],
- $info['joins']
- );
- $record = $store->newRevisionFromRow(
- $row,
- [],
- $page->getTitle()
- );
- $this->assertRevisionRecordMatchesRevision( $rev, $record );
- $this->assertSame( $text, $rev->getContent()->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
- */
- public function testNewRevisionFromRow_anonEdit() {
- $page = $this->getTestPage();
- $text = __METHOD__ . 'a-ä';
- /** @var Revision $rev */
- $rev = $page->doEditContent(
- new WikitextContent( $text ),
- __METHOD__ . 'a'
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $record = $store->newRevisionFromRow(
- $this->revisionToRow( $rev ),
- [],
- $page->getTitle()
- );
- $this->assertRevisionRecordMatchesRevision( $rev, $record );
- $this->assertSame( $text, $rev->getContent()->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
- */
- public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
- $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
- $this->overrideMwServices();
- $page = $this->getTestPage();
- $text = __METHOD__ . 'a-ä';
- /** @var Revision $rev */
- $rev = $page->doEditContent(
- new WikitextContent( $text ),
- __METHOD__ . 'a'
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $record = $store->newRevisionFromRow(
- $this->revisionToRow( $rev ),
- [],
- $page->getTitle()
- );
- $this->assertRevisionRecordMatchesRevision( $rev, $record );
- $this->assertSame( $text, $rev->getContent()->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
- */
- public function testNewRevisionFromRow_userEdit() {
- $page = $this->getTestPage();
- $text = __METHOD__ . 'b-ä';
- /** @var Revision $rev */
- $rev = $page->doEditContent(
- new WikitextContent( $text ),
- __METHOD__ . 'b',
- 0,
- false,
- $this->getTestUser()->getUser()
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $record = $store->newRevisionFromRow(
- $this->revisionToRow( $rev ),
- [],
- $page->getTitle()
- );
- $this->assertRevisionRecordMatchesRevision( $rev, $record );
- $this->assertSame( $text, $rev->getContent()->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
- * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
- */
- public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $title = Title::newFromText( __METHOD__ );
- $text = __METHOD__ . '-bä';
- $page = WikiPage::factory( $title );
- /** @var Revision $orig */
- $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
- ->value['revision'];
- $page->doDeleteArticle( __METHOD__ );
- $db = wfGetDB( DB_MASTER );
- $arQuery = $store->getArchiveQueryInfo();
- $res = $db->select(
- $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
- __METHOD__, [], $arQuery['joins']
- );
- $this->assertTrue( is_object( $res ), 'query failed' );
- $row = $res->fetchObject();
- $res->free();
- $record = $store->newRevisionFromArchiveRow( $row );
- $this->assertRevisionRecordMatchesRevision( $orig, $record );
- $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
- */
- public function testNewRevisionFromArchiveRow_legacyEncoding() {
- $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
- $this->overrideMwServices();
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $title = Title::newFromText( __METHOD__ );
- $text = __METHOD__ . '-bä';
- $page = WikiPage::factory( $title );
- /** @var Revision $orig */
- $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
- ->value['revision'];
- $page->doDeleteArticle( __METHOD__ );
- $db = wfGetDB( DB_MASTER );
- $arQuery = $store->getArchiveQueryInfo();
- $res = $db->select(
- $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
- __METHOD__, [], $arQuery['joins']
- );
- $this->assertTrue( is_object( $res ), 'query failed' );
- $row = $res->fetchObject();
- $res->free();
- $record = $store->newRevisionFromArchiveRow( $row );
- $this->assertRevisionRecordMatchesRevision( $orig, $record );
- $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
- */
- public function testNewRevisionFromArchiveRow_no_user() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $row = (object)[
- 'ar_id' => '1',
- 'ar_page_id' => '2',
- 'ar_namespace' => '0',
- 'ar_title' => 'Something',
- 'ar_rev_id' => '2',
- 'ar_text_id' => '47',
- 'ar_timestamp' => '20180528192356',
- 'ar_minor_edit' => '0',
- 'ar_deleted' => '0',
- 'ar_len' => '78',
- 'ar_parent_id' => '0',
- 'ar_sha1' => 'deadbeef',
- 'ar_comment_text' => 'whatever',
- 'ar_comment_data' => null,
- 'ar_comment_cid' => null,
- 'ar_user' => '0',
- 'ar_user_text' => '', // this is the important bit
- 'ar_actor' => null,
- 'ar_content_format' => null,
- 'ar_content_model' => null,
- ];
- \Wikimedia\suppressWarnings();
- $record = $store->newRevisionFromArchiveRow( $row );
- \Wikimedia\suppressWarnings( true );
- $this->assertInstanceOf( RevisionRecord::class, $record );
- $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
- $this->assertSame( 'Unknown user', $record->getUser()->getName() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
- */
- public function testNewRevisionFromRow_no_user() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $title = Title::newFromText( __METHOD__ );
- $row = (object)[
- 'rev_id' => '2',
- 'rev_page' => '2',
- 'page_namespace' => '0',
- 'page_title' => $title->getText(),
- 'rev_text_id' => '47',
- 'rev_timestamp' => '20180528192356',
- 'rev_minor_edit' => '0',
- 'rev_deleted' => '0',
- 'rev_len' => '78',
- 'rev_parent_id' => '0',
- 'rev_sha1' => 'deadbeef',
- 'rev_comment_text' => 'whatever',
- 'rev_comment_data' => null,
- 'rev_comment_cid' => null,
- 'rev_user' => '0',
- 'rev_user_text' => '', // this is the important bit
- 'rev_actor' => null,
- 'rev_content_format' => null,
- 'rev_content_model' => null,
- ];
- \Wikimedia\suppressWarnings();
- $record = $store->newRevisionFromRow( $row, 0, $title );
- \Wikimedia\suppressWarnings( true );
- $this->assertNotNull( $record );
- $this->assertNotNull( $record->getUser() );
- $this->assertNotEmpty( $record->getUser()->getName() );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
- */
- public function testInsertRevisionOn_archive() {
- // This is a round trip test for deletion and undeletion of a
- // revision row via the archive table.
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $title = Title::newFromText( __METHOD__ );
- $page = WikiPage::factory( $title );
- /** @var Revision $origRev */
- $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
- $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
- ->value['revision'];
- $orig = $origRev->getRevisionRecord();
- $page->doDeleteArticle( __METHOD__ );
- // re-create page, so we can later load revisions for it
- $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
- $db = wfGetDB( DB_MASTER );
- $arQuery = $store->getArchiveQueryInfo();
- $row = $db->selectRow(
- $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
- __METHOD__, [], $arQuery['joins']
- );
- $this->assertNotFalse( $row, 'query failed' );
- $record = $store->newRevisionFromArchiveRow(
- $row,
- 0,
- $title,
- [ 'page_id' => $title->getArticleID() ]
- );
- $restored = $store->insertRevisionOn( $record, $db );
- // is the new revision correct?
- $this->assertRevisionCompleteness( $restored );
- $this->assertRevisionRecordsEqual( $record, $restored );
- // does the new revision use the original slot?
- $recMain = $record->getSlot( 'main' );
- $restMain = $restored->getSlot( 'main' );
- $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
- $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
- $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
- $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
- // can we load it from the store?
- $loaded = $store->getRevisionById( $restored->getId() );
- $this->assertNotNull( $loaded );
- $this->assertRevisionCompleteness( $loaded );
- $this->assertRevisionRecordsEqual( $restored, $loaded );
- // can we find it directly in the database?
- $this->assertRevisionExistsInDatabase( $restored );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
- */
- public function testLoadRevisionFromId() {
- $title = Title::newFromText( __METHOD__ );
- $page = WikiPage::factory( $title );
- /** @var Revision $rev */
- $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
- $this->assertRevisionRecordMatchesRevision( $rev, $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
- */
- public function testLoadRevisionFromPageId() {
- $title = Title::newFromText( __METHOD__ );
- $page = WikiPage::factory( $title );
- /** @var Revision $rev */
- $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
- $this->assertRevisionRecordMatchesRevision( $rev, $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
- */
- public function testLoadRevisionFromTitle() {
- $title = Title::newFromText( __METHOD__ );
- $page = WikiPage::factory( $title );
- /** @var Revision $rev */
- $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
- $this->assertRevisionRecordMatchesRevision( $rev, $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
- */
- public function testLoadRevisionFromTimestamp() {
- $title = Title::newFromText( __METHOD__ );
- $page = WikiPage::factory( $title );
- /** @var Revision $revOne */
- $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- // Sleep to ensure different timestamps... )(evil)
- sleep( 1 );
- /** @var Revision $revTwo */
- $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $this->assertNull(
- $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
- );
- $this->assertSame(
- $revOne->getId(),
- $store->loadRevisionFromTimestamp(
- wfGetDB( DB_MASTER ),
- $title,
- $revOne->getTimestamp()
- )->getId()
- );
- $this->assertSame(
- $revTwo->getId(),
- $store->loadRevisionFromTimestamp(
- wfGetDB( DB_MASTER ),
- $title,
- $revTwo->getTimestamp()
- )->getId()
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
- */
- public function testGetParentLengths() {
- $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
- /** @var Revision $revOne */
- $revOne = $page->doEditContent(
- new WikitextContent( __METHOD__ ), __METHOD__
- )->value['revision'];
- /** @var Revision $revTwo */
- $revTwo = $page->doEditContent(
- new WikitextContent( __METHOD__ . '2' ), __METHOD__
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $this->assertSame(
- [
- $revOne->getId() => strlen( __METHOD__ ),
- ],
- $store->listRevisionSizes(
- wfGetDB( DB_MASTER ),
- [ $revOne->getId() ]
- )
- );
- $this->assertSame(
- [
- $revOne->getId() => strlen( __METHOD__ ),
- $revTwo->getId() => strlen( __METHOD__ ) + 1,
- ],
- $store->listRevisionSizes(
- wfGetDB( DB_MASTER ),
- [ $revOne->getId(), $revTwo->getId() ]
- )
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
- */
- public function testGetPreviousRevision() {
- $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
- /** @var Revision $revOne */
- $revOne = $page->doEditContent(
- new WikitextContent( __METHOD__ ), __METHOD__
- )->value['revision'];
- /** @var Revision $revTwo */
- $revTwo = $page->doEditContent(
- new WikitextContent( __METHOD__ . '2' ), __METHOD__
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $this->assertNull(
- $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
- );
- $this->assertSame(
- $revOne->getId(),
- $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
- */
- public function testGetNextRevision() {
- $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
- /** @var Revision $revOne */
- $revOne = $page->doEditContent(
- new WikitextContent( __METHOD__ ), __METHOD__
- )->value['revision'];
- /** @var Revision $revTwo */
- $revTwo = $page->doEditContent(
- new WikitextContent( __METHOD__ . '2' ), __METHOD__
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $this->assertSame(
- $revTwo->getId(),
- $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
- );
- $this->assertNull(
- $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
- */
- public function testGetTimestampFromId_found() {
- $page = $this->getTestPage();
- /** @var Revision $rev */
- $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->getTimestampFromId(
- $page->getTitle(),
- $rev->getId()
- );
- $this->assertSame( $rev->getTimestamp(), $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
- */
- public function testGetTimestampFromId_notFound() {
- $page = $this->getTestPage();
- /** @var Revision $rev */
- $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
- ->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->getTimestampFromId(
- $page->getTitle(),
- $rev->getId() + 1
- );
- $this->assertFalse( $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
- */
- public function testCountRevisionsByPageId() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
- $this->assertSame(
- 0,
- $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
- );
- $page->doEditContent( new WikitextContent( 'a' ), 'a' );
- $this->assertSame(
- 1,
- $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
- );
- $page->doEditContent( new WikitextContent( 'b' ), 'b' );
- $this->assertSame(
- 2,
- $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
- */
- public function testCountRevisionsByTitle() {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
- $this->assertSame(
- 0,
- $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
- );
- $page->doEditContent( new WikitextContent( 'a' ), 'a' );
- $this->assertSame(
- 1,
- $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
- );
- $page->doEditContent( new WikitextContent( 'b' ), 'b' );
- $this->assertSame(
- 2,
- $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
- );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
- */
- public function testUserWasLastToEdit_false() {
- $sysop = $this->getTestSysop()->getUser();
- $page = $this->getTestPage();
- $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->userWasLastToEdit(
- wfGetDB( DB_MASTER ),
- $page->getId(),
- $sysop->getId(),
- '20160101010101'
- );
- $this->assertFalse( $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
- */
- public function testUserWasLastToEdit_true() {
- $startTime = wfTimestampNow();
- $sysop = $this->getTestSysop()->getUser();
- $page = $this->getTestPage();
- $page->doEditContent(
- new WikitextContent( __METHOD__ ),
- __METHOD__,
- 0,
- false,
- $sysop
- );
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $result = $store->userWasLastToEdit(
- wfGetDB( DB_MASTER ),
- $page->getId(),
- $sysop->getId(),
- $startTime
- );
- $this->assertTrue( $result );
- }
- /**
- * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
- */
- public function testGetKnownCurrentRevision() {
- $page = $this->getTestPage();
- /** @var Revision $rev */
- $rev = $page->doEditContent(
- new WikitextContent( __METHOD__ . 'b' ),
- __METHOD__ . 'b',
- 0,
- false,
- $this->getTestUser()->getUser()
- )->value['revision'];
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- $record = $store->getKnownCurrentRevision(
- $page->getTitle(),
- $rev->getId()
- );
- $this->assertRevisionRecordMatchesRevision( $rev, $record );
- }
- public function provideNewMutableRevisionFromArray() {
- yield 'Basic array, content object' => [
- [
- 'id' => 2,
- 'page' => 1,
- 'timestamp' => '20171017114835',
- 'user_text' => '111.0.1.2',
- 'user' => 0,
- 'minor_edit' => false,
- 'deleted' => 0,
- 'len' => 46,
- 'parent_id' => 1,
- 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
- 'comment' => 'Goat Comment!',
- 'content' => new WikitextContent( 'Some Content' ),
- ]
- ];
- yield 'Basic array, serialized text' => [
- [
- 'id' => 2,
- 'page' => 1,
- 'timestamp' => '20171017114835',
- 'user_text' => '111.0.1.2',
- 'user' => 0,
- 'minor_edit' => false,
- 'deleted' => 0,
- 'len' => 46,
- 'parent_id' => 1,
- 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
- 'comment' => 'Goat Comment!',
- 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
- ]
- ];
- yield 'Basic array, serialized text, utf-8 flags' => [
- [
- 'id' => 2,
- 'page' => 1,
- 'timestamp' => '20171017114835',
- 'user_text' => '111.0.1.2',
- 'user' => 0,
- 'minor_edit' => false,
- 'deleted' => 0,
- 'len' => 46,
- 'parent_id' => 1,
- 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
- 'comment' => 'Goat Comment!',
- 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
- 'flags' => 'utf-8',
- ]
- ];
- yield 'Basic array, with title' => [
- [
- 'title' => Title::newFromText( 'SomeText' ),
- 'timestamp' => '20171017114835',
- 'user_text' => '111.0.1.2',
- 'user' => 0,
- 'minor_edit' => false,
- 'deleted' => 0,
- 'len' => 46,
- 'parent_id' => 1,
- 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
- 'comment' => 'Goat Comment!',
- 'content' => new WikitextContent( 'Some Content' ),
- ]
- ];
- yield 'Basic array, no user field' => [
- [
- 'id' => 2,
- 'page' => 1,
- 'timestamp' => '20171017114835',
- 'user_text' => '111.0.1.3',
- 'minor_edit' => false,
- 'deleted' => 0,
- 'len' => 46,
- 'parent_id' => 1,
- 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
- 'comment' => 'Goat Comment!',
- 'content' => new WikitextContent( 'Some Content' ),
- ]
- ];
- }
- /**
- * @dataProvider provideNewMutableRevisionFromArray
- * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
- */
- public function testNewMutableRevisionFromArray( array $array ) {
- $store = MediaWikiServices::getInstance()->getRevisionStore();
- // HACK: if $array['page'] is given, make sure that the page exists
- if ( isset( $array['page'] ) ) {
- $t = Title::newFromID( $array['page'] );
- if ( !$t || !$t->exists() ) {
- $t = Title::makeTitle( NS_MAIN, __METHOD__ );
- $info = $this->insertPage( $t );
- $array['page'] = $info['id'];
- }
- }
- $result = $store->newMutableRevisionFromArray( $array );
- if ( isset( $array['id'] ) ) {
- $this->assertSame( $array['id'], $result->getId() );
- }
- if ( isset( $array['page'] ) ) {
- $this->assertSame( $array['page'], $result->getPageId() );
- }
- $this->assertSame( $array['timestamp'], $result->getTimestamp() );
- $this->assertSame( $array['user_text'], $result->getUser()->getName() );
- if ( isset( $array['user'] ) ) {
- $this->assertSame( $array['user'], $result->getUser()->getId() );
- }
- $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
- $this->assertSame( $array['deleted'], $result->getVisibility() );
- $this->assertSame( $array['len'], $result->getSize() );
- $this->assertSame( $array['parent_id'], $result->getParentId() );
- $this->assertSame( $array['sha1'], $result->getSha1() );
- $this->assertSame( $array['comment'], $result->getComment()->text );
- if ( isset( $array['content'] ) ) {
- foreach ( $array['content'] as $role => $content ) {
- $this->assertTrue(
- $result->getContent( $role )->equals( $content )
- );
- }
- } elseif ( isset( $array['text'] ) ) {
- $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
- } elseif ( isset( $array['content_format'] ) ) {
- $this->assertSame(
- $array['content_format'],
- $result->getSlot( 'main' )->getContent()->getDefaultFormat()
- );
- $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
- }
- }
- /**
- * @dataProvider provideNewMutableRevisionFromArray
- * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
- */
- public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
- $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
- $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $blobStore = new SqlBlobStore( $lb, $cache );
- $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
- $factory = $this->getMockBuilder( BlobStoreFactory::class )
- ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
- ->disableOriginalConstructor()
- ->getMock();
- $factory->expects( $this->any() )
- ->method( 'newBlobStore' )
- ->willReturn( $blobStore );
- $factory->expects( $this->any() )
- ->method( 'newSqlBlobStore' )
- ->willReturn( $blobStore );
- $this->setService( 'BlobStoreFactory', $factory );
- $this->testNewMutableRevisionFromArray( $array );
- }
- }
|