Avoid using stale data for revision visibility and actor data
[lhc/web/wiklou.git] / tests / phpunit / includes / Revision / RevisionStoreDbTestBase.php
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
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\Revision\IncompleteRevisionException;
14 use MediaWiki\Revision\MutableRevisionRecord;
15 use MediaWiki\Revision\RevisionArchiveRecord;
16 use MediaWiki\Revision\RevisionRecord;
17 use MediaWiki\Revision\RevisionStoreRecord;
18 use MediaWiki\Revision\RevisionSlots;
19 use MediaWiki\Revision\RevisionStore;
20 use MediaWiki\Revision\SlotRecord;
21 use MediaWiki\Storage\BlobStoreFactory;
22 use MediaWiki\Storage\SqlBlobStore;
23 use MediaWiki\User\UserIdentityValue;
24 use MediaWikiTestCase;
25 use PHPUnit_Framework_MockObject_MockObject;
26 use Revision;
27 use TestUserRegistry;
28 use Title;
29 use WANObjectCache;
30 use Wikimedia\Rdbms\Database;
31 use Wikimedia\Rdbms\DatabaseDomain;
32 use Wikimedia\Rdbms\DatabaseSqlite;
33 use Wikimedia\Rdbms\FakeResultWrapper;
34 use Wikimedia\Rdbms\LoadBalancer;
35 use Wikimedia\Rdbms\TransactionProfiler;
36 use WikiPage;
37 use WikitextContent;
38
39 /**
40 * @group Database
41 * @group RevisionStore
42 */
43 abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
44
45 /**
46 * @var Title
47 */
48 private $testPageTitle;
49
50 /**
51 * @var WikiPage
52 */
53 private $testPage;
54
55 /**
56 * @return int
57 */
58 abstract protected function getMcrMigrationStage();
59
60 /**
61 * @return bool
62 */
63 protected function getContentHandlerUseDB() {
64 return true;
65 }
66
67 /**
68 * @return string[]
69 */
70 abstract protected function getMcrTablesToReset();
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 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD,
85 ] );
86
87 $this->overrideMwServices();
88 }
89
90 protected function addCoreDBData() {
91 // Blank out. This would fail with a modified schema, and we don't need it.
92 }
93
94 /**
95 * @return Title
96 */
97 protected function getTestPageTitle() {
98 if ( $this->testPageTitle ) {
99 return $this->testPageTitle;
100 }
101
102 $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
103 return $this->testPageTitle;
104 }
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 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
140
141 $lb = $this->getMockBuilder( LoadBalancer::class )
142 ->setMethods( [ 'reallyOpenConnection' ] )
143 ->setConstructorArgs( [
144 [ 'servers' => [ $server ], 'localDomain' => $domain ]
145 ] )
146 ->getMock();
147
148 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
149 function ( array $server, $dbNameOverride ) {
150 return $this->getDatabaseMock( $server );
151 }
152 );
153
154 return $lb;
155 }
156
157 /**
158 * @return Database|PHPUnit_Framework_MockObject_MockObject
159 */
160 private function getDatabaseMock( array $params ) {
161 $db = $this->getMockBuilder( DatabaseSqlite::class )
162 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
163 ->setConstructorArgs( [ $params ] )
164 ->getMock();
165
166 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
167 $db->method( 'isOpen' )->willReturn( true );
168
169 return $db;
170 }
171
172 public function provideDomainCheck() {
173 yield [ false, 'test', '' ];
174 yield [ 'test', 'test', '' ];
175
176 yield [ false, 'test', 'foo_' ];
177 yield [ 'test-foo_', 'test', 'foo_' ];
178
179 yield [ false, 'dash-test', '' ];
180 yield [ 'dash-test', 'dash-test', '' ];
181
182 yield [ false, 'underscore_test', 'foo_' ];
183 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
184 }
185
186 /**
187 * @dataProvider provideDomainCheck
188 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
189 */
190 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
191 $this->setMwGlobals(
192 [
193 'wgDBname' => $dbName,
194 'wgDBprefix' => $dbPrefix,
195 ]
196 );
197
198 $loadBalancer = $this->getLoadBalancerMock(
199 [
200 'host' => '*dummy*',
201 'dbDirectory' => '*dummy*',
202 'user' => 'test',
203 'password' => 'test',
204 'flags' => 0,
205 'variables' => [],
206 'schema' => '',
207 'cliMode' => true,
208 'agent' => '',
209 'load' => 100,
210 'profiler' => null,
211 'trxProfiler' => new TransactionProfiler(),
212 'connLogger' => new \Psr\Log\NullLogger(),
213 'queryLogger' => new \Psr\Log\NullLogger(),
214 'errorLogger' => function () {
215 },
216 'deprecationLogger' => function () {
217 },
218 'type' => 'test',
219 'dbname' => $dbName,
220 'tablePrefix' => $dbPrefix,
221 ]
222 );
223 $db = $loadBalancer->getConnection( DB_REPLICA );
224
225 /** @var SqlBlobStore $blobStore */
226 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
227 ->disableOriginalConstructor()
228 ->getMock();
229
230 $store = new RevisionStore(
231 $loadBalancer,
232 $blobStore,
233 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
234 MediaWikiServices::getInstance()->getCommentStore(),
235 MediaWikiServices::getInstance()->getContentModelStore(),
236 MediaWikiServices::getInstance()->getSlotRoleStore(),
237 MediaWikiServices::getInstance()->getSlotRoleRegistry(),
238 $this->getMcrMigrationStage(),
239 MediaWikiServices::getInstance()->getActorMigration(),
240 $wikiId
241 );
242
243 $count = $store->countRevisionsByPageId( $db, 0 );
244
245 // Dummy check to make PhpUnit happy. We are really only interested in
246 // countRevisionsByPageId not failing due to the DB domain check.
247 $this->assertSame( 0, $count );
248 }
249
250 protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
251 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
252 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
253 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
254 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
255 }
256
257 protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
258 $this->assertEquals(
259 $r1->getPageAsLinkTarget()->getNamespace(),
260 $r2->getPageAsLinkTarget()->getNamespace()
261 );
262
263 $this->assertEquals(
264 $r1->getPageAsLinkTarget()->getText(),
265 $r2->getPageAsLinkTarget()->getText()
266 );
267
268 if ( $r1->getParentId() ) {
269 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
270 }
271
272 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
273 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
274 $this->assertEquals( $r1->getComment(), $r2->getComment() );
275 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
276 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
277 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
278 $this->assertEquals( $r1->getSize(), $r2->getSize() );
279 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
280 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
281 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
282 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
283 foreach ( $r1->getSlotRoles() as $role ) {
284 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
285 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
286 }
287 foreach ( [
288 RevisionRecord::DELETED_TEXT,
289 RevisionRecord::DELETED_COMMENT,
290 RevisionRecord::DELETED_USER,
291 RevisionRecord::DELETED_RESTRICTED,
292 ] as $field ) {
293 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
294 }
295 }
296
297 protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
298 $this->assertSame( $s1->getRole(), $s2->getRole() );
299 $this->assertSame( $s1->getModel(), $s2->getModel() );
300 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
301 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
302 $this->assertSame( $s1->getSize(), $s2->getSize() );
303 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
304
305 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
306 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
307 }
308
309 protected function assertRevisionCompleteness( RevisionRecord $r ) {
310 $this->assertTrue( $r->hasSlot( SlotRecord::MAIN ) );
311 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( SlotRecord::MAIN ) );
312 $this->assertInstanceOf( Content::class, $r->getContent( SlotRecord::MAIN ) );
313
314 foreach ( $r->getSlotRoles() as $role ) {
315 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
316 }
317 }
318
319 protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
320 $this->assertTrue( $slot->hasAddress() );
321 $this->assertSame( $r->getId(), $slot->getRevision() );
322
323 $this->assertInstanceOf( Content::class, $slot->getContent() );
324 }
325
326 /**
327 * @param mixed[] $details
328 *
329 * @return RevisionRecord
330 */
331 private function getRevisionRecordFromDetailsArray( $details = [] ) {
332 // Convert some values that can't be provided by dataProviders
333 if ( isset( $details['user'] ) && $details['user'] === true ) {
334 $details['user'] = $this->getTestUser()->getUser();
335 }
336 if ( isset( $details['page'] ) && $details['page'] === true ) {
337 $details['page'] = $this->getTestPage()->getId();
338 }
339 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
340 $details['parent'] = $this->getTestPage()->getLatest();
341 }
342
343 // Create the RevisionRecord with any available data
344 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
345 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
346 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
347 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
348 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
349 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
350 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
351 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
352 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
353 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
354 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
355 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
356
357 if ( isset( $details['content'] ) ) {
358 foreach ( $details['content'] as $role => $content ) {
359 $rev->setContent( $role, $content );
360 }
361 }
362
363 return $rev;
364 }
365
366 public function provideInsertRevisionOn_successes() {
367 yield 'Bare minimum revision insertion' => [
368 [
369 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
370 'page' => true,
371 'comment' => $this->getRandomCommentStoreComment(),
372 'timestamp' => '20171117010101',
373 'user' => true,
374 ],
375 ];
376 yield 'Detailed revision insertion' => [
377 [
378 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
379 'parent' => true,
380 'page' => true,
381 'comment' => $this->getRandomCommentStoreComment(),
382 'timestamp' => '20171117010101',
383 'user' => true,
384 'minor' => true,
385 'visibility' => RevisionRecord::DELETED_RESTRICTED,
386 ],
387 ];
388 }
389
390 protected function getRandomCommentStoreComment() {
391 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
392 }
393
394 /**
395 * @dataProvider provideInsertRevisionOn_successes
396 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
397 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
398 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
399 */
400 public function testInsertRevisionOn_successes(
401 array $revDetails = []
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\Revision\RevisionStore::insertRevisionOn
462 */
463 public function testInsertRevisionOn_blobAddressExists() {
464 $title = $this->getTestPageTitle();
465 $revDetails = [
466 'slot' => SlotRecord::newUnsaved( SlotRecord::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( SlotRecord::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( SlotRecord::MAIN );
490 $secondMainSlot = $secondReturn->getSlot( SlotRecord::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( SlotRecord::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( SlotRecord::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( SlotRecord::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\Revision\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\Revision\RevisionStore::newNullRevision
584 * @covers \MediaWiki\Revision\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->assertArrayEquals(
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\Revision\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\Revision\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\Revision\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\Revision\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\Revision\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( SlotRecord::MAIN )->getContent()->equals( $content ) );
730 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
731 }
732
733 /**
734 * @covers \MediaWiki\Revision\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( SlotRecord::MAIN )->getContent()->equals( $content ) );
748 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
749 }
750
751 /**
752 * @covers \MediaWiki\Revision\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( SlotRecord::MAIN )->getContent()->equals( $content ) );
766 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
767 }
768
769 /**
770 * @covers \MediaWiki\Revision\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( SlotRecord::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 protected 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(),
869 $record->getContent( SlotRecord::MAIN )->getDefaultFormat() );
870 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord::MAIN )->getModel() );
871 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
872
873 $revRec = $rev->getRevisionRecord();
874 $revMain = $revRec->getSlot( SlotRecord::MAIN );
875 $recMain = $record->getSlot( SlotRecord::MAIN );
876
877 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
878 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
879 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
880
881 if ( $revMain->hasOrigin() ) {
882 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
883 }
884
885 if ( $revMain->hasAddress() ) {
886 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
887 }
888
889 if ( $revMain->hasContentId() ) {
890 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
891 }
892 }
893
894 /**
895 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
896 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
897 */
898 public function testNewRevisionFromRow_getQueryInfo() {
899 $page = $this->getTestPage();
900 $text = __METHOD__ . 'a-ä';
901 /** @var Revision $rev */
902 $rev = $page->doEditContent(
903 new WikitextContent( $text ),
904 __METHOD__ . 'a'
905 )->value['revision'];
906
907 $store = MediaWikiServices::getInstance()->getRevisionStore();
908 $info = $store->getQueryInfo();
909 $row = $this->db->selectRow(
910 $info['tables'],
911 $info['fields'],
912 [ 'rev_id' => $rev->getId() ],
913 __METHOD__,
914 [],
915 $info['joins']
916 );
917 $record = $store->newRevisionFromRow(
918 $row,
919 [],
920 $page->getTitle()
921 );
922 $this->assertRevisionRecordMatchesRevision( $rev, $record );
923 $this->assertSame( $text, $rev->getContent()->serialize() );
924 }
925
926 /**
927 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
928 */
929 public function testNewRevisionFromRow_anonEdit() {
930 $page = $this->getTestPage();
931 $text = __METHOD__ . 'a-ä';
932 /** @var Revision $rev */
933 $rev = $page->doEditContent(
934 new WikitextContent( $text ),
935 __METHOD__ . 'a'
936 )->value['revision'];
937
938 $store = MediaWikiServices::getInstance()->getRevisionStore();
939 $record = $store->newRevisionFromRow(
940 $this->revisionToRow( $rev ),
941 [],
942 $page->getTitle()
943 );
944 $this->assertRevisionRecordMatchesRevision( $rev, $record );
945 $this->assertSame( $text, $rev->getContent()->serialize() );
946 }
947
948 /**
949 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
950 */
951 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
952 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
953 $this->overrideMwServices();
954 $page = $this->getTestPage();
955 $text = __METHOD__ . 'a-ä';
956 /** @var Revision $rev */
957 $rev = $page->doEditContent(
958 new WikitextContent( $text ),
959 __METHOD__ . 'a'
960 )->value['revision'];
961
962 $store = MediaWikiServices::getInstance()->getRevisionStore();
963 $record = $store->newRevisionFromRow(
964 $this->revisionToRow( $rev ),
965 [],
966 $page->getTitle()
967 );
968 $this->assertRevisionRecordMatchesRevision( $rev, $record );
969 $this->assertSame( $text, $rev->getContent()->serialize() );
970 }
971
972 /**
973 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
974 */
975 public function testNewRevisionFromRow_userEdit() {
976 $page = $this->getTestPage();
977 $text = __METHOD__ . 'b-ä';
978 /** @var Revision $rev */
979 $rev = $page->doEditContent(
980 new WikitextContent( $text ),
981 __METHOD__ . 'b',
982 0,
983 false,
984 $this->getTestUser()->getUser()
985 )->value['revision'];
986
987 $store = MediaWikiServices::getInstance()->getRevisionStore();
988 $record = $store->newRevisionFromRow(
989 $this->revisionToRow( $rev ),
990 [],
991 $page->getTitle()
992 );
993 $this->assertRevisionRecordMatchesRevision( $rev, $record );
994 $this->assertSame( $text, $rev->getContent()->serialize() );
995 }
996
997 /**
998 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
999 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1000 */
1001 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1002 $store = MediaWikiServices::getInstance()->getRevisionStore();
1003 $title = Title::newFromText( __METHOD__ );
1004 $text = __METHOD__ . '-bä';
1005 $page = WikiPage::factory( $title );
1006 /** @var Revision $orig */
1007 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1008 ->value['revision'];
1009 $page->doDeleteArticle( __METHOD__ );
1010
1011 $db = wfGetDB( DB_MASTER );
1012 $arQuery = $store->getArchiveQueryInfo();
1013 $res = $db->select(
1014 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1015 __METHOD__, [], $arQuery['joins']
1016 );
1017 $this->assertTrue( is_object( $res ), 'query failed' );
1018
1019 $row = $res->fetchObject();
1020 $res->free();
1021 $record = $store->newRevisionFromArchiveRow( $row );
1022
1023 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1024 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1025 }
1026
1027 /**
1028 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1029 */
1030 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1031 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1032 $this->overrideMwServices();
1033 $store = MediaWikiServices::getInstance()->getRevisionStore();
1034 $title = Title::newFromText( __METHOD__ );
1035 $text = __METHOD__ . '-bä';
1036 $page = WikiPage::factory( $title );
1037 /** @var Revision $orig */
1038 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1039 ->value['revision'];
1040 $page->doDeleteArticle( __METHOD__ );
1041
1042 $db = wfGetDB( DB_MASTER );
1043 $arQuery = $store->getArchiveQueryInfo();
1044 $res = $db->select(
1045 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1046 __METHOD__, [], $arQuery['joins']
1047 );
1048 $this->assertTrue( is_object( $res ), 'query failed' );
1049
1050 $row = $res->fetchObject();
1051 $res->free();
1052 $record = $store->newRevisionFromArchiveRow( $row );
1053
1054 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1055 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1056 }
1057
1058 /**
1059 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1060 */
1061 public function testNewRevisionFromArchiveRow_no_user() {
1062 $store = MediaWikiServices::getInstance()->getRevisionStore();
1063
1064 $row = (object)[
1065 'ar_id' => '1',
1066 'ar_page_id' => '2',
1067 'ar_namespace' => '0',
1068 'ar_title' => 'Something',
1069 'ar_rev_id' => '2',
1070 'ar_text_id' => '47',
1071 'ar_timestamp' => '20180528192356',
1072 'ar_minor_edit' => '0',
1073 'ar_deleted' => '0',
1074 'ar_len' => '78',
1075 'ar_parent_id' => '0',
1076 'ar_sha1' => 'deadbeef',
1077 'ar_comment_text' => 'whatever',
1078 'ar_comment_data' => null,
1079 'ar_comment_cid' => null,
1080 'ar_user' => '0',
1081 'ar_user_text' => '', // this is the important bit
1082 'ar_actor' => null,
1083 'ar_content_format' => null,
1084 'ar_content_model' => null,
1085 ];
1086
1087 \Wikimedia\suppressWarnings();
1088 $record = $store->newRevisionFromArchiveRow( $row );
1089 \Wikimedia\suppressWarnings( true );
1090
1091 $this->assertInstanceOf( RevisionRecord::class, $record );
1092 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1093 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1094 }
1095
1096 /**
1097 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1098 */
1099 public function testNewRevisionFromRow_no_user() {
1100 $store = MediaWikiServices::getInstance()->getRevisionStore();
1101 $title = Title::newFromText( __METHOD__ );
1102
1103 $row = (object)[
1104 'rev_id' => '2',
1105 'rev_page' => '2',
1106 'page_namespace' => '0',
1107 'page_title' => $title->getText(),
1108 'rev_text_id' => '47',
1109 'rev_timestamp' => '20180528192356',
1110 'rev_minor_edit' => '0',
1111 'rev_deleted' => '0',
1112 'rev_len' => '78',
1113 'rev_parent_id' => '0',
1114 'rev_sha1' => 'deadbeef',
1115 'rev_comment_text' => 'whatever',
1116 'rev_comment_data' => null,
1117 'rev_comment_cid' => null,
1118 'rev_user' => '0',
1119 'rev_user_text' => '', // this is the important bit
1120 'rev_actor' => null,
1121 'rev_content_format' => null,
1122 'rev_content_model' => null,
1123 ];
1124
1125 \Wikimedia\suppressWarnings();
1126 $record = $store->newRevisionFromRow( $row, 0, $title );
1127 \Wikimedia\suppressWarnings( true );
1128
1129 $this->assertNotNull( $record );
1130 $this->assertNotNull( $record->getUser() );
1131 $this->assertNotEmpty( $record->getUser()->getName() );
1132 }
1133
1134 /**
1135 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1136 */
1137 public function testInsertRevisionOn_archive() {
1138 // This is a round trip test for deletion and undeletion of a
1139 // revision row via the archive table.
1140
1141 $store = MediaWikiServices::getInstance()->getRevisionStore();
1142 $title = Title::newFromText( __METHOD__ );
1143
1144 $page = WikiPage::factory( $title );
1145 /** @var Revision $origRev */
1146 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1147 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1148 ->value['revision'];
1149 $orig = $origRev->getRevisionRecord();
1150 $page->doDeleteArticle( __METHOD__ );
1151
1152 // re-create page, so we can later load revisions for it
1153 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1154
1155 $db = wfGetDB( DB_MASTER );
1156 $arQuery = $store->getArchiveQueryInfo();
1157 $row = $db->selectRow(
1158 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1159 __METHOD__, [], $arQuery['joins']
1160 );
1161
1162 $this->assertNotFalse( $row, 'query failed' );
1163
1164 $record = $store->newRevisionFromArchiveRow(
1165 $row,
1166 0,
1167 $title,
1168 [ 'page_id' => $title->getArticleID() ]
1169 );
1170
1171 $restored = $store->insertRevisionOn( $record, $db );
1172
1173 // is the new revision correct?
1174 $this->assertRevisionCompleteness( $restored );
1175 $this->assertRevisionRecordsEqual( $record, $restored );
1176
1177 // does the new revision use the original slot?
1178 $recMain = $record->getSlot( SlotRecord::MAIN );
1179 $restMain = $restored->getSlot( SlotRecord::MAIN );
1180 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1181 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1182 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1183 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1184
1185 // can we load it from the store?
1186 $loaded = $store->getRevisionById( $restored->getId() );
1187 $this->assertNotNull( $loaded );
1188 $this->assertRevisionCompleteness( $loaded );
1189 $this->assertRevisionRecordsEqual( $restored, $loaded );
1190
1191 // can we find it directly in the database?
1192 $this->assertRevisionExistsInDatabase( $restored );
1193 }
1194
1195 /**
1196 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1197 */
1198 public function testLoadRevisionFromId() {
1199 $title = Title::newFromText( __METHOD__ );
1200 $page = WikiPage::factory( $title );
1201 /** @var Revision $rev */
1202 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1203 ->value['revision'];
1204
1205 $store = MediaWikiServices::getInstance()->getRevisionStore();
1206 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1207 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1208 }
1209
1210 /**
1211 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1212 */
1213 public function testLoadRevisionFromPageId() {
1214 $title = Title::newFromText( __METHOD__ );
1215 $page = WikiPage::factory( $title );
1216 /** @var Revision $rev */
1217 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1218 ->value['revision'];
1219
1220 $store = MediaWikiServices::getInstance()->getRevisionStore();
1221 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1222 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1223 }
1224
1225 /**
1226 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1227 */
1228 public function testLoadRevisionFromTitle() {
1229 $title = Title::newFromText( __METHOD__ );
1230 $page = WikiPage::factory( $title );
1231 /** @var Revision $rev */
1232 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1233 ->value['revision'];
1234
1235 $store = MediaWikiServices::getInstance()->getRevisionStore();
1236 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1237 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1238 }
1239
1240 /**
1241 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1242 */
1243 public function testLoadRevisionFromTimestamp() {
1244 $title = Title::newFromText( __METHOD__ );
1245 $page = WikiPage::factory( $title );
1246 /** @var Revision $revOne */
1247 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1248 ->value['revision'];
1249 // Sleep to ensure different timestamps... )(evil)
1250 sleep( 1 );
1251 /** @var Revision $revTwo */
1252 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1253 ->value['revision'];
1254
1255 $store = MediaWikiServices::getInstance()->getRevisionStore();
1256 $this->assertNull(
1257 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1258 );
1259 $this->assertSame(
1260 $revOne->getId(),
1261 $store->loadRevisionFromTimestamp(
1262 wfGetDB( DB_MASTER ),
1263 $title,
1264 $revOne->getTimestamp()
1265 )->getId()
1266 );
1267 $this->assertSame(
1268 $revTwo->getId(),
1269 $store->loadRevisionFromTimestamp(
1270 wfGetDB( DB_MASTER ),
1271 $title,
1272 $revTwo->getTimestamp()
1273 )->getId()
1274 );
1275 }
1276
1277 /**
1278 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1279 */
1280 public function testGetParentLengths() {
1281 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1282 /** @var Revision $revOne */
1283 $revOne = $page->doEditContent(
1284 new WikitextContent( __METHOD__ ), __METHOD__
1285 )->value['revision'];
1286 /** @var Revision $revTwo */
1287 $revTwo = $page->doEditContent(
1288 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1289 )->value['revision'];
1290
1291 $store = MediaWikiServices::getInstance()->getRevisionStore();
1292 $this->assertSame(
1293 [
1294 $revOne->getId() => strlen( __METHOD__ ),
1295 ],
1296 $store->listRevisionSizes(
1297 wfGetDB( DB_MASTER ),
1298 [ $revOne->getId() ]
1299 )
1300 );
1301 $this->assertSame(
1302 [
1303 $revOne->getId() => strlen( __METHOD__ ),
1304 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1305 ],
1306 $store->listRevisionSizes(
1307 wfGetDB( DB_MASTER ),
1308 [ $revOne->getId(), $revTwo->getId() ]
1309 )
1310 );
1311 }
1312
1313 /**
1314 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1315 */
1316 public function testGetPreviousRevision() {
1317 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1318 /** @var Revision $revOne */
1319 $revOne = $page->doEditContent(
1320 new WikitextContent( __METHOD__ ), __METHOD__
1321 )->value['revision'];
1322 /** @var Revision $revTwo */
1323 $revTwo = $page->doEditContent(
1324 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1325 )->value['revision'];
1326
1327 $store = MediaWikiServices::getInstance()->getRevisionStore();
1328 $this->assertNull(
1329 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1330 );
1331 $this->assertSame(
1332 $revOne->getId(),
1333 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1334 );
1335 }
1336
1337 /**
1338 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1339 */
1340 public function testGetNextRevision() {
1341 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1342 /** @var Revision $revOne */
1343 $revOne = $page->doEditContent(
1344 new WikitextContent( __METHOD__ ), __METHOD__
1345 )->value['revision'];
1346 /** @var Revision $revTwo */
1347 $revTwo = $page->doEditContent(
1348 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1349 )->value['revision'];
1350
1351 $store = MediaWikiServices::getInstance()->getRevisionStore();
1352 $this->assertSame(
1353 $revTwo->getId(),
1354 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1355 );
1356 $this->assertNull(
1357 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1358 );
1359 }
1360
1361 public function provideNonHistoryRevision() {
1362 $title = Title::newFromText( __METHOD__ );
1363 $rev = new MutableRevisionRecord( $title );
1364 yield [ $rev ];
1365
1366 $user = new UserIdentityValue( 7, 'Frank', 0 );
1367 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1368 $row = (object)[
1369 'ar_id' => 3,
1370 'ar_rev_id' => 34567,
1371 'ar_page_id' => 5,
1372 'ar_deleted' => 0,
1373 'ar_minor_edit' => 0,
1374 'ar_timestamp' => '20180101020202',
1375 ];
1376 $slots = new RevisionSlots( [] );
1377 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1378 yield [ $rev ];
1379 }
1380
1381 /**
1382 * @dataProvider provideNonHistoryRevision
1383 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1384 */
1385 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1386 $store = MediaWikiServices::getInstance()->getRevisionStore();
1387 $this->assertNull( $store->getPreviousRevision( $rev ) );
1388 }
1389
1390 /**
1391 * @dataProvider provideNonHistoryRevision
1392 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1393 */
1394 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1395 $store = MediaWikiServices::getInstance()->getRevisionStore();
1396 $this->assertNull( $store->getNextRevision( $rev ) );
1397 }
1398
1399 /**
1400 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1401 */
1402 public function testGetTimestampFromId_found() {
1403 $page = $this->getTestPage();
1404 /** @var Revision $rev */
1405 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1406 ->value['revision'];
1407
1408 $store = MediaWikiServices::getInstance()->getRevisionStore();
1409 $result = $store->getTimestampFromId(
1410 $page->getTitle(),
1411 $rev->getId()
1412 );
1413
1414 $this->assertSame( $rev->getTimestamp(), $result );
1415 }
1416
1417 /**
1418 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1419 */
1420 public function testGetTimestampFromId_notFound() {
1421 $page = $this->getTestPage();
1422 /** @var Revision $rev */
1423 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1424 ->value['revision'];
1425
1426 $store = MediaWikiServices::getInstance()->getRevisionStore();
1427 $result = $store->getTimestampFromId(
1428 $page->getTitle(),
1429 $rev->getId() + 1
1430 );
1431
1432 $this->assertFalse( $result );
1433 }
1434
1435 /**
1436 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1437 */
1438 public function testCountRevisionsByPageId() {
1439 $store = MediaWikiServices::getInstance()->getRevisionStore();
1440 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1441
1442 $this->assertSame(
1443 0,
1444 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1445 );
1446 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1447 $this->assertSame(
1448 1,
1449 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1450 );
1451 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1452 $this->assertSame(
1453 2,
1454 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1455 );
1456 }
1457
1458 /**
1459 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1460 */
1461 public function testCountRevisionsByTitle() {
1462 $store = MediaWikiServices::getInstance()->getRevisionStore();
1463 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1464
1465 $this->assertSame(
1466 0,
1467 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1468 );
1469 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1470 $this->assertSame(
1471 1,
1472 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1473 );
1474 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1475 $this->assertSame(
1476 2,
1477 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1478 );
1479 }
1480
1481 /**
1482 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1483 */
1484 public function testUserWasLastToEdit_false() {
1485 $sysop = $this->getTestSysop()->getUser();
1486 $page = $this->getTestPage();
1487 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1488
1489 $store = MediaWikiServices::getInstance()->getRevisionStore();
1490 $result = $store->userWasLastToEdit(
1491 wfGetDB( DB_MASTER ),
1492 $page->getId(),
1493 $sysop->getId(),
1494 '20160101010101'
1495 );
1496 $this->assertFalse( $result );
1497 }
1498
1499 /**
1500 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1501 */
1502 public function testUserWasLastToEdit_true() {
1503 $startTime = wfTimestampNow();
1504 $sysop = $this->getTestSysop()->getUser();
1505 $page = $this->getTestPage();
1506 $page->doEditContent(
1507 new WikitextContent( __METHOD__ ),
1508 __METHOD__,
1509 0,
1510 false,
1511 $sysop
1512 );
1513
1514 $store = MediaWikiServices::getInstance()->getRevisionStore();
1515 $result = $store->userWasLastToEdit(
1516 wfGetDB( DB_MASTER ),
1517 $page->getId(),
1518 $sysop->getId(),
1519 $startTime
1520 );
1521 $this->assertTrue( $result );
1522 }
1523
1524 /**
1525 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1526 */
1527 public function testGetKnownCurrentRevision() {
1528 $page = $this->getTestPage();
1529 /** @var Revision $rev */
1530 $rev = $page->doEditContent(
1531 new WikitextContent( __METHOD__ . 'b' ),
1532 __METHOD__ . 'b',
1533 0,
1534 false,
1535 $this->getTestUser()->getUser()
1536 )->value['revision'];
1537
1538 $store = MediaWikiServices::getInstance()->getRevisionStore();
1539 $record = $store->getKnownCurrentRevision(
1540 $page->getTitle(),
1541 $rev->getId()
1542 );
1543
1544 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1545 }
1546
1547 public function provideNewMutableRevisionFromArray() {
1548 yield 'Basic array, content object' => [
1549 [
1550 'id' => 2,
1551 'page' => 1,
1552 'timestamp' => '20171017114835',
1553 'user_text' => '111.0.1.2',
1554 'user' => 0,
1555 'minor_edit' => false,
1556 'deleted' => 0,
1557 'len' => 46,
1558 'parent_id' => 1,
1559 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1560 'comment' => 'Goat Comment!',
1561 'content' => new WikitextContent( 'Some Content' ),
1562 ]
1563 ];
1564 yield 'Basic array, serialized text' => [
1565 [
1566 'id' => 2,
1567 'page' => 1,
1568 'timestamp' => '20171017114835',
1569 'user_text' => '111.0.1.2',
1570 'user' => 0,
1571 'minor_edit' => false,
1572 'deleted' => 0,
1573 'len' => 46,
1574 'parent_id' => 1,
1575 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1576 'comment' => 'Goat Comment!',
1577 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1578 ]
1579 ];
1580 yield 'Basic array, serialized text, utf-8 flags' => [
1581 [
1582 'id' => 2,
1583 'page' => 1,
1584 'timestamp' => '20171017114835',
1585 'user_text' => '111.0.1.2',
1586 'user' => 0,
1587 'minor_edit' => false,
1588 'deleted' => 0,
1589 'len' => 46,
1590 'parent_id' => 1,
1591 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1592 'comment' => 'Goat Comment!',
1593 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1594 'flags' => 'utf-8',
1595 ]
1596 ];
1597 yield 'Basic array, with title' => [
1598 [
1599 'title' => Title::newFromText( 'SomeText' ),
1600 'timestamp' => '20171017114835',
1601 'user_text' => '111.0.1.2',
1602 'user' => 0,
1603 'minor_edit' => false,
1604 'deleted' => 0,
1605 'len' => 46,
1606 'parent_id' => 1,
1607 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1608 'comment' => 'Goat Comment!',
1609 'content' => new WikitextContent( 'Some Content' ),
1610 ]
1611 ];
1612 yield 'Basic array, no user field' => [
1613 [
1614 'id' => 2,
1615 'page' => 1,
1616 'timestamp' => '20171017114835',
1617 'user_text' => '111.0.1.3',
1618 'minor_edit' => false,
1619 'deleted' => 0,
1620 'len' => 46,
1621 'parent_id' => 1,
1622 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1623 'comment' => 'Goat Comment!',
1624 'content' => new WikitextContent( 'Some Content' ),
1625 ]
1626 ];
1627 }
1628
1629 /**
1630 * @dataProvider provideNewMutableRevisionFromArray
1631 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1632 */
1633 public function testNewMutableRevisionFromArray( array $array ) {
1634 $store = MediaWikiServices::getInstance()->getRevisionStore();
1635
1636 // HACK: if $array['page'] is given, make sure that the page exists
1637 if ( isset( $array['page'] ) ) {
1638 $t = Title::newFromID( $array['page'] );
1639 if ( !$t || !$t->exists() ) {
1640 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1641 $info = $this->insertPage( $t );
1642 $array['page'] = $info['id'];
1643 }
1644 }
1645
1646 $result = $store->newMutableRevisionFromArray( $array );
1647
1648 if ( isset( $array['id'] ) ) {
1649 $this->assertSame( $array['id'], $result->getId() );
1650 }
1651 if ( isset( $array['page'] ) ) {
1652 $this->assertSame( $array['page'], $result->getPageId() );
1653 }
1654 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1655 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1656 if ( isset( $array['user'] ) ) {
1657 $this->assertSame( $array['user'], $result->getUser()->getId() );
1658 }
1659 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1660 $this->assertSame( $array['deleted'], $result->getVisibility() );
1661 $this->assertSame( $array['len'], $result->getSize() );
1662 $this->assertSame( $array['parent_id'], $result->getParentId() );
1663 $this->assertSame( $array['sha1'], $result->getSha1() );
1664 $this->assertSame( $array['comment'], $result->getComment()->text );
1665 if ( isset( $array['content'] ) ) {
1666 foreach ( $array['content'] as $role => $content ) {
1667 $this->assertTrue(
1668 $result->getContent( $role )->equals( $content )
1669 );
1670 }
1671 } elseif ( isset( $array['text'] ) ) {
1672 $this->assertSame( $array['text'],
1673 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1674 } elseif ( isset( $array['content_format'] ) ) {
1675 $this->assertSame(
1676 $array['content_format'],
1677 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1678 );
1679 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1680 }
1681 }
1682
1683 /**
1684 * @dataProvider provideNewMutableRevisionFromArray
1685 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1686 */
1687 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1688 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1689 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1690 $blobStore = new SqlBlobStore( $lb, $cache );
1691 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1692
1693 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1694 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1695 ->disableOriginalConstructor()
1696 ->getMock();
1697 $factory->expects( $this->any() )
1698 ->method( 'newBlobStore' )
1699 ->willReturn( $blobStore );
1700 $factory->expects( $this->any() )
1701 ->method( 'newSqlBlobStore' )
1702 ->willReturn( $blobStore );
1703
1704 $this->setService( 'BlobStoreFactory', $factory );
1705
1706 $this->testNewMutableRevisionFromArray( $array );
1707 }
1708
1709 /**
1710 * Creates a new revision for testing caching behavior
1711 *
1712 * @param WikiPage $page the page for the new revision
1713 * @param RevisionStore $store store object to use for creating the revision
1714 * @return bool|RevisionStoreRecord the revision created, or false if missing
1715 */
1716 private function createRevisionStoreCacheRecord( $page, $store ) {
1717 $user = MediaWikiTestCase::getMutableTestUser()->getUser();
1718 $updater = $page->newPageUpdater( $user );
1719 $updater->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) );
1720 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1721 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1722 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1723 }
1724
1725 /**
1726 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1727 */
1728 public function testGetKnownCurrentRevision_userNameChange() {
1729 global $wgActorTableSchemaMigrationStage;
1730
1731 $this->overrideMwServices();
1732 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1733 $this->setService( 'MainWANObjectCache', $cache );
1734
1735 $store = MediaWikiServices::getInstance()->getRevisionStore();
1736 $page = $this->getNonexistingTestPage();
1737 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1738
1739 // Grab the user name
1740 $userNameBefore = $rev->getUser()->getName();
1741
1742 // Change the user name in the database, "behind the back" of the cache
1743 $newUserName = "Renamed $userNameBefore";
1744 $this->db->update( 'revision',
1745 [ 'rev_user_text' => $newUserName ],
1746 [ 'rev_id' => $rev->getId() ] );
1747 $this->db->update( 'user',
1748 [ 'user_name' => $newUserName ],
1749 [ 'user_id' => $rev->getUser()->getId() ] );
1750 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1751 $this->db->update( 'actor',
1752 [ 'actor_name' => $newUserName ],
1753 [ 'actor_user' => $rev->getUser()->getId() ] );
1754 }
1755
1756 // Reload the revision and regrab the user name.
1757 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1758 $userNameAfter = $revAfter->getUser()->getName();
1759
1760 // The two user names should be different.
1761 // If they are the same, we are seeing a cached value, which is bad.
1762 $this->assertNotSame( $userNameBefore, $userNameAfter );
1763
1764 // This is implied by the above assertion, but explicitly check it, for completeness
1765 $this->assertSame( $newUserName, $userNameAfter );
1766 }
1767
1768 /**
1769 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1770 */
1771 public function testGetKnownCurrentRevision_revDelete() {
1772 $this->overrideMwServices();
1773 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1774 $this->setService( 'MainWANObjectCache', $cache );
1775
1776 $store = MediaWikiServices::getInstance()->getRevisionStore();
1777 $page = $this->getNonexistingTestPage();
1778 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1779
1780 // Grab the deleted bitmask
1781 $deletedBefore = $rev->getVisibility();
1782
1783 // Change the deleted bitmask in the database, "behind the back" of the cache
1784 $this->db->update( 'revision',
1785 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1786 [ 'rev_id' => $rev->getId() ] );
1787
1788 // Reload the revision and regrab the visibility flag.
1789 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1790 $deletedAfter = $revAfter->getVisibility();
1791
1792 // The two deleted flags should be different.
1793 // If they are the same, we are seeing a cached value, which is bad.
1794 $this->assertNotSame( $deletedBefore, $deletedAfter );
1795
1796 // This is implied by the above assertion, but explicitly check it, for completeness
1797 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1798 }
1799
1800 /**
1801 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1802 */
1803 public function testNewRevisionFromRow_userNameChange() {
1804 global $wgActorTableSchemaMigrationStage;
1805
1806 $page = $this->getTestPage();
1807 $text = __METHOD__;
1808 /** @var Revision $rev */
1809 $rev = $page->doEditContent(
1810 new WikitextContent( $text ),
1811 __METHOD__
1812 )->value['revision'];
1813
1814 $store = MediaWikiServices::getInstance()->getRevisionStore();
1815 $record = $store->newRevisionFromRow(
1816 $this->revisionToRow( $rev ),
1817 [],
1818 $page->getTitle()
1819 );
1820
1821 // Grab the user name
1822 $userNameBefore = $record->getUser()->getName();
1823
1824 // Change the user name in the database
1825 $newUserName = "Renamed $userNameBefore";
1826 $this->db->update( 'revision',
1827 [ 'rev_user_text' => $newUserName ],
1828 [ 'rev_id' => $record->getId() ] );
1829 $this->db->update( 'user',
1830 [ 'user_name' => $newUserName ],
1831 [ 'user_id' => $record->getUser()->getId() ] );
1832 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1833 $this->db->update( 'actor',
1834 [ 'actor_name' => $newUserName ],
1835 [ 'actor_user' => $record->getUser()->getId() ] );
1836 }
1837
1838 // Reload the record, passing $fromCache as true to force fresh info from the db,
1839 // and regrab the user name
1840 $recordAfter = $store->newRevisionFromRow(
1841 $this->revisionToRow( $rev ),
1842 [],
1843 $page->getTitle(),
1844 true
1845 );
1846 $userNameAfter = $recordAfter->getUser()->getName();
1847
1848 // The two user names should be different.
1849 // If they are the same, we are seeing a cached value, which is bad.
1850 $this->assertNotSame( $userNameBefore, $userNameAfter );
1851
1852 // This is implied by the above assertion, but explicitly check it, for completeness
1853 $this->assertSame( $newUserName, $userNameAfter );
1854 }
1855
1856 /**
1857 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1858 */
1859 public function testNewRevisionFromRow_revDelete() {
1860 $page = $this->getTestPage();
1861 $text = __METHOD__;
1862 /** @var Revision $rev */
1863 $rev = $page->doEditContent(
1864 new WikitextContent( $text ),
1865 __METHOD__
1866 )->value['revision'];
1867
1868 $store = MediaWikiServices::getInstance()->getRevisionStore();
1869 $record = $store->newRevisionFromRow(
1870 $this->revisionToRow( $rev ),
1871 [],
1872 $page->getTitle()
1873 );
1874
1875 // Grab the deleted bitmask
1876 $deletedBefore = $record->getVisibility();
1877
1878 // Change the deleted bitmask in the database
1879 $this->db->update( 'revision',
1880 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1881 [ 'rev_id' => $record->getId() ] );
1882
1883 // Reload the record, passing $fromCache as true to force fresh info from the db,
1884 // and regrab the deleted bitmask
1885 $recordAfter = $store->newRevisionFromRow(
1886 $this->revisionToRow( $rev ),
1887 [],
1888 $page->getTitle(),
1889 true
1890 );
1891 $deletedAfter = $recordAfter->getVisibility();
1892
1893 // The two deleted flags should be different, because we modified the database.
1894 $this->assertNotSame( $deletedBefore, $deletedAfter );
1895
1896 // This is implied by the above assertion, but explicitly check it, for completeness
1897 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1898 }
1899
1900 }