979350232a57e8237741a637411d6dc3284f2d1d
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionStoreDbTestBase.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use CommentStoreComment;
6 use Content;
7 use Exception;
8 use HashBagOStuff;
9 use InvalidArgumentException;
10 use Language;
11 use MediaWiki\Linker\LinkTarget;
12 use MediaWiki\MediaWikiServices;
13 use MediaWiki\Storage\BlobStoreFactory;
14 use MediaWiki\Storage\IncompleteRevisionException;
15 use MediaWiki\Storage\MutableRevisionRecord;
16 use MediaWiki\Storage\RevisionRecord;
17 use MediaWiki\Storage\RevisionStore;
18 use MediaWiki\Storage\SlotRecord;
19 use MediaWiki\Storage\SqlBlobStore;
20 use MediaWikiTestCase;
21 use PHPUnit_Framework_MockObject_MockObject;
22 use Revision;
23 use TestUserRegistry;
24 use Title;
25 use WANObjectCache;
26 use Wikimedia\Rdbms\Database;
27 use Wikimedia\Rdbms\DatabaseSqlite;
28 use Wikimedia\Rdbms\FakeResultWrapper;
29 use Wikimedia\Rdbms\LoadBalancer;
30 use Wikimedia\Rdbms\TransactionProfiler;
31 use WikiPage;
32 use WikitextContent;
33
34 /**
35 * @group Database
36 * @group RevisionStore
37 */
38 abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
39
40 /**
41 * @var Title
42 */
43 private $testPageTitle;
44
45 /**
46 * @var WikiPage
47 */
48 private $testPage;
49
50 /**
51 * @return int
52 */
53 abstract protected function getMcrMigrationStage();
54
55 /**
56 * @return bool
57 */
58 protected function getContentHandlerUseDB() {
59 return true;
60 }
61
62 /**
63 * @return string[]
64 */
65 abstract protected function getMcrTablesToReset();
66
67 public function setUp() {
68 parent::setUp();
69 $this->tablesUsed[] = 'archive';
70 $this->tablesUsed[] = 'page';
71 $this->tablesUsed[] = 'revision';
72 $this->tablesUsed[] = 'comment';
73
74 $this->tablesUsed += $this->getMcrTablesToReset();
75
76 $this->setMwGlobals(
77 'wgMultiContentRevisionSchemaMigrationStage',
78 $this->getMcrMigrationStage()
79 );
80
81 $this->setMwGlobals(
82 'wgContentHandlerUseDB',
83 $this->getContentHandlerUseDB()
84 );
85
86 $this->overrideMwServices();
87 }
88
89 protected function addCoreDBData() {
90 // Blank out. This would fail with a modified schema, and we don't need it.
91 }
92
93 /**
94 * @return Title
95 */
96 protected function getTestPageTitle() {
97 if ( $this->testPageTitle ) {
98 return $this->testPageTitle;
99 }
100
101 $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
102 return $this->testPageTitle;
103 }
104 /**
105 * @return WikiPage
106 */
107 protected function getTestPage() {
108 if ( $this->testPage ) {
109 return $this->testPage;
110 }
111
112 $title = $this->getTestPageTitle();
113 $this->testPage = WikiPage::factory( $title );
114
115 if ( !$this->testPage->exists() ) {
116 // Make sure we don't write to the live db.
117 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER ) );
118
119 $user = static::getTestSysop()->getUser();
120
121 $this->testPage->doEditContent(
122 new WikitextContent( 'UTContent-' . __CLASS__ ),
123 'UTPageSummary-' . __CLASS__,
124 EDIT_NEW | EDIT_SUPPRESS_RC,
125 false,
126 $user
127 );
128 }
129
130 return $this->testPage;
131 }
132
133 /**
134 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
135 */
136 private function getLoadBalancerMock( array $server ) {
137 $lb = $this->getMockBuilder( LoadBalancer::class )
138 ->setMethods( [ 'reallyOpenConnection' ] )
139 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
140 ->getMock();
141
142 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
143 function ( array $server, $dbNameOverride ) {
144 return $this->getDatabaseMock( $server );
145 }
146 );
147
148 return $lb;
149 }
150
151 /**
152 * @return Database|PHPUnit_Framework_MockObject_MockObject
153 */
154 private function getDatabaseMock( array $params ) {
155 $db = $this->getMockBuilder( DatabaseSqlite::class )
156 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
157 ->setConstructorArgs( [ $params ] )
158 ->getMock();
159
160 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
161 $db->method( 'isOpen' )->willReturn( true );
162
163 return $db;
164 }
165
166 public function provideDomainCheck() {
167 yield [ false, 'test', '' ];
168 yield [ 'test', 'test', '' ];
169
170 yield [ false, 'test', 'foo_' ];
171 yield [ 'test-foo_', 'test', 'foo_' ];
172
173 yield [ false, 'dash-test', '' ];
174 yield [ 'dash-test', 'dash-test', '' ];
175
176 yield [ false, 'underscore_test', 'foo_' ];
177 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
178 }
179
180 /**
181 * @dataProvider provideDomainCheck
182 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
183 */
184 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
185 $this->setMwGlobals(
186 [
187 'wgDBname' => $dbName,
188 'wgDBprefix' => $dbPrefix,
189 ]
190 );
191
192 $loadBalancer = $this->getLoadBalancerMock(
193 [
194 'host' => '*dummy*',
195 'dbDirectory' => '*dummy*',
196 'user' => 'test',
197 'password' => 'test',
198 'flags' => 0,
199 'variables' => [],
200 'schema' => '',
201 'cliMode' => true,
202 'agent' => '',
203 'load' => 100,
204 'profiler' => null,
205 'trxProfiler' => new TransactionProfiler(),
206 'connLogger' => new \Psr\Log\NullLogger(),
207 'queryLogger' => new \Psr\Log\NullLogger(),
208 'errorLogger' => function () {
209 },
210 'deprecationLogger' => function () {
211 },
212 'type' => 'test',
213 'dbname' => $dbName,
214 'tablePrefix' => $dbPrefix,
215 ]
216 );
217 $db = $loadBalancer->getConnection( DB_REPLICA );
218
219 /** @var SqlBlobStore $blobStore */
220 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
221 ->disableOriginalConstructor()
222 ->getMock();
223
224 $store = new RevisionStore(
225 $loadBalancer,
226 $blobStore,
227 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
228 MediaWikiServices::getInstance()->getCommentStore(),
229 MediaWikiServices::getInstance()->getContentModelStore(),
230 MediaWikiServices::getInstance()->getSlotRoleStore(),
231 $this->getMcrMigrationStage(),
232 MediaWikiServices::getInstance()->getActorMigration(),
233 $wikiId
234 );
235
236 $count = $store->countRevisionsByPageId( $db, 0 );
237
238 // Dummy check to make PhpUnit happy. We are really only interested in
239 // countRevisionsByPageId not failing due to the DB domain check.
240 $this->assertSame( 0, $count );
241 }
242
243 private function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
244 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
245 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
246 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
247 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
248 }
249
250 private function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
251 $this->assertEquals(
252 $r1->getPageAsLinkTarget()->getNamespace(),
253 $r2->getPageAsLinkTarget()->getNamespace()
254 );
255
256 $this->assertEquals(
257 $r1->getPageAsLinkTarget()->getText(),
258 $r2->getPageAsLinkTarget()->getText()
259 );
260
261 if ( $r1->getParentId() ) {
262 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
263 }
264
265 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
266 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
267 $this->assertEquals( $r1->getComment(), $r2->getComment() );
268 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
269 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
270 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
271 $this->assertEquals( $r1->getSize(), $r2->getSize() );
272 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
273 $this->assertArrayEqualsIgnoringIntKeyOrder( $r1->getSlotRoles(), $r2->getSlotRoles() );
274 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
275 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
276 foreach ( $r1->getSlotRoles() as $role ) {
277 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
278 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
279 }
280 foreach ( [
281 RevisionRecord::DELETED_TEXT,
282 RevisionRecord::DELETED_COMMENT,
283 RevisionRecord::DELETED_USER,
284 RevisionRecord::DELETED_RESTRICTED,
285 ] as $field ) {
286 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
287 }
288 }
289
290 private function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
291 $this->assertSame( $s1->getRole(), $s2->getRole() );
292 $this->assertSame( $s1->getModel(), $s2->getModel() );
293 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
294 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
295 $this->assertSame( $s1->getSize(), $s2->getSize() );
296 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
297
298 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
299 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
300 }
301
302 private function assertRevisionCompleteness( RevisionRecord $r ) {
303 $this->assertTrue( $r->hasSlot( 'main' ) );
304 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( 'main' ) );
305 $this->assertInstanceOf( Content::class, $r->getContent( 'main' ) );
306
307 foreach ( $r->getSlotRoles() as $role ) {
308 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
309 }
310 }
311
312 private function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
313 $this->assertTrue( $slot->hasAddress() );
314 $this->assertSame( $r->getId(), $slot->getRevision() );
315
316 $this->assertInstanceOf( Content::class, $slot->getContent() );
317 }
318
319 /**
320 * @param mixed[] $details
321 *
322 * @return RevisionRecord
323 */
324 private function getRevisionRecordFromDetailsArray( $details = [] ) {
325 // Convert some values that can't be provided by dataProviders
326 if ( isset( $details['user'] ) && $details['user'] === true ) {
327 $details['user'] = $this->getTestUser()->getUser();
328 }
329 if ( isset( $details['page'] ) && $details['page'] === true ) {
330 $details['page'] = $this->getTestPage()->getId();
331 }
332 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
333 $details['parent'] = $this->getTestPage()->getLatest();
334 }
335
336 // Create the RevisionRecord with any available data
337 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
338 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
339 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
340 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
341 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
342 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
343 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
344 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
345 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
346 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
347 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
348 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
349
350 if ( isset( $details['content'] ) ) {
351 foreach ( $details['content'] as $role => $content ) {
352 $rev->setContent( $role, $content );
353 }
354 }
355
356 return $rev;
357 }
358
359 public function provideInsertRevisionOn_successes() {
360 yield 'Bare minimum revision insertion' => [
361 [
362 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
363 'page' => true,
364 'comment' => $this->getRandomCommentStoreComment(),
365 'timestamp' => '20171117010101',
366 'user' => true,
367 ],
368 ];
369 yield 'Detailed revision insertion' => [
370 [
371 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
372 'parent' => true,
373 'page' => true,
374 'comment' => $this->getRandomCommentStoreComment(),
375 'timestamp' => '20171117010101',
376 'user' => true,
377 'minor' => true,
378 'visibility' => RevisionRecord::DELETED_RESTRICTED,
379 ],
380 ];
381 }
382
383 protected function getRandomCommentStoreComment() {
384 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
385 }
386
387 /**
388 * @dataProvider provideInsertRevisionOn_successes
389 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
390 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
391 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
392 */
393 public function testInsertRevisionOn_successes(
394 array $revDetails = []
395 ) {
396 // FIXME: fails under postgres
397 $this->markTestSkippedIfDbType( 'postgres' );
398
399 $title = $this->getTestPageTitle();
400 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
401
402 $this->overrideMwServices();
403 $store = MediaWikiServices::getInstance()->getRevisionStore();
404 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
405
406 // is the new revision correct?
407 $this->assertRevisionCompleteness( $return );
408 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
409 $this->assertRevisionRecordsEqual( $rev, $return );
410
411 // can we load it from the store?
412 $loaded = $store->getRevisionById( $return->getId() );
413 $this->assertRevisionCompleteness( $loaded );
414 $this->assertRevisionRecordsEqual( $return, $loaded );
415
416 // can we find it directly in the database?
417 $this->assertRevisionExistsInDatabase( $return );
418 }
419
420 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
421 $row = $this->revisionToRow( new Revision( $rev ), [] );
422
423 // unset nulled fields
424 unset( $row->rev_content_model );
425 unset( $row->rev_content_format );
426
427 // unset fake fields
428 unset( $row->rev_comment_text );
429 unset( $row->rev_comment_data );
430 unset( $row->rev_comment_cid );
431 unset( $row->rev_comment_id );
432
433 $store = MediaWikiServices::getInstance()->getRevisionStore();
434 $queryInfo = $store->getQueryInfo( [ 'user' ] );
435
436 $row = get_object_vars( $row );
437 $this->assertSelect(
438 $queryInfo['tables'],
439 array_keys( $row ),
440 [ 'rev_id' => $rev->getId() ],
441 [ array_values( $row ) ],
442 [],
443 $queryInfo['joins']
444 );
445 }
446
447 /**
448 * @param SlotRecord $a
449 * @param SlotRecord $b
450 */
451 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
452 // Assert that the same blob address has been used.
453 $this->assertSame( $a->getAddress(), $b->getAddress() );
454 }
455
456 /**
457 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
458 */
459 public function testInsertRevisionOn_blobAddressExists() {
460 $title = $this->getTestPageTitle();
461 $revDetails = [
462 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
463 'parent' => true,
464 'comment' => $this->getRandomCommentStoreComment(),
465 'timestamp' => '20171117010101',
466 'user' => true,
467 ];
468
469 $this->overrideMwServices();
470 $store = MediaWikiServices::getInstance()->getRevisionStore();
471
472 // Insert the first revision
473 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
474 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
475 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
476 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
477
478 // Insert a second revision inheriting the same blob address
479 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) );
480 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
481 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
482 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
483 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
484
485 $firstMainSlot = $firstReturn->getSlot( 'main' );
486 $secondMainSlot = $secondReturn->getSlot( 'main' );
487
488 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
489
490 // And that different revisions have been created.
491 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
492
493 // Make sure the slot rows reference the correct revision
494 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
495 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
496 }
497
498 public function provideInsertRevisionOn_failures() {
499 yield 'no slot' => [
500 [
501 'comment' => $this->getRandomCommentStoreComment(),
502 'timestamp' => '20171117010101',
503 'user' => true,
504 ],
505 new InvalidArgumentException( 'main slot must be provided' )
506 ];
507 yield 'no main slot' => [
508 [
509 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
510 'comment' => $this->getRandomCommentStoreComment(),
511 'timestamp' => '20171117010101',
512 'user' => true,
513 ],
514 new InvalidArgumentException( 'main slot must be provided' )
515 ];
516 yield 'no timestamp' => [
517 [
518 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
519 'comment' => $this->getRandomCommentStoreComment(),
520 'user' => true,
521 ],
522 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
523 ];
524 yield 'no comment' => [
525 [
526 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
527 'timestamp' => '20171117010101',
528 'user' => true,
529 ],
530 new IncompleteRevisionException( 'comment must not be NULL!' )
531 ];
532 yield 'no user' => [
533 [
534 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
535 'comment' => $this->getRandomCommentStoreComment(),
536 'timestamp' => '20171117010101',
537 ],
538 new IncompleteRevisionException( 'user must not be NULL!' )
539 ];
540 }
541
542 /**
543 * @dataProvider provideInsertRevisionOn_failures
544 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
545 */
546 public function testInsertRevisionOn_failures(
547 array $revDetails = [],
548 Exception $exception
549 ) {
550 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
551
552 $store = MediaWikiServices::getInstance()->getRevisionStore();
553
554 $this->setExpectedException(
555 get_class( $exception ),
556 $exception->getMessage(),
557 $exception->getCode()
558 );
559 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
560 }
561
562 public function provideNewNullRevision() {
563 yield [
564 Title::newFromText( 'UTPage_notAutoCreated' ),
565 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
566 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
567 true,
568 ];
569 yield [
570 Title::newFromText( 'UTPage_notAutoCreated' ),
571 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
572 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
573 false,
574 ];
575 }
576
577 /**
578 * @dataProvider provideNewNullRevision
579 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
580 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
581 */
582 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
583 $this->overrideMwServices();
584
585 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
586 $page = WikiPage::factory( $title );
587
588 if ( !$page->exists() ) {
589 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
590 }
591
592 $revDetails['page'] = $page->getId();
593 $revDetails['timestamp'] = wfTimestampNow();
594 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
595 $revDetails['user'] = $user;
596
597 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
598 $store = MediaWikiServices::getInstance()->getRevisionStore();
599
600 $dbw = wfGetDB( DB_MASTER );
601 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
602 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
603
604 $record = $store->newNullRevision(
605 wfGetDB( DB_MASTER ),
606 $title,
607 $comment,
608 $minor,
609 $user
610 );
611
612 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
613 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
614 $this->assertEquals( $comment, $record->getComment() );
615 $this->assertEquals( $minor, $record->isMinor() );
616 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
617 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
618
619 $this->assertArrayEqualsIgnoringIntKeyOrder(
620 $baseRev->getSlotRoles(),
621 $record->getSlotRoles()
622 );
623
624 foreach ( $baseRev->getSlotRoles() as $role ) {
625 $parentSlot = $baseRev->getSlot( $role );
626 $slot = $record->getSlot( $role );
627
628 $this->assertTrue( $slot->isInherited(), 'isInherited' );
629 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
630 $this->assertSameSlotContent( $parentSlot, $slot );
631 }
632 }
633
634 /**
635 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
636 */
637 public function testNewNullRevision_nonExistingTitle() {
638 $store = MediaWikiServices::getInstance()->getRevisionStore();
639 $record = $store->newNullRevision(
640 wfGetDB( DB_MASTER ),
641 Title::newFromText( __METHOD__ . '.iDontExist!' ),
642 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
643 false,
644 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
645 );
646 $this->assertNull( $record );
647 }
648
649 /**
650 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
651 */
652 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
653 $page = $this->getTestPage();
654 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
655 /** @var Revision $rev */
656 $rev = $status->value['revision'];
657
658 $store = MediaWikiServices::getInstance()->getRevisionStore();
659 $revisionRecord = $store->getRevisionById( $rev->getId() );
660 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
661
662 $this->assertGreaterThan( 0, $result );
663 $this->assertSame(
664 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
665 $result
666 );
667 }
668
669 /**
670 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
671 */
672 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
673 // This assumes that sysops are auto patrolled
674 $sysop = $this->getTestSysop()->getUser();
675 $page = $this->getTestPage();
676 $status = $page->doEditContent(
677 new WikitextContent( __METHOD__ ),
678 __METHOD__,
679 0,
680 false,
681 $sysop
682 );
683 /** @var Revision $rev */
684 $rev = $status->value['revision'];
685
686 $store = MediaWikiServices::getInstance()->getRevisionStore();
687 $revisionRecord = $store->getRevisionById( $rev->getId() );
688 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
689
690 $this->assertSame( 0, $result );
691 }
692
693 /**
694 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
695 */
696 public function testGetRecentChange() {
697 $page = $this->getTestPage();
698 $content = new WikitextContent( __METHOD__ );
699 $status = $page->doEditContent( $content, __METHOD__ );
700 /** @var Revision $rev */
701 $rev = $status->value['revision'];
702
703 $store = MediaWikiServices::getInstance()->getRevisionStore();
704 $revRecord = $store->getRevisionById( $rev->getId() );
705 $recentChange = $store->getRecentChange( $revRecord );
706
707 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
708 $this->assertEquals( $rev->getRecentChange(), $recentChange );
709 }
710
711 /**
712 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
713 */
714 public function testGetRevisionById() {
715 $page = $this->getTestPage();
716 $content = new WikitextContent( __METHOD__ );
717 $status = $page->doEditContent( $content, __METHOD__ );
718 /** @var Revision $rev */
719 $rev = $status->value['revision'];
720
721 $store = MediaWikiServices::getInstance()->getRevisionStore();
722 $revRecord = $store->getRevisionById( $rev->getId() );
723
724 $this->assertSame( $rev->getId(), $revRecord->getId() );
725 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
726 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
727 }
728
729 /**
730 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
731 */
732 public function testGetRevisionByTitle() {
733 $page = $this->getTestPage();
734 $content = new WikitextContent( __METHOD__ );
735 $status = $page->doEditContent( $content, __METHOD__ );
736 /** @var Revision $rev */
737 $rev = $status->value['revision'];
738
739 $store = MediaWikiServices::getInstance()->getRevisionStore();
740 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
741
742 $this->assertSame( $rev->getId(), $revRecord->getId() );
743 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
744 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
745 }
746
747 /**
748 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
749 */
750 public function testGetRevisionByPageId() {
751 $page = $this->getTestPage();
752 $content = new WikitextContent( __METHOD__ );
753 $status = $page->doEditContent( $content, __METHOD__ );
754 /** @var Revision $rev */
755 $rev = $status->value['revision'];
756
757 $store = MediaWikiServices::getInstance()->getRevisionStore();
758 $revRecord = $store->getRevisionByPageId( $page->getId() );
759
760 $this->assertSame( $rev->getId(), $revRecord->getId() );
761 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
762 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
763 }
764
765 /**
766 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
767 */
768 public function testGetRevisionByTimestamp() {
769 // Make sure there is 1 second between the last revision and the rev we create...
770 // Otherwise we might not get the correct revision and the test may fail...
771 // :(
772 $page = $this->getTestPage();
773 sleep( 1 );
774 $content = new WikitextContent( __METHOD__ );
775 $status = $page->doEditContent( $content, __METHOD__ );
776 /** @var Revision $rev */
777 $rev = $status->value['revision'];
778
779 $store = MediaWikiServices::getInstance()->getRevisionStore();
780 $revRecord = $store->getRevisionByTimestamp(
781 $page->getTitle(),
782 $rev->getTimestamp()
783 );
784
785 $this->assertSame( $rev->getId(), $revRecord->getId() );
786 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
787 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
788 }
789
790 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
791 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
792 $page = WikiPage::factory( $rev->getTitle() );
793
794 $fields = [
795 'rev_id' => (string)$rev->getId(),
796 'rev_page' => (string)$rev->getPage(),
797 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
798 'rev_user_text' => (string)$rev->getUserText(),
799 'rev_user' => (string)$rev->getUser(),
800 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
801 'rev_deleted' => (string)$rev->getVisibility(),
802 'rev_len' => (string)$rev->getSize(),
803 'rev_parent_id' => (string)$rev->getParentId(),
804 'rev_sha1' => (string)$rev->getSha1(),
805 ];
806
807 if ( in_array( 'page', $options ) ) {
808 $fields += [
809 'page_namespace' => (string)$page->getTitle()->getNamespace(),
810 'page_title' => $page->getTitle()->getDBkey(),
811 'page_id' => (string)$page->getId(),
812 'page_latest' => (string)$page->getLatest(),
813 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
814 'page_len' => (string)$page->getContent()->getSize(),
815 ];
816 }
817
818 if ( in_array( 'user', $options ) ) {
819 $fields += [
820 'user_name' => (string)$rev->getUserText(),
821 ];
822 }
823
824 if ( in_array( 'comment', $options ) ) {
825 $fields += [
826 'rev_comment_text' => $rev->getComment(),
827 'rev_comment_data' => null,
828 'rev_comment_cid' => null,
829 ];
830 }
831
832 if ( $rev->getId() ) {
833 $fields += [
834 'rev_id' => (string)$rev->getId(),
835 ];
836 }
837
838 return (object)$fields;
839 }
840
841 private function assertRevisionRecordMatchesRevision(
842 Revision $rev,
843 RevisionRecord $record
844 ) {
845 $this->assertSame( $rev->getId(), $record->getId() );
846 $this->assertSame( $rev->getPage(), $record->getPageId() );
847 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
848 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
849 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
850 $this->assertSame( $rev->isMinor(), $record->isMinor() );
851 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
852 $this->assertSame( $rev->getSize(), $record->getSize() );
853 /**
854 * @note As of MW 1.31, the database schema allows the parent ID to be
855 * NULL to indicate that it is unknown.
856 */
857 $expectedParent = $rev->getParentId();
858 if ( $expectedParent === null ) {
859 $expectedParent = 0;
860 }
861 $this->assertSame( $expectedParent, $record->getParentId() );
862 $this->assertSame( $rev->getSha1(), $record->getSha1() );
863 $this->assertSame( $rev->getComment(), $record->getComment()->text );
864 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
865 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
866 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
867
868 $revRec = $rev->getRevisionRecord();
869 $revMain = $revRec->getSlot( 'main' );
870 $recMain = $record->getSlot( 'main' );
871
872 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
873 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
874 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
875
876 if ( $revMain->hasOrigin() ) {
877 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
878 }
879
880 if ( $revMain->hasAddress() ) {
881 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
882 }
883
884 if ( $revMain->hasContentId() ) {
885 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
886 }
887 }
888
889 /**
890 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
891 */
892 public function testNewRevisionFromRow_anonEdit() {
893 $page = $this->getTestPage();
894 $text = __METHOD__ . 'a-ä';
895 /** @var Revision $rev */
896 $rev = $page->doEditContent(
897 new WikitextContent( $text ),
898 __METHOD__ . 'a'
899 )->value['revision'];
900
901 $store = MediaWikiServices::getInstance()->getRevisionStore();
902 $record = $store->newRevisionFromRow(
903 $this->revisionToRow( $rev ),
904 [],
905 $page->getTitle()
906 );
907 $this->assertRevisionRecordMatchesRevision( $rev, $record );
908 $this->assertSame( $text, $rev->getContent()->serialize() );
909 }
910
911 /**
912 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
913 */
914 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
915 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
916 $this->overrideMwServices();
917 $page = $this->getTestPage();
918 $text = __METHOD__ . 'a-ä';
919 /** @var Revision $rev */
920 $rev = $page->doEditContent(
921 new WikitextContent( $text ),
922 __METHOD__. 'a'
923 )->value['revision'];
924
925 $store = MediaWikiServices::getInstance()->getRevisionStore();
926 $record = $store->newRevisionFromRow(
927 $this->revisionToRow( $rev ),
928 [],
929 $page->getTitle()
930 );
931 $this->assertRevisionRecordMatchesRevision( $rev, $record );
932 $this->assertSame( $text, $rev->getContent()->serialize() );
933 }
934
935 /**
936 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
937 */
938 public function testNewRevisionFromRow_userEdit() {
939 $page = $this->getTestPage();
940 $text = __METHOD__ . 'b-ä';
941 /** @var Revision $rev */
942 $rev = $page->doEditContent(
943 new WikitextContent( $text ),
944 __METHOD__ . 'b',
945 0,
946 false,
947 $this->getTestUser()->getUser()
948 )->value['revision'];
949
950 $store = MediaWikiServices::getInstance()->getRevisionStore();
951 $record = $store->newRevisionFromRow(
952 $this->revisionToRow( $rev ),
953 [],
954 $page->getTitle()
955 );
956 $this->assertRevisionRecordMatchesRevision( $rev, $record );
957 $this->assertSame( $text, $rev->getContent()->serialize() );
958 }
959
960 /**
961 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
962 */
963 public function testNewRevisionFromArchiveRow() {
964 $store = MediaWikiServices::getInstance()->getRevisionStore();
965 $title = Title::newFromText( __METHOD__ );
966 $text = __METHOD__ . '-bä';
967 $page = WikiPage::factory( $title );
968 /** @var Revision $orig */
969 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
970 ->value['revision'];
971 $page->doDeleteArticle( __METHOD__ );
972
973 $db = wfGetDB( DB_MASTER );
974 $arQuery = $store->getArchiveQueryInfo();
975 $res = $db->select(
976 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
977 __METHOD__, [], $arQuery['joins']
978 );
979 $this->assertTrue( is_object( $res ), 'query failed' );
980
981 $row = $res->fetchObject();
982 $res->free();
983 $record = $store->newRevisionFromArchiveRow( $row );
984
985 $this->assertRevisionRecordMatchesRevision( $orig, $record );
986 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
987 }
988
989 /**
990 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
991 */
992 public function testNewRevisionFromArchiveRow_legacyEncoding() {
993 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
994 $this->overrideMwServices();
995 $store = MediaWikiServices::getInstance()->getRevisionStore();
996 $title = Title::newFromText( __METHOD__ );
997 $text = __METHOD__ . '-bä';
998 $page = WikiPage::factory( $title );
999 /** @var Revision $orig */
1000 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1001 ->value['revision'];
1002 $page->doDeleteArticle( __METHOD__ );
1003
1004 $db = wfGetDB( DB_MASTER );
1005 $arQuery = $store->getArchiveQueryInfo();
1006 $res = $db->select(
1007 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1008 __METHOD__, [], $arQuery['joins']
1009 );
1010 $this->assertTrue( is_object( $res ), 'query failed' );
1011
1012 $row = $res->fetchObject();
1013 $res->free();
1014 $record = $store->newRevisionFromArchiveRow( $row );
1015
1016 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1017 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1018 }
1019
1020 /**
1021 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1022 */
1023 public function testInsertRevisionOn_archive() {
1024 $store = MediaWikiServices::getInstance()->getRevisionStore();
1025 $title = Title::newFromText( __METHOD__ );
1026
1027 $page = WikiPage::factory( $title );
1028 /** @var Revision $origRev */
1029 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1030 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1031 ->value['revision'];
1032 $orig = $origRev->getRevisionRecord();
1033 $page->doDeleteArticle( __METHOD__ );
1034
1035 $db = wfGetDB( DB_MASTER );
1036 $arQuery = $store->getArchiveQueryInfo();
1037 $row = $db->selectRow(
1038 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1039 __METHOD__, [], $arQuery['joins']
1040 );
1041
1042 $record = $store->newRevisionFromArchiveRow( $row );
1043
1044 $restored = $store->insertRevisionOn( $record, $db );
1045 $this->assertSame( $orig->getPageId(), $restored->getPageId() );
1046 $this->assertSame( $orig->getId(), $restored->getId() );
1047 $this->assertSame( $orig->getComment()->text, $restored->getComment()->text );
1048
1049 $origMain = $orig->getSlot( 'main' );
1050 $restoredMain = $restored->getSlot( 'main' );
1051 $this->assertSame(
1052 $origMain->getOrigin(),
1053 $restoredMain->getOrigin()
1054 );
1055
1056 if ( $origMain->hasContentId() ) {
1057 $this->assertSame(
1058 $origMain->getContentId(),
1059 $restoredMain->getContentId()
1060 );
1061 }
1062
1063 // NOTE: we didn't restore the page row, so we can't use RevisionStore::getRevisionById
1064 $this->assertSelect(
1065 'revision',
1066 [ 'rev_id' ],
1067 [ 'rev_id' => $orig->getId() ],
1068 [ [ $orig->getId() ] ]
1069 );
1070 }
1071
1072 /**
1073 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1074 */
1075 public function testLoadRevisionFromId() {
1076 $title = Title::newFromText( __METHOD__ );
1077 $page = WikiPage::factory( $title );
1078 /** @var Revision $rev */
1079 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1080 ->value['revision'];
1081
1082 $store = MediaWikiServices::getInstance()->getRevisionStore();
1083 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1084 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1085 }
1086
1087 /**
1088 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1089 */
1090 public function testLoadRevisionFromPageId() {
1091 $title = Title::newFromText( __METHOD__ );
1092 $page = WikiPage::factory( $title );
1093 /** @var Revision $rev */
1094 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1095 ->value['revision'];
1096
1097 $store = MediaWikiServices::getInstance()->getRevisionStore();
1098 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1099 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1100 }
1101
1102 /**
1103 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1104 */
1105 public function testLoadRevisionFromTitle() {
1106 $title = Title::newFromText( __METHOD__ );
1107 $page = WikiPage::factory( $title );
1108 /** @var Revision $rev */
1109 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1110 ->value['revision'];
1111
1112 $store = MediaWikiServices::getInstance()->getRevisionStore();
1113 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1114 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1115 }
1116
1117 /**
1118 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1119 */
1120 public function testLoadRevisionFromTimestamp() {
1121 $title = Title::newFromText( __METHOD__ );
1122 $page = WikiPage::factory( $title );
1123 /** @var Revision $revOne */
1124 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1125 ->value['revision'];
1126 // Sleep to ensure different timestamps... )(evil)
1127 sleep( 1 );
1128 /** @var Revision $revTwo */
1129 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1130 ->value['revision'];
1131
1132 $store = MediaWikiServices::getInstance()->getRevisionStore();
1133 $this->assertNull(
1134 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1135 );
1136 $this->assertSame(
1137 $revOne->getId(),
1138 $store->loadRevisionFromTimestamp(
1139 wfGetDB( DB_MASTER ),
1140 $title,
1141 $revOne->getTimestamp()
1142 )->getId()
1143 );
1144 $this->assertSame(
1145 $revTwo->getId(),
1146 $store->loadRevisionFromTimestamp(
1147 wfGetDB( DB_MASTER ),
1148 $title,
1149 $revTwo->getTimestamp()
1150 )->getId()
1151 );
1152 }
1153
1154 /**
1155 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1156 */
1157 public function testGetParentLengths() {
1158 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1159 /** @var Revision $revOne */
1160 $revOne = $page->doEditContent(
1161 new WikitextContent( __METHOD__ ), __METHOD__
1162 )->value['revision'];
1163 /** @var Revision $revTwo */
1164 $revTwo = $page->doEditContent(
1165 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1166 )->value['revision'];
1167
1168 $store = MediaWikiServices::getInstance()->getRevisionStore();
1169 $this->assertSame(
1170 [
1171 $revOne->getId() => strlen( __METHOD__ ),
1172 ],
1173 $store->listRevisionSizes(
1174 wfGetDB( DB_MASTER ),
1175 [ $revOne->getId() ]
1176 )
1177 );
1178 $this->assertSame(
1179 [
1180 $revOne->getId() => strlen( __METHOD__ ),
1181 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1182 ],
1183 $store->listRevisionSizes(
1184 wfGetDB( DB_MASTER ),
1185 [ $revOne->getId(), $revTwo->getId() ]
1186 )
1187 );
1188 }
1189
1190 /**
1191 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1192 */
1193 public function testGetPreviousRevision() {
1194 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1195 /** @var Revision $revOne */
1196 $revOne = $page->doEditContent(
1197 new WikitextContent( __METHOD__ ), __METHOD__
1198 )->value['revision'];
1199 /** @var Revision $revTwo */
1200 $revTwo = $page->doEditContent(
1201 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1202 )->value['revision'];
1203
1204 $store = MediaWikiServices::getInstance()->getRevisionStore();
1205 $this->assertNull(
1206 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1207 );
1208 $this->assertSame(
1209 $revOne->getId(),
1210 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1211 );
1212 }
1213
1214 /**
1215 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1216 */
1217 public function testGetNextRevision() {
1218 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1219 /** @var Revision $revOne */
1220 $revOne = $page->doEditContent(
1221 new WikitextContent( __METHOD__ ), __METHOD__
1222 )->value['revision'];
1223 /** @var Revision $revTwo */
1224 $revTwo = $page->doEditContent(
1225 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1226 )->value['revision'];
1227
1228 $store = MediaWikiServices::getInstance()->getRevisionStore();
1229 $this->assertSame(
1230 $revTwo->getId(),
1231 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1232 );
1233 $this->assertNull(
1234 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1235 );
1236 }
1237
1238 /**
1239 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1240 */
1241 public function testGetTimestampFromId_found() {
1242 $page = $this->getTestPage();
1243 /** @var Revision $rev */
1244 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1245 ->value['revision'];
1246
1247 $store = MediaWikiServices::getInstance()->getRevisionStore();
1248 $result = $store->getTimestampFromId(
1249 $page->getTitle(),
1250 $rev->getId()
1251 );
1252
1253 $this->assertSame( $rev->getTimestamp(), $result );
1254 }
1255
1256 /**
1257 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1258 */
1259 public function testGetTimestampFromId_notFound() {
1260 $page = $this->getTestPage();
1261 /** @var Revision $rev */
1262 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1263 ->value['revision'];
1264
1265 $store = MediaWikiServices::getInstance()->getRevisionStore();
1266 $result = $store->getTimestampFromId(
1267 $page->getTitle(),
1268 $rev->getId() + 1
1269 );
1270
1271 $this->assertFalse( $result );
1272 }
1273
1274 /**
1275 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1276 */
1277 public function testCountRevisionsByPageId() {
1278 $store = MediaWikiServices::getInstance()->getRevisionStore();
1279 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1280
1281 $this->assertSame(
1282 0,
1283 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1284 );
1285 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1286 $this->assertSame(
1287 1,
1288 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1289 );
1290 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1291 $this->assertSame(
1292 2,
1293 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1294 );
1295 }
1296
1297 /**
1298 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1299 */
1300 public function testCountRevisionsByTitle() {
1301 $store = MediaWikiServices::getInstance()->getRevisionStore();
1302 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1303
1304 $this->assertSame(
1305 0,
1306 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1307 );
1308 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1309 $this->assertSame(
1310 1,
1311 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1312 );
1313 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1314 $this->assertSame(
1315 2,
1316 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1317 );
1318 }
1319
1320 /**
1321 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1322 */
1323 public function testUserWasLastToEdit_false() {
1324 $sysop = $this->getTestSysop()->getUser();
1325 $page = $this->getTestPage();
1326 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1327
1328 $store = MediaWikiServices::getInstance()->getRevisionStore();
1329 $result = $store->userWasLastToEdit(
1330 wfGetDB( DB_MASTER ),
1331 $page->getId(),
1332 $sysop->getId(),
1333 '20160101010101'
1334 );
1335 $this->assertFalse( $result );
1336 }
1337
1338 /**
1339 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1340 */
1341 public function testUserWasLastToEdit_true() {
1342 $startTime = wfTimestampNow();
1343 $sysop = $this->getTestSysop()->getUser();
1344 $page = $this->getTestPage();
1345 $page->doEditContent(
1346 new WikitextContent( __METHOD__ ),
1347 __METHOD__,
1348 0,
1349 false,
1350 $sysop
1351 );
1352
1353 $store = MediaWikiServices::getInstance()->getRevisionStore();
1354 $result = $store->userWasLastToEdit(
1355 wfGetDB( DB_MASTER ),
1356 $page->getId(),
1357 $sysop->getId(),
1358 $startTime
1359 );
1360 $this->assertTrue( $result );
1361 }
1362
1363 /**
1364 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1365 */
1366 public function testGetKnownCurrentRevision() {
1367 $page = $this->getTestPage();
1368 /** @var Revision $rev */
1369 $rev = $page->doEditContent(
1370 new WikitextContent( __METHOD__ . 'b' ),
1371 __METHOD__ . 'b',
1372 0,
1373 false,
1374 $this->getTestUser()->getUser()
1375 )->value['revision'];
1376
1377 $store = MediaWikiServices::getInstance()->getRevisionStore();
1378 $record = $store->getKnownCurrentRevision(
1379 $page->getTitle(),
1380 $rev->getId()
1381 );
1382
1383 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1384 }
1385
1386 public function provideNewMutableRevisionFromArray() {
1387 yield 'Basic array, content object' => [
1388 [
1389 'id' => 2,
1390 'page' => 1,
1391 'timestamp' => '20171017114835',
1392 'user_text' => '111.0.1.2',
1393 'user' => 0,
1394 'minor_edit' => false,
1395 'deleted' => 0,
1396 'len' => 46,
1397 'parent_id' => 1,
1398 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1399 'comment' => 'Goat Comment!',
1400 'content' => new WikitextContent( 'Some Content' ),
1401 ]
1402 ];
1403 yield 'Basic array, serialized text' => [
1404 [
1405 'id' => 2,
1406 'page' => 1,
1407 'timestamp' => '20171017114835',
1408 'user_text' => '111.0.1.2',
1409 'user' => 0,
1410 'minor_edit' => false,
1411 'deleted' => 0,
1412 'len' => 46,
1413 'parent_id' => 1,
1414 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1415 'comment' => 'Goat Comment!',
1416 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1417 ]
1418 ];
1419 yield 'Basic array, serialized text, utf-8 flags' => [
1420 [
1421 'id' => 2,
1422 'page' => 1,
1423 'timestamp' => '20171017114835',
1424 'user_text' => '111.0.1.2',
1425 'user' => 0,
1426 'minor_edit' => false,
1427 'deleted' => 0,
1428 'len' => 46,
1429 'parent_id' => 1,
1430 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1431 'comment' => 'Goat Comment!',
1432 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1433 'flags' => 'utf-8',
1434 ]
1435 ];
1436 yield 'Basic array, with title' => [
1437 [
1438 'title' => Title::newFromText( 'SomeText' ),
1439 'timestamp' => '20171017114835',
1440 'user_text' => '111.0.1.2',
1441 'user' => 0,
1442 'minor_edit' => false,
1443 'deleted' => 0,
1444 'len' => 46,
1445 'parent_id' => 1,
1446 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1447 'comment' => 'Goat Comment!',
1448 'content' => new WikitextContent( 'Some Content' ),
1449 ]
1450 ];
1451 yield 'Basic array, no user field' => [
1452 [
1453 'id' => 2,
1454 'page' => 1,
1455 'timestamp' => '20171017114835',
1456 'user_text' => '111.0.1.3',
1457 'minor_edit' => false,
1458 'deleted' => 0,
1459 'len' => 46,
1460 'parent_id' => 1,
1461 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1462 'comment' => 'Goat Comment!',
1463 'content' => new WikitextContent( 'Some Content' ),
1464 ]
1465 ];
1466 }
1467
1468 /**
1469 * @dataProvider provideNewMutableRevisionFromArray
1470 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1471 */
1472 public function testNewMutableRevisionFromArray( array $array ) {
1473 $store = MediaWikiServices::getInstance()->getRevisionStore();
1474
1475 // HACK: if $array['page'] is given, make sure that the page exists
1476 if ( isset( $array['page'] ) ) {
1477 $t = Title::newFromID( $array['page'] );
1478 if ( !$t || !$t->exists() ) {
1479 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1480 $info = $this->insertPage( $t );
1481 $array['page'] = $info['id'];
1482 }
1483 }
1484
1485 $result = $store->newMutableRevisionFromArray( $array );
1486
1487 if ( isset( $array['id'] ) ) {
1488 $this->assertSame( $array['id'], $result->getId() );
1489 }
1490 if ( isset( $array['page'] ) ) {
1491 $this->assertSame( $array['page'], $result->getPageId() );
1492 }
1493 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1494 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1495 if ( isset( $array['user'] ) ) {
1496 $this->assertSame( $array['user'], $result->getUser()->getId() );
1497 }
1498 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1499 $this->assertSame( $array['deleted'], $result->getVisibility() );
1500 $this->assertSame( $array['len'], $result->getSize() );
1501 $this->assertSame( $array['parent_id'], $result->getParentId() );
1502 $this->assertSame( $array['sha1'], $result->getSha1() );
1503 $this->assertSame( $array['comment'], $result->getComment()->text );
1504 if ( isset( $array['content'] ) ) {
1505 foreach ( $array['content'] as $role => $content ) {
1506 $this->assertTrue(
1507 $result->getContent( $role )->equals( $content )
1508 );
1509 }
1510 } elseif ( isset( $array['text'] ) ) {
1511 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1512 } elseif ( isset( $array['content_format'] ) ) {
1513 $this->assertSame(
1514 $array['content_format'],
1515 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1516 );
1517 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1518 }
1519 }
1520
1521 /**
1522 * @dataProvider provideNewMutableRevisionFromArray
1523 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1524 */
1525 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1526 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1527 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1528 $blobStore = new SqlBlobStore( $lb, $cache );
1529 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1530
1531 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1532 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1533 ->disableOriginalConstructor()
1534 ->getMock();
1535 $factory->expects( $this->any() )
1536 ->method( 'newBlobStore' )
1537 ->willReturn( $blobStore );
1538 $factory->expects( $this->any() )
1539 ->method( 'newSqlBlobStore' )
1540 ->willReturn( $blobStore );
1541
1542 $this->setService( 'BlobStoreFactory', $factory );
1543
1544 $this->testNewMutableRevisionFromArray( $array );
1545 }
1546
1547 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1548 $fields = [
1549 'rev_id',
1550 'rev_page',
1551 'rev_timestamp',
1552 'rev_minor_edit',
1553 'rev_deleted',
1554 'rev_len',
1555 'rev_parent_id',
1556 'rev_sha1',
1557 ];
1558 if ( $returnTextIdField ) {
1559 $fields[] = 'rev_text_id';
1560 }
1561 return $fields;
1562 }
1563
1564 protected function getCommentQueryFields() {
1565 return [
1566 'rev_comment_text' => 'rev_comment',
1567 'rev_comment_data' => 'NULL',
1568 'rev_comment_cid' => 'NULL',
1569 ];
1570 }
1571
1572 protected function getActorQueryFields() {
1573 return [
1574 'rev_user' => 'rev_user',
1575 'rev_user_text' => 'rev_user_text',
1576 'rev_actor' => 'NULL',
1577 ];
1578 }
1579
1580 protected function getContentHandlerQueryFields() {
1581 return [
1582 'rev_content_format',
1583 'rev_content_model',
1584 ];
1585 }
1586
1587 abstract public function provideGetQueryInfo();
1588
1589 /**
1590 * @dataProvider provideGetQueryInfo
1591 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1592 */
1593 public function testGetQueryInfo( $options, $expected ) {
1594 $store = MediaWikiServices::getInstance()->getRevisionStore();
1595
1596 $queryInfo = $store->getQueryInfo( $options );
1597
1598 $this->assertArrayEqualsIgnoringIntKeyOrder(
1599 $expected['tables'],
1600 $queryInfo['tables']
1601 );
1602 $this->assertArrayEqualsIgnoringIntKeyOrder(
1603 $expected['fields'],
1604 $queryInfo['fields']
1605 );
1606 $this->assertArrayEqualsIgnoringIntKeyOrder(
1607 $expected['joins'],
1608 $queryInfo['joins']
1609 );
1610 }
1611
1612 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1613 $fields = [
1614 'ar_id',
1615 'ar_page_id',
1616 'ar_namespace',
1617 'ar_title',
1618 'ar_rev_id',
1619 'ar_timestamp',
1620 'ar_minor_edit',
1621 'ar_deleted',
1622 'ar_len',
1623 'ar_parent_id',
1624 'ar_sha1',
1625 ];
1626 if ( $returnTextFields ) {
1627 $fields[] = 'ar_text_id';
1628 }
1629 return $fields;
1630 }
1631
1632 abstract public function provideGetArchiveQueryInfo();
1633
1634 /**
1635 * @dataProvider provideGetArchiveQueryInfo
1636 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1637 */
1638 public function testGetArchiveQueryInfo( $expected ) {
1639 $store = MediaWikiServices::getInstance()->getRevisionStore();
1640
1641 $archiveQueryInfo = $store->getArchiveQueryInfo();
1642
1643 $this->assertArrayEqualsIgnoringIntKeyOrder(
1644 $expected['tables'],
1645 $archiveQueryInfo['tables']
1646 );
1647
1648 $this->assertArrayEqualsIgnoringIntKeyOrder(
1649 $expected['fields'],
1650 $archiveQueryInfo['fields']
1651 );
1652
1653 $this->assertArrayEqualsIgnoringIntKeyOrder(
1654 $expected['joins'],
1655 $archiveQueryInfo['joins']
1656 );
1657 }
1658
1659 abstract public function provideGetSlotsQueryInfo();
1660
1661 /**
1662 * @dataProvider provideGetSlotsQueryInfo
1663 * @covers \MediaWiki\Storage\RevisionStore::getSlotsQueryInfo
1664 */
1665 public function testGetSlotsQueryInfo( $options, $expected ) {
1666 $store = MediaWikiServices::getInstance()->getRevisionStore();
1667
1668 $archiveQueryInfo = $store->getSlotsQueryInfo( $options );
1669
1670 $this->assertArrayEqualsIgnoringIntKeyOrder(
1671 $expected['tables'],
1672 $archiveQueryInfo['tables']
1673 );
1674
1675 $this->assertArrayEqualsIgnoringIntKeyOrder(
1676 $expected['fields'],
1677 $archiveQueryInfo['fields']
1678 );
1679
1680 $this->assertArrayEqualsIgnoringIntKeyOrder(
1681 $expected['joins'],
1682 $archiveQueryInfo['joins']
1683 );
1684 }
1685
1686 /**
1687 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1688 * keys.
1689 *
1690 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1691 * split into a string key array and an int key array before assertions occur.
1692 *
1693 * @param array $expected
1694 * @param array $actual
1695 */
1696 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1697 $this->objectAssociativeSort( $expected );
1698 $this->objectAssociativeSort( $actual );
1699
1700 // Separate the int key values from the string key values so that assertion failures are
1701 // easier to understand.
1702 $expectedIntKeyValues = [];
1703 $actualIntKeyValues = [];
1704
1705 // Remove all int keys and re add them at the end after sorting by value
1706 // This will result in all int keys being in the same order with same ints at the end of
1707 // the array
1708 foreach ( $expected as $key => $value ) {
1709 if ( is_int( $key ) ) {
1710 unset( $expected[$key] );
1711 $expectedIntKeyValues[] = $value;
1712 }
1713 }
1714 foreach ( $actual as $key => $value ) {
1715 if ( is_int( $key ) ) {
1716 unset( $actual[$key] );
1717 $actualIntKeyValues[] = $value;
1718 }
1719 }
1720
1721 $this->assertArrayEquals( $expected, $actual, false, true );
1722 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );
1723 }
1724
1725 }