RevisionStore: Avoid exception on prev/next of deleted revision
[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\RevisionSlots;
18 use MediaWiki\Revision\RevisionStore;
19 use MediaWiki\Revision\SlotRecord;
20 use MediaWiki\Storage\BlobStoreFactory;
21 use MediaWiki\Storage\SqlBlobStore;
22 use MediaWiki\User\UserIdentityValue;
23 use MediaWikiTestCase;
24 use PHPUnit_Framework_MockObject_MockObject;
25 use Revision;
26 use TestUserRegistry;
27 use Title;
28 use WANObjectCache;
29 use Wikimedia\Rdbms\Database;
30 use Wikimedia\Rdbms\DatabaseDomain;
31 use Wikimedia\Rdbms\DatabaseSqlite;
32 use Wikimedia\Rdbms\FakeResultWrapper;
33 use Wikimedia\Rdbms\LoadBalancer;
34 use Wikimedia\Rdbms\TransactionProfiler;
35 use WikiPage;
36 use WikitextContent;
37
38 /**
39 * @group Database
40 * @group RevisionStore
41 */
42 abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
43
44 /**
45 * @var Title
46 */
47 private $testPageTitle;
48
49 /**
50 * @var WikiPage
51 */
52 private $testPage;
53
54 /**
55 * @return int
56 */
57 abstract protected function getMcrMigrationStage();
58
59 /**
60 * @return bool
61 */
62 protected function getContentHandlerUseDB() {
63 return true;
64 }
65
66 /**
67 * @return string[]
68 */
69 abstract protected function getMcrTablesToReset();
70
71 public function setUp() {
72 parent::setUp();
73 $this->tablesUsed[] = 'archive';
74 $this->tablesUsed[] = 'page';
75 $this->tablesUsed[] = 'revision';
76 $this->tablesUsed[] = 'comment';
77
78 $this->tablesUsed += $this->getMcrTablesToReset();
79
80 $this->setMwGlobals( [
81 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
82 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
83 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
84 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD,
85 ] );
86
87 $this->overrideMwServices();
88 }
89
90 protected function addCoreDBData() {
91 // Blank out. This would fail with a modified schema, and we don't need it.
92 }
93
94 /**
95 * @return Title
96 */
97 protected function getTestPageTitle() {
98 if ( $this->testPageTitle ) {
99 return $this->testPageTitle;
100 }
101
102 $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
103 return $this->testPageTitle;
104 }
105 /**
106 * @return WikiPage
107 */
108 protected function getTestPage() {
109 if ( $this->testPage ) {
110 return $this->testPage;
111 }
112
113 $title = $this->getTestPageTitle();
114 $this->testPage = WikiPage::factory( $title );
115
116 if ( !$this->testPage->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 $this->testPage->doEditContent(
123 new WikitextContent( 'UTContent-' . __CLASS__ ),
124 'UTPageSummary-' . __CLASS__,
125 EDIT_NEW | EDIT_SUPPRESS_RC,
126 false,
127 $user
128 );
129 }
130
131 return $this->testPage;
132 }
133
134 /**
135 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
136 */
137 private function getLoadBalancerMock( array $server ) {
138 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
139
140 $lb = $this->getMockBuilder( LoadBalancer::class )
141 ->setMethods( [ 'reallyOpenConnection' ] )
142 ->setConstructorArgs( [
143 [ 'servers' => [ $server ], 'localDomain' => $domain ]
144 ] )
145 ->getMock();
146
147 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
148 function ( array $server, $dbNameOverride ) {
149 return $this->getDatabaseMock( $server );
150 }
151 );
152
153 return $lb;
154 }
155
156 /**
157 * @return Database|PHPUnit_Framework_MockObject_MockObject
158 */
159 private function getDatabaseMock( array $params ) {
160 $db = $this->getMockBuilder( DatabaseSqlite::class )
161 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
162 ->setConstructorArgs( [ $params ] )
163 ->getMock();
164
165 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
166 $db->method( 'isOpen' )->willReturn( true );
167
168 return $db;
169 }
170
171 public function provideDomainCheck() {
172 yield [ false, 'test', '' ];
173 yield [ 'test', 'test', '' ];
174
175 yield [ false, 'test', 'foo_' ];
176 yield [ 'test-foo_', 'test', 'foo_' ];
177
178 yield [ false, 'dash-test', '' ];
179 yield [ 'dash-test', 'dash-test', '' ];
180
181 yield [ false, 'underscore_test', 'foo_' ];
182 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
183 }
184
185 /**
186 * @dataProvider provideDomainCheck
187 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
188 */
189 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
190 $this->setMwGlobals(
191 [
192 'wgDBname' => $dbName,
193 'wgDBprefix' => $dbPrefix,
194 ]
195 );
196
197 $loadBalancer = $this->getLoadBalancerMock(
198 [
199 'host' => '*dummy*',
200 'dbDirectory' => '*dummy*',
201 'user' => 'test',
202 'password' => 'test',
203 'flags' => 0,
204 'variables' => [],
205 'schema' => '',
206 'cliMode' => true,
207 'agent' => '',
208 'load' => 100,
209 'profiler' => null,
210 'trxProfiler' => new TransactionProfiler(),
211 'connLogger' => new \Psr\Log\NullLogger(),
212 'queryLogger' => new \Psr\Log\NullLogger(),
213 'errorLogger' => function () {
214 },
215 'deprecationLogger' => function () {
216 },
217 'type' => 'test',
218 'dbname' => $dbName,
219 'tablePrefix' => $dbPrefix,
220 ]
221 );
222 $db = $loadBalancer->getConnection( DB_REPLICA );
223
224 /** @var SqlBlobStore $blobStore */
225 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
226 ->disableOriginalConstructor()
227 ->getMock();
228
229 $store = new RevisionStore(
230 $loadBalancer,
231 $blobStore,
232 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
233 MediaWikiServices::getInstance()->getCommentStore(),
234 MediaWikiServices::getInstance()->getContentModelStore(),
235 MediaWikiServices::getInstance()->getSlotRoleStore(),
236 $this->getMcrMigrationStage(),
237 MediaWikiServices::getInstance()->getActorMigration(),
238 $wikiId
239 );
240
241 $count = $store->countRevisionsByPageId( $db, 0 );
242
243 // Dummy check to make PhpUnit happy. We are really only interested in
244 // countRevisionsByPageId not failing due to the DB domain check.
245 $this->assertSame( 0, $count );
246 }
247
248 protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
249 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
250 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
251 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
252 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
253 }
254
255 protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
256 $this->assertEquals(
257 $r1->getPageAsLinkTarget()->getNamespace(),
258 $r2->getPageAsLinkTarget()->getNamespace()
259 );
260
261 $this->assertEquals(
262 $r1->getPageAsLinkTarget()->getText(),
263 $r2->getPageAsLinkTarget()->getText()
264 );
265
266 if ( $r1->getParentId() ) {
267 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
268 }
269
270 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
271 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
272 $this->assertEquals( $r1->getComment(), $r2->getComment() );
273 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
274 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
275 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
276 $this->assertEquals( $r1->getSize(), $r2->getSize() );
277 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
278 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
279 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
280 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
281 foreach ( $r1->getSlotRoles() as $role ) {
282 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
283 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
284 }
285 foreach ( [
286 RevisionRecord::DELETED_TEXT,
287 RevisionRecord::DELETED_COMMENT,
288 RevisionRecord::DELETED_USER,
289 RevisionRecord::DELETED_RESTRICTED,
290 ] as $field ) {
291 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
292 }
293 }
294
295 protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
296 $this->assertSame( $s1->getRole(), $s2->getRole() );
297 $this->assertSame( $s1->getModel(), $s2->getModel() );
298 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
299 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
300 $this->assertSame( $s1->getSize(), $s2->getSize() );
301 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
302
303 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
304 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
305 }
306
307 protected function assertRevisionCompleteness( RevisionRecord $r ) {
308 $this->assertTrue( $r->hasSlot( SlotRecord::MAIN ) );
309 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( SlotRecord::MAIN ) );
310 $this->assertInstanceOf( Content::class, $r->getContent( SlotRecord::MAIN ) );
311
312 foreach ( $r->getSlotRoles() as $role ) {
313 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
314 }
315 }
316
317 protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
318 $this->assertTrue( $slot->hasAddress() );
319 $this->assertSame( $r->getId(), $slot->getRevision() );
320
321 $this->assertInstanceOf( Content::class, $slot->getContent() );
322 }
323
324 /**
325 * @param mixed[] $details
326 *
327 * @return RevisionRecord
328 */
329 private function getRevisionRecordFromDetailsArray( $details = [] ) {
330 // Convert some values that can't be provided by dataProviders
331 if ( isset( $details['user'] ) && $details['user'] === true ) {
332 $details['user'] = $this->getTestUser()->getUser();
333 }
334 if ( isset( $details['page'] ) && $details['page'] === true ) {
335 $details['page'] = $this->getTestPage()->getId();
336 }
337 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
338 $details['parent'] = $this->getTestPage()->getLatest();
339 }
340
341 // Create the RevisionRecord with any available data
342 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
343 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
344 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
345 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
346 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
347 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
348 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
349 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
350 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
351 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
352 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
353 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
354
355 if ( isset( $details['content'] ) ) {
356 foreach ( $details['content'] as $role => $content ) {
357 $rev->setContent( $role, $content );
358 }
359 }
360
361 return $rev;
362 }
363
364 public function provideInsertRevisionOn_successes() {
365 yield 'Bare minimum revision insertion' => [
366 [
367 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
368 'page' => true,
369 'comment' => $this->getRandomCommentStoreComment(),
370 'timestamp' => '20171117010101',
371 'user' => true,
372 ],
373 ];
374 yield 'Detailed revision insertion' => [
375 [
376 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
377 'parent' => true,
378 'page' => true,
379 'comment' => $this->getRandomCommentStoreComment(),
380 'timestamp' => '20171117010101',
381 'user' => true,
382 'minor' => true,
383 'visibility' => RevisionRecord::DELETED_RESTRICTED,
384 ],
385 ];
386 }
387
388 protected function getRandomCommentStoreComment() {
389 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
390 }
391
392 /**
393 * @dataProvider provideInsertRevisionOn_successes
394 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
395 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
396 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
397 */
398 public function testInsertRevisionOn_successes(
399 array $revDetails = []
400 ) {
401 $title = $this->getTestPageTitle();
402 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
403
404 $this->overrideMwServices();
405 $store = MediaWikiServices::getInstance()->getRevisionStore();
406 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
407
408 // is the new revision correct?
409 $this->assertRevisionCompleteness( $return );
410 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
411 $this->assertRevisionRecordsEqual( $rev, $return );
412
413 // can we load it from the store?
414 $loaded = $store->getRevisionById( $return->getId() );
415 $this->assertRevisionCompleteness( $loaded );
416 $this->assertRevisionRecordsEqual( $return, $loaded );
417
418 // can we find it directly in the database?
419 $this->assertRevisionExistsInDatabase( $return );
420 }
421
422 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
423 $row = $this->revisionToRow( new Revision( $rev ), [] );
424
425 // unset nulled fields
426 unset( $row->rev_content_model );
427 unset( $row->rev_content_format );
428
429 // unset fake fields
430 unset( $row->rev_comment_text );
431 unset( $row->rev_comment_data );
432 unset( $row->rev_comment_cid );
433 unset( $row->rev_comment_id );
434
435 $store = MediaWikiServices::getInstance()->getRevisionStore();
436 $queryInfo = $store->getQueryInfo( [ 'user' ] );
437
438 $row = get_object_vars( $row );
439 $this->assertSelect(
440 $queryInfo['tables'],
441 array_keys( $row ),
442 [ 'rev_id' => $rev->getId() ],
443 [ array_values( $row ) ],
444 [],
445 $queryInfo['joins']
446 );
447 }
448
449 /**
450 * @param SlotRecord $a
451 * @param SlotRecord $b
452 */
453 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
454 // Assert that the same blob address has been used.
455 $this->assertSame( $a->getAddress(), $b->getAddress() );
456 }
457
458 /**
459 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
460 */
461 public function testInsertRevisionOn_blobAddressExists() {
462 $title = $this->getTestPageTitle();
463 $revDetails = [
464 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
465 'parent' => true,
466 'comment' => $this->getRandomCommentStoreComment(),
467 'timestamp' => '20171117010101',
468 'user' => true,
469 ];
470
471 $this->overrideMwServices();
472 $store = MediaWikiServices::getInstance()->getRevisionStore();
473
474 // Insert the first revision
475 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
476 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
477 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
478 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
479
480 // Insert a second revision inheriting the same blob address
481 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( SlotRecord::MAIN ) );
482 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
483 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
484 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
485 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
486
487 $firstMainSlot = $firstReturn->getSlot( SlotRecord::MAIN );
488 $secondMainSlot = $secondReturn->getSlot( SlotRecord::MAIN );
489
490 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
491
492 // And that different revisions have been created.
493 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
494
495 // Make sure the slot rows reference the correct revision
496 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
497 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
498 }
499
500 public function provideInsertRevisionOn_failures() {
501 yield 'no slot' => [
502 [
503 'comment' => $this->getRandomCommentStoreComment(),
504 'timestamp' => '20171117010101',
505 'user' => true,
506 ],
507 new InvalidArgumentException( 'main slot must be provided' )
508 ];
509 yield 'no main slot' => [
510 [
511 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
512 'comment' => $this->getRandomCommentStoreComment(),
513 'timestamp' => '20171117010101',
514 'user' => true,
515 ],
516 new InvalidArgumentException( 'main slot must be provided' )
517 ];
518 yield 'no timestamp' => [
519 [
520 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
521 'comment' => $this->getRandomCommentStoreComment(),
522 'user' => true,
523 ],
524 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
525 ];
526 yield 'no comment' => [
527 [
528 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
529 'timestamp' => '20171117010101',
530 'user' => true,
531 ],
532 new IncompleteRevisionException( 'comment must not be NULL!' )
533 ];
534 yield 'no user' => [
535 [
536 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
537 'comment' => $this->getRandomCommentStoreComment(),
538 'timestamp' => '20171117010101',
539 ],
540 new IncompleteRevisionException( 'user must not be NULL!' )
541 ];
542 }
543
544 /**
545 * @dataProvider provideInsertRevisionOn_failures
546 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
547 */
548 public function testInsertRevisionOn_failures(
549 array $revDetails = [],
550 Exception $exception
551 ) {
552 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
553
554 $store = MediaWikiServices::getInstance()->getRevisionStore();
555
556 $this->setExpectedException(
557 get_class( $exception ),
558 $exception->getMessage(),
559 $exception->getCode()
560 );
561 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
562 }
563
564 public function provideNewNullRevision() {
565 yield [
566 Title::newFromText( 'UTPage_notAutoCreated' ),
567 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
568 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
569 true,
570 ];
571 yield [
572 Title::newFromText( 'UTPage_notAutoCreated' ),
573 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
574 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
575 false,
576 ];
577 }
578
579 /**
580 * @dataProvider provideNewNullRevision
581 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
582 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
583 */
584 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
585 $this->overrideMwServices();
586
587 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
588 $page = WikiPage::factory( $title );
589
590 if ( !$page->exists() ) {
591 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
592 }
593
594 $revDetails['page'] = $page->getId();
595 $revDetails['timestamp'] = wfTimestampNow();
596 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
597 $revDetails['user'] = $user;
598
599 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
600 $store = MediaWikiServices::getInstance()->getRevisionStore();
601
602 $dbw = wfGetDB( DB_MASTER );
603 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
604 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
605
606 $record = $store->newNullRevision(
607 wfGetDB( DB_MASTER ),
608 $title,
609 $comment,
610 $minor,
611 $user
612 );
613
614 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
615 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
616 $this->assertEquals( $comment, $record->getComment() );
617 $this->assertEquals( $minor, $record->isMinor() );
618 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
619 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
620
621 $this->assertArrayEquals(
622 $baseRev->getSlotRoles(),
623 $record->getSlotRoles()
624 );
625
626 foreach ( $baseRev->getSlotRoles() as $role ) {
627 $parentSlot = $baseRev->getSlot( $role );
628 $slot = $record->getSlot( $role );
629
630 $this->assertTrue( $slot->isInherited(), 'isInherited' );
631 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
632 $this->assertSameSlotContent( $parentSlot, $slot );
633 }
634 }
635
636 /**
637 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
638 */
639 public function testNewNullRevision_nonExistingTitle() {
640 $store = MediaWikiServices::getInstance()->getRevisionStore();
641 $record = $store->newNullRevision(
642 wfGetDB( DB_MASTER ),
643 Title::newFromText( __METHOD__ . '.iDontExist!' ),
644 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
645 false,
646 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
647 );
648 $this->assertNull( $record );
649 }
650
651 /**
652 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
653 */
654 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
655 $page = $this->getTestPage();
656 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
657 /** @var Revision $rev */
658 $rev = $status->value['revision'];
659
660 $store = MediaWikiServices::getInstance()->getRevisionStore();
661 $revisionRecord = $store->getRevisionById( $rev->getId() );
662 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
663
664 $this->assertGreaterThan( 0, $result );
665 $this->assertSame(
666 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
667 $result
668 );
669 }
670
671 /**
672 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
673 */
674 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
675 // This assumes that sysops are auto patrolled
676 $sysop = $this->getTestSysop()->getUser();
677 $page = $this->getTestPage();
678 $status = $page->doEditContent(
679 new WikitextContent( __METHOD__ ),
680 __METHOD__,
681 0,
682 false,
683 $sysop
684 );
685 /** @var Revision $rev */
686 $rev = $status->value['revision'];
687
688 $store = MediaWikiServices::getInstance()->getRevisionStore();
689 $revisionRecord = $store->getRevisionById( $rev->getId() );
690 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
691
692 $this->assertSame( 0, $result );
693 }
694
695 /**
696 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
697 */
698 public function testGetRecentChange() {
699 $page = $this->getTestPage();
700 $content = new WikitextContent( __METHOD__ );
701 $status = $page->doEditContent( $content, __METHOD__ );
702 /** @var Revision $rev */
703 $rev = $status->value['revision'];
704
705 $store = MediaWikiServices::getInstance()->getRevisionStore();
706 $revRecord = $store->getRevisionById( $rev->getId() );
707 $recentChange = $store->getRecentChange( $revRecord );
708
709 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
710 $this->assertEquals( $rev->getRecentChange(), $recentChange );
711 }
712
713 /**
714 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
715 */
716 public function testGetRevisionById() {
717 $page = $this->getTestPage();
718 $content = new WikitextContent( __METHOD__ );
719 $status = $page->doEditContent( $content, __METHOD__ );
720 /** @var Revision $rev */
721 $rev = $status->value['revision'];
722
723 $store = MediaWikiServices::getInstance()->getRevisionStore();
724 $revRecord = $store->getRevisionById( $rev->getId() );
725
726 $this->assertSame( $rev->getId(), $revRecord->getId() );
727 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
728 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
729 }
730
731 /**
732 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
733 */
734 public function testGetRevisionByTitle() {
735 $page = $this->getTestPage();
736 $content = new WikitextContent( __METHOD__ );
737 $status = $page->doEditContent( $content, __METHOD__ );
738 /** @var Revision $rev */
739 $rev = $status->value['revision'];
740
741 $store = MediaWikiServices::getInstance()->getRevisionStore();
742 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
743
744 $this->assertSame( $rev->getId(), $revRecord->getId() );
745 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
746 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
747 }
748
749 /**
750 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
751 */
752 public function testGetRevisionByPageId() {
753 $page = $this->getTestPage();
754 $content = new WikitextContent( __METHOD__ );
755 $status = $page->doEditContent( $content, __METHOD__ );
756 /** @var Revision $rev */
757 $rev = $status->value['revision'];
758
759 $store = MediaWikiServices::getInstance()->getRevisionStore();
760 $revRecord = $store->getRevisionByPageId( $page->getId() );
761
762 $this->assertSame( $rev->getId(), $revRecord->getId() );
763 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
764 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
765 }
766
767 /**
768 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
769 */
770 public function testGetRevisionByTimestamp() {
771 // Make sure there is 1 second between the last revision and the rev we create...
772 // Otherwise we might not get the correct revision and the test may fail...
773 // :(
774 $page = $this->getTestPage();
775 sleep( 1 );
776 $content = new WikitextContent( __METHOD__ );
777 $status = $page->doEditContent( $content, __METHOD__ );
778 /** @var Revision $rev */
779 $rev = $status->value['revision'];
780
781 $store = MediaWikiServices::getInstance()->getRevisionStore();
782 $revRecord = $store->getRevisionByTimestamp(
783 $page->getTitle(),
784 $rev->getTimestamp()
785 );
786
787 $this->assertSame( $rev->getId(), $revRecord->getId() );
788 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
789 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
790 }
791
792 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
793 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
794 $page = WikiPage::factory( $rev->getTitle() );
795
796 $fields = [
797 'rev_id' => (string)$rev->getId(),
798 'rev_page' => (string)$rev->getPage(),
799 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
800 'rev_user_text' => (string)$rev->getUserText(),
801 'rev_user' => (string)$rev->getUser(),
802 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
803 'rev_deleted' => (string)$rev->getVisibility(),
804 'rev_len' => (string)$rev->getSize(),
805 'rev_parent_id' => (string)$rev->getParentId(),
806 'rev_sha1' => (string)$rev->getSha1(),
807 ];
808
809 if ( in_array( 'page', $options ) ) {
810 $fields += [
811 'page_namespace' => (string)$page->getTitle()->getNamespace(),
812 'page_title' => $page->getTitle()->getDBkey(),
813 'page_id' => (string)$page->getId(),
814 'page_latest' => (string)$page->getLatest(),
815 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
816 'page_len' => (string)$page->getContent()->getSize(),
817 ];
818 }
819
820 if ( in_array( 'user', $options ) ) {
821 $fields += [
822 'user_name' => (string)$rev->getUserText(),
823 ];
824 }
825
826 if ( in_array( 'comment', $options ) ) {
827 $fields += [
828 'rev_comment_text' => $rev->getComment(),
829 'rev_comment_data' => null,
830 'rev_comment_cid' => null,
831 ];
832 }
833
834 if ( $rev->getId() ) {
835 $fields += [
836 'rev_id' => (string)$rev->getId(),
837 ];
838 }
839
840 return (object)$fields;
841 }
842
843 protected function assertRevisionRecordMatchesRevision(
844 Revision $rev,
845 RevisionRecord $record
846 ) {
847 $this->assertSame( $rev->getId(), $record->getId() );
848 $this->assertSame( $rev->getPage(), $record->getPageId() );
849 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
850 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
851 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
852 $this->assertSame( $rev->isMinor(), $record->isMinor() );
853 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
854 $this->assertSame( $rev->getSize(), $record->getSize() );
855 /**
856 * @note As of MW 1.31, the database schema allows the parent ID to be
857 * NULL to indicate that it is unknown.
858 */
859 $expectedParent = $rev->getParentId();
860 if ( $expectedParent === null ) {
861 $expectedParent = 0;
862 }
863 $this->assertSame( $expectedParent, $record->getParentId() );
864 $this->assertSame( $rev->getSha1(), $record->getSha1() );
865 $this->assertSame( $rev->getComment(), $record->getComment()->text );
866 $this->assertSame( $rev->getContentFormat(),
867 $record->getContent( SlotRecord::MAIN )->getDefaultFormat() );
868 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord::MAIN )->getModel() );
869 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
870
871 $revRec = $rev->getRevisionRecord();
872 $revMain = $revRec->getSlot( SlotRecord::MAIN );
873 $recMain = $record->getSlot( SlotRecord::MAIN );
874
875 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
876 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
877 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
878
879 if ( $revMain->hasOrigin() ) {
880 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
881 }
882
883 if ( $revMain->hasAddress() ) {
884 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
885 }
886
887 if ( $revMain->hasContentId() ) {
888 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
889 }
890 }
891
892 /**
893 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
894 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
895 */
896 public function testNewRevisionFromRow_getQueryInfo() {
897 $page = $this->getTestPage();
898 $text = __METHOD__ . 'a-ä';
899 /** @var Revision $rev */
900 $rev = $page->doEditContent(
901 new WikitextContent( $text ),
902 __METHOD__ . 'a'
903 )->value['revision'];
904
905 $store = MediaWikiServices::getInstance()->getRevisionStore();
906 $info = $store->getQueryInfo();
907 $row = $this->db->selectRow(
908 $info['tables'],
909 $info['fields'],
910 [ 'rev_id' => $rev->getId() ],
911 __METHOD__,
912 [],
913 $info['joins']
914 );
915 $record = $store->newRevisionFromRow(
916 $row,
917 [],
918 $page->getTitle()
919 );
920 $this->assertRevisionRecordMatchesRevision( $rev, $record );
921 $this->assertSame( $text, $rev->getContent()->serialize() );
922 }
923
924 /**
925 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
926 */
927 public function testNewRevisionFromRow_anonEdit() {
928 $page = $this->getTestPage();
929 $text = __METHOD__ . 'a-ä';
930 /** @var Revision $rev */
931 $rev = $page->doEditContent(
932 new WikitextContent( $text ),
933 __METHOD__ . 'a'
934 )->value['revision'];
935
936 $store = MediaWikiServices::getInstance()->getRevisionStore();
937 $record = $store->newRevisionFromRow(
938 $this->revisionToRow( $rev ),
939 [],
940 $page->getTitle()
941 );
942 $this->assertRevisionRecordMatchesRevision( $rev, $record );
943 $this->assertSame( $text, $rev->getContent()->serialize() );
944 }
945
946 /**
947 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
948 */
949 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
950 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
951 $this->overrideMwServices();
952 $page = $this->getTestPage();
953 $text = __METHOD__ . 'a-ä';
954 /** @var Revision $rev */
955 $rev = $page->doEditContent(
956 new WikitextContent( $text ),
957 __METHOD__ . 'a'
958 )->value['revision'];
959
960 $store = MediaWikiServices::getInstance()->getRevisionStore();
961 $record = $store->newRevisionFromRow(
962 $this->revisionToRow( $rev ),
963 [],
964 $page->getTitle()
965 );
966 $this->assertRevisionRecordMatchesRevision( $rev, $record );
967 $this->assertSame( $text, $rev->getContent()->serialize() );
968 }
969
970 /**
971 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
972 */
973 public function testNewRevisionFromRow_userEdit() {
974 $page = $this->getTestPage();
975 $text = __METHOD__ . 'b-ä';
976 /** @var Revision $rev */
977 $rev = $page->doEditContent(
978 new WikitextContent( $text ),
979 __METHOD__ . 'b',
980 0,
981 false,
982 $this->getTestUser()->getUser()
983 )->value['revision'];
984
985 $store = MediaWikiServices::getInstance()->getRevisionStore();
986 $record = $store->newRevisionFromRow(
987 $this->revisionToRow( $rev ),
988 [],
989 $page->getTitle()
990 );
991 $this->assertRevisionRecordMatchesRevision( $rev, $record );
992 $this->assertSame( $text, $rev->getContent()->serialize() );
993 }
994
995 /**
996 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
997 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
998 */
999 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1000 $store = MediaWikiServices::getInstance()->getRevisionStore();
1001 $title = Title::newFromText( __METHOD__ );
1002 $text = __METHOD__ . '-bä';
1003 $page = WikiPage::factory( $title );
1004 /** @var Revision $orig */
1005 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1006 ->value['revision'];
1007 $page->doDeleteArticle( __METHOD__ );
1008
1009 $db = wfGetDB( DB_MASTER );
1010 $arQuery = $store->getArchiveQueryInfo();
1011 $res = $db->select(
1012 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1013 __METHOD__, [], $arQuery['joins']
1014 );
1015 $this->assertTrue( is_object( $res ), 'query failed' );
1016
1017 $row = $res->fetchObject();
1018 $res->free();
1019 $record = $store->newRevisionFromArchiveRow( $row );
1020
1021 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1022 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1023 }
1024
1025 /**
1026 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1027 */
1028 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1029 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1030 $this->overrideMwServices();
1031 $store = MediaWikiServices::getInstance()->getRevisionStore();
1032 $title = Title::newFromText( __METHOD__ );
1033 $text = __METHOD__ . '-bä';
1034 $page = WikiPage::factory( $title );
1035 /** @var Revision $orig */
1036 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1037 ->value['revision'];
1038 $page->doDeleteArticle( __METHOD__ );
1039
1040 $db = wfGetDB( DB_MASTER );
1041 $arQuery = $store->getArchiveQueryInfo();
1042 $res = $db->select(
1043 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1044 __METHOD__, [], $arQuery['joins']
1045 );
1046 $this->assertTrue( is_object( $res ), 'query failed' );
1047
1048 $row = $res->fetchObject();
1049 $res->free();
1050 $record = $store->newRevisionFromArchiveRow( $row );
1051
1052 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1053 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1054 }
1055
1056 /**
1057 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1058 */
1059 public function testNewRevisionFromArchiveRow_no_user() {
1060 $store = MediaWikiServices::getInstance()->getRevisionStore();
1061
1062 $row = (object)[
1063 'ar_id' => '1',
1064 'ar_page_id' => '2',
1065 'ar_namespace' => '0',
1066 'ar_title' => 'Something',
1067 'ar_rev_id' => '2',
1068 'ar_text_id' => '47',
1069 'ar_timestamp' => '20180528192356',
1070 'ar_minor_edit' => '0',
1071 'ar_deleted' => '0',
1072 'ar_len' => '78',
1073 'ar_parent_id' => '0',
1074 'ar_sha1' => 'deadbeef',
1075 'ar_comment_text' => 'whatever',
1076 'ar_comment_data' => null,
1077 'ar_comment_cid' => null,
1078 'ar_user' => '0',
1079 'ar_user_text' => '', // this is the important bit
1080 'ar_actor' => null,
1081 'ar_content_format' => null,
1082 'ar_content_model' => null,
1083 ];
1084
1085 \Wikimedia\suppressWarnings();
1086 $record = $store->newRevisionFromArchiveRow( $row );
1087 \Wikimedia\suppressWarnings( true );
1088
1089 $this->assertInstanceOf( RevisionRecord::class, $record );
1090 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1091 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1092 }
1093
1094 /**
1095 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1096 */
1097 public function testNewRevisionFromRow_no_user() {
1098 $store = MediaWikiServices::getInstance()->getRevisionStore();
1099 $title = Title::newFromText( __METHOD__ );
1100
1101 $row = (object)[
1102 'rev_id' => '2',
1103 'rev_page' => '2',
1104 'page_namespace' => '0',
1105 'page_title' => $title->getText(),
1106 'rev_text_id' => '47',
1107 'rev_timestamp' => '20180528192356',
1108 'rev_minor_edit' => '0',
1109 'rev_deleted' => '0',
1110 'rev_len' => '78',
1111 'rev_parent_id' => '0',
1112 'rev_sha1' => 'deadbeef',
1113 'rev_comment_text' => 'whatever',
1114 'rev_comment_data' => null,
1115 'rev_comment_cid' => null,
1116 'rev_user' => '0',
1117 'rev_user_text' => '', // this is the important bit
1118 'rev_actor' => null,
1119 'rev_content_format' => null,
1120 'rev_content_model' => null,
1121 ];
1122
1123 \Wikimedia\suppressWarnings();
1124 $record = $store->newRevisionFromRow( $row, 0, $title );
1125 \Wikimedia\suppressWarnings( true );
1126
1127 $this->assertNotNull( $record );
1128 $this->assertNotNull( $record->getUser() );
1129 $this->assertNotEmpty( $record->getUser()->getName() );
1130 }
1131
1132 /**
1133 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1134 */
1135 public function testInsertRevisionOn_archive() {
1136 // This is a round trip test for deletion and undeletion of a
1137 // revision row via the archive table.
1138
1139 $store = MediaWikiServices::getInstance()->getRevisionStore();
1140 $title = Title::newFromText( __METHOD__ );
1141
1142 $page = WikiPage::factory( $title );
1143 /** @var Revision $origRev */
1144 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1145 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1146 ->value['revision'];
1147 $orig = $origRev->getRevisionRecord();
1148 $page->doDeleteArticle( __METHOD__ );
1149
1150 // re-create page, so we can later load revisions for it
1151 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1152
1153 $db = wfGetDB( DB_MASTER );
1154 $arQuery = $store->getArchiveQueryInfo();
1155 $row = $db->selectRow(
1156 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1157 __METHOD__, [], $arQuery['joins']
1158 );
1159
1160 $this->assertNotFalse( $row, 'query failed' );
1161
1162 $record = $store->newRevisionFromArchiveRow(
1163 $row,
1164 0,
1165 $title,
1166 [ 'page_id' => $title->getArticleID() ]
1167 );
1168
1169 $restored = $store->insertRevisionOn( $record, $db );
1170
1171 // is the new revision correct?
1172 $this->assertRevisionCompleteness( $restored );
1173 $this->assertRevisionRecordsEqual( $record, $restored );
1174
1175 // does the new revision use the original slot?
1176 $recMain = $record->getSlot( SlotRecord::MAIN );
1177 $restMain = $restored->getSlot( SlotRecord::MAIN );
1178 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1179 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1180 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1181 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1182
1183 // can we load it from the store?
1184 $loaded = $store->getRevisionById( $restored->getId() );
1185 $this->assertNotNull( $loaded );
1186 $this->assertRevisionCompleteness( $loaded );
1187 $this->assertRevisionRecordsEqual( $restored, $loaded );
1188
1189 // can we find it directly in the database?
1190 $this->assertRevisionExistsInDatabase( $restored );
1191 }
1192
1193 /**
1194 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1195 */
1196 public function testLoadRevisionFromId() {
1197 $title = Title::newFromText( __METHOD__ );
1198 $page = WikiPage::factory( $title );
1199 /** @var Revision $rev */
1200 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1201 ->value['revision'];
1202
1203 $store = MediaWikiServices::getInstance()->getRevisionStore();
1204 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1205 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1206 }
1207
1208 /**
1209 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1210 */
1211 public function testLoadRevisionFromPageId() {
1212 $title = Title::newFromText( __METHOD__ );
1213 $page = WikiPage::factory( $title );
1214 /** @var Revision $rev */
1215 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1216 ->value['revision'];
1217
1218 $store = MediaWikiServices::getInstance()->getRevisionStore();
1219 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1220 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1221 }
1222
1223 /**
1224 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1225 */
1226 public function testLoadRevisionFromTitle() {
1227 $title = Title::newFromText( __METHOD__ );
1228 $page = WikiPage::factory( $title );
1229 /** @var Revision $rev */
1230 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1231 ->value['revision'];
1232
1233 $store = MediaWikiServices::getInstance()->getRevisionStore();
1234 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1235 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1236 }
1237
1238 /**
1239 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1240 */
1241 public function testLoadRevisionFromTimestamp() {
1242 $title = Title::newFromText( __METHOD__ );
1243 $page = WikiPage::factory( $title );
1244 /** @var Revision $revOne */
1245 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1246 ->value['revision'];
1247 // Sleep to ensure different timestamps... )(evil)
1248 sleep( 1 );
1249 /** @var Revision $revTwo */
1250 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1251 ->value['revision'];
1252
1253 $store = MediaWikiServices::getInstance()->getRevisionStore();
1254 $this->assertNull(
1255 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1256 );
1257 $this->assertSame(
1258 $revOne->getId(),
1259 $store->loadRevisionFromTimestamp(
1260 wfGetDB( DB_MASTER ),
1261 $title,
1262 $revOne->getTimestamp()
1263 )->getId()
1264 );
1265 $this->assertSame(
1266 $revTwo->getId(),
1267 $store->loadRevisionFromTimestamp(
1268 wfGetDB( DB_MASTER ),
1269 $title,
1270 $revTwo->getTimestamp()
1271 )->getId()
1272 );
1273 }
1274
1275 /**
1276 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1277 */
1278 public function testGetParentLengths() {
1279 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1280 /** @var Revision $revOne */
1281 $revOne = $page->doEditContent(
1282 new WikitextContent( __METHOD__ ), __METHOD__
1283 )->value['revision'];
1284 /** @var Revision $revTwo */
1285 $revTwo = $page->doEditContent(
1286 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1287 )->value['revision'];
1288
1289 $store = MediaWikiServices::getInstance()->getRevisionStore();
1290 $this->assertSame(
1291 [
1292 $revOne->getId() => strlen( __METHOD__ ),
1293 ],
1294 $store->listRevisionSizes(
1295 wfGetDB( DB_MASTER ),
1296 [ $revOne->getId() ]
1297 )
1298 );
1299 $this->assertSame(
1300 [
1301 $revOne->getId() => strlen( __METHOD__ ),
1302 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1303 ],
1304 $store->listRevisionSizes(
1305 wfGetDB( DB_MASTER ),
1306 [ $revOne->getId(), $revTwo->getId() ]
1307 )
1308 );
1309 }
1310
1311 /**
1312 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1313 */
1314 public function testGetPreviousRevision() {
1315 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1316 /** @var Revision $revOne */
1317 $revOne = $page->doEditContent(
1318 new WikitextContent( __METHOD__ ), __METHOD__
1319 )->value['revision'];
1320 /** @var Revision $revTwo */
1321 $revTwo = $page->doEditContent(
1322 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1323 )->value['revision'];
1324
1325 $store = MediaWikiServices::getInstance()->getRevisionStore();
1326 $this->assertNull(
1327 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1328 );
1329 $this->assertSame(
1330 $revOne->getId(),
1331 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1332 );
1333 }
1334
1335 /**
1336 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1337 */
1338 public function testGetNextRevision() {
1339 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1340 /** @var Revision $revOne */
1341 $revOne = $page->doEditContent(
1342 new WikitextContent( __METHOD__ ), __METHOD__
1343 )->value['revision'];
1344 /** @var Revision $revTwo */
1345 $revTwo = $page->doEditContent(
1346 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1347 )->value['revision'];
1348
1349 $store = MediaWikiServices::getInstance()->getRevisionStore();
1350 $this->assertSame(
1351 $revTwo->getId(),
1352 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1353 );
1354 $this->assertNull(
1355 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1356 );
1357 }
1358
1359 public function provideNonHistoryRevision() {
1360 $title = Title::newFromText( __METHOD__ );
1361 $rev = new MutableRevisionRecord( $title );
1362 yield [ $rev ];
1363
1364 $user = new UserIdentityValue( 7, 'Frank', 0 );
1365 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1366 $row = (object)[
1367 'ar_id' => 3,
1368 'ar_rev_id' => 34567,
1369 'ar_page_id' => 5,
1370 'ar_deleted' => 0,
1371 'ar_minor_edit' => 0,
1372 'ar_timestamp' => '20180101020202',
1373 ];
1374 $slots = new RevisionSlots( [] );
1375 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1376 yield [ $rev ];
1377 }
1378
1379 /**
1380 * @dataProvider provideNonHistoryRevision
1381 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1382 */
1383 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1384 $store = MediaWikiServices::getInstance()->getRevisionStore();
1385 $this->assertNull( $store->getPreviousRevision( $rev ) );
1386 }
1387
1388 /**
1389 * @dataProvider provideNonHistoryRevision
1390 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1391 */
1392 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1393 $store = MediaWikiServices::getInstance()->getRevisionStore();
1394 $this->assertNull( $store->getNextRevision( $rev ) );
1395 }
1396
1397 /**
1398 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1399 */
1400 public function testGetTimestampFromId_found() {
1401 $page = $this->getTestPage();
1402 /** @var Revision $rev */
1403 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1404 ->value['revision'];
1405
1406 $store = MediaWikiServices::getInstance()->getRevisionStore();
1407 $result = $store->getTimestampFromId(
1408 $page->getTitle(),
1409 $rev->getId()
1410 );
1411
1412 $this->assertSame( $rev->getTimestamp(), $result );
1413 }
1414
1415 /**
1416 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1417 */
1418 public function testGetTimestampFromId_notFound() {
1419 $page = $this->getTestPage();
1420 /** @var Revision $rev */
1421 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1422 ->value['revision'];
1423
1424 $store = MediaWikiServices::getInstance()->getRevisionStore();
1425 $result = $store->getTimestampFromId(
1426 $page->getTitle(),
1427 $rev->getId() + 1
1428 );
1429
1430 $this->assertFalse( $result );
1431 }
1432
1433 /**
1434 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1435 */
1436 public function testCountRevisionsByPageId() {
1437 $store = MediaWikiServices::getInstance()->getRevisionStore();
1438 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1439
1440 $this->assertSame(
1441 0,
1442 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1443 );
1444 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1445 $this->assertSame(
1446 1,
1447 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1448 );
1449 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1450 $this->assertSame(
1451 2,
1452 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1453 );
1454 }
1455
1456 /**
1457 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1458 */
1459 public function testCountRevisionsByTitle() {
1460 $store = MediaWikiServices::getInstance()->getRevisionStore();
1461 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1462
1463 $this->assertSame(
1464 0,
1465 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1466 );
1467 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1468 $this->assertSame(
1469 1,
1470 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1471 );
1472 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1473 $this->assertSame(
1474 2,
1475 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1476 );
1477 }
1478
1479 /**
1480 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1481 */
1482 public function testUserWasLastToEdit_false() {
1483 $sysop = $this->getTestSysop()->getUser();
1484 $page = $this->getTestPage();
1485 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1486
1487 $store = MediaWikiServices::getInstance()->getRevisionStore();
1488 $result = $store->userWasLastToEdit(
1489 wfGetDB( DB_MASTER ),
1490 $page->getId(),
1491 $sysop->getId(),
1492 '20160101010101'
1493 );
1494 $this->assertFalse( $result );
1495 }
1496
1497 /**
1498 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1499 */
1500 public function testUserWasLastToEdit_true() {
1501 $startTime = wfTimestampNow();
1502 $sysop = $this->getTestSysop()->getUser();
1503 $page = $this->getTestPage();
1504 $page->doEditContent(
1505 new WikitextContent( __METHOD__ ),
1506 __METHOD__,
1507 0,
1508 false,
1509 $sysop
1510 );
1511
1512 $store = MediaWikiServices::getInstance()->getRevisionStore();
1513 $result = $store->userWasLastToEdit(
1514 wfGetDB( DB_MASTER ),
1515 $page->getId(),
1516 $sysop->getId(),
1517 $startTime
1518 );
1519 $this->assertTrue( $result );
1520 }
1521
1522 /**
1523 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1524 */
1525 public function testGetKnownCurrentRevision() {
1526 $page = $this->getTestPage();
1527 /** @var Revision $rev */
1528 $rev = $page->doEditContent(
1529 new WikitextContent( __METHOD__ . 'b' ),
1530 __METHOD__ . 'b',
1531 0,
1532 false,
1533 $this->getTestUser()->getUser()
1534 )->value['revision'];
1535
1536 $store = MediaWikiServices::getInstance()->getRevisionStore();
1537 $record = $store->getKnownCurrentRevision(
1538 $page->getTitle(),
1539 $rev->getId()
1540 );
1541
1542 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1543 }
1544
1545 public function provideNewMutableRevisionFromArray() {
1546 yield 'Basic array, content object' => [
1547 [
1548 'id' => 2,
1549 'page' => 1,
1550 'timestamp' => '20171017114835',
1551 'user_text' => '111.0.1.2',
1552 'user' => 0,
1553 'minor_edit' => false,
1554 'deleted' => 0,
1555 'len' => 46,
1556 'parent_id' => 1,
1557 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1558 'comment' => 'Goat Comment!',
1559 'content' => new WikitextContent( 'Some Content' ),
1560 ]
1561 ];
1562 yield 'Basic array, serialized text' => [
1563 [
1564 'id' => 2,
1565 'page' => 1,
1566 'timestamp' => '20171017114835',
1567 'user_text' => '111.0.1.2',
1568 'user' => 0,
1569 'minor_edit' => false,
1570 'deleted' => 0,
1571 'len' => 46,
1572 'parent_id' => 1,
1573 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1574 'comment' => 'Goat Comment!',
1575 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1576 ]
1577 ];
1578 yield 'Basic array, serialized text, utf-8 flags' => [
1579 [
1580 'id' => 2,
1581 'page' => 1,
1582 'timestamp' => '20171017114835',
1583 'user_text' => '111.0.1.2',
1584 'user' => 0,
1585 'minor_edit' => false,
1586 'deleted' => 0,
1587 'len' => 46,
1588 'parent_id' => 1,
1589 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1590 'comment' => 'Goat Comment!',
1591 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1592 'flags' => 'utf-8',
1593 ]
1594 ];
1595 yield 'Basic array, with title' => [
1596 [
1597 'title' => Title::newFromText( 'SomeText' ),
1598 'timestamp' => '20171017114835',
1599 'user_text' => '111.0.1.2',
1600 'user' => 0,
1601 'minor_edit' => false,
1602 'deleted' => 0,
1603 'len' => 46,
1604 'parent_id' => 1,
1605 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1606 'comment' => 'Goat Comment!',
1607 'content' => new WikitextContent( 'Some Content' ),
1608 ]
1609 ];
1610 yield 'Basic array, no user field' => [
1611 [
1612 'id' => 2,
1613 'page' => 1,
1614 'timestamp' => '20171017114835',
1615 'user_text' => '111.0.1.3',
1616 'minor_edit' => false,
1617 'deleted' => 0,
1618 'len' => 46,
1619 'parent_id' => 1,
1620 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1621 'comment' => 'Goat Comment!',
1622 'content' => new WikitextContent( 'Some Content' ),
1623 ]
1624 ];
1625 }
1626
1627 /**
1628 * @dataProvider provideNewMutableRevisionFromArray
1629 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1630 */
1631 public function testNewMutableRevisionFromArray( array $array ) {
1632 $store = MediaWikiServices::getInstance()->getRevisionStore();
1633
1634 // HACK: if $array['page'] is given, make sure that the page exists
1635 if ( isset( $array['page'] ) ) {
1636 $t = Title::newFromID( $array['page'] );
1637 if ( !$t || !$t->exists() ) {
1638 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1639 $info = $this->insertPage( $t );
1640 $array['page'] = $info['id'];
1641 }
1642 }
1643
1644 $result = $store->newMutableRevisionFromArray( $array );
1645
1646 if ( isset( $array['id'] ) ) {
1647 $this->assertSame( $array['id'], $result->getId() );
1648 }
1649 if ( isset( $array['page'] ) ) {
1650 $this->assertSame( $array['page'], $result->getPageId() );
1651 }
1652 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1653 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1654 if ( isset( $array['user'] ) ) {
1655 $this->assertSame( $array['user'], $result->getUser()->getId() );
1656 }
1657 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1658 $this->assertSame( $array['deleted'], $result->getVisibility() );
1659 $this->assertSame( $array['len'], $result->getSize() );
1660 $this->assertSame( $array['parent_id'], $result->getParentId() );
1661 $this->assertSame( $array['sha1'], $result->getSha1() );
1662 $this->assertSame( $array['comment'], $result->getComment()->text );
1663 if ( isset( $array['content'] ) ) {
1664 foreach ( $array['content'] as $role => $content ) {
1665 $this->assertTrue(
1666 $result->getContent( $role )->equals( $content )
1667 );
1668 }
1669 } elseif ( isset( $array['text'] ) ) {
1670 $this->assertSame( $array['text'],
1671 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1672 } elseif ( isset( $array['content_format'] ) ) {
1673 $this->assertSame(
1674 $array['content_format'],
1675 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1676 );
1677 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1678 }
1679 }
1680
1681 /**
1682 * @dataProvider provideNewMutableRevisionFromArray
1683 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1684 */
1685 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1686 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1687 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1688 $blobStore = new SqlBlobStore( $lb, $cache );
1689 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1690
1691 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1692 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1693 ->disableOriginalConstructor()
1694 ->getMock();
1695 $factory->expects( $this->any() )
1696 ->method( 'newBlobStore' )
1697 ->willReturn( $blobStore );
1698 $factory->expects( $this->any() )
1699 ->method( 'newSqlBlobStore' )
1700 ->willReturn( $blobStore );
1701
1702 $this->setService( 'BlobStoreFactory', $factory );
1703
1704 $this->testNewMutableRevisionFromArray( $array );
1705 }
1706
1707 }