Default $wgActorTableSchemaMigrationStage to SCHEMA_COMPAT_NEW
[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_NEW,
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
442 // Use aliased fields from $queryInfo, e.g. rev_user
443 $keys = array_keys( $row );
444 $keys = array_combine( $keys, $keys );
445 $fields = array_intersect_key( $queryInfo['fields'], $keys ) + $keys;
446
447 // assertSelect() fails unless the orders match.
448 ksort( $fields );
449 ksort( $row );
450
451 $this->assertSelect(
452 $queryInfo['tables'],
453 $fields,
454 [ 'rev_id' => $rev->getId() ],
455 [ array_values( $row ) ],
456 [],
457 $queryInfo['joins']
458 );
459 }
460
461 /**
462 * @param SlotRecord $a
463 * @param SlotRecord $b
464 */
465 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
466 // Assert that the same blob address has been used.
467 $this->assertSame( $a->getAddress(), $b->getAddress() );
468 }
469
470 /**
471 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
472 */
473 public function testInsertRevisionOn_blobAddressExists() {
474 $title = $this->getTestPageTitle();
475 $revDetails = [
476 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
477 'parent' => true,
478 'comment' => $this->getRandomCommentStoreComment(),
479 'timestamp' => '20171117010101',
480 'user' => true,
481 ];
482
483 $this->overrideMwServices();
484 $store = MediaWikiServices::getInstance()->getRevisionStore();
485
486 // Insert the first revision
487 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
488 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
489 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
490 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
491
492 // Insert a second revision inheriting the same blob address
493 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( SlotRecord::MAIN ) );
494 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
495 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
496 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
497 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
498
499 $firstMainSlot = $firstReturn->getSlot( SlotRecord::MAIN );
500 $secondMainSlot = $secondReturn->getSlot( SlotRecord::MAIN );
501
502 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
503
504 // And that different revisions have been created.
505 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
506
507 // Make sure the slot rows reference the correct revision
508 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
509 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
510 }
511
512 public function provideInsertRevisionOn_failures() {
513 yield 'no slot' => [
514 [
515 'comment' => $this->getRandomCommentStoreComment(),
516 'timestamp' => '20171117010101',
517 'user' => true,
518 ],
519 new InvalidArgumentException( 'main slot must be provided' )
520 ];
521 yield 'no main slot' => [
522 [
523 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
524 'comment' => $this->getRandomCommentStoreComment(),
525 'timestamp' => '20171117010101',
526 'user' => true,
527 ],
528 new InvalidArgumentException( 'main slot must be provided' )
529 ];
530 yield 'no timestamp' => [
531 [
532 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
533 'comment' => $this->getRandomCommentStoreComment(),
534 'user' => true,
535 ],
536 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
537 ];
538 yield 'no comment' => [
539 [
540 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
541 'timestamp' => '20171117010101',
542 'user' => true,
543 ],
544 new IncompleteRevisionException( 'comment must not be NULL!' )
545 ];
546 yield 'no user' => [
547 [
548 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
549 'comment' => $this->getRandomCommentStoreComment(),
550 'timestamp' => '20171117010101',
551 ],
552 new IncompleteRevisionException( 'user must not be NULL!' )
553 ];
554 }
555
556 /**
557 * @dataProvider provideInsertRevisionOn_failures
558 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
559 */
560 public function testInsertRevisionOn_failures(
561 array $revDetails = [],
562 Exception $exception
563 ) {
564 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
565
566 $store = MediaWikiServices::getInstance()->getRevisionStore();
567
568 $this->setExpectedException(
569 get_class( $exception ),
570 $exception->getMessage(),
571 $exception->getCode()
572 );
573 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
574 }
575
576 public function provideNewNullRevision() {
577 yield [
578 Title::newFromText( 'UTPage_notAutoCreated' ),
579 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
580 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
581 true,
582 ];
583 yield [
584 Title::newFromText( 'UTPage_notAutoCreated' ),
585 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
586 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
587 false,
588 ];
589 }
590
591 /**
592 * @dataProvider provideNewNullRevision
593 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
594 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
595 */
596 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
597 $this->overrideMwServices();
598
599 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
600 $page = WikiPage::factory( $title );
601
602 if ( !$page->exists() ) {
603 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
604 }
605
606 $revDetails['page'] = $page->getId();
607 $revDetails['timestamp'] = wfTimestampNow();
608 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
609 $revDetails['user'] = $user;
610
611 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
612 $store = MediaWikiServices::getInstance()->getRevisionStore();
613
614 $dbw = wfGetDB( DB_MASTER );
615 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
616 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
617
618 $record = $store->newNullRevision(
619 wfGetDB( DB_MASTER ),
620 $title,
621 $comment,
622 $minor,
623 $user
624 );
625
626 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
627 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
628 $this->assertEquals( $comment, $record->getComment() );
629 $this->assertEquals( $minor, $record->isMinor() );
630 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
631 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
632
633 $this->assertArrayEquals(
634 $baseRev->getSlotRoles(),
635 $record->getSlotRoles()
636 );
637
638 foreach ( $baseRev->getSlotRoles() as $role ) {
639 $parentSlot = $baseRev->getSlot( $role );
640 $slot = $record->getSlot( $role );
641
642 $this->assertTrue( $slot->isInherited(), 'isInherited' );
643 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
644 $this->assertSameSlotContent( $parentSlot, $slot );
645 }
646 }
647
648 /**
649 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
650 */
651 public function testNewNullRevision_nonExistingTitle() {
652 $store = MediaWikiServices::getInstance()->getRevisionStore();
653 $record = $store->newNullRevision(
654 wfGetDB( DB_MASTER ),
655 Title::newFromText( __METHOD__ . '.iDontExist!' ),
656 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
657 false,
658 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
659 );
660 $this->assertNull( $record );
661 }
662
663 /**
664 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
665 */
666 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
667 $page = $this->getTestPage();
668 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
669 /** @var Revision $rev */
670 $rev = $status->value['revision'];
671
672 $store = MediaWikiServices::getInstance()->getRevisionStore();
673 $revisionRecord = $store->getRevisionById( $rev->getId() );
674 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
675
676 $this->assertGreaterThan( 0, $result );
677 $this->assertSame(
678 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
679 $result
680 );
681 }
682
683 /**
684 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
685 */
686 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
687 // This assumes that sysops are auto patrolled
688 $sysop = $this->getTestSysop()->getUser();
689 $page = $this->getTestPage();
690 $status = $page->doEditContent(
691 new WikitextContent( __METHOD__ ),
692 __METHOD__,
693 0,
694 false,
695 $sysop
696 );
697 /** @var Revision $rev */
698 $rev = $status->value['revision'];
699
700 $store = MediaWikiServices::getInstance()->getRevisionStore();
701 $revisionRecord = $store->getRevisionById( $rev->getId() );
702 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
703
704 $this->assertSame( 0, $result );
705 }
706
707 /**
708 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
709 */
710 public function testGetRecentChange() {
711 $page = $this->getTestPage();
712 $content = new WikitextContent( __METHOD__ );
713 $status = $page->doEditContent( $content, __METHOD__ );
714 /** @var Revision $rev */
715 $rev = $status->value['revision'];
716
717 $store = MediaWikiServices::getInstance()->getRevisionStore();
718 $revRecord = $store->getRevisionById( $rev->getId() );
719 $recentChange = $store->getRecentChange( $revRecord );
720
721 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
722 $this->assertEquals( $rev->getRecentChange(), $recentChange );
723 }
724
725 /**
726 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
727 */
728 public function testGetRevisionById() {
729 $page = $this->getTestPage();
730 $content = new WikitextContent( __METHOD__ );
731 $status = $page->doEditContent( $content, __METHOD__ );
732 /** @var Revision $rev */
733 $rev = $status->value['revision'];
734
735 $store = MediaWikiServices::getInstance()->getRevisionStore();
736 $revRecord = $store->getRevisionById( $rev->getId() );
737
738 $this->assertSame( $rev->getId(), $revRecord->getId() );
739 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
740 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
741 }
742
743 /**
744 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
745 */
746 public function testGetRevisionByTitle() {
747 $page = $this->getTestPage();
748 $content = new WikitextContent( __METHOD__ );
749 $status = $page->doEditContent( $content, __METHOD__ );
750 /** @var Revision $rev */
751 $rev = $status->value['revision'];
752
753 $store = MediaWikiServices::getInstance()->getRevisionStore();
754 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
755
756 $this->assertSame( $rev->getId(), $revRecord->getId() );
757 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
758 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
759 }
760
761 /**
762 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
763 */
764 public function testGetRevisionByPageId() {
765 $page = $this->getTestPage();
766 $content = new WikitextContent( __METHOD__ );
767 $status = $page->doEditContent( $content, __METHOD__ );
768 /** @var Revision $rev */
769 $rev = $status->value['revision'];
770
771 $store = MediaWikiServices::getInstance()->getRevisionStore();
772 $revRecord = $store->getRevisionByPageId( $page->getId() );
773
774 $this->assertSame( $rev->getId(), $revRecord->getId() );
775 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
776 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
777 }
778
779 /**
780 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
781 */
782 public function testGetRevisionByTimestamp() {
783 // Make sure there is 1 second between the last revision and the rev we create...
784 // Otherwise we might not get the correct revision and the test may fail...
785 // :(
786 $page = $this->getTestPage();
787 sleep( 1 );
788 $content = new WikitextContent( __METHOD__ );
789 $status = $page->doEditContent( $content, __METHOD__ );
790 /** @var Revision $rev */
791 $rev = $status->value['revision'];
792
793 $store = MediaWikiServices::getInstance()->getRevisionStore();
794 $revRecord = $store->getRevisionByTimestamp(
795 $page->getTitle(),
796 $rev->getTimestamp()
797 );
798
799 $this->assertSame( $rev->getId(), $revRecord->getId() );
800 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
801 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
802 }
803
804 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
805 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
806 $page = WikiPage::factory( $rev->getTitle() );
807
808 $fields = [
809 'rev_id' => (string)$rev->getId(),
810 'rev_page' => (string)$rev->getPage(),
811 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
812 'rev_user_text' => (string)$rev->getUserText(),
813 'rev_user' => (string)$rev->getUser() ?: null,
814 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
815 'rev_deleted' => (string)$rev->getVisibility(),
816 'rev_len' => (string)$rev->getSize(),
817 'rev_parent_id' => (string)$rev->getParentId(),
818 'rev_sha1' => (string)$rev->getSha1(),
819 ];
820
821 if ( in_array( 'page', $options ) ) {
822 $fields += [
823 'page_namespace' => (string)$page->getTitle()->getNamespace(),
824 'page_title' => $page->getTitle()->getDBkey(),
825 'page_id' => (string)$page->getId(),
826 'page_latest' => (string)$page->getLatest(),
827 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
828 'page_len' => (string)$page->getContent()->getSize(),
829 ];
830 }
831
832 if ( in_array( 'user', $options ) ) {
833 $fields += [
834 'user_name' => (string)$rev->getUserText(),
835 ];
836 }
837
838 if ( in_array( 'comment', $options ) ) {
839 $fields += [
840 'rev_comment_text' => $rev->getComment(),
841 'rev_comment_data' => null,
842 'rev_comment_cid' => null,
843 ];
844 }
845
846 if ( $rev->getId() ) {
847 $fields += [
848 'rev_id' => (string)$rev->getId(),
849 ];
850 }
851
852 return (object)$fields;
853 }
854
855 protected function assertRevisionRecordMatchesRevision(
856 Revision $rev,
857 RevisionRecord $record
858 ) {
859 $this->assertSame( $rev->getId(), $record->getId() );
860 $this->assertSame( $rev->getPage(), $record->getPageId() );
861 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
862 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
863 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
864 $this->assertSame( $rev->isMinor(), $record->isMinor() );
865 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
866 $this->assertSame( $rev->getSize(), $record->getSize() );
867 /**
868 * @note As of MW 1.31, the database schema allows the parent ID to be
869 * NULL to indicate that it is unknown.
870 */
871 $expectedParent = $rev->getParentId();
872 if ( $expectedParent === null ) {
873 $expectedParent = 0;
874 }
875 $this->assertSame( $expectedParent, $record->getParentId() );
876 $this->assertSame( $rev->getSha1(), $record->getSha1() );
877 $this->assertSame( $rev->getComment(), $record->getComment()->text );
878 $this->assertSame( $rev->getContentFormat(),
879 $record->getContent( SlotRecord::MAIN )->getDefaultFormat() );
880 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord::MAIN )->getModel() );
881 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
882
883 $revRec = $rev->getRevisionRecord();
884 $revMain = $revRec->getSlot( SlotRecord::MAIN );
885 $recMain = $record->getSlot( SlotRecord::MAIN );
886
887 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
888 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
889 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
890
891 if ( $revMain->hasOrigin() ) {
892 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
893 }
894
895 if ( $revMain->hasAddress() ) {
896 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
897 }
898
899 if ( $revMain->hasContentId() ) {
900 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
901 }
902 }
903
904 /**
905 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
906 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
907 */
908 public function testNewRevisionFromRow_getQueryInfo() {
909 $page = $this->getTestPage();
910 $text = __METHOD__ . 'a-ä';
911 /** @var Revision $rev */
912 $rev = $page->doEditContent(
913 new WikitextContent( $text ),
914 __METHOD__ . 'a'
915 )->value['revision'];
916
917 $store = MediaWikiServices::getInstance()->getRevisionStore();
918 $info = $store->getQueryInfo();
919 $row = $this->db->selectRow(
920 $info['tables'],
921 $info['fields'],
922 [ 'rev_id' => $rev->getId() ],
923 __METHOD__,
924 [],
925 $info['joins']
926 );
927 $record = $store->newRevisionFromRow(
928 $row,
929 [],
930 $page->getTitle()
931 );
932 $this->assertRevisionRecordMatchesRevision( $rev, $record );
933 $this->assertSame( $text, $rev->getContent()->serialize() );
934 }
935
936 /**
937 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
938 */
939 public function testNewRevisionFromRow_anonEdit() {
940 $page = $this->getTestPage();
941 $text = __METHOD__ . 'a-ä';
942 /** @var Revision $rev */
943 $rev = $page->doEditContent(
944 new WikitextContent( $text ),
945 __METHOD__ . 'a'
946 )->value['revision'];
947
948 $store = MediaWikiServices::getInstance()->getRevisionStore();
949 $record = $store->newRevisionFromRow(
950 $this->revisionToRow( $rev ),
951 [],
952 $page->getTitle()
953 );
954 $this->assertRevisionRecordMatchesRevision( $rev, $record );
955 $this->assertSame( $text, $rev->getContent()->serialize() );
956 }
957
958 /**
959 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
960 */
961 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
962 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
963 $this->overrideMwServices();
964 $page = $this->getTestPage();
965 $text = __METHOD__ . 'a-ä';
966 /** @var Revision $rev */
967 $rev = $page->doEditContent(
968 new WikitextContent( $text ),
969 __METHOD__ . 'a'
970 )->value['revision'];
971
972 $store = MediaWikiServices::getInstance()->getRevisionStore();
973 $record = $store->newRevisionFromRow(
974 $this->revisionToRow( $rev ),
975 [],
976 $page->getTitle()
977 );
978 $this->assertRevisionRecordMatchesRevision( $rev, $record );
979 $this->assertSame( $text, $rev->getContent()->serialize() );
980 }
981
982 /**
983 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
984 */
985 public function testNewRevisionFromRow_userEdit() {
986 $page = $this->getTestPage();
987 $text = __METHOD__ . 'b-ä';
988 /** @var Revision $rev */
989 $rev = $page->doEditContent(
990 new WikitextContent( $text ),
991 __METHOD__ . 'b',
992 0,
993 false,
994 $this->getTestUser()->getUser()
995 )->value['revision'];
996
997 $store = MediaWikiServices::getInstance()->getRevisionStore();
998 $record = $store->newRevisionFromRow(
999 $this->revisionToRow( $rev ),
1000 [],
1001 $page->getTitle()
1002 );
1003 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1004 $this->assertSame( $text, $rev->getContent()->serialize() );
1005 }
1006
1007 /**
1008 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1009 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1010 */
1011 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1012 $store = MediaWikiServices::getInstance()->getRevisionStore();
1013 $title = Title::newFromText( __METHOD__ );
1014 $text = __METHOD__ . '-bä';
1015 $page = WikiPage::factory( $title );
1016 /** @var Revision $orig */
1017 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1018 ->value['revision'];
1019 $page->doDeleteArticle( __METHOD__ );
1020
1021 $db = wfGetDB( DB_MASTER );
1022 $arQuery = $store->getArchiveQueryInfo();
1023 $res = $db->select(
1024 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1025 __METHOD__, [], $arQuery['joins']
1026 );
1027 $this->assertTrue( is_object( $res ), 'query failed' );
1028
1029 $row = $res->fetchObject();
1030 $res->free();
1031 $record = $store->newRevisionFromArchiveRow( $row );
1032
1033 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1034 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1035 }
1036
1037 /**
1038 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1039 */
1040 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1041 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1042 $this->overrideMwServices();
1043 $store = MediaWikiServices::getInstance()->getRevisionStore();
1044 $title = Title::newFromText( __METHOD__ );
1045 $text = __METHOD__ . '-bä';
1046 $page = WikiPage::factory( $title );
1047 /** @var Revision $orig */
1048 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1049 ->value['revision'];
1050 $page->doDeleteArticle( __METHOD__ );
1051
1052 $db = wfGetDB( DB_MASTER );
1053 $arQuery = $store->getArchiveQueryInfo();
1054 $res = $db->select(
1055 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1056 __METHOD__, [], $arQuery['joins']
1057 );
1058 $this->assertTrue( is_object( $res ), 'query failed' );
1059
1060 $row = $res->fetchObject();
1061 $res->free();
1062 $record = $store->newRevisionFromArchiveRow( $row );
1063
1064 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1065 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1066 }
1067
1068 /**
1069 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1070 */
1071 public function testNewRevisionFromArchiveRow_no_user() {
1072 $store = MediaWikiServices::getInstance()->getRevisionStore();
1073
1074 $row = (object)[
1075 'ar_id' => '1',
1076 'ar_page_id' => '2',
1077 'ar_namespace' => '0',
1078 'ar_title' => 'Something',
1079 'ar_rev_id' => '2',
1080 'ar_text_id' => '47',
1081 'ar_timestamp' => '20180528192356',
1082 'ar_minor_edit' => '0',
1083 'ar_deleted' => '0',
1084 'ar_len' => '78',
1085 'ar_parent_id' => '0',
1086 'ar_sha1' => 'deadbeef',
1087 'ar_comment_text' => 'whatever',
1088 'ar_comment_data' => null,
1089 'ar_comment_cid' => null,
1090 'ar_user' => '0',
1091 'ar_user_text' => '', // this is the important bit
1092 'ar_actor' => null,
1093 'ar_content_format' => null,
1094 'ar_content_model' => null,
1095 ];
1096
1097 \Wikimedia\suppressWarnings();
1098 $record = $store->newRevisionFromArchiveRow( $row );
1099 \Wikimedia\suppressWarnings( true );
1100
1101 $this->assertInstanceOf( RevisionRecord::class, $record );
1102 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1103 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1104 }
1105
1106 /**
1107 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1108 */
1109 public function testNewRevisionFromRow_no_user() {
1110 $store = MediaWikiServices::getInstance()->getRevisionStore();
1111 $title = Title::newFromText( __METHOD__ );
1112
1113 $row = (object)[
1114 'rev_id' => '2',
1115 'rev_page' => '2',
1116 'page_namespace' => '0',
1117 'page_title' => $title->getText(),
1118 'rev_text_id' => '47',
1119 'rev_timestamp' => '20180528192356',
1120 'rev_minor_edit' => '0',
1121 'rev_deleted' => '0',
1122 'rev_len' => '78',
1123 'rev_parent_id' => '0',
1124 'rev_sha1' => 'deadbeef',
1125 'rev_comment_text' => 'whatever',
1126 'rev_comment_data' => null,
1127 'rev_comment_cid' => null,
1128 'rev_user' => '0',
1129 'rev_user_text' => '', // this is the important bit
1130 'rev_actor' => null,
1131 'rev_content_format' => null,
1132 'rev_content_model' => null,
1133 ];
1134
1135 \Wikimedia\suppressWarnings();
1136 $record = $store->newRevisionFromRow( $row, 0, $title );
1137 \Wikimedia\suppressWarnings( true );
1138
1139 $this->assertNotNull( $record );
1140 $this->assertNotNull( $record->getUser() );
1141 $this->assertNotEmpty( $record->getUser()->getName() );
1142 }
1143
1144 /**
1145 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1146 */
1147 public function testInsertRevisionOn_archive() {
1148 // This is a round trip test for deletion and undeletion of a
1149 // revision row via the archive table.
1150
1151 $store = MediaWikiServices::getInstance()->getRevisionStore();
1152 $title = Title::newFromText( __METHOD__ );
1153
1154 $page = WikiPage::factory( $title );
1155 /** @var Revision $origRev */
1156 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1157 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1158 ->value['revision'];
1159 $orig = $origRev->getRevisionRecord();
1160 $page->doDeleteArticle( __METHOD__ );
1161
1162 // re-create page, so we can later load revisions for it
1163 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1164
1165 $db = wfGetDB( DB_MASTER );
1166 $arQuery = $store->getArchiveQueryInfo();
1167 $row = $db->selectRow(
1168 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1169 __METHOD__, [], $arQuery['joins']
1170 );
1171
1172 $this->assertNotFalse( $row, 'query failed' );
1173
1174 $record = $store->newRevisionFromArchiveRow(
1175 $row,
1176 0,
1177 $title,
1178 [ 'page_id' => $title->getArticleID() ]
1179 );
1180
1181 $restored = $store->insertRevisionOn( $record, $db );
1182
1183 // is the new revision correct?
1184 $this->assertRevisionCompleteness( $restored );
1185 $this->assertRevisionRecordsEqual( $record, $restored );
1186
1187 // does the new revision use the original slot?
1188 $recMain = $record->getSlot( SlotRecord::MAIN );
1189 $restMain = $restored->getSlot( SlotRecord::MAIN );
1190 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1191 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1192 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1193 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1194
1195 // can we load it from the store?
1196 $loaded = $store->getRevisionById( $restored->getId() );
1197 $this->assertNotNull( $loaded );
1198 $this->assertRevisionCompleteness( $loaded );
1199 $this->assertRevisionRecordsEqual( $restored, $loaded );
1200
1201 // can we find it directly in the database?
1202 $this->assertRevisionExistsInDatabase( $restored );
1203 }
1204
1205 /**
1206 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1207 */
1208 public function testLoadRevisionFromId() {
1209 $title = Title::newFromText( __METHOD__ );
1210 $page = WikiPage::factory( $title );
1211 /** @var Revision $rev */
1212 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1213 ->value['revision'];
1214
1215 $store = MediaWikiServices::getInstance()->getRevisionStore();
1216 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1217 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1218 }
1219
1220 /**
1221 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1222 */
1223 public function testLoadRevisionFromPageId() {
1224 $title = Title::newFromText( __METHOD__ );
1225 $page = WikiPage::factory( $title );
1226 /** @var Revision $rev */
1227 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1228 ->value['revision'];
1229
1230 $store = MediaWikiServices::getInstance()->getRevisionStore();
1231 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1232 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1233 }
1234
1235 /**
1236 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1237 */
1238 public function testLoadRevisionFromTitle() {
1239 $title = Title::newFromText( __METHOD__ );
1240 $page = WikiPage::factory( $title );
1241 /** @var Revision $rev */
1242 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1243 ->value['revision'];
1244
1245 $store = MediaWikiServices::getInstance()->getRevisionStore();
1246 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1247 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1248 }
1249
1250 /**
1251 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1252 */
1253 public function testLoadRevisionFromTimestamp() {
1254 $title = Title::newFromText( __METHOD__ );
1255 $page = WikiPage::factory( $title );
1256 /** @var Revision $revOne */
1257 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1258 ->value['revision'];
1259 // Sleep to ensure different timestamps... )(evil)
1260 sleep( 1 );
1261 /** @var Revision $revTwo */
1262 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1263 ->value['revision'];
1264
1265 $store = MediaWikiServices::getInstance()->getRevisionStore();
1266 $this->assertNull(
1267 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1268 );
1269 $this->assertSame(
1270 $revOne->getId(),
1271 $store->loadRevisionFromTimestamp(
1272 wfGetDB( DB_MASTER ),
1273 $title,
1274 $revOne->getTimestamp()
1275 )->getId()
1276 );
1277 $this->assertSame(
1278 $revTwo->getId(),
1279 $store->loadRevisionFromTimestamp(
1280 wfGetDB( DB_MASTER ),
1281 $title,
1282 $revTwo->getTimestamp()
1283 )->getId()
1284 );
1285 }
1286
1287 /**
1288 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1289 */
1290 public function testGetParentLengths() {
1291 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1292 /** @var Revision $revOne */
1293 $revOne = $page->doEditContent(
1294 new WikitextContent( __METHOD__ ), __METHOD__
1295 )->value['revision'];
1296 /** @var Revision $revTwo */
1297 $revTwo = $page->doEditContent(
1298 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1299 )->value['revision'];
1300
1301 $store = MediaWikiServices::getInstance()->getRevisionStore();
1302 $this->assertSame(
1303 [
1304 $revOne->getId() => strlen( __METHOD__ ),
1305 ],
1306 $store->listRevisionSizes(
1307 wfGetDB( DB_MASTER ),
1308 [ $revOne->getId() ]
1309 )
1310 );
1311 $this->assertSame(
1312 [
1313 $revOne->getId() => strlen( __METHOD__ ),
1314 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1315 ],
1316 $store->listRevisionSizes(
1317 wfGetDB( DB_MASTER ),
1318 [ $revOne->getId(), $revTwo->getId() ]
1319 )
1320 );
1321 }
1322
1323 /**
1324 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1325 */
1326 public function testGetPreviousRevision() {
1327 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1328 /** @var Revision $revOne */
1329 $revOne = $page->doEditContent(
1330 new WikitextContent( __METHOD__ ), __METHOD__
1331 )->value['revision'];
1332 /** @var Revision $revTwo */
1333 $revTwo = $page->doEditContent(
1334 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1335 )->value['revision'];
1336
1337 $store = MediaWikiServices::getInstance()->getRevisionStore();
1338 $this->assertNull(
1339 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1340 );
1341 $this->assertSame(
1342 $revOne->getId(),
1343 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1344 );
1345 }
1346
1347 /**
1348 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1349 */
1350 public function testGetNextRevision() {
1351 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1352 /** @var Revision $revOne */
1353 $revOne = $page->doEditContent(
1354 new WikitextContent( __METHOD__ ), __METHOD__
1355 )->value['revision'];
1356 /** @var Revision $revTwo */
1357 $revTwo = $page->doEditContent(
1358 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1359 )->value['revision'];
1360
1361 $store = MediaWikiServices::getInstance()->getRevisionStore();
1362 $this->assertSame(
1363 $revTwo->getId(),
1364 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1365 );
1366 $this->assertNull(
1367 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1368 );
1369 }
1370
1371 public function provideNonHistoryRevision() {
1372 $title = Title::newFromText( __METHOD__ );
1373 $rev = new MutableRevisionRecord( $title );
1374 yield [ $rev ];
1375
1376 $user = new UserIdentityValue( 7, 'Frank', 0 );
1377 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1378 $row = (object)[
1379 'ar_id' => 3,
1380 'ar_rev_id' => 34567,
1381 'ar_page_id' => 5,
1382 'ar_deleted' => 0,
1383 'ar_minor_edit' => 0,
1384 'ar_timestamp' => '20180101020202',
1385 ];
1386 $slots = new RevisionSlots( [] );
1387 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1388 yield [ $rev ];
1389 }
1390
1391 /**
1392 * @dataProvider provideNonHistoryRevision
1393 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1394 */
1395 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1396 $store = MediaWikiServices::getInstance()->getRevisionStore();
1397 $this->assertNull( $store->getPreviousRevision( $rev ) );
1398 }
1399
1400 /**
1401 * @dataProvider provideNonHistoryRevision
1402 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1403 */
1404 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1405 $store = MediaWikiServices::getInstance()->getRevisionStore();
1406 $this->assertNull( $store->getNextRevision( $rev ) );
1407 }
1408
1409 /**
1410 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1411 */
1412 public function testGetTimestampFromId_found() {
1413 $page = $this->getTestPage();
1414 /** @var Revision $rev */
1415 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1416 ->value['revision'];
1417
1418 $store = MediaWikiServices::getInstance()->getRevisionStore();
1419 $result = $store->getTimestampFromId(
1420 $page->getTitle(),
1421 $rev->getId()
1422 );
1423
1424 $this->assertSame( $rev->getTimestamp(), $result );
1425 }
1426
1427 /**
1428 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1429 */
1430 public function testGetTimestampFromId_notFound() {
1431 $page = $this->getTestPage();
1432 /** @var Revision $rev */
1433 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1434 ->value['revision'];
1435
1436 $store = MediaWikiServices::getInstance()->getRevisionStore();
1437 $result = $store->getTimestampFromId(
1438 $page->getTitle(),
1439 $rev->getId() + 1
1440 );
1441
1442 $this->assertFalse( $result );
1443 }
1444
1445 /**
1446 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1447 */
1448 public function testCountRevisionsByPageId() {
1449 $store = MediaWikiServices::getInstance()->getRevisionStore();
1450 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1451
1452 $this->assertSame(
1453 0,
1454 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1455 );
1456 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1457 $this->assertSame(
1458 1,
1459 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1460 );
1461 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1462 $this->assertSame(
1463 2,
1464 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1465 );
1466 }
1467
1468 /**
1469 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1470 */
1471 public function testCountRevisionsByTitle() {
1472 $store = MediaWikiServices::getInstance()->getRevisionStore();
1473 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1474
1475 $this->assertSame(
1476 0,
1477 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1478 );
1479 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1480 $this->assertSame(
1481 1,
1482 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1483 );
1484 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1485 $this->assertSame(
1486 2,
1487 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1488 );
1489 }
1490
1491 /**
1492 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1493 */
1494 public function testUserWasLastToEdit_false() {
1495 $sysop = $this->getTestSysop()->getUser();
1496 $page = $this->getTestPage();
1497 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1498
1499 $store = MediaWikiServices::getInstance()->getRevisionStore();
1500 $result = $store->userWasLastToEdit(
1501 wfGetDB( DB_MASTER ),
1502 $page->getId(),
1503 $sysop->getId(),
1504 '20160101010101'
1505 );
1506 $this->assertFalse( $result );
1507 }
1508
1509 /**
1510 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1511 */
1512 public function testUserWasLastToEdit_true() {
1513 $startTime = wfTimestampNow();
1514 $sysop = $this->getTestSysop()->getUser();
1515 $page = $this->getTestPage();
1516 $page->doEditContent(
1517 new WikitextContent( __METHOD__ ),
1518 __METHOD__,
1519 0,
1520 false,
1521 $sysop
1522 );
1523
1524 $store = MediaWikiServices::getInstance()->getRevisionStore();
1525 $result = $store->userWasLastToEdit(
1526 wfGetDB( DB_MASTER ),
1527 $page->getId(),
1528 $sysop->getId(),
1529 $startTime
1530 );
1531 $this->assertTrue( $result );
1532 }
1533
1534 /**
1535 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1536 */
1537 public function testGetKnownCurrentRevision() {
1538 $page = $this->getTestPage();
1539 /** @var Revision $rev */
1540 $rev = $page->doEditContent(
1541 new WikitextContent( __METHOD__ . 'b' ),
1542 __METHOD__ . 'b',
1543 0,
1544 false,
1545 $this->getTestUser()->getUser()
1546 )->value['revision'];
1547
1548 $store = MediaWikiServices::getInstance()->getRevisionStore();
1549 $record = $store->getKnownCurrentRevision(
1550 $page->getTitle(),
1551 $rev->getId()
1552 );
1553
1554 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1555 }
1556
1557 public function provideNewMutableRevisionFromArray() {
1558 yield 'Basic array, content object' => [
1559 [
1560 'id' => 2,
1561 'page' => 1,
1562 'timestamp' => '20171017114835',
1563 'user_text' => '111.0.1.2',
1564 'user' => 0,
1565 'minor_edit' => false,
1566 'deleted' => 0,
1567 'len' => 46,
1568 'parent_id' => 1,
1569 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1570 'comment' => 'Goat Comment!',
1571 'content' => new WikitextContent( 'Some Content' ),
1572 ]
1573 ];
1574 yield 'Basic array, serialized text' => [
1575 [
1576 'id' => 2,
1577 'page' => 1,
1578 'timestamp' => '20171017114835',
1579 'user_text' => '111.0.1.2',
1580 'user' => 0,
1581 'minor_edit' => false,
1582 'deleted' => 0,
1583 'len' => 46,
1584 'parent_id' => 1,
1585 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1586 'comment' => 'Goat Comment!',
1587 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1588 ]
1589 ];
1590 yield 'Basic array, serialized text, utf-8 flags' => [
1591 [
1592 'id' => 2,
1593 'page' => 1,
1594 'timestamp' => '20171017114835',
1595 'user_text' => '111.0.1.2',
1596 'user' => 0,
1597 'minor_edit' => false,
1598 'deleted' => 0,
1599 'len' => 46,
1600 'parent_id' => 1,
1601 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1602 'comment' => 'Goat Comment!',
1603 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1604 'flags' => 'utf-8',
1605 ]
1606 ];
1607 yield 'Basic array, with title' => [
1608 [
1609 'title' => Title::newFromText( 'SomeText' ),
1610 'timestamp' => '20171017114835',
1611 'user_text' => '111.0.1.2',
1612 'user' => 0,
1613 'minor_edit' => false,
1614 'deleted' => 0,
1615 'len' => 46,
1616 'parent_id' => 1,
1617 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1618 'comment' => 'Goat Comment!',
1619 'content' => new WikitextContent( 'Some Content' ),
1620 ]
1621 ];
1622 yield 'Basic array, no user field' => [
1623 [
1624 'id' => 2,
1625 'page' => 1,
1626 'timestamp' => '20171017114835',
1627 'user_text' => '111.0.1.3',
1628 'minor_edit' => false,
1629 'deleted' => 0,
1630 'len' => 46,
1631 'parent_id' => 1,
1632 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1633 'comment' => 'Goat Comment!',
1634 'content' => new WikitextContent( 'Some Content' ),
1635 ]
1636 ];
1637 }
1638
1639 /**
1640 * @dataProvider provideNewMutableRevisionFromArray
1641 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1642 */
1643 public function testNewMutableRevisionFromArray( array $array ) {
1644 $store = MediaWikiServices::getInstance()->getRevisionStore();
1645
1646 // HACK: if $array['page'] is given, make sure that the page exists
1647 if ( isset( $array['page'] ) ) {
1648 $t = Title::newFromID( $array['page'] );
1649 if ( !$t || !$t->exists() ) {
1650 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1651 $info = $this->insertPage( $t );
1652 $array['page'] = $info['id'];
1653 }
1654 }
1655
1656 $result = $store->newMutableRevisionFromArray( $array );
1657
1658 if ( isset( $array['id'] ) ) {
1659 $this->assertSame( $array['id'], $result->getId() );
1660 }
1661 if ( isset( $array['page'] ) ) {
1662 $this->assertSame( $array['page'], $result->getPageId() );
1663 }
1664 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1665 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1666 if ( isset( $array['user'] ) ) {
1667 $this->assertSame( $array['user'], $result->getUser()->getId() );
1668 }
1669 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1670 $this->assertSame( $array['deleted'], $result->getVisibility() );
1671 $this->assertSame( $array['len'], $result->getSize() );
1672 $this->assertSame( $array['parent_id'], $result->getParentId() );
1673 $this->assertSame( $array['sha1'], $result->getSha1() );
1674 $this->assertSame( $array['comment'], $result->getComment()->text );
1675 if ( isset( $array['content'] ) ) {
1676 foreach ( $array['content'] as $role => $content ) {
1677 $this->assertTrue(
1678 $result->getContent( $role )->equals( $content )
1679 );
1680 }
1681 } elseif ( isset( $array['text'] ) ) {
1682 $this->assertSame( $array['text'],
1683 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1684 } elseif ( isset( $array['content_format'] ) ) {
1685 $this->assertSame(
1686 $array['content_format'],
1687 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1688 );
1689 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1690 }
1691 }
1692
1693 /**
1694 * @dataProvider provideNewMutableRevisionFromArray
1695 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1696 */
1697 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1698 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1699 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1700 $blobStore = new SqlBlobStore( $lb, $cache );
1701 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1702
1703 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1704 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1705 ->disableOriginalConstructor()
1706 ->getMock();
1707 $factory->expects( $this->any() )
1708 ->method( 'newBlobStore' )
1709 ->willReturn( $blobStore );
1710 $factory->expects( $this->any() )
1711 ->method( 'newSqlBlobStore' )
1712 ->willReturn( $blobStore );
1713
1714 $this->setService( 'BlobStoreFactory', $factory );
1715
1716 $this->testNewMutableRevisionFromArray( $array );
1717 }
1718
1719 /**
1720 * Creates a new revision for testing caching behavior
1721 *
1722 * @param WikiPage $page the page for the new revision
1723 * @param RevisionStore $store store object to use for creating the revision
1724 * @return bool|RevisionStoreRecord the revision created, or false if missing
1725 */
1726 private function createRevisionStoreCacheRecord( $page, $store ) {
1727 $user = MediaWikiTestCase::getMutableTestUser()->getUser();
1728 $updater = $page->newPageUpdater( $user );
1729 $updater->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) );
1730 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1731 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1732 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1733 }
1734
1735 /**
1736 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1737 */
1738 public function testGetKnownCurrentRevision_userNameChange() {
1739 global $wgActorTableSchemaMigrationStage;
1740
1741 $this->overrideMwServices();
1742 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1743 $this->setService( 'MainWANObjectCache', $cache );
1744
1745 $store = MediaWikiServices::getInstance()->getRevisionStore();
1746 $page = $this->getNonexistingTestPage();
1747 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1748
1749 // Grab the user name
1750 $userNameBefore = $rev->getUser()->getName();
1751
1752 // Change the user name in the database, "behind the back" of the cache
1753 $newUserName = "Renamed $userNameBefore";
1754 $this->db->update( 'revision',
1755 [ 'rev_user_text' => $newUserName ],
1756 [ 'rev_id' => $rev->getId() ] );
1757 $this->db->update( 'user',
1758 [ 'user_name' => $newUserName ],
1759 [ 'user_id' => $rev->getUser()->getId() ] );
1760 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1761 $this->db->update( 'actor',
1762 [ 'actor_name' => $newUserName ],
1763 [ 'actor_user' => $rev->getUser()->getId() ] );
1764 }
1765
1766 // Reload the revision and regrab the user name.
1767 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1768 $userNameAfter = $revAfter->getUser()->getName();
1769
1770 // The two user names should be different.
1771 // If they are the same, we are seeing a cached value, which is bad.
1772 $this->assertNotSame( $userNameBefore, $userNameAfter );
1773
1774 // This is implied by the above assertion, but explicitly check it, for completeness
1775 $this->assertSame( $newUserName, $userNameAfter );
1776 }
1777
1778 /**
1779 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1780 */
1781 public function testGetKnownCurrentRevision_revDelete() {
1782 $this->overrideMwServices();
1783 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1784 $this->setService( 'MainWANObjectCache', $cache );
1785
1786 $store = MediaWikiServices::getInstance()->getRevisionStore();
1787 $page = $this->getNonexistingTestPage();
1788 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1789
1790 // Grab the deleted bitmask
1791 $deletedBefore = $rev->getVisibility();
1792
1793 // Change the deleted bitmask in the database, "behind the back" of the cache
1794 $this->db->update( 'revision',
1795 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1796 [ 'rev_id' => $rev->getId() ] );
1797
1798 // Reload the revision and regrab the visibility flag.
1799 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1800 $deletedAfter = $revAfter->getVisibility();
1801
1802 // The two deleted flags should be different.
1803 // If they are the same, we are seeing a cached value, which is bad.
1804 $this->assertNotSame( $deletedBefore, $deletedAfter );
1805
1806 // This is implied by the above assertion, but explicitly check it, for completeness
1807 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1808 }
1809
1810 /**
1811 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1812 */
1813 public function testNewRevisionFromRow_userNameChange() {
1814 global $wgActorTableSchemaMigrationStage;
1815
1816 $page = $this->getTestPage();
1817 $text = __METHOD__;
1818 /** @var Revision $rev */
1819 $rev = $page->doEditContent(
1820 new WikitextContent( $text ),
1821 __METHOD__,
1822 0,
1823 false,
1824 $this->getMutableTestUser()->getUser()
1825 )->value['revision'];
1826
1827 $store = MediaWikiServices::getInstance()->getRevisionStore();
1828 $record = $store->newRevisionFromRow(
1829 $this->revisionToRow( $rev ),
1830 [],
1831 $page->getTitle()
1832 );
1833
1834 // Grab the user name
1835 $userNameBefore = $record->getUser()->getName();
1836
1837 // Change the user name in the database
1838 $newUserName = "Renamed $userNameBefore";
1839 $this->db->update( 'revision',
1840 [ 'rev_user_text' => $newUserName ],
1841 [ 'rev_id' => $record->getId() ] );
1842 $this->db->update( 'user',
1843 [ 'user_name' => $newUserName ],
1844 [ 'user_id' => $record->getUser()->getId() ] );
1845 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1846 $this->db->update( 'actor',
1847 [ 'actor_name' => $newUserName ],
1848 [ 'actor_user' => $record->getUser()->getId() ] );
1849 }
1850
1851 // Reload the record, passing $fromCache as true to force fresh info from the db,
1852 // and regrab the user name
1853 $recordAfter = $store->newRevisionFromRow(
1854 $this->revisionToRow( $rev ),
1855 [],
1856 $page->getTitle(),
1857 true
1858 );
1859 $userNameAfter = $recordAfter->getUser()->getName();
1860
1861 // The two user names should be different.
1862 // If they are the same, we are seeing a cached value, which is bad.
1863 $this->assertNotSame( $userNameBefore, $userNameAfter );
1864
1865 // This is implied by the above assertion, but explicitly check it, for completeness
1866 $this->assertSame( $newUserName, $userNameAfter );
1867 }
1868
1869 /**
1870 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1871 */
1872 public function testNewRevisionFromRow_revDelete() {
1873 $page = $this->getTestPage();
1874 $text = __METHOD__;
1875 /** @var Revision $rev */
1876 $rev = $page->doEditContent(
1877 new WikitextContent( $text ),
1878 __METHOD__
1879 )->value['revision'];
1880
1881 $store = MediaWikiServices::getInstance()->getRevisionStore();
1882 $record = $store->newRevisionFromRow(
1883 $this->revisionToRow( $rev ),
1884 [],
1885 $page->getTitle()
1886 );
1887
1888 // Grab the deleted bitmask
1889 $deletedBefore = $record->getVisibility();
1890
1891 // Change the deleted bitmask in the database
1892 $this->db->update( 'revision',
1893 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1894 [ 'rev_id' => $record->getId() ] );
1895
1896 // Reload the record, passing $fromCache as true to force fresh info from the db,
1897 // and regrab the deleted bitmask
1898 $recordAfter = $store->newRevisionFromRow(
1899 $this->revisionToRow( $rev ),
1900 [],
1901 $page->getTitle(),
1902 true
1903 );
1904 $deletedAfter = $recordAfter->getVisibility();
1905
1906 // The two deleted flags should be different, because we modified the database.
1907 $this->assertNotSame( $deletedBefore, $deletedAfter );
1908
1909 // This is implied by the above assertion, but explicitly check it, for completeness
1910 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1911 }
1912
1913 }