Merge "WebRequest: Change getFullRequestURL() to use local getProtocol()"
[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( $rev->getId() );
1420
1421 $this->assertSame( $rev->getTimestamp(), $result );
1422 }
1423
1424 /**
1425 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1426 */
1427 public function testGetTimestampFromId_notFound() {
1428 $page = $this->getTestPage();
1429 /** @var Revision $rev */
1430 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1431 ->value['revision'];
1432
1433 $store = MediaWikiServices::getInstance()->getRevisionStore();
1434 $result = $store->getTimestampFromId( $rev->getId() + 1 );
1435
1436 $this->assertFalse( $result );
1437 }
1438
1439 /**
1440 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1441 */
1442 public function testCountRevisionsByPageId() {
1443 $store = MediaWikiServices::getInstance()->getRevisionStore();
1444 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1445
1446 $this->assertSame(
1447 0,
1448 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1449 );
1450 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1451 $this->assertSame(
1452 1,
1453 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1454 );
1455 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1456 $this->assertSame(
1457 2,
1458 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1459 );
1460 }
1461
1462 /**
1463 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1464 */
1465 public function testCountRevisionsByTitle() {
1466 $store = MediaWikiServices::getInstance()->getRevisionStore();
1467 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1468
1469 $this->assertSame(
1470 0,
1471 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1472 );
1473 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1474 $this->assertSame(
1475 1,
1476 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1477 );
1478 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1479 $this->assertSame(
1480 2,
1481 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1482 );
1483 }
1484
1485 /**
1486 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1487 */
1488 public function testUserWasLastToEdit_false() {
1489 $sysop = $this->getTestSysop()->getUser();
1490 $page = $this->getTestPage();
1491 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1492
1493 $store = MediaWikiServices::getInstance()->getRevisionStore();
1494 $result = $store->userWasLastToEdit(
1495 wfGetDB( DB_MASTER ),
1496 $page->getId(),
1497 $sysop->getId(),
1498 '20160101010101'
1499 );
1500 $this->assertFalse( $result );
1501 }
1502
1503 /**
1504 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1505 */
1506 public function testUserWasLastToEdit_true() {
1507 $startTime = wfTimestampNow();
1508 $sysop = $this->getTestSysop()->getUser();
1509 $page = $this->getTestPage();
1510 $page->doEditContent(
1511 new WikitextContent( __METHOD__ ),
1512 __METHOD__,
1513 0,
1514 false,
1515 $sysop
1516 );
1517
1518 $store = MediaWikiServices::getInstance()->getRevisionStore();
1519 $result = $store->userWasLastToEdit(
1520 wfGetDB( DB_MASTER ),
1521 $page->getId(),
1522 $sysop->getId(),
1523 $startTime
1524 );
1525 $this->assertTrue( $result );
1526 }
1527
1528 /**
1529 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1530 */
1531 public function testGetKnownCurrentRevision() {
1532 $page = $this->getTestPage();
1533 /** @var Revision $rev */
1534 $rev = $page->doEditContent(
1535 new WikitextContent( __METHOD__ . 'b' ),
1536 __METHOD__ . 'b',
1537 0,
1538 false,
1539 $this->getTestUser()->getUser()
1540 )->value['revision'];
1541
1542 $store = MediaWikiServices::getInstance()->getRevisionStore();
1543 $record = $store->getKnownCurrentRevision(
1544 $page->getTitle(),
1545 $rev->getId()
1546 );
1547
1548 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1549 }
1550
1551 public function provideNewMutableRevisionFromArray() {
1552 yield 'Basic array, content object' => [
1553 [
1554 'id' => 2,
1555 'page' => 1,
1556 'timestamp' => '20171017114835',
1557 'user_text' => '111.0.1.2',
1558 'user' => 0,
1559 'minor_edit' => false,
1560 'deleted' => 0,
1561 'len' => 46,
1562 'parent_id' => 1,
1563 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1564 'comment' => 'Goat Comment!',
1565 'content' => new WikitextContent( 'Some Content' ),
1566 ]
1567 ];
1568 yield 'Basic array, serialized text' => [
1569 [
1570 'id' => 2,
1571 'page' => 1,
1572 'timestamp' => '20171017114835',
1573 'user_text' => '111.0.1.2',
1574 'user' => 0,
1575 'minor_edit' => false,
1576 'deleted' => 0,
1577 'len' => 46,
1578 'parent_id' => 1,
1579 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1580 'comment' => 'Goat Comment!',
1581 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1582 ]
1583 ];
1584 yield 'Basic array, serialized text, utf-8 flags' => [
1585 [
1586 'id' => 2,
1587 'page' => 1,
1588 'timestamp' => '20171017114835',
1589 'user_text' => '111.0.1.2',
1590 'user' => 0,
1591 'minor_edit' => false,
1592 'deleted' => 0,
1593 'len' => 46,
1594 'parent_id' => 1,
1595 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1596 'comment' => 'Goat Comment!',
1597 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1598 'flags' => 'utf-8',
1599 ]
1600 ];
1601 yield 'Basic array, with title' => [
1602 [
1603 'title' => Title::newFromText( 'SomeText' ),
1604 'timestamp' => '20171017114835',
1605 'user_text' => '111.0.1.2',
1606 'user' => 0,
1607 'minor_edit' => false,
1608 'deleted' => 0,
1609 'len' => 46,
1610 'parent_id' => 1,
1611 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1612 'comment' => 'Goat Comment!',
1613 'content' => new WikitextContent( 'Some Content' ),
1614 ]
1615 ];
1616 yield 'Basic array, no user field' => [
1617 [
1618 'id' => 2,
1619 'page' => 1,
1620 'timestamp' => '20171017114835',
1621 'user_text' => '111.0.1.3',
1622 'minor_edit' => false,
1623 'deleted' => 0,
1624 'len' => 46,
1625 'parent_id' => 1,
1626 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1627 'comment' => 'Goat Comment!',
1628 'content' => new WikitextContent( 'Some Content' ),
1629 ]
1630 ];
1631 }
1632
1633 /**
1634 * @dataProvider provideNewMutableRevisionFromArray
1635 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1636 */
1637 public function testNewMutableRevisionFromArray( array $array ) {
1638 $store = MediaWikiServices::getInstance()->getRevisionStore();
1639
1640 // HACK: if $array['page'] is given, make sure that the page exists
1641 if ( isset( $array['page'] ) ) {
1642 $t = Title::newFromID( $array['page'] );
1643 if ( !$t || !$t->exists() ) {
1644 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1645 $info = $this->insertPage( $t );
1646 $array['page'] = $info['id'];
1647 }
1648 }
1649
1650 $result = $store->newMutableRevisionFromArray( $array );
1651
1652 if ( isset( $array['id'] ) ) {
1653 $this->assertSame( $array['id'], $result->getId() );
1654 }
1655 if ( isset( $array['page'] ) ) {
1656 $this->assertSame( $array['page'], $result->getPageId() );
1657 }
1658 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1659 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1660 if ( isset( $array['user'] ) ) {
1661 $this->assertSame( $array['user'], $result->getUser()->getId() );
1662 }
1663 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1664 $this->assertSame( $array['deleted'], $result->getVisibility() );
1665 $this->assertSame( $array['len'], $result->getSize() );
1666 $this->assertSame( $array['parent_id'], $result->getParentId() );
1667 $this->assertSame( $array['sha1'], $result->getSha1() );
1668 $this->assertSame( $array['comment'], $result->getComment()->text );
1669 if ( isset( $array['content'] ) ) {
1670 foreach ( $array['content'] as $role => $content ) {
1671 $this->assertTrue(
1672 $result->getContent( $role )->equals( $content )
1673 );
1674 }
1675 } elseif ( isset( $array['text'] ) ) {
1676 $this->assertSame( $array['text'],
1677 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1678 } elseif ( isset( $array['content_format'] ) ) {
1679 $this->assertSame(
1680 $array['content_format'],
1681 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1682 );
1683 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1684 }
1685 }
1686
1687 /**
1688 * @dataProvider provideNewMutableRevisionFromArray
1689 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1690 */
1691 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1692 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1693 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1694 $blobStore = new SqlBlobStore( $lb, $cache );
1695 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1696
1697 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1698 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1699 ->disableOriginalConstructor()
1700 ->getMock();
1701 $factory->expects( $this->any() )
1702 ->method( 'newBlobStore' )
1703 ->willReturn( $blobStore );
1704 $factory->expects( $this->any() )
1705 ->method( 'newSqlBlobStore' )
1706 ->willReturn( $blobStore );
1707
1708 $this->setService( 'BlobStoreFactory', $factory );
1709
1710 $this->testNewMutableRevisionFromArray( $array );
1711 }
1712
1713 /**
1714 * Creates a new revision for testing caching behavior
1715 *
1716 * @param WikiPage $page the page for the new revision
1717 * @param RevisionStore $store store object to use for creating the revision
1718 * @return bool|RevisionStoreRecord the revision created, or false if missing
1719 */
1720 private function createRevisionStoreCacheRecord( $page, $store ) {
1721 $user = MediaWikiTestCase::getMutableTestUser()->getUser();
1722 $updater = $page->newPageUpdater( $user );
1723 $updater->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) );
1724 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1725 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1726 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1727 }
1728
1729 /**
1730 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1731 */
1732 public function testGetKnownCurrentRevision_userNameChange() {
1733 global $wgActorTableSchemaMigrationStage;
1734
1735 $this->overrideMwServices();
1736 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1737 $this->setService( 'MainWANObjectCache', $cache );
1738
1739 $store = MediaWikiServices::getInstance()->getRevisionStore();
1740 $page = $this->getNonexistingTestPage();
1741 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1742
1743 // Grab the user name
1744 $userNameBefore = $rev->getUser()->getName();
1745
1746 // Change the user name in the database, "behind the back" of the cache
1747 $newUserName = "Renamed $userNameBefore";
1748 $this->db->update( 'revision',
1749 [ 'rev_user_text' => $newUserName ],
1750 [ 'rev_id' => $rev->getId() ] );
1751 $this->db->update( 'user',
1752 [ 'user_name' => $newUserName ],
1753 [ 'user_id' => $rev->getUser()->getId() ] );
1754 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1755 $this->db->update( 'actor',
1756 [ 'actor_name' => $newUserName ],
1757 [ 'actor_user' => $rev->getUser()->getId() ] );
1758 }
1759
1760 // Reload the revision and regrab the user name.
1761 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1762 $userNameAfter = $revAfter->getUser()->getName();
1763
1764 // The two user names should be different.
1765 // If they are the same, we are seeing a cached value, which is bad.
1766 $this->assertNotSame( $userNameBefore, $userNameAfter );
1767
1768 // This is implied by the above assertion, but explicitly check it, for completeness
1769 $this->assertSame( $newUserName, $userNameAfter );
1770 }
1771
1772 /**
1773 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1774 */
1775 public function testGetKnownCurrentRevision_revDelete() {
1776 $this->overrideMwServices();
1777 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1778 $this->setService( 'MainWANObjectCache', $cache );
1779
1780 $store = MediaWikiServices::getInstance()->getRevisionStore();
1781 $page = $this->getNonexistingTestPage();
1782 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1783
1784 // Grab the deleted bitmask
1785 $deletedBefore = $rev->getVisibility();
1786
1787 // Change the deleted bitmask in the database, "behind the back" of the cache
1788 $this->db->update( 'revision',
1789 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1790 [ 'rev_id' => $rev->getId() ] );
1791
1792 // Reload the revision and regrab the visibility flag.
1793 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1794 $deletedAfter = $revAfter->getVisibility();
1795
1796 // The two deleted flags should be different.
1797 // If they are the same, we are seeing a cached value, which is bad.
1798 $this->assertNotSame( $deletedBefore, $deletedAfter );
1799
1800 // This is implied by the above assertion, but explicitly check it, for completeness
1801 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1802 }
1803
1804 /**
1805 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1806 */
1807 public function testNewRevisionFromRow_userNameChange() {
1808 global $wgActorTableSchemaMigrationStage;
1809
1810 $page = $this->getTestPage();
1811 $text = __METHOD__;
1812 /** @var Revision $rev */
1813 $rev = $page->doEditContent(
1814 new WikitextContent( $text ),
1815 __METHOD__,
1816 0,
1817 false,
1818 $this->getMutableTestUser()->getUser()
1819 )->value['revision'];
1820
1821 $store = MediaWikiServices::getInstance()->getRevisionStore();
1822 $record = $store->newRevisionFromRow(
1823 $this->revisionToRow( $rev ),
1824 [],
1825 $page->getTitle()
1826 );
1827
1828 // Grab the user name
1829 $userNameBefore = $record->getUser()->getName();
1830
1831 // Change the user name in the database
1832 $newUserName = "Renamed $userNameBefore";
1833 $this->db->update( 'revision',
1834 [ 'rev_user_text' => $newUserName ],
1835 [ 'rev_id' => $record->getId() ] );
1836 $this->db->update( 'user',
1837 [ 'user_name' => $newUserName ],
1838 [ 'user_id' => $record->getUser()->getId() ] );
1839 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1840 $this->db->update( 'actor',
1841 [ 'actor_name' => $newUserName ],
1842 [ 'actor_user' => $record->getUser()->getId() ] );
1843 }
1844
1845 // Reload the record, passing $fromCache as true to force fresh info from the db,
1846 // and regrab the user name
1847 $recordAfter = $store->newRevisionFromRow(
1848 $this->revisionToRow( $rev ),
1849 [],
1850 $page->getTitle(),
1851 true
1852 );
1853 $userNameAfter = $recordAfter->getUser()->getName();
1854
1855 // The two user names should be different.
1856 // If they are the same, we are seeing a cached value, which is bad.
1857 $this->assertNotSame( $userNameBefore, $userNameAfter );
1858
1859 // This is implied by the above assertion, but explicitly check it, for completeness
1860 $this->assertSame( $newUserName, $userNameAfter );
1861 }
1862
1863 /**
1864 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1865 */
1866 public function testNewRevisionFromRow_revDelete() {
1867 $page = $this->getTestPage();
1868 $text = __METHOD__;
1869 /** @var Revision $rev */
1870 $rev = $page->doEditContent(
1871 new WikitextContent( $text ),
1872 __METHOD__
1873 )->value['revision'];
1874
1875 $store = MediaWikiServices::getInstance()->getRevisionStore();
1876 $record = $store->newRevisionFromRow(
1877 $this->revisionToRow( $rev ),
1878 [],
1879 $page->getTitle()
1880 );
1881
1882 // Grab the deleted bitmask
1883 $deletedBefore = $record->getVisibility();
1884
1885 // Change the deleted bitmask in the database
1886 $this->db->update( 'revision',
1887 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1888 [ 'rev_id' => $record->getId() ] );
1889
1890 // Reload the record, passing $fromCache as true to force fresh info from the db,
1891 // and regrab the deleted bitmask
1892 $recordAfter = $store->newRevisionFromRow(
1893 $this->revisionToRow( $rev ),
1894 [],
1895 $page->getTitle(),
1896 true
1897 );
1898 $deletedAfter = $recordAfter->getVisibility();
1899
1900 // The two deleted flags should be different, because we modified the database.
1901 $this->assertNotSame( $deletedBefore, $deletedAfter );
1902
1903 // This is implied by the above assertion, but explicitly check it, for completeness
1904 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1905 }
1906
1907 }