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