Merge "test: disable hook when testing default preferences"
[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 protected 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::newRevisionFromArchiveRow
1053 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1054 */
1055 public function testInsertRevisionOn_archive() {
1056 // This is a round trip test for deletion and undeletion of a
1057 // revision row via the archive table.
1058
1059 $store = MediaWikiServices::getInstance()->getRevisionStore();
1060 $title = Title::newFromText( __METHOD__ );
1061
1062 $page = WikiPage::factory( $title );
1063 /** @var Revision $origRev */
1064 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1065 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1066 ->value['revision'];
1067 $orig = $origRev->getRevisionRecord();
1068 $page->doDeleteArticle( __METHOD__ );
1069
1070 // re-create page, so we can later load revisions for it
1071 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1072
1073 $db = wfGetDB( DB_MASTER );
1074 $arQuery = $store->getArchiveQueryInfo();
1075 $row = $db->selectRow(
1076 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1077 __METHOD__, [], $arQuery['joins']
1078 );
1079
1080 $this->assertNotFalse( $row, 'query failed' );
1081
1082 $record = $store->newRevisionFromArchiveRow(
1083 $row,
1084 0,
1085 $title,
1086 [ 'page_id' => $title->getArticleID() ]
1087 );
1088
1089 $restored = $store->insertRevisionOn( $record, $db );
1090
1091 // is the new revision correct?
1092 $this->assertRevisionCompleteness( $restored );
1093 $this->assertRevisionRecordsEqual( $record, $restored );
1094
1095 // does the new revision use the original slot?
1096 $recMain = $record->getSlot( 'main' );
1097 $restMain = $restored->getSlot( 'main' );
1098 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1099 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1100 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1101 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1102
1103 // can we load it from the store?
1104 $loaded = $store->getRevisionById( $restored->getId() );
1105 $this->assertNotNull( $loaded );
1106 $this->assertRevisionCompleteness( $loaded );
1107 $this->assertRevisionRecordsEqual( $restored, $loaded );
1108
1109 // can we find it directly in the database?
1110 $this->assertRevisionExistsInDatabase( $restored );
1111 }
1112
1113 /**
1114 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1115 */
1116 public function testLoadRevisionFromId() {
1117 $title = Title::newFromText( __METHOD__ );
1118 $page = WikiPage::factory( $title );
1119 /** @var Revision $rev */
1120 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1121 ->value['revision'];
1122
1123 $store = MediaWikiServices::getInstance()->getRevisionStore();
1124 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1125 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1126 }
1127
1128 /**
1129 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1130 */
1131 public function testLoadRevisionFromPageId() {
1132 $title = Title::newFromText( __METHOD__ );
1133 $page = WikiPage::factory( $title );
1134 /** @var Revision $rev */
1135 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1136 ->value['revision'];
1137
1138 $store = MediaWikiServices::getInstance()->getRevisionStore();
1139 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1140 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1141 }
1142
1143 /**
1144 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1145 */
1146 public function testLoadRevisionFromTitle() {
1147 $title = Title::newFromText( __METHOD__ );
1148 $page = WikiPage::factory( $title );
1149 /** @var Revision $rev */
1150 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1151 ->value['revision'];
1152
1153 $store = MediaWikiServices::getInstance()->getRevisionStore();
1154 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1155 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1156 }
1157
1158 /**
1159 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1160 */
1161 public function testLoadRevisionFromTimestamp() {
1162 $title = Title::newFromText( __METHOD__ );
1163 $page = WikiPage::factory( $title );
1164 /** @var Revision $revOne */
1165 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1166 ->value['revision'];
1167 // Sleep to ensure different timestamps... )(evil)
1168 sleep( 1 );
1169 /** @var Revision $revTwo */
1170 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1171 ->value['revision'];
1172
1173 $store = MediaWikiServices::getInstance()->getRevisionStore();
1174 $this->assertNull(
1175 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1176 );
1177 $this->assertSame(
1178 $revOne->getId(),
1179 $store->loadRevisionFromTimestamp(
1180 wfGetDB( DB_MASTER ),
1181 $title,
1182 $revOne->getTimestamp()
1183 )->getId()
1184 );
1185 $this->assertSame(
1186 $revTwo->getId(),
1187 $store->loadRevisionFromTimestamp(
1188 wfGetDB( DB_MASTER ),
1189 $title,
1190 $revTwo->getTimestamp()
1191 )->getId()
1192 );
1193 }
1194
1195 /**
1196 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1197 */
1198 public function testGetParentLengths() {
1199 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1200 /** @var Revision $revOne */
1201 $revOne = $page->doEditContent(
1202 new WikitextContent( __METHOD__ ), __METHOD__
1203 )->value['revision'];
1204 /** @var Revision $revTwo */
1205 $revTwo = $page->doEditContent(
1206 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1207 )->value['revision'];
1208
1209 $store = MediaWikiServices::getInstance()->getRevisionStore();
1210 $this->assertSame(
1211 [
1212 $revOne->getId() => strlen( __METHOD__ ),
1213 ],
1214 $store->listRevisionSizes(
1215 wfGetDB( DB_MASTER ),
1216 [ $revOne->getId() ]
1217 )
1218 );
1219 $this->assertSame(
1220 [
1221 $revOne->getId() => strlen( __METHOD__ ),
1222 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1223 ],
1224 $store->listRevisionSizes(
1225 wfGetDB( DB_MASTER ),
1226 [ $revOne->getId(), $revTwo->getId() ]
1227 )
1228 );
1229 }
1230
1231 /**
1232 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1233 */
1234 public function testGetPreviousRevision() {
1235 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1236 /** @var Revision $revOne */
1237 $revOne = $page->doEditContent(
1238 new WikitextContent( __METHOD__ ), __METHOD__
1239 )->value['revision'];
1240 /** @var Revision $revTwo */
1241 $revTwo = $page->doEditContent(
1242 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1243 )->value['revision'];
1244
1245 $store = MediaWikiServices::getInstance()->getRevisionStore();
1246 $this->assertNull(
1247 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1248 );
1249 $this->assertSame(
1250 $revOne->getId(),
1251 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1252 );
1253 }
1254
1255 /**
1256 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1257 */
1258 public function testGetNextRevision() {
1259 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1260 /** @var Revision $revOne */
1261 $revOne = $page->doEditContent(
1262 new WikitextContent( __METHOD__ ), __METHOD__
1263 )->value['revision'];
1264 /** @var Revision $revTwo */
1265 $revTwo = $page->doEditContent(
1266 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1267 )->value['revision'];
1268
1269 $store = MediaWikiServices::getInstance()->getRevisionStore();
1270 $this->assertSame(
1271 $revTwo->getId(),
1272 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1273 );
1274 $this->assertNull(
1275 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1276 );
1277 }
1278
1279 /**
1280 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1281 */
1282 public function testGetTimestampFromId_found() {
1283 $page = $this->getTestPage();
1284 /** @var Revision $rev */
1285 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1286 ->value['revision'];
1287
1288 $store = MediaWikiServices::getInstance()->getRevisionStore();
1289 $result = $store->getTimestampFromId(
1290 $page->getTitle(),
1291 $rev->getId()
1292 );
1293
1294 $this->assertSame( $rev->getTimestamp(), $result );
1295 }
1296
1297 /**
1298 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1299 */
1300 public function testGetTimestampFromId_notFound() {
1301 $page = $this->getTestPage();
1302 /** @var Revision $rev */
1303 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1304 ->value['revision'];
1305
1306 $store = MediaWikiServices::getInstance()->getRevisionStore();
1307 $result = $store->getTimestampFromId(
1308 $page->getTitle(),
1309 $rev->getId() + 1
1310 );
1311
1312 $this->assertFalse( $result );
1313 }
1314
1315 /**
1316 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1317 */
1318 public function testCountRevisionsByPageId() {
1319 $store = MediaWikiServices::getInstance()->getRevisionStore();
1320 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1321
1322 $this->assertSame(
1323 0,
1324 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1325 );
1326 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1327 $this->assertSame(
1328 1,
1329 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1330 );
1331 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1332 $this->assertSame(
1333 2,
1334 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1335 );
1336 }
1337
1338 /**
1339 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1340 */
1341 public function testCountRevisionsByTitle() {
1342 $store = MediaWikiServices::getInstance()->getRevisionStore();
1343 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1344
1345 $this->assertSame(
1346 0,
1347 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1348 );
1349 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1350 $this->assertSame(
1351 1,
1352 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1353 );
1354 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1355 $this->assertSame(
1356 2,
1357 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1358 );
1359 }
1360
1361 /**
1362 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1363 */
1364 public function testUserWasLastToEdit_false() {
1365 $sysop = $this->getTestSysop()->getUser();
1366 $page = $this->getTestPage();
1367 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1368
1369 $store = MediaWikiServices::getInstance()->getRevisionStore();
1370 $result = $store->userWasLastToEdit(
1371 wfGetDB( DB_MASTER ),
1372 $page->getId(),
1373 $sysop->getId(),
1374 '20160101010101'
1375 );
1376 $this->assertFalse( $result );
1377 }
1378
1379 /**
1380 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1381 */
1382 public function testUserWasLastToEdit_true() {
1383 $startTime = wfTimestampNow();
1384 $sysop = $this->getTestSysop()->getUser();
1385 $page = $this->getTestPage();
1386 $page->doEditContent(
1387 new WikitextContent( __METHOD__ ),
1388 __METHOD__,
1389 0,
1390 false,
1391 $sysop
1392 );
1393
1394 $store = MediaWikiServices::getInstance()->getRevisionStore();
1395 $result = $store->userWasLastToEdit(
1396 wfGetDB( DB_MASTER ),
1397 $page->getId(),
1398 $sysop->getId(),
1399 $startTime
1400 );
1401 $this->assertTrue( $result );
1402 }
1403
1404 /**
1405 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1406 */
1407 public function testGetKnownCurrentRevision() {
1408 $page = $this->getTestPage();
1409 /** @var Revision $rev */
1410 $rev = $page->doEditContent(
1411 new WikitextContent( __METHOD__ . 'b' ),
1412 __METHOD__ . 'b',
1413 0,
1414 false,
1415 $this->getTestUser()->getUser()
1416 )->value['revision'];
1417
1418 $store = MediaWikiServices::getInstance()->getRevisionStore();
1419 $record = $store->getKnownCurrentRevision(
1420 $page->getTitle(),
1421 $rev->getId()
1422 );
1423
1424 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1425 }
1426
1427 public function provideNewMutableRevisionFromArray() {
1428 yield 'Basic array, content object' => [
1429 [
1430 'id' => 2,
1431 'page' => 1,
1432 'timestamp' => '20171017114835',
1433 'user_text' => '111.0.1.2',
1434 'user' => 0,
1435 'minor_edit' => false,
1436 'deleted' => 0,
1437 'len' => 46,
1438 'parent_id' => 1,
1439 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1440 'comment' => 'Goat Comment!',
1441 'content' => new WikitextContent( 'Some Content' ),
1442 ]
1443 ];
1444 yield 'Basic array, serialized text' => [
1445 [
1446 'id' => 2,
1447 'page' => 1,
1448 'timestamp' => '20171017114835',
1449 'user_text' => '111.0.1.2',
1450 'user' => 0,
1451 'minor_edit' => false,
1452 'deleted' => 0,
1453 'len' => 46,
1454 'parent_id' => 1,
1455 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1456 'comment' => 'Goat Comment!',
1457 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1458 ]
1459 ];
1460 yield 'Basic array, serialized text, utf-8 flags' => [
1461 [
1462 'id' => 2,
1463 'page' => 1,
1464 'timestamp' => '20171017114835',
1465 'user_text' => '111.0.1.2',
1466 'user' => 0,
1467 'minor_edit' => false,
1468 'deleted' => 0,
1469 'len' => 46,
1470 'parent_id' => 1,
1471 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1472 'comment' => 'Goat Comment!',
1473 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1474 'flags' => 'utf-8',
1475 ]
1476 ];
1477 yield 'Basic array, with title' => [
1478 [
1479 'title' => Title::newFromText( 'SomeText' ),
1480 'timestamp' => '20171017114835',
1481 'user_text' => '111.0.1.2',
1482 'user' => 0,
1483 'minor_edit' => false,
1484 'deleted' => 0,
1485 'len' => 46,
1486 'parent_id' => 1,
1487 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1488 'comment' => 'Goat Comment!',
1489 'content' => new WikitextContent( 'Some Content' ),
1490 ]
1491 ];
1492 yield 'Basic array, no user field' => [
1493 [
1494 'id' => 2,
1495 'page' => 1,
1496 'timestamp' => '20171017114835',
1497 'user_text' => '111.0.1.3',
1498 'minor_edit' => false,
1499 'deleted' => 0,
1500 'len' => 46,
1501 'parent_id' => 1,
1502 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1503 'comment' => 'Goat Comment!',
1504 'content' => new WikitextContent( 'Some Content' ),
1505 ]
1506 ];
1507 }
1508
1509 /**
1510 * @dataProvider provideNewMutableRevisionFromArray
1511 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1512 */
1513 public function testNewMutableRevisionFromArray( array $array ) {
1514 $store = MediaWikiServices::getInstance()->getRevisionStore();
1515
1516 // HACK: if $array['page'] is given, make sure that the page exists
1517 if ( isset( $array['page'] ) ) {
1518 $t = Title::newFromID( $array['page'] );
1519 if ( !$t || !$t->exists() ) {
1520 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1521 $info = $this->insertPage( $t );
1522 $array['page'] = $info['id'];
1523 }
1524 }
1525
1526 $result = $store->newMutableRevisionFromArray( $array );
1527
1528 if ( isset( $array['id'] ) ) {
1529 $this->assertSame( $array['id'], $result->getId() );
1530 }
1531 if ( isset( $array['page'] ) ) {
1532 $this->assertSame( $array['page'], $result->getPageId() );
1533 }
1534 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1535 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1536 if ( isset( $array['user'] ) ) {
1537 $this->assertSame( $array['user'], $result->getUser()->getId() );
1538 }
1539 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1540 $this->assertSame( $array['deleted'], $result->getVisibility() );
1541 $this->assertSame( $array['len'], $result->getSize() );
1542 $this->assertSame( $array['parent_id'], $result->getParentId() );
1543 $this->assertSame( $array['sha1'], $result->getSha1() );
1544 $this->assertSame( $array['comment'], $result->getComment()->text );
1545 if ( isset( $array['content'] ) ) {
1546 foreach ( $array['content'] as $role => $content ) {
1547 $this->assertTrue(
1548 $result->getContent( $role )->equals( $content )
1549 );
1550 }
1551 } elseif ( isset( $array['text'] ) ) {
1552 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1553 } elseif ( isset( $array['content_format'] ) ) {
1554 $this->assertSame(
1555 $array['content_format'],
1556 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1557 );
1558 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1559 }
1560 }
1561
1562 /**
1563 * @dataProvider provideNewMutableRevisionFromArray
1564 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1565 */
1566 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1567 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1568 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1569 $blobStore = new SqlBlobStore( $lb, $cache );
1570 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1571
1572 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1573 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1574 ->disableOriginalConstructor()
1575 ->getMock();
1576 $factory->expects( $this->any() )
1577 ->method( 'newBlobStore' )
1578 ->willReturn( $blobStore );
1579 $factory->expects( $this->any() )
1580 ->method( 'newSqlBlobStore' )
1581 ->willReturn( $blobStore );
1582
1583 $this->setService( 'BlobStoreFactory', $factory );
1584
1585 $this->testNewMutableRevisionFromArray( $array );
1586 }
1587
1588 }