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