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