Introduce RevisionStore::newRevisionsFromBatch
[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
88 protected function addCoreDBData() {
89 // Blank out. This would fail with a modified schema, and we don't need it.
90 }
91
92 /**
93 * @return Title
94 */
95 protected function getTestPageTitle() {
96 if ( $this->testPageTitle ) {
97 return $this->testPageTitle;
98 }
99
100 $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
101 return $this->testPageTitle;
102 }
103
104 /**
105 * @param string|null $pageTitle whether to force-create a new page
106 * @return WikiPage
107 */
108 protected function getTestPage( $pageTitle = null ) {
109 if ( !is_null( $pageTitle ) && $this->testPage ) {
110 return $this->testPage;
111 }
112
113 $title = is_null( $pageTitle ) ? $this->getTestPageTitle() : Title::newFromText( $pageTitle );
114 $page = WikiPage::factory( $title );
115
116 if ( !$page->exists() ) {
117 // Make sure we don't write to the live db.
118 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER ) );
119
120 $user = static::getTestSysop()->getUser();
121
122 $page->doEditContent(
123 new WikitextContent( 'UTContent-' . __CLASS__ ),
124 'UTPageSummary-' . __CLASS__,
125 EDIT_NEW | EDIT_SUPPRESS_RC,
126 false,
127 $user
128 );
129 }
130
131 if ( is_null( $pageTitle ) ) {
132 $this->testPage = $page;
133 }
134 return $page;
135 }
136
137 /**
138 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
139 */
140 private function getLoadBalancerMock( array $server ) {
141 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
142
143 $lb = $this->getMockBuilder( LoadBalancer::class )
144 ->setMethods( [ 'reallyOpenConnection' ] )
145 ->setConstructorArgs( [
146 [ 'servers' => [ $server ], 'localDomain' => $domain ]
147 ] )
148 ->getMock();
149
150 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
151 function ( array $server, $dbNameOverride ) {
152 return $this->getDatabaseMock( $server );
153 }
154 );
155
156 return $lb;
157 }
158
159 /**
160 * @return Database|PHPUnit_Framework_MockObject_MockObject
161 */
162 private function getDatabaseMock( array $params ) {
163 $db = $this->getMockBuilder( DatabaseSqlite::class )
164 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
165 ->setConstructorArgs( [ $params ] )
166 ->getMock();
167
168 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
169 $db->method( 'isOpen' )->willReturn( true );
170
171 return $db;
172 }
173
174 public function provideDomainCheck() {
175 yield [ false, 'test', '' ];
176 yield [ 'test', 'test', '' ];
177
178 yield [ false, 'test', 'foo_' ];
179 yield [ 'test-foo_', 'test', 'foo_' ];
180
181 yield [ false, 'dash-test', '' ];
182 yield [ 'dash-test', 'dash-test', '' ];
183
184 yield [ false, 'underscore_test', 'foo_' ];
185 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
186 }
187
188 /**
189 * @dataProvider provideDomainCheck
190 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseDomain
191 */
192 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
193 $this->setMwGlobals(
194 [
195 'wgDBname' => $dbName,
196 'wgDBprefix' => $dbPrefix,
197 ]
198 );
199
200 $loadBalancer = $this->getLoadBalancerMock(
201 [
202 'host' => '*dummy*',
203 'dbDirectory' => '*dummy*',
204 'user' => 'test',
205 'password' => 'test',
206 'flags' => 0,
207 'variables' => [],
208 'schema' => '',
209 'cliMode' => true,
210 'agent' => '',
211 'load' => 100,
212 'profiler' => null,
213 'trxProfiler' => new TransactionProfiler(),
214 'connLogger' => new \Psr\Log\NullLogger(),
215 'queryLogger' => new \Psr\Log\NullLogger(),
216 'errorLogger' => function () {
217 },
218 'deprecationLogger' => function () {
219 },
220 'type' => 'test',
221 'dbname' => $dbName,
222 'tablePrefix' => $dbPrefix,
223 ]
224 );
225 $db = $loadBalancer->getConnection( DB_REPLICA );
226
227 /** @var SqlBlobStore $blobStore */
228 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
229 ->disableOriginalConstructor()
230 ->getMock();
231
232 $store = new RevisionStore(
233 $loadBalancer,
234 $blobStore,
235 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
236 MediaWikiServices::getInstance()->getCommentStore(),
237 MediaWikiServices::getInstance()->getContentModelStore(),
238 MediaWikiServices::getInstance()->getSlotRoleStore(),
239 MediaWikiServices::getInstance()->getSlotRoleRegistry(),
240 $this->getMcrMigrationStage(),
241 MediaWikiServices::getInstance()->getActorMigration(),
242 $wikiId
243 );
244
245 $count = $store->countRevisionsByPageId( $db, 0 );
246
247 // Dummy check to make PhpUnit happy. We are really only interested in
248 // countRevisionsByPageId not failing due to the DB domain check.
249 $this->assertSame( 0, $count );
250 }
251
252 protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
253 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
254 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
255 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
256 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
257 }
258
259 protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
260 $this->assertEquals(
261 $r1->getPageAsLinkTarget()->getNamespace(),
262 $r2->getPageAsLinkTarget()->getNamespace()
263 );
264
265 $this->assertEquals(
266 $r1->getPageAsLinkTarget()->getText(),
267 $r2->getPageAsLinkTarget()->getText()
268 );
269
270 if ( $r1->getParentId() ) {
271 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
272 }
273
274 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
275 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
276 $this->assertEquals( $r1->getComment(), $r2->getComment() );
277 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
278 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
279 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
280 $this->assertEquals( $r1->getSize(), $r2->getSize() );
281 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
282 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
283 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
284 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
285 foreach ( $r1->getSlotRoles() as $role ) {
286 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
287 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
288 }
289 foreach ( [
290 RevisionRecord::DELETED_TEXT,
291 RevisionRecord::DELETED_COMMENT,
292 RevisionRecord::DELETED_USER,
293 RevisionRecord::DELETED_RESTRICTED,
294 ] as $field ) {
295 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
296 }
297 }
298
299 protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
300 $this->assertSame( $s1->getRole(), $s2->getRole() );
301 $this->assertSame( $s1->getModel(), $s2->getModel() );
302 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
303 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
304 $this->assertSame( $s1->getSize(), $s2->getSize() );
305 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
306
307 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
308 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
309 }
310
311 protected function assertRevisionCompleteness( RevisionRecord $r ) {
312 $this->assertTrue( $r->hasSlot( SlotRecord::MAIN ) );
313 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( SlotRecord::MAIN ) );
314 $this->assertInstanceOf( Content::class, $r->getContent( SlotRecord::MAIN ) );
315
316 foreach ( $r->getSlotRoles() as $role ) {
317 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
318 }
319 }
320
321 protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
322 $this->assertTrue( $slot->hasAddress() );
323 $this->assertSame( $r->getId(), $slot->getRevision() );
324
325 $this->assertInstanceOf( Content::class, $slot->getContent() );
326 }
327
328 /**
329 * @param mixed[] $details
330 *
331 * @return RevisionRecord
332 */
333 private function getRevisionRecordFromDetailsArray( $details = [] ) {
334 // Convert some values that can't be provided by dataProviders
335 if ( isset( $details['user'] ) && $details['user'] === true ) {
336 $details['user'] = $this->getTestUser()->getUser();
337 }
338 if ( isset( $details['page'] ) && $details['page'] === true ) {
339 $details['page'] = $this->getTestPage()->getId();
340 }
341 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
342 $details['parent'] = $this->getTestPage()->getLatest();
343 }
344
345 // Create the RevisionRecord with any available data
346 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
347 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
348 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
349 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
350 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
351 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
352 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
353 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
354 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
355 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
356 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
357 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
358
359 if ( isset( $details['content'] ) ) {
360 foreach ( $details['content'] as $role => $content ) {
361 $rev->setContent( $role, $content );
362 }
363 }
364
365 return $rev;
366 }
367
368 public function provideInsertRevisionOn_successes() {
369 yield 'Bare minimum revision insertion' => [
370 [
371 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
372 'page' => true,
373 'comment' => $this->getRandomCommentStoreComment(),
374 'timestamp' => '20171117010101',
375 'user' => true,
376 ],
377 ];
378 yield 'Detailed revision insertion' => [
379 [
380 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
381 'parent' => true,
382 'page' => true,
383 'comment' => $this->getRandomCommentStoreComment(),
384 'timestamp' => '20171117010101',
385 'user' => true,
386 'minor' => true,
387 'visibility' => RevisionRecord::DELETED_RESTRICTED,
388 ],
389 ];
390 }
391
392 protected function getRandomCommentStoreComment() {
393 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
394 }
395
396 /**
397 * @dataProvider provideInsertRevisionOn_successes
398 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
399 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
400 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
401 */
402 public function testInsertRevisionOn_successes(
403 array $revDetails = []
404 ) {
405 $title = $this->getTestPageTitle();
406 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
407
408 $store = MediaWikiServices::getInstance()->getRevisionStore();
409 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
410
411 // is the new revision correct?
412 $this->assertRevisionCompleteness( $return );
413 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
414 $this->assertRevisionRecordsEqual( $rev, $return );
415
416 // can we load it from the store?
417 $loaded = $store->getRevisionById( $return->getId() );
418 $this->assertRevisionCompleteness( $loaded );
419 $this->assertRevisionRecordsEqual( $return, $loaded );
420
421 // can we find it directly in the database?
422 $this->assertRevisionExistsInDatabase( $return );
423 }
424
425 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
426 $row = $this->revisionToRow( new Revision( $rev ), [] );
427
428 // unset nulled fields
429 unset( $row->rev_content_model );
430 unset( $row->rev_content_format );
431
432 // unset fake fields
433 unset( $row->rev_comment_text );
434 unset( $row->rev_comment_data );
435 unset( $row->rev_comment_cid );
436 unset( $row->rev_comment_id );
437
438 $store = MediaWikiServices::getInstance()->getRevisionStore();
439 $queryInfo = $store->getQueryInfo( [ 'user' ] );
440
441 $row = get_object_vars( $row );
442
443 // Use aliased fields from $queryInfo, e.g. rev_user
444 $keys = array_keys( $row );
445 $keys = array_combine( $keys, $keys );
446 $fields = array_intersect_key( $queryInfo['fields'], $keys ) + $keys;
447
448 // assertSelect() fails unless the orders match.
449 ksort( $fields );
450 ksort( $row );
451
452 $this->assertSelect(
453 $queryInfo['tables'],
454 $fields,
455 [ 'rev_id' => $rev->getId() ],
456 [ array_values( $row ) ],
457 [],
458 $queryInfo['joins']
459 );
460 }
461
462 /**
463 * @param SlotRecord $a
464 * @param SlotRecord $b
465 */
466 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
467 // Assert that the same blob address has been used.
468 $this->assertSame( $a->getAddress(), $b->getAddress() );
469 }
470
471 /**
472 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
473 */
474 public function testInsertRevisionOn_blobAddressExists() {
475 $title = $this->getTestPageTitle();
476 $revDetails = [
477 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
478 'parent' => true,
479 'comment' => $this->getRandomCommentStoreComment(),
480 'timestamp' => '20171117010101',
481 'user' => true,
482 ];
483
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 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
598 $page = WikiPage::factory( $title );
599
600 if ( !$page->exists() ) {
601 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
602 }
603
604 $revDetails['page'] = $page->getId();
605 $revDetails['timestamp'] = wfTimestampNow();
606 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
607 $revDetails['user'] = $user;
608
609 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
610 $store = MediaWikiServices::getInstance()->getRevisionStore();
611
612 $dbw = wfGetDB( DB_MASTER );
613 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
614 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
615
616 $record = $store->newNullRevision(
617 wfGetDB( DB_MASTER ),
618 $title,
619 $comment,
620 $minor,
621 $user
622 );
623
624 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
625 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
626 $this->assertEquals( $comment, $record->getComment() );
627 $this->assertEquals( $minor, $record->isMinor() );
628 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
629 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
630
631 $this->assertArrayEquals(
632 $baseRev->getSlotRoles(),
633 $record->getSlotRoles()
634 );
635
636 foreach ( $baseRev->getSlotRoles() as $role ) {
637 $parentSlot = $baseRev->getSlot( $role );
638 $slot = $record->getSlot( $role );
639
640 $this->assertTrue( $slot->isInherited(), 'isInherited' );
641 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
642 $this->assertSameSlotContent( $parentSlot, $slot );
643 }
644 }
645
646 /**
647 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
648 */
649 public function testNewNullRevision_nonExistingTitle() {
650 $store = MediaWikiServices::getInstance()->getRevisionStore();
651 $record = $store->newNullRevision(
652 wfGetDB( DB_MASTER ),
653 Title::newFromText( __METHOD__ . '.iDontExist!' ),
654 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
655 false,
656 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
657 );
658 $this->assertNull( $record );
659 }
660
661 /**
662 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
663 */
664 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
665 $page = $this->getTestPage();
666 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
667 /** @var Revision $rev */
668 $rev = $status->value['revision'];
669
670 $store = MediaWikiServices::getInstance()->getRevisionStore();
671 $revisionRecord = $store->getRevisionById( $rev->getId() );
672 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
673
674 $this->assertGreaterThan( 0, $result );
675 $this->assertSame(
676 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
677 $result
678 );
679 }
680
681 /**
682 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
683 */
684 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
685 // This assumes that sysops are auto patrolled
686 $sysop = $this->getTestSysop()->getUser();
687 $page = $this->getTestPage();
688 $status = $page->doEditContent(
689 new WikitextContent( __METHOD__ ),
690 __METHOD__,
691 0,
692 false,
693 $sysop
694 );
695 /** @var Revision $rev */
696 $rev = $status->value['revision'];
697
698 $store = MediaWikiServices::getInstance()->getRevisionStore();
699 $revisionRecord = $store->getRevisionById( $rev->getId() );
700 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
701
702 $this->assertSame( 0, $result );
703 }
704
705 /**
706 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
707 */
708 public function testGetRecentChange() {
709 $page = $this->getTestPage();
710 $content = new WikitextContent( __METHOD__ );
711 $status = $page->doEditContent( $content, __METHOD__ );
712 /** @var Revision $rev */
713 $rev = $status->value['revision'];
714
715 $store = MediaWikiServices::getInstance()->getRevisionStore();
716 $revRecord = $store->getRevisionById( $rev->getId() );
717 $recentChange = $store->getRecentChange( $revRecord );
718
719 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
720 $this->assertEquals( $rev->getRecentChange(), $recentChange );
721 }
722
723 /**
724 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
725 */
726 public function testGetRevisionById() {
727 $page = $this->getTestPage();
728 $content = new WikitextContent( __METHOD__ );
729 $status = $page->doEditContent( $content, __METHOD__ );
730 /** @var Revision $rev */
731 $rev = $status->value['revision'];
732
733 $store = MediaWikiServices::getInstance()->getRevisionStore();
734 $revRecord = $store->getRevisionById( $rev->getId() );
735
736 $this->assertSame( $rev->getId(), $revRecord->getId() );
737 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
738 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
739 }
740
741 /**
742 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
743 */
744 public function testGetRevisionByTitle() {
745 $page = $this->getTestPage();
746 $content = new WikitextContent( __METHOD__ );
747 $status = $page->doEditContent( $content, __METHOD__ );
748 /** @var Revision $rev */
749 $rev = $status->value['revision'];
750
751 $store = MediaWikiServices::getInstance()->getRevisionStore();
752 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
753
754 $this->assertSame( $rev->getId(), $revRecord->getId() );
755 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
756 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
757 }
758
759 /**
760 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
761 */
762 public function testGetRevisionByPageId() {
763 $page = $this->getTestPage();
764 $content = new WikitextContent( __METHOD__ );
765 $status = $page->doEditContent( $content, __METHOD__ );
766 /** @var Revision $rev */
767 $rev = $status->value['revision'];
768
769 $store = MediaWikiServices::getInstance()->getRevisionStore();
770 $revRecord = $store->getRevisionByPageId( $page->getId() );
771
772 $this->assertSame( $rev->getId(), $revRecord->getId() );
773 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
774 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
775 }
776
777 /**
778 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
779 */
780 public function testGetRevisionByTimestamp() {
781 // Make sure there is 1 second between the last revision and the rev we create...
782 // Otherwise we might not get the correct revision and the test may fail...
783 // :(
784 $page = $this->getTestPage();
785 sleep( 1 );
786 $content = new WikitextContent( __METHOD__ );
787 $status = $page->doEditContent( $content, __METHOD__ );
788 /** @var Revision $rev */
789 $rev = $status->value['revision'];
790
791 $store = MediaWikiServices::getInstance()->getRevisionStore();
792 $revRecord = $store->getRevisionByTimestamp(
793 $page->getTitle(),
794 $rev->getTimestamp()
795 );
796
797 $this->assertSame( $rev->getId(), $revRecord->getId() );
798 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
799 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
800 }
801
802 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
803 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
804 $page = WikiPage::factory( $rev->getTitle() );
805
806 $fields = [
807 'rev_id' => (string)$rev->getId(),
808 'rev_page' => (string)$rev->getPage(),
809 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
810 'rev_user_text' => (string)$rev->getUserText(),
811 'rev_user' => (string)$rev->getUser() ?: null,
812 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
813 'rev_deleted' => (string)$rev->getVisibility(),
814 'rev_len' => (string)$rev->getSize(),
815 'rev_parent_id' => (string)$rev->getParentId(),
816 'rev_sha1' => (string)$rev->getSha1(),
817 ];
818
819 if ( in_array( 'page', $options ) ) {
820 $fields += [
821 'page_namespace' => (string)$page->getTitle()->getNamespace(),
822 'page_title' => $page->getTitle()->getDBkey(),
823 'page_id' => (string)$page->getId(),
824 'page_latest' => (string)$page->getLatest(),
825 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
826 'page_len' => (string)$page->getContent()->getSize(),
827 ];
828 }
829
830 if ( in_array( 'user', $options ) ) {
831 $fields += [
832 'user_name' => (string)$rev->getUserText(),
833 ];
834 }
835
836 if ( in_array( 'comment', $options ) ) {
837 $fields += [
838 'rev_comment_text' => $rev->getComment(),
839 'rev_comment_data' => null,
840 'rev_comment_cid' => null,
841 ];
842 }
843
844 if ( $rev->getId() ) {
845 $fields += [
846 'rev_id' => (string)$rev->getId(),
847 ];
848 }
849
850 return (object)$fields;
851 }
852
853 protected function assertRevisionRecordMatchesRevision(
854 Revision $rev,
855 RevisionRecord $record
856 ) {
857 $this->assertSame( $rev->getId(), $record->getId() );
858 $this->assertSame( $rev->getPage(), $record->getPageId() );
859 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
860 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
861 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
862 $this->assertSame( $rev->isMinor(), $record->isMinor() );
863 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
864 $this->assertSame( $rev->getSize(), $record->getSize() );
865 /**
866 * @note As of MW 1.31, the database schema allows the parent ID to be
867 * NULL to indicate that it is unknown.
868 */
869 $expectedParent = $rev->getParentId();
870 if ( $expectedParent === null ) {
871 $expectedParent = 0;
872 }
873 $this->assertSame( $expectedParent, $record->getParentId() );
874 $this->assertSame( $rev->getSha1(), $record->getSha1() );
875 $this->assertSame( $rev->getComment(), $record->getComment()->text );
876 $this->assertSame( $rev->getContentFormat(),
877 $record->getContent( SlotRecord::MAIN )->getDefaultFormat() );
878 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord::MAIN )->getModel() );
879 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
880
881 $revRec = $rev->getRevisionRecord();
882 $revMain = $revRec->getSlot( SlotRecord::MAIN );
883 $recMain = $record->getSlot( SlotRecord::MAIN );
884
885 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
886 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
887 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
888
889 if ( $revMain->hasOrigin() ) {
890 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
891 }
892
893 if ( $revMain->hasAddress() ) {
894 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
895 }
896
897 if ( $revMain->hasContentId() ) {
898 // XXX: the content ID value is ill-defined when SCHEMA_COMPAT_WRITE_BOTH and
899 // SCHEMA_COMPAT_READ_OLD is set, since revision insertion will report the
900 // content ID used with the new schema, while loading the revision from the
901 // old schema will report an emulated ID.
902 if ( $this->getMcrMigrationStage() & SCHEMA_COMPAT_READ_NEW ) {
903 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
904 }
905 }
906 }
907
908 /**
909 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
910 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
911 */
912 public function testNewRevisionFromRowAndSlot_getQueryInfo() {
913 $page = $this->getTestPage();
914 $text = __METHOD__ . 'o-รถ';
915 /** @var Revision $rev */
916 $rev = $page->doEditContent(
917 new WikitextContent( $text ),
918 __METHOD__ . 'a'
919 )->value['revision'];
920
921 $store = MediaWikiServices::getInstance()->getRevisionStore();
922 $info = $store->getQueryInfo();
923 $row = $this->db->selectRow(
924 $info['tables'],
925 $info['fields'],
926 [ 'rev_id' => $rev->getId() ],
927 __METHOD__,
928 [],
929 $info['joins']
930 );
931
932 $info = $store->getSlotsQueryInfo( [ 'content' ] );
933 $slotRows = $this->db->select(
934 $info['tables'],
935 $info['fields'],
936 $this->getSlotRevisionConditions( $rev->getId() ),
937 __METHOD__,
938 [],
939 $info['joins']
940 );
941
942 $record = $store->newRevisionFromRowAndSlots(
943 $row,
944 iterator_to_array( $slotRows ),
945 [],
946 $page->getTitle()
947 );
948 $this->assertRevisionRecordMatchesRevision( $rev, $record );
949 $this->assertSame( $text, $rev->getContent()->serialize() );
950 }
951
952 /**
953 * Conditions to use together with getSlotsQueryInfo() when selecting slot rows for a given
954 * revision.
955 *
956 * @return array
957 */
958 abstract protected function getSlotRevisionConditions( $revId );
959
960 /**
961 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
962 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
963 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
964 */
965 public function testNewRevisionFromRow_getQueryInfo() {
966 $page = $this->getTestPage();
967 $text = __METHOD__ . 'a-รค';
968 /** @var Revision $rev */
969 $rev = $page->doEditContent(
970 new WikitextContent( $text ),
971 __METHOD__ . 'a'
972 )->value['revision'];
973
974 $store = MediaWikiServices::getInstance()->getRevisionStore();
975 $info = $store->getQueryInfo();
976 $row = $this->db->selectRow(
977 $info['tables'],
978 $info['fields'],
979 [ 'rev_id' => $rev->getId() ],
980 __METHOD__,
981 [],
982 $info['joins']
983 );
984 $record = $store->newRevisionFromRow(
985 $row,
986 [],
987 $page->getTitle()
988 );
989 $this->assertRevisionRecordMatchesRevision( $rev, $record );
990 $this->assertSame( $text, $rev->getContent()->serialize() );
991 }
992
993 /**
994 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
995 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
996 */
997 public function testNewRevisionFromRow_anonEdit() {
998 $page = $this->getTestPage();
999 $text = __METHOD__ . 'a-รค';
1000 /** @var Revision $rev */
1001 $rev = $page->doEditContent(
1002 new WikitextContent( $text ),
1003 __METHOD__ . 'a'
1004 )->value['revision'];
1005
1006 $store = MediaWikiServices::getInstance()->getRevisionStore();
1007 $record = $store->newRevisionFromRow(
1008 $this->revisionToRow( $rev ),
1009 [],
1010 $page->getTitle()
1011 );
1012 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1013 $this->assertSame( $text, $rev->getContent()->serialize() );
1014 }
1015
1016 /**
1017 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1018 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1019 */
1020 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1021 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1022 $page = $this->getTestPage();
1023 $text = __METHOD__ . 'a-รค';
1024 /** @var Revision $rev */
1025 $rev = $page->doEditContent(
1026 new WikitextContent( $text ),
1027 __METHOD__ . 'a'
1028 )->value['revision'];
1029
1030 $store = MediaWikiServices::getInstance()->getRevisionStore();
1031 $record = $store->newRevisionFromRow(
1032 $this->revisionToRow( $rev ),
1033 [],
1034 $page->getTitle()
1035 );
1036 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1037 $this->assertSame( $text, $rev->getContent()->serialize() );
1038 }
1039
1040 /**
1041 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1042 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1043 */
1044 public function testNewRevisionFromRow_userEdit() {
1045 $page = $this->getTestPage();
1046 $text = __METHOD__ . 'b-รค';
1047 /** @var Revision $rev */
1048 $rev = $page->doEditContent(
1049 new WikitextContent( $text ),
1050 __METHOD__ . 'b',
1051 0,
1052 false,
1053 $this->getTestUser()->getUser()
1054 )->value['revision'];
1055
1056 $store = MediaWikiServices::getInstance()->getRevisionStore();
1057 $record = $store->newRevisionFromRow(
1058 $this->revisionToRow( $rev ),
1059 [],
1060 $page->getTitle()
1061 );
1062 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1063 $this->assertSame( $text, $rev->getContent()->serialize() );
1064 }
1065
1066 /**
1067 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1068 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1069 */
1070 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1071 $store = MediaWikiServices::getInstance()->getRevisionStore();
1072 $title = Title::newFromText( __METHOD__ );
1073 $text = __METHOD__ . '-bรค';
1074 $page = WikiPage::factory( $title );
1075 /** @var Revision $orig */
1076 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1077 ->value['revision'];
1078 $page->doDeleteArticle( __METHOD__ );
1079
1080 $db = wfGetDB( DB_MASTER );
1081 $arQuery = $store->getArchiveQueryInfo();
1082 $res = $db->select(
1083 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1084 __METHOD__, [], $arQuery['joins']
1085 );
1086 $this->assertTrue( is_object( $res ), 'query failed' );
1087
1088 $row = $res->fetchObject();
1089 $res->free();
1090 $record = $store->newRevisionFromArchiveRow( $row );
1091
1092 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1093 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1094 }
1095
1096 /**
1097 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1098 */
1099 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1100 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1101 $store = MediaWikiServices::getInstance()->getRevisionStore();
1102 $title = Title::newFromText( __METHOD__ );
1103 $text = __METHOD__ . '-bรค';
1104 $page = WikiPage::factory( $title );
1105 /** @var Revision $orig */
1106 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1107 ->value['revision'];
1108 $page->doDeleteArticle( __METHOD__ );
1109
1110 $db = wfGetDB( DB_MASTER );
1111 $arQuery = $store->getArchiveQueryInfo();
1112 $res = $db->select(
1113 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1114 __METHOD__, [], $arQuery['joins']
1115 );
1116 $this->assertTrue( is_object( $res ), 'query failed' );
1117
1118 $row = $res->fetchObject();
1119 $res->free();
1120 $record = $store->newRevisionFromArchiveRow( $row );
1121
1122 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1123 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1124 }
1125
1126 /**
1127 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1128 */
1129 public function testNewRevisionFromArchiveRow_no_user() {
1130 $store = MediaWikiServices::getInstance()->getRevisionStore();
1131
1132 $row = (object)[
1133 'ar_id' => '1',
1134 'ar_page_id' => '2',
1135 'ar_namespace' => '0',
1136 'ar_title' => 'Something',
1137 'ar_rev_id' => '2',
1138 'ar_text_id' => '47',
1139 'ar_timestamp' => '20180528192356',
1140 'ar_minor_edit' => '0',
1141 'ar_deleted' => '0',
1142 'ar_len' => '78',
1143 'ar_parent_id' => '0',
1144 'ar_sha1' => 'deadbeef',
1145 'ar_comment_text' => 'whatever',
1146 'ar_comment_data' => null,
1147 'ar_comment_cid' => null,
1148 'ar_user' => '0',
1149 'ar_user_text' => '', // this is the important bit
1150 'ar_actor' => null,
1151 'ar_content_format' => null,
1152 'ar_content_model' => null,
1153 ];
1154
1155 \Wikimedia\suppressWarnings();
1156 $record = $store->newRevisionFromArchiveRow( $row );
1157 \Wikimedia\suppressWarnings( true );
1158
1159 $this->assertInstanceOf( RevisionRecord::class, $record );
1160 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1161 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1162 }
1163
1164 /**
1165 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1166 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1167 */
1168 public function testNewRevisionFromRow_no_user() {
1169 $store = MediaWikiServices::getInstance()->getRevisionStore();
1170 $title = Title::newFromText( __METHOD__ );
1171
1172 $row = (object)[
1173 'rev_id' => '2',
1174 'rev_page' => '2',
1175 'page_namespace' => '0',
1176 'page_title' => $title->getText(),
1177 'rev_text_id' => '47',
1178 'rev_timestamp' => '20180528192356',
1179 'rev_minor_edit' => '0',
1180 'rev_deleted' => '0',
1181 'rev_len' => '78',
1182 'rev_parent_id' => '0',
1183 'rev_sha1' => 'deadbeef',
1184 'rev_comment_text' => 'whatever',
1185 'rev_comment_data' => null,
1186 'rev_comment_cid' => null,
1187 'rev_user' => '0',
1188 'rev_user_text' => '', // this is the important bit
1189 'rev_actor' => null,
1190 'rev_content_format' => null,
1191 'rev_content_model' => null,
1192 ];
1193
1194 \Wikimedia\suppressWarnings();
1195 $record = $store->newRevisionFromRow( $row, 0, $title );
1196 \Wikimedia\suppressWarnings( true );
1197
1198 $this->assertNotNull( $record );
1199 $this->assertNotNull( $record->getUser() );
1200 $this->assertNotEmpty( $record->getUser()->getName() );
1201 }
1202
1203 /**
1204 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1205 */
1206 public function testInsertRevisionOn_archive() {
1207 // This is a round trip test for deletion and undeletion of a
1208 // revision row via the archive table.
1209
1210 $store = MediaWikiServices::getInstance()->getRevisionStore();
1211 $title = Title::newFromText( __METHOD__ );
1212
1213 $page = WikiPage::factory( $title );
1214 /** @var Revision $origRev */
1215 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1216 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1217 ->value['revision'];
1218 $orig = $origRev->getRevisionRecord();
1219 $page->doDeleteArticle( __METHOD__ );
1220
1221 // re-create page, so we can later load revisions for it
1222 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1223
1224 $db = wfGetDB( DB_MASTER );
1225 $arQuery = $store->getArchiveQueryInfo();
1226 $row = $db->selectRow(
1227 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1228 __METHOD__, [], $arQuery['joins']
1229 );
1230
1231 $this->assertNotFalse( $row, 'query failed' );
1232
1233 $record = $store->newRevisionFromArchiveRow(
1234 $row,
1235 0,
1236 $title,
1237 [ 'page_id' => $title->getArticleID() ]
1238 );
1239
1240 $restored = $store->insertRevisionOn( $record, $db );
1241
1242 // is the new revision correct?
1243 $this->assertRevisionCompleteness( $restored );
1244 $this->assertRevisionRecordsEqual( $record, $restored );
1245
1246 // does the new revision use the original slot?
1247 $recMain = $record->getSlot( SlotRecord::MAIN );
1248 $restMain = $restored->getSlot( SlotRecord::MAIN );
1249 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1250 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1251 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1252 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1253
1254 // can we load it from the store?
1255 $loaded = $store->getRevisionById( $restored->getId() );
1256 $this->assertNotNull( $loaded );
1257 $this->assertRevisionCompleteness( $loaded );
1258 $this->assertRevisionRecordsEqual( $restored, $loaded );
1259
1260 // can we find it directly in the database?
1261 $this->assertRevisionExistsInDatabase( $restored );
1262 }
1263
1264 /**
1265 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1266 */
1267 public function testLoadRevisionFromId() {
1268 $title = Title::newFromText( __METHOD__ );
1269 $page = WikiPage::factory( $title );
1270 /** @var Revision $rev */
1271 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1272 ->value['revision'];
1273
1274 $store = MediaWikiServices::getInstance()->getRevisionStore();
1275 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1276 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1277 }
1278
1279 /**
1280 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1281 */
1282 public function testLoadRevisionFromPageId() {
1283 $title = Title::newFromText( __METHOD__ );
1284 $page = WikiPage::factory( $title );
1285 /** @var Revision $rev */
1286 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1287 ->value['revision'];
1288
1289 $store = MediaWikiServices::getInstance()->getRevisionStore();
1290 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1291 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1292 }
1293
1294 /**
1295 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1296 */
1297 public function testLoadRevisionFromTitle() {
1298 $title = Title::newFromText( __METHOD__ );
1299 $page = WikiPage::factory( $title );
1300 /** @var Revision $rev */
1301 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1302 ->value['revision'];
1303
1304 $store = MediaWikiServices::getInstance()->getRevisionStore();
1305 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1306 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1307 }
1308
1309 /**
1310 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1311 */
1312 public function testLoadRevisionFromTimestamp() {
1313 $title = Title::newFromText( __METHOD__ );
1314 $page = WikiPage::factory( $title );
1315 /** @var Revision $revOne */
1316 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1317 ->value['revision'];
1318 // Sleep to ensure different timestamps... )(evil)
1319 sleep( 1 );
1320 /** @var Revision $revTwo */
1321 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1322 ->value['revision'];
1323
1324 $store = MediaWikiServices::getInstance()->getRevisionStore();
1325 $this->assertNull(
1326 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1327 );
1328 $this->assertSame(
1329 $revOne->getId(),
1330 $store->loadRevisionFromTimestamp(
1331 wfGetDB( DB_MASTER ),
1332 $title,
1333 $revOne->getTimestamp()
1334 )->getId()
1335 );
1336 $this->assertSame(
1337 $revTwo->getId(),
1338 $store->loadRevisionFromTimestamp(
1339 wfGetDB( DB_MASTER ),
1340 $title,
1341 $revTwo->getTimestamp()
1342 )->getId()
1343 );
1344 }
1345
1346 /**
1347 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1348 */
1349 public function testGetParentLengths() {
1350 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1351 /** @var Revision $revOne */
1352 $revOne = $page->doEditContent(
1353 new WikitextContent( __METHOD__ ), __METHOD__
1354 )->value['revision'];
1355 /** @var Revision $revTwo */
1356 $revTwo = $page->doEditContent(
1357 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1358 )->value['revision'];
1359
1360 $store = MediaWikiServices::getInstance()->getRevisionStore();
1361 $this->assertSame(
1362 [
1363 $revOne->getId() => strlen( __METHOD__ ),
1364 ],
1365 $store->listRevisionSizes(
1366 wfGetDB( DB_MASTER ),
1367 [ $revOne->getId() ]
1368 )
1369 );
1370 $this->assertSame(
1371 [
1372 $revOne->getId() => strlen( __METHOD__ ),
1373 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1374 ],
1375 $store->listRevisionSizes(
1376 wfGetDB( DB_MASTER ),
1377 [ $revOne->getId(), $revTwo->getId() ]
1378 )
1379 );
1380 }
1381
1382 /**
1383 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1384 */
1385 public function testGetPreviousRevision() {
1386 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1387 /** @var Revision $revOne */
1388 $revOne = $page->doEditContent(
1389 new WikitextContent( __METHOD__ ), __METHOD__
1390 )->value['revision'];
1391 /** @var Revision $revTwo */
1392 $revTwo = $page->doEditContent(
1393 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1394 )->value['revision'];
1395
1396 $store = MediaWikiServices::getInstance()->getRevisionStore();
1397 $this->assertNull(
1398 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1399 );
1400 $this->assertSame(
1401 $revOne->getId(),
1402 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1403 );
1404 }
1405
1406 /**
1407 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1408 */
1409 public function testGetNextRevision() {
1410 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1411 /** @var Revision $revOne */
1412 $revOne = $page->doEditContent(
1413 new WikitextContent( __METHOD__ ), __METHOD__
1414 )->value['revision'];
1415 /** @var Revision $revTwo */
1416 $revTwo = $page->doEditContent(
1417 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1418 )->value['revision'];
1419
1420 $store = MediaWikiServices::getInstance()->getRevisionStore();
1421 $this->assertSame(
1422 $revTwo->getId(),
1423 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1424 );
1425 $this->assertNull(
1426 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1427 );
1428 }
1429
1430 public function provideNonHistoryRevision() {
1431 $title = Title::newFromText( __METHOD__ );
1432 $rev = new MutableRevisionRecord( $title );
1433 yield [ $rev ];
1434
1435 $user = new UserIdentityValue( 7, 'Frank', 0 );
1436 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1437 $row = (object)[
1438 'ar_id' => 3,
1439 'ar_rev_id' => 34567,
1440 'ar_page_id' => 5,
1441 'ar_deleted' => 0,
1442 'ar_minor_edit' => 0,
1443 'ar_timestamp' => '20180101020202',
1444 ];
1445 $slots = new RevisionSlots( [] );
1446 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1447 yield [ $rev ];
1448 }
1449
1450 /**
1451 * @dataProvider provideNonHistoryRevision
1452 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1453 */
1454 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1455 $store = MediaWikiServices::getInstance()->getRevisionStore();
1456 $this->assertNull( $store->getPreviousRevision( $rev ) );
1457 }
1458
1459 /**
1460 * @dataProvider provideNonHistoryRevision
1461 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1462 */
1463 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1464 $store = MediaWikiServices::getInstance()->getRevisionStore();
1465 $this->assertNull( $store->getNextRevision( $rev ) );
1466 }
1467
1468 /**
1469 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1470 */
1471 public function testGetTimestampFromId_found() {
1472 $page = $this->getTestPage();
1473 /** @var Revision $rev */
1474 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1475 ->value['revision'];
1476
1477 $store = MediaWikiServices::getInstance()->getRevisionStore();
1478 $result = $store->getTimestampFromId( $rev->getId() );
1479
1480 $this->assertSame( $rev->getTimestamp(), $result );
1481 }
1482
1483 /**
1484 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1485 */
1486 public function testGetTimestampFromId_notFound() {
1487 $page = $this->getTestPage();
1488 /** @var Revision $rev */
1489 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1490 ->value['revision'];
1491
1492 $store = MediaWikiServices::getInstance()->getRevisionStore();
1493 $result = $store->getTimestampFromId( $rev->getId() + 1 );
1494
1495 $this->assertFalse( $result );
1496 }
1497
1498 /**
1499 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1500 */
1501 public function testCountRevisionsByPageId() {
1502 $store = MediaWikiServices::getInstance()->getRevisionStore();
1503 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1504
1505 $this->assertSame(
1506 0,
1507 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1508 );
1509 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1510 $this->assertSame(
1511 1,
1512 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1513 );
1514 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1515 $this->assertSame(
1516 2,
1517 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1518 );
1519 }
1520
1521 /**
1522 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1523 */
1524 public function testCountRevisionsByTitle() {
1525 $store = MediaWikiServices::getInstance()->getRevisionStore();
1526 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1527
1528 $this->assertSame(
1529 0,
1530 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1531 );
1532 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1533 $this->assertSame(
1534 1,
1535 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1536 );
1537 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1538 $this->assertSame(
1539 2,
1540 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1541 );
1542 }
1543
1544 /**
1545 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1546 */
1547 public function testUserWasLastToEdit_false() {
1548 $sysop = $this->getTestSysop()->getUser();
1549 $page = $this->getTestPage();
1550 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1551
1552 $store = MediaWikiServices::getInstance()->getRevisionStore();
1553 $result = $store->userWasLastToEdit(
1554 wfGetDB( DB_MASTER ),
1555 $page->getId(),
1556 $sysop->getId(),
1557 '20160101010101'
1558 );
1559 $this->assertFalse( $result );
1560 }
1561
1562 /**
1563 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1564 */
1565 public function testUserWasLastToEdit_true() {
1566 $startTime = wfTimestampNow();
1567 $sysop = $this->getTestSysop()->getUser();
1568 $page = $this->getTestPage();
1569 $page->doEditContent(
1570 new WikitextContent( __METHOD__ ),
1571 __METHOD__,
1572 0,
1573 false,
1574 $sysop
1575 );
1576
1577 $store = MediaWikiServices::getInstance()->getRevisionStore();
1578 $result = $store->userWasLastToEdit(
1579 wfGetDB( DB_MASTER ),
1580 $page->getId(),
1581 $sysop->getId(),
1582 $startTime
1583 );
1584 $this->assertTrue( $result );
1585 }
1586
1587 /**
1588 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1589 */
1590 public function testGetKnownCurrentRevision() {
1591 $page = $this->getTestPage();
1592 /** @var Revision $rev */
1593 $rev = $page->doEditContent(
1594 new WikitextContent( __METHOD__ . 'b' ),
1595 __METHOD__ . 'b',
1596 0,
1597 false,
1598 $this->getTestUser()->getUser()
1599 )->value['revision'];
1600
1601 $store = MediaWikiServices::getInstance()->getRevisionStore();
1602 $record = $store->getKnownCurrentRevision(
1603 $page->getTitle(),
1604 $rev->getId()
1605 );
1606
1607 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1608 }
1609
1610 public function provideNewMutableRevisionFromArray() {
1611 yield 'Basic array, content object' => [
1612 [
1613 'id' => 2,
1614 'page' => 1,
1615 'timestamp' => '20171017114835',
1616 'user_text' => '111.0.1.2',
1617 'user' => 0,
1618 'minor_edit' => false,
1619 'deleted' => 0,
1620 'len' => 46,
1621 'parent_id' => 1,
1622 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1623 'comment' => 'Goat Comment!',
1624 'content' => new WikitextContent( 'Some Content' ),
1625 ]
1626 ];
1627 yield 'Basic array, serialized text' => [
1628 [
1629 'id' => 2,
1630 'page' => 1,
1631 'timestamp' => '20171017114835',
1632 'user_text' => '111.0.1.2',
1633 'user' => 0,
1634 'minor_edit' => false,
1635 'deleted' => 0,
1636 'len' => 46,
1637 'parent_id' => 1,
1638 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1639 'comment' => 'Goat Comment!',
1640 'text' => ( new WikitextContent( 'Sรถme Content' ) )->serialize(),
1641 ]
1642 ];
1643 yield 'Basic array, serialized text, utf-8 flags' => [
1644 [
1645 'id' => 2,
1646 'page' => 1,
1647 'timestamp' => '20171017114835',
1648 'user_text' => '111.0.1.2',
1649 'user' => 0,
1650 'minor_edit' => false,
1651 'deleted' => 0,
1652 'len' => 46,
1653 'parent_id' => 1,
1654 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1655 'comment' => 'Goat Comment!',
1656 'text' => ( new WikitextContent( 'Sรถme Content' ) )->serialize(),
1657 'flags' => 'utf-8',
1658 ]
1659 ];
1660 yield 'Basic array, with title' => [
1661 [
1662 'title' => Title::newFromText( 'SomeText' ),
1663 'timestamp' => '20171017114835',
1664 'user_text' => '111.0.1.2',
1665 'user' => 0,
1666 'minor_edit' => false,
1667 'deleted' => 0,
1668 'len' => 46,
1669 'parent_id' => 1,
1670 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1671 'comment' => 'Goat Comment!',
1672 'content' => new WikitextContent( 'Some Content' ),
1673 ]
1674 ];
1675 yield 'Basic array, no user field' => [
1676 [
1677 'id' => 2,
1678 'page' => 1,
1679 'timestamp' => '20171017114835',
1680 'user_text' => '111.0.1.3',
1681 'minor_edit' => false,
1682 'deleted' => 0,
1683 'len' => 46,
1684 'parent_id' => 1,
1685 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1686 'comment' => 'Goat Comment!',
1687 'content' => new WikitextContent( 'Some Content' ),
1688 ]
1689 ];
1690 }
1691
1692 /**
1693 * @dataProvider provideNewMutableRevisionFromArray
1694 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1695 */
1696 public function testNewMutableRevisionFromArray( array $array ) {
1697 $store = MediaWikiServices::getInstance()->getRevisionStore();
1698
1699 // HACK: if $array['page'] is given, make sure that the page exists
1700 if ( isset( $array['page'] ) ) {
1701 $t = Title::newFromID( $array['page'] );
1702 if ( !$t || !$t->exists() ) {
1703 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1704 $info = $this->insertPage( $t );
1705 $array['page'] = $info['id'];
1706 }
1707 }
1708
1709 $result = $store->newMutableRevisionFromArray( $array );
1710
1711 if ( isset( $array['id'] ) ) {
1712 $this->assertSame( $array['id'], $result->getId() );
1713 }
1714 if ( isset( $array['page'] ) ) {
1715 $this->assertSame( $array['page'], $result->getPageId() );
1716 }
1717 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1718 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1719 if ( isset( $array['user'] ) ) {
1720 $this->assertSame( $array['user'], $result->getUser()->getId() );
1721 }
1722 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1723 $this->assertSame( $array['deleted'], $result->getVisibility() );
1724 $this->assertSame( $array['len'], $result->getSize() );
1725 $this->assertSame( $array['parent_id'], $result->getParentId() );
1726 $this->assertSame( $array['sha1'], $result->getSha1() );
1727 $this->assertSame( $array['comment'], $result->getComment()->text );
1728 if ( isset( $array['content'] ) ) {
1729 foreach ( $array['content'] as $role => $content ) {
1730 $this->assertTrue(
1731 $result->getContent( $role )->equals( $content )
1732 );
1733 }
1734 } elseif ( isset( $array['text'] ) ) {
1735 $this->assertSame( $array['text'],
1736 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1737 } elseif ( isset( $array['content_format'] ) ) {
1738 $this->assertSame(
1739 $array['content_format'],
1740 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1741 );
1742 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1743 }
1744 }
1745
1746 /**
1747 * @dataProvider provideNewMutableRevisionFromArray
1748 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1749 */
1750 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1751 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1752 $services = MediaWikiServices::getInstance();
1753 $lb = $services->getDBLoadBalancer();
1754 $access = $services->getExternalStoreAccess();
1755 $blobStore = new SqlBlobStore( $lb, $access, $cache );
1756 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1757
1758 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1759 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1760 ->disableOriginalConstructor()
1761 ->getMock();
1762 $factory->expects( $this->any() )
1763 ->method( 'newBlobStore' )
1764 ->willReturn( $blobStore );
1765 $factory->expects( $this->any() )
1766 ->method( 'newSqlBlobStore' )
1767 ->willReturn( $blobStore );
1768
1769 $this->setService( 'BlobStoreFactory', $factory );
1770
1771 $this->testNewMutableRevisionFromArray( $array );
1772 }
1773
1774 /**
1775 * Creates a new revision for testing caching behavior
1776 *
1777 * @param WikiPage $page the page for the new revision
1778 * @param RevisionStore $store store object to use for creating the revision
1779 * @return bool|RevisionStoreRecord the revision created, or false if missing
1780 */
1781 private function createRevisionStoreCacheRecord( $page, $store ) {
1782 $user = MediaWikiTestCase::getMutableTestUser()->getUser();
1783 $updater = $page->newPageUpdater( $user );
1784 $updater->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) );
1785 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1786 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1787 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1788 }
1789
1790 /**
1791 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1792 */
1793 public function testGetKnownCurrentRevision_userNameChange() {
1794 global $wgActorTableSchemaMigrationStage;
1795
1796 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1797 $this->setService( 'MainWANObjectCache', $cache );
1798
1799 $store = MediaWikiServices::getInstance()->getRevisionStore();
1800 $page = $this->getNonexistingTestPage();
1801 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1802
1803 // Grab the user name
1804 $userNameBefore = $rev->getUser()->getName();
1805
1806 // Change the user name in the database, "behind the back" of the cache
1807 $newUserName = "Renamed $userNameBefore";
1808 $this->db->update( 'revision',
1809 [ 'rev_user_text' => $newUserName ],
1810 [ 'rev_id' => $rev->getId() ] );
1811 $this->db->update( 'user',
1812 [ 'user_name' => $newUserName ],
1813 [ 'user_id' => $rev->getUser()->getId() ] );
1814 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1815 $this->db->update( 'actor',
1816 [ 'actor_name' => $newUserName ],
1817 [ 'actor_user' => $rev->getUser()->getId() ] );
1818 }
1819
1820 // Reload the revision and regrab the user name.
1821 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1822 $userNameAfter = $revAfter->getUser()->getName();
1823
1824 // The two user names should be different.
1825 // If they are the same, we are seeing a cached value, which is bad.
1826 $this->assertNotSame( $userNameBefore, $userNameAfter );
1827
1828 // This is implied by the above assertion, but explicitly check it, for completeness
1829 $this->assertSame( $newUserName, $userNameAfter );
1830 }
1831
1832 /**
1833 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1834 */
1835 public function testGetKnownCurrentRevision_revDelete() {
1836 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1837 $this->setService( 'MainWANObjectCache', $cache );
1838
1839 $store = MediaWikiServices::getInstance()->getRevisionStore();
1840 $page = $this->getNonexistingTestPage();
1841 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1842
1843 // Grab the deleted bitmask
1844 $deletedBefore = $rev->getVisibility();
1845
1846 // Change the deleted bitmask in the database, "behind the back" of the cache
1847 $this->db->update( 'revision',
1848 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1849 [ 'rev_id' => $rev->getId() ] );
1850
1851 // Reload the revision and regrab the visibility flag.
1852 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1853 $deletedAfter = $revAfter->getVisibility();
1854
1855 // The two deleted flags should be different.
1856 // If they are the same, we are seeing a cached value, which is bad.
1857 $this->assertNotSame( $deletedBefore, $deletedAfter );
1858
1859 // This is implied by the above assertion, but explicitly check it, for completeness
1860 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1861 }
1862
1863 /**
1864 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1865 */
1866 public function testNewRevisionFromRow_userNameChange() {
1867 global $wgActorTableSchemaMigrationStage;
1868
1869 $page = $this->getTestPage();
1870 $text = __METHOD__;
1871 /** @var Revision $rev */
1872 $rev = $page->doEditContent(
1873 new WikitextContent( $text ),
1874 __METHOD__,
1875 0,
1876 false,
1877 $this->getMutableTestUser()->getUser()
1878 )->value['revision'];
1879
1880 $store = MediaWikiServices::getInstance()->getRevisionStore();
1881 $record = $store->newRevisionFromRow(
1882 $this->revisionToRow( $rev ),
1883 [],
1884 $page->getTitle()
1885 );
1886
1887 // Grab the user name
1888 $userNameBefore = $record->getUser()->getName();
1889
1890 // Change the user name in the database
1891 $newUserName = "Renamed $userNameBefore";
1892 $this->db->update( 'revision',
1893 [ 'rev_user_text' => $newUserName ],
1894 [ 'rev_id' => $record->getId() ] );
1895 $this->db->update( 'user',
1896 [ 'user_name' => $newUserName ],
1897 [ 'user_id' => $record->getUser()->getId() ] );
1898 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
1899 $this->db->update( 'actor',
1900 [ 'actor_name' => $newUserName ],
1901 [ 'actor_user' => $record->getUser()->getId() ] );
1902 }
1903
1904 // Reload the record, passing $fromCache as true to force fresh info from the db,
1905 // and regrab the user name
1906 $recordAfter = $store->newRevisionFromRow(
1907 $this->revisionToRow( $rev ),
1908 [],
1909 $page->getTitle(),
1910 true
1911 );
1912 $userNameAfter = $recordAfter->getUser()->getName();
1913
1914 // The two user names should be different.
1915 // If they are the same, we are seeing a cached value, which is bad.
1916 $this->assertNotSame( $userNameBefore, $userNameAfter );
1917
1918 // This is implied by the above assertion, but explicitly check it, for completeness
1919 $this->assertSame( $newUserName, $userNameAfter );
1920 }
1921
1922 /**
1923 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1924 */
1925 public function testNewRevisionFromRow_revDelete() {
1926 $page = $this->getTestPage();
1927 $text = __METHOD__;
1928 /** @var Revision $rev */
1929 $rev = $page->doEditContent(
1930 new WikitextContent( $text ),
1931 __METHOD__
1932 )->value['revision'];
1933
1934 $store = MediaWikiServices::getInstance()->getRevisionStore();
1935 $record = $store->newRevisionFromRow(
1936 $this->revisionToRow( $rev ),
1937 [],
1938 $page->getTitle()
1939 );
1940
1941 // Grab the deleted bitmask
1942 $deletedBefore = $record->getVisibility();
1943
1944 // Change the deleted bitmask in the database
1945 $this->db->update( 'revision',
1946 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1947 [ 'rev_id' => $record->getId() ] );
1948
1949 // Reload the record, passing $fromCache as true to force fresh info from the db,
1950 // and regrab the deleted bitmask
1951 $recordAfter = $store->newRevisionFromRow(
1952 $this->revisionToRow( $rev ),
1953 [],
1954 $page->getTitle(),
1955 true
1956 );
1957 $deletedAfter = $recordAfter->getVisibility();
1958
1959 // The two deleted flags should be different, because we modified the database.
1960 $this->assertNotSame( $deletedBefore, $deletedAfter );
1961
1962 // This is implied by the above assertion, but explicitly check it, for completeness
1963 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1964 }
1965
1966 public function provideNewRevisionsFromBatchOptions() {
1967 yield 'No preload slots or content, single page' => [
1968 null,
1969 []
1970 ];
1971 yield 'Preload slots and content, single page' => [
1972 null,
1973 [
1974 'slots' => [ SlotRecord::MAIN ],
1975 'content' => true
1976 ]
1977 ];
1978 yield 'No preload slots or content, multiple pages' => [
1979 'Other_Page',
1980 []
1981 ];
1982 yield 'Preload slots and content, multiple pages' => [
1983 'Other_Page',
1984 [
1985 'slots' => [ SlotRecord::MAIN ],
1986 'content' => true
1987 ]
1988 ];
1989 }
1990
1991 /**
1992 * @dataProvider provideNewRevisionsFromBatchOptions
1993 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
1994 * @param string|null $otherPageTitle
1995 * @param array|null $options
1996 * @throws \MWException
1997 */
1998 public function testNewRevisionsFromBatch_preloadContent(
1999 $otherPageTitle = null,
2000 array $options = []
2001 ) {
2002 $page1 = $this->getTestPage();
2003 $text = __METHOD__ . 'b-รค';
2004 /** @var Revision $rev1 */
2005 $rev1 = $page1->doEditContent(
2006 new WikitextContent( $text . '1' ),
2007 __METHOD__ . 'b',
2008 0,
2009 false,
2010 $this->getTestUser()->getUser()
2011 )->value['revision'];
2012 $page2 = $this->getTestPage( $otherPageTitle );
2013 /** @var Revision $rev2 */
2014 $rev2 = $page2->doEditContent(
2015 new WikitextContent( $text . '2' ),
2016 __METHOD__ . 'b',
2017 0,
2018 false,
2019 $this->getTestUser()->getUser()
2020 )->value['revision'];
2021 $store = MediaWikiServices::getInstance()->getRevisionStore();
2022 $result = $store->newRevisionsFromBatch(
2023 [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev2 ) ],
2024 $options
2025 );
2026 $this->assertTrue( $result->isGood() );
2027 $this->assertEmpty( $result->getErrors() );
2028 $records = $result->getValue();
2029 $this->assertRevisionRecordMatchesRevision( $rev1, $records[$rev1->getId()] );
2030 $this->assertRevisionRecordMatchesRevision( $rev2, $records[$rev2->getId()] );
2031
2032 $this->assertSame( $text . '1',
2033 $records[$rev1->getId()]->getContent( SlotRecord::MAIN )->serialize() );
2034 $this->assertSame( $text . '2',
2035 $records[$rev2->getId()]->getContent( SlotRecord::MAIN )->serialize() );
2036 $this->assertEquals( $page1->getTitle()->getDBkey(),
2037 $records[$rev1->getId()]->getPageAsLinkTarget()->getDBkey() );
2038 $this->assertEquals( $page2->getTitle()->getDBkey(),
2039 $records[$rev2->getId()]->getPageAsLinkTarget()->getDBkey() );
2040 }
2041
2042 /**
2043 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
2044 */
2045 public function testNewRevisionsFromBatch_emptyBatch() {
2046 $result = MediaWikiServices::getInstance()->getRevisionStore()
2047 ->newRevisionsFromBatch(
2048 [],
2049 [
2050 'slots' => [ SlotRecord::MAIN ],
2051 'content' => true
2052 ]
2053 );
2054 $this->assertTrue( $result->isGood() );
2055 $this->assertEmpty( $result->getValue() );
2056 $this->assertEmpty( $result->getErrors() );
2057 }
2058 }