Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[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( 'main' ) );
302 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( 'main' ) );
303 $this->assertInstanceOf( Content::class, $r->getContent( '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( '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( '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( '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( '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( 'main' );
481 $secondMainSlot = $secondReturn->getSlot( '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( '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( '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( '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( '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( '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( '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( '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(), $record->getContent( 'main' )->getDefaultFormat() );
860 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
861 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
862
863 $revRec = $rev->getRevisionRecord();
864 $revMain = $revRec->getSlot( 'main' );
865 $recMain = $record->getSlot( 'main' );
866
867 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
868 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
869 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
870
871 if ( $revMain->hasOrigin() ) {
872 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
873 }
874
875 if ( $revMain->hasAddress() ) {
876 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
877 }
878
879 if ( $revMain->hasContentId() ) {
880 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
881 }
882 }
883
884 /**
885 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
886 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
887 */
888 public function testNewRevisionFromRow_getQueryInfo() {
889 $page = $this->getTestPage();
890 $text = __METHOD__ . 'a-ä';
891 /** @var Revision $rev */
892 $rev = $page->doEditContent(
893 new WikitextContent( $text ),
894 __METHOD__ . 'a'
895 )->value['revision'];
896
897 $store = MediaWikiServices::getInstance()->getRevisionStore();
898 $info = $store->getQueryInfo();
899 $row = $this->db->selectRow(
900 $info['tables'],
901 $info['fields'],
902 [ 'rev_id' => $rev->getId() ],
903 __METHOD__,
904 [],
905 $info['joins']
906 );
907 $record = $store->newRevisionFromRow(
908 $row,
909 [],
910 $page->getTitle()
911 );
912 $this->assertRevisionRecordMatchesRevision( $rev, $record );
913 $this->assertSame( $text, $rev->getContent()->serialize() );
914 }
915
916 /**
917 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
918 */
919 public function testNewRevisionFromRow_anonEdit() {
920 $page = $this->getTestPage();
921 $text = __METHOD__ . 'a-ä';
922 /** @var Revision $rev */
923 $rev = $page->doEditContent(
924 new WikitextContent( $text ),
925 __METHOD__ . 'a'
926 )->value['revision'];
927
928 $store = MediaWikiServices::getInstance()->getRevisionStore();
929 $record = $store->newRevisionFromRow(
930 $this->revisionToRow( $rev ),
931 [],
932 $page->getTitle()
933 );
934 $this->assertRevisionRecordMatchesRevision( $rev, $record );
935 $this->assertSame( $text, $rev->getContent()->serialize() );
936 }
937
938 /**
939 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
940 */
941 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
942 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
943 $this->overrideMwServices();
944 $page = $this->getTestPage();
945 $text = __METHOD__ . 'a-ä';
946 /** @var Revision $rev */
947 $rev = $page->doEditContent(
948 new WikitextContent( $text ),
949 __METHOD__ . 'a'
950 )->value['revision'];
951
952 $store = MediaWikiServices::getInstance()->getRevisionStore();
953 $record = $store->newRevisionFromRow(
954 $this->revisionToRow( $rev ),
955 [],
956 $page->getTitle()
957 );
958 $this->assertRevisionRecordMatchesRevision( $rev, $record );
959 $this->assertSame( $text, $rev->getContent()->serialize() );
960 }
961
962 /**
963 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
964 */
965 public function testNewRevisionFromRow_userEdit() {
966 $page = $this->getTestPage();
967 $text = __METHOD__ . 'b-ä';
968 /** @var Revision $rev */
969 $rev = $page->doEditContent(
970 new WikitextContent( $text ),
971 __METHOD__ . 'b',
972 0,
973 false,
974 $this->getTestUser()->getUser()
975 )->value['revision'];
976
977 $store = MediaWikiServices::getInstance()->getRevisionStore();
978 $record = $store->newRevisionFromRow(
979 $this->revisionToRow( $rev ),
980 [],
981 $page->getTitle()
982 );
983 $this->assertRevisionRecordMatchesRevision( $rev, $record );
984 $this->assertSame( $text, $rev->getContent()->serialize() );
985 }
986
987 /**
988 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
989 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
990 */
991 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
992 $store = MediaWikiServices::getInstance()->getRevisionStore();
993 $title = Title::newFromText( __METHOD__ );
994 $text = __METHOD__ . '-bä';
995 $page = WikiPage::factory( $title );
996 /** @var Revision $orig */
997 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
998 ->value['revision'];
999 $page->doDeleteArticle( __METHOD__ );
1000
1001 $db = wfGetDB( DB_MASTER );
1002 $arQuery = $store->getArchiveQueryInfo();
1003 $res = $db->select(
1004 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1005 __METHOD__, [], $arQuery['joins']
1006 );
1007 $this->assertTrue( is_object( $res ), 'query failed' );
1008
1009 $row = $res->fetchObject();
1010 $res->free();
1011 $record = $store->newRevisionFromArchiveRow( $row );
1012
1013 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1014 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1015 }
1016
1017 /**
1018 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1019 */
1020 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1021 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1022 $this->overrideMwServices();
1023 $store = MediaWikiServices::getInstance()->getRevisionStore();
1024 $title = Title::newFromText( __METHOD__ );
1025 $text = __METHOD__ . '-bä';
1026 $page = WikiPage::factory( $title );
1027 /** @var Revision $orig */
1028 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1029 ->value['revision'];
1030 $page->doDeleteArticle( __METHOD__ );
1031
1032 $db = wfGetDB( DB_MASTER );
1033 $arQuery = $store->getArchiveQueryInfo();
1034 $res = $db->select(
1035 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1036 __METHOD__, [], $arQuery['joins']
1037 );
1038 $this->assertTrue( is_object( $res ), 'query failed' );
1039
1040 $row = $res->fetchObject();
1041 $res->free();
1042 $record = $store->newRevisionFromArchiveRow( $row );
1043
1044 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1045 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1046 }
1047
1048 /**
1049 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1050 */
1051 public function testNewRevisionFromArchiveRow_no_user() {
1052 $store = MediaWikiServices::getInstance()->getRevisionStore();
1053
1054 $row = (object)[
1055 'ar_id' => '1',
1056 'ar_page_id' => '2',
1057 'ar_namespace' => '0',
1058 'ar_title' => 'Something',
1059 'ar_rev_id' => '2',
1060 'ar_text_id' => '47',
1061 'ar_timestamp' => '20180528192356',
1062 'ar_minor_edit' => '0',
1063 'ar_deleted' => '0',
1064 'ar_len' => '78',
1065 'ar_parent_id' => '0',
1066 'ar_sha1' => 'deadbeef',
1067 'ar_comment_text' => 'whatever',
1068 'ar_comment_data' => null,
1069 'ar_comment_cid' => null,
1070 'ar_user' => '0',
1071 'ar_user_text' => '', // this is the important bit
1072 'ar_actor' => null,
1073 'ar_content_format' => null,
1074 'ar_content_model' => null,
1075 ];
1076
1077 \Wikimedia\suppressWarnings();
1078 $record = $store->newRevisionFromArchiveRow( $row );
1079 \Wikimedia\suppressWarnings( true );
1080
1081 $this->assertInstanceOf( RevisionRecord::class, $record );
1082 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1083 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1084 }
1085
1086 /**
1087 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
1088 */
1089 public function testNewRevisionFromRow_no_user() {
1090 $store = MediaWikiServices::getInstance()->getRevisionStore();
1091 $title = Title::newFromText( __METHOD__ );
1092
1093 $row = (object)[
1094 'rev_id' => '2',
1095 'rev_page' => '2',
1096 'page_namespace' => '0',
1097 'page_title' => $title->getText(),
1098 'rev_text_id' => '47',
1099 'rev_timestamp' => '20180528192356',
1100 'rev_minor_edit' => '0',
1101 'rev_deleted' => '0',
1102 'rev_len' => '78',
1103 'rev_parent_id' => '0',
1104 'rev_sha1' => 'deadbeef',
1105 'rev_comment_text' => 'whatever',
1106 'rev_comment_data' => null,
1107 'rev_comment_cid' => null,
1108 'rev_user' => '0',
1109 'rev_user_text' => '', // this is the important bit
1110 'rev_actor' => null,
1111 'rev_content_format' => null,
1112 'rev_content_model' => null,
1113 ];
1114
1115 \Wikimedia\suppressWarnings();
1116 $record = $store->newRevisionFromRow( $row, 0, $title );
1117 \Wikimedia\suppressWarnings( true );
1118
1119 $this->assertNotNull( $record );
1120 $this->assertNotNull( $record->getUser() );
1121 $this->assertNotEmpty( $record->getUser()->getName() );
1122 }
1123
1124 /**
1125 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1126 */
1127 public function testInsertRevisionOn_archive() {
1128 // This is a round trip test for deletion and undeletion of a
1129 // revision row via the archive table.
1130
1131 $store = MediaWikiServices::getInstance()->getRevisionStore();
1132 $title = Title::newFromText( __METHOD__ );
1133
1134 $page = WikiPage::factory( $title );
1135 /** @var Revision $origRev */
1136 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1137 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1138 ->value['revision'];
1139 $orig = $origRev->getRevisionRecord();
1140 $page->doDeleteArticle( __METHOD__ );
1141
1142 // re-create page, so we can later load revisions for it
1143 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1144
1145 $db = wfGetDB( DB_MASTER );
1146 $arQuery = $store->getArchiveQueryInfo();
1147 $row = $db->selectRow(
1148 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1149 __METHOD__, [], $arQuery['joins']
1150 );
1151
1152 $this->assertNotFalse( $row, 'query failed' );
1153
1154 $record = $store->newRevisionFromArchiveRow(
1155 $row,
1156 0,
1157 $title,
1158 [ 'page_id' => $title->getArticleID() ]
1159 );
1160
1161 $restored = $store->insertRevisionOn( $record, $db );
1162
1163 // is the new revision correct?
1164 $this->assertRevisionCompleteness( $restored );
1165 $this->assertRevisionRecordsEqual( $record, $restored );
1166
1167 // does the new revision use the original slot?
1168 $recMain = $record->getSlot( 'main' );
1169 $restMain = $restored->getSlot( 'main' );
1170 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1171 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1172 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1173 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1174
1175 // can we load it from the store?
1176 $loaded = $store->getRevisionById( $restored->getId() );
1177 $this->assertNotNull( $loaded );
1178 $this->assertRevisionCompleteness( $loaded );
1179 $this->assertRevisionRecordsEqual( $restored, $loaded );
1180
1181 // can we find it directly in the database?
1182 $this->assertRevisionExistsInDatabase( $restored );
1183 }
1184
1185 /**
1186 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1187 */
1188 public function testLoadRevisionFromId() {
1189 $title = Title::newFromText( __METHOD__ );
1190 $page = WikiPage::factory( $title );
1191 /** @var Revision $rev */
1192 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1193 ->value['revision'];
1194
1195 $store = MediaWikiServices::getInstance()->getRevisionStore();
1196 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1197 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1198 }
1199
1200 /**
1201 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1202 */
1203 public function testLoadRevisionFromPageId() {
1204 $title = Title::newFromText( __METHOD__ );
1205 $page = WikiPage::factory( $title );
1206 /** @var Revision $rev */
1207 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1208 ->value['revision'];
1209
1210 $store = MediaWikiServices::getInstance()->getRevisionStore();
1211 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1212 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1213 }
1214
1215 /**
1216 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1217 */
1218 public function testLoadRevisionFromTitle() {
1219 $title = Title::newFromText( __METHOD__ );
1220 $page = WikiPage::factory( $title );
1221 /** @var Revision $rev */
1222 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1223 ->value['revision'];
1224
1225 $store = MediaWikiServices::getInstance()->getRevisionStore();
1226 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1227 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1228 }
1229
1230 /**
1231 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1232 */
1233 public function testLoadRevisionFromTimestamp() {
1234 $title = Title::newFromText( __METHOD__ );
1235 $page = WikiPage::factory( $title );
1236 /** @var Revision $revOne */
1237 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1238 ->value['revision'];
1239 // Sleep to ensure different timestamps... )(evil)
1240 sleep( 1 );
1241 /** @var Revision $revTwo */
1242 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1243 ->value['revision'];
1244
1245 $store = MediaWikiServices::getInstance()->getRevisionStore();
1246 $this->assertNull(
1247 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1248 );
1249 $this->assertSame(
1250 $revOne->getId(),
1251 $store->loadRevisionFromTimestamp(
1252 wfGetDB( DB_MASTER ),
1253 $title,
1254 $revOne->getTimestamp()
1255 )->getId()
1256 );
1257 $this->assertSame(
1258 $revTwo->getId(),
1259 $store->loadRevisionFromTimestamp(
1260 wfGetDB( DB_MASTER ),
1261 $title,
1262 $revTwo->getTimestamp()
1263 )->getId()
1264 );
1265 }
1266
1267 /**
1268 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1269 */
1270 public function testGetParentLengths() {
1271 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1272 /** @var Revision $revOne */
1273 $revOne = $page->doEditContent(
1274 new WikitextContent( __METHOD__ ), __METHOD__
1275 )->value['revision'];
1276 /** @var Revision $revTwo */
1277 $revTwo = $page->doEditContent(
1278 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1279 )->value['revision'];
1280
1281 $store = MediaWikiServices::getInstance()->getRevisionStore();
1282 $this->assertSame(
1283 [
1284 $revOne->getId() => strlen( __METHOD__ ),
1285 ],
1286 $store->listRevisionSizes(
1287 wfGetDB( DB_MASTER ),
1288 [ $revOne->getId() ]
1289 )
1290 );
1291 $this->assertSame(
1292 [
1293 $revOne->getId() => strlen( __METHOD__ ),
1294 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1295 ],
1296 $store->listRevisionSizes(
1297 wfGetDB( DB_MASTER ),
1298 [ $revOne->getId(), $revTwo->getId() ]
1299 )
1300 );
1301 }
1302
1303 /**
1304 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1305 */
1306 public function testGetPreviousRevision() {
1307 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1308 /** @var Revision $revOne */
1309 $revOne = $page->doEditContent(
1310 new WikitextContent( __METHOD__ ), __METHOD__
1311 )->value['revision'];
1312 /** @var Revision $revTwo */
1313 $revTwo = $page->doEditContent(
1314 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1315 )->value['revision'];
1316
1317 $store = MediaWikiServices::getInstance()->getRevisionStore();
1318 $this->assertNull(
1319 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1320 );
1321 $this->assertSame(
1322 $revOne->getId(),
1323 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1324 );
1325 }
1326
1327 /**
1328 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1329 */
1330 public function testGetNextRevision() {
1331 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1332 /** @var Revision $revOne */
1333 $revOne = $page->doEditContent(
1334 new WikitextContent( __METHOD__ ), __METHOD__
1335 )->value['revision'];
1336 /** @var Revision $revTwo */
1337 $revTwo = $page->doEditContent(
1338 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1339 )->value['revision'];
1340
1341 $store = MediaWikiServices::getInstance()->getRevisionStore();
1342 $this->assertSame(
1343 $revTwo->getId(),
1344 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1345 );
1346 $this->assertNull(
1347 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1348 );
1349 }
1350
1351 /**
1352 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1353 */
1354 public function testGetTimestampFromId_found() {
1355 $page = $this->getTestPage();
1356 /** @var Revision $rev */
1357 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1358 ->value['revision'];
1359
1360 $store = MediaWikiServices::getInstance()->getRevisionStore();
1361 $result = $store->getTimestampFromId(
1362 $page->getTitle(),
1363 $rev->getId()
1364 );
1365
1366 $this->assertSame( $rev->getTimestamp(), $result );
1367 }
1368
1369 /**
1370 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1371 */
1372 public function testGetTimestampFromId_notFound() {
1373 $page = $this->getTestPage();
1374 /** @var Revision $rev */
1375 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1376 ->value['revision'];
1377
1378 $store = MediaWikiServices::getInstance()->getRevisionStore();
1379 $result = $store->getTimestampFromId(
1380 $page->getTitle(),
1381 $rev->getId() + 1
1382 );
1383
1384 $this->assertFalse( $result );
1385 }
1386
1387 /**
1388 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1389 */
1390 public function testCountRevisionsByPageId() {
1391 $store = MediaWikiServices::getInstance()->getRevisionStore();
1392 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1393
1394 $this->assertSame(
1395 0,
1396 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1397 );
1398 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1399 $this->assertSame(
1400 1,
1401 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1402 );
1403 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1404 $this->assertSame(
1405 2,
1406 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1407 );
1408 }
1409
1410 /**
1411 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1412 */
1413 public function testCountRevisionsByTitle() {
1414 $store = MediaWikiServices::getInstance()->getRevisionStore();
1415 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1416
1417 $this->assertSame(
1418 0,
1419 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1420 );
1421 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1422 $this->assertSame(
1423 1,
1424 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1425 );
1426 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1427 $this->assertSame(
1428 2,
1429 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1430 );
1431 }
1432
1433 /**
1434 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1435 */
1436 public function testUserWasLastToEdit_false() {
1437 $sysop = $this->getTestSysop()->getUser();
1438 $page = $this->getTestPage();
1439 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1440
1441 $store = MediaWikiServices::getInstance()->getRevisionStore();
1442 $result = $store->userWasLastToEdit(
1443 wfGetDB( DB_MASTER ),
1444 $page->getId(),
1445 $sysop->getId(),
1446 '20160101010101'
1447 );
1448 $this->assertFalse( $result );
1449 }
1450
1451 /**
1452 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1453 */
1454 public function testUserWasLastToEdit_true() {
1455 $startTime = wfTimestampNow();
1456 $sysop = $this->getTestSysop()->getUser();
1457 $page = $this->getTestPage();
1458 $page->doEditContent(
1459 new WikitextContent( __METHOD__ ),
1460 __METHOD__,
1461 0,
1462 false,
1463 $sysop
1464 );
1465
1466 $store = MediaWikiServices::getInstance()->getRevisionStore();
1467 $result = $store->userWasLastToEdit(
1468 wfGetDB( DB_MASTER ),
1469 $page->getId(),
1470 $sysop->getId(),
1471 $startTime
1472 );
1473 $this->assertTrue( $result );
1474 }
1475
1476 /**
1477 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1478 */
1479 public function testGetKnownCurrentRevision() {
1480 $page = $this->getTestPage();
1481 /** @var Revision $rev */
1482 $rev = $page->doEditContent(
1483 new WikitextContent( __METHOD__ . 'b' ),
1484 __METHOD__ . 'b',
1485 0,
1486 false,
1487 $this->getTestUser()->getUser()
1488 )->value['revision'];
1489
1490 $store = MediaWikiServices::getInstance()->getRevisionStore();
1491 $record = $store->getKnownCurrentRevision(
1492 $page->getTitle(),
1493 $rev->getId()
1494 );
1495
1496 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1497 }
1498
1499 public function provideNewMutableRevisionFromArray() {
1500 yield 'Basic array, content object' => [
1501 [
1502 'id' => 2,
1503 'page' => 1,
1504 'timestamp' => '20171017114835',
1505 'user_text' => '111.0.1.2',
1506 'user' => 0,
1507 'minor_edit' => false,
1508 'deleted' => 0,
1509 'len' => 46,
1510 'parent_id' => 1,
1511 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1512 'comment' => 'Goat Comment!',
1513 'content' => new WikitextContent( 'Some Content' ),
1514 ]
1515 ];
1516 yield 'Basic array, serialized text' => [
1517 [
1518 'id' => 2,
1519 'page' => 1,
1520 'timestamp' => '20171017114835',
1521 'user_text' => '111.0.1.2',
1522 'user' => 0,
1523 'minor_edit' => false,
1524 'deleted' => 0,
1525 'len' => 46,
1526 'parent_id' => 1,
1527 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1528 'comment' => 'Goat Comment!',
1529 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1530 ]
1531 ];
1532 yield 'Basic array, serialized text, utf-8 flags' => [
1533 [
1534 'id' => 2,
1535 'page' => 1,
1536 'timestamp' => '20171017114835',
1537 'user_text' => '111.0.1.2',
1538 'user' => 0,
1539 'minor_edit' => false,
1540 'deleted' => 0,
1541 'len' => 46,
1542 'parent_id' => 1,
1543 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1544 'comment' => 'Goat Comment!',
1545 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1546 'flags' => 'utf-8',
1547 ]
1548 ];
1549 yield 'Basic array, with title' => [
1550 [
1551 'title' => Title::newFromText( 'SomeText' ),
1552 'timestamp' => '20171017114835',
1553 'user_text' => '111.0.1.2',
1554 'user' => 0,
1555 'minor_edit' => false,
1556 'deleted' => 0,
1557 'len' => 46,
1558 'parent_id' => 1,
1559 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1560 'comment' => 'Goat Comment!',
1561 'content' => new WikitextContent( 'Some Content' ),
1562 ]
1563 ];
1564 yield 'Basic array, no user field' => [
1565 [
1566 'id' => 2,
1567 'page' => 1,
1568 'timestamp' => '20171017114835',
1569 'user_text' => '111.0.1.3',
1570 'minor_edit' => false,
1571 'deleted' => 0,
1572 'len' => 46,
1573 'parent_id' => 1,
1574 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1575 'comment' => 'Goat Comment!',
1576 'content' => new WikitextContent( 'Some Content' ),
1577 ]
1578 ];
1579 }
1580
1581 /**
1582 * @dataProvider provideNewMutableRevisionFromArray
1583 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1584 */
1585 public function testNewMutableRevisionFromArray( array $array ) {
1586 $store = MediaWikiServices::getInstance()->getRevisionStore();
1587
1588 // HACK: if $array['page'] is given, make sure that the page exists
1589 if ( isset( $array['page'] ) ) {
1590 $t = Title::newFromID( $array['page'] );
1591 if ( !$t || !$t->exists() ) {
1592 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1593 $info = $this->insertPage( $t );
1594 $array['page'] = $info['id'];
1595 }
1596 }
1597
1598 $result = $store->newMutableRevisionFromArray( $array );
1599
1600 if ( isset( $array['id'] ) ) {
1601 $this->assertSame( $array['id'], $result->getId() );
1602 }
1603 if ( isset( $array['page'] ) ) {
1604 $this->assertSame( $array['page'], $result->getPageId() );
1605 }
1606 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1607 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1608 if ( isset( $array['user'] ) ) {
1609 $this->assertSame( $array['user'], $result->getUser()->getId() );
1610 }
1611 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1612 $this->assertSame( $array['deleted'], $result->getVisibility() );
1613 $this->assertSame( $array['len'], $result->getSize() );
1614 $this->assertSame( $array['parent_id'], $result->getParentId() );
1615 $this->assertSame( $array['sha1'], $result->getSha1() );
1616 $this->assertSame( $array['comment'], $result->getComment()->text );
1617 if ( isset( $array['content'] ) ) {
1618 foreach ( $array['content'] as $role => $content ) {
1619 $this->assertTrue(
1620 $result->getContent( $role )->equals( $content )
1621 );
1622 }
1623 } elseif ( isset( $array['text'] ) ) {
1624 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1625 } elseif ( isset( $array['content_format'] ) ) {
1626 $this->assertSame(
1627 $array['content_format'],
1628 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1629 );
1630 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1631 }
1632 }
1633
1634 /**
1635 * @dataProvider provideNewMutableRevisionFromArray
1636 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1637 */
1638 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1639 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1640 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1641 $blobStore = new SqlBlobStore( $lb, $cache );
1642 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1643
1644 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1645 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1646 ->disableOriginalConstructor()
1647 ->getMock();
1648 $factory->expects( $this->any() )
1649 ->method( 'newBlobStore' )
1650 ->willReturn( $blobStore );
1651 $factory->expects( $this->any() )
1652 ->method( 'newSqlBlobStore' )
1653 ->willReturn( $blobStore );
1654
1655 $this->setService( 'BlobStoreFactory', $factory );
1656
1657 $this->testNewMutableRevisionFromArray( $array );
1658 }
1659
1660 }