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