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