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