Merge "Expand special page aliases for Serbian"
[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->assertArrayEqualsIgnoringIntKeyOrder( $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 // FIXME: fails under postgres
398 $this->markTestSkippedIfDbType( 'postgres' );
399
400 $title = $this->getTestPageTitle();
401 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
402
403 $this->overrideMwServices();
404 $store = MediaWikiServices::getInstance()->getRevisionStore();
405 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
406
407 // is the new revision correct?
408 $this->assertRevisionCompleteness( $return );
409 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
410 $this->assertRevisionRecordsEqual( $rev, $return );
411
412 // can we load it from the store?
413 $loaded = $store->getRevisionById( $return->getId() );
414 $this->assertRevisionCompleteness( $loaded );
415 $this->assertRevisionRecordsEqual( $return, $loaded );
416
417 // can we find it directly in the database?
418 $this->assertRevisionExistsInDatabase( $return );
419 }
420
421 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
422 $row = $this->revisionToRow( new Revision( $rev ), [] );
423
424 // unset nulled fields
425 unset( $row->rev_content_model );
426 unset( $row->rev_content_format );
427
428 // unset fake fields
429 unset( $row->rev_comment_text );
430 unset( $row->rev_comment_data );
431 unset( $row->rev_comment_cid );
432 unset( $row->rev_comment_id );
433
434 $store = MediaWikiServices::getInstance()->getRevisionStore();
435 $queryInfo = $store->getQueryInfo( [ 'user' ] );
436
437 $row = get_object_vars( $row );
438 $this->assertSelect(
439 $queryInfo['tables'],
440 array_keys( $row ),
441 [ 'rev_id' => $rev->getId() ],
442 [ array_values( $row ) ],
443 [],
444 $queryInfo['joins']
445 );
446 }
447
448 /**
449 * @param SlotRecord $a
450 * @param SlotRecord $b
451 */
452 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
453 // Assert that the same blob address has been used.
454 $this->assertSame( $a->getAddress(), $b->getAddress() );
455 }
456
457 /**
458 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
459 */
460 public function testInsertRevisionOn_blobAddressExists() {
461 $title = $this->getTestPageTitle();
462 $revDetails = [
463 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
464 'parent' => true,
465 'comment' => $this->getRandomCommentStoreComment(),
466 'timestamp' => '20171117010101',
467 'user' => true,
468 ];
469
470 $this->overrideMwServices();
471 $store = MediaWikiServices::getInstance()->getRevisionStore();
472
473 // Insert the first revision
474 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
475 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
476 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
477 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
478
479 // Insert a second revision inheriting the same blob address
480 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) );
481 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
482 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
483 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
484 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
485
486 $firstMainSlot = $firstReturn->getSlot( 'main' );
487 $secondMainSlot = $secondReturn->getSlot( 'main' );
488
489 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
490
491 // And that different revisions have been created.
492 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
493
494 // Make sure the slot rows reference the correct revision
495 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
496 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
497 }
498
499 public function provideInsertRevisionOn_failures() {
500 yield 'no slot' => [
501 [
502 'comment' => $this->getRandomCommentStoreComment(),
503 'timestamp' => '20171117010101',
504 'user' => true,
505 ],
506 new InvalidArgumentException( 'main slot must be provided' )
507 ];
508 yield 'no main slot' => [
509 [
510 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
511 'comment' => $this->getRandomCommentStoreComment(),
512 'timestamp' => '20171117010101',
513 'user' => true,
514 ],
515 new InvalidArgumentException( 'main slot must be provided' )
516 ];
517 yield 'no timestamp' => [
518 [
519 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
520 'comment' => $this->getRandomCommentStoreComment(),
521 'user' => true,
522 ],
523 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
524 ];
525 yield 'no comment' => [
526 [
527 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
528 'timestamp' => '20171117010101',
529 'user' => true,
530 ],
531 new IncompleteRevisionException( 'comment must not be NULL!' )
532 ];
533 yield 'no user' => [
534 [
535 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
536 'comment' => $this->getRandomCommentStoreComment(),
537 'timestamp' => '20171117010101',
538 ],
539 new IncompleteRevisionException( 'user must not be NULL!' )
540 ];
541 }
542
543 /**
544 * @dataProvider provideInsertRevisionOn_failures
545 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
546 */
547 public function testInsertRevisionOn_failures(
548 array $revDetails = [],
549 Exception $exception
550 ) {
551 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
552
553 $store = MediaWikiServices::getInstance()->getRevisionStore();
554
555 $this->setExpectedException(
556 get_class( $exception ),
557 $exception->getMessage(),
558 $exception->getCode()
559 );
560 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
561 }
562
563 public function provideNewNullRevision() {
564 yield [
565 Title::newFromText( 'UTPage_notAutoCreated' ),
566 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
567 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
568 true,
569 ];
570 yield [
571 Title::newFromText( 'UTPage_notAutoCreated' ),
572 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
573 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
574 false,
575 ];
576 }
577
578 /**
579 * @dataProvider provideNewNullRevision
580 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
581 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
582 */
583 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
584 $this->overrideMwServices();
585
586 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
587 $page = WikiPage::factory( $title );
588
589 if ( !$page->exists() ) {
590 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
591 }
592
593 $revDetails['page'] = $page->getId();
594 $revDetails['timestamp'] = wfTimestampNow();
595 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
596 $revDetails['user'] = $user;
597
598 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
599 $store = MediaWikiServices::getInstance()->getRevisionStore();
600
601 $dbw = wfGetDB( DB_MASTER );
602 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
603 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
604
605 $record = $store->newNullRevision(
606 wfGetDB( DB_MASTER ),
607 $title,
608 $comment,
609 $minor,
610 $user
611 );
612
613 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
614 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
615 $this->assertEquals( $comment, $record->getComment() );
616 $this->assertEquals( $minor, $record->isMinor() );
617 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
618 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
619
620 $this->assertArrayEqualsIgnoringIntKeyOrder(
621 $baseRev->getSlotRoles(),
622 $record->getSlotRoles()
623 );
624
625 foreach ( $baseRev->getSlotRoles() as $role ) {
626 $parentSlot = $baseRev->getSlot( $role );
627 $slot = $record->getSlot( $role );
628
629 $this->assertTrue( $slot->isInherited(), 'isInherited' );
630 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
631 $this->assertSameSlotContent( $parentSlot, $slot );
632 }
633 }
634
635 /**
636 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
637 */
638 public function testNewNullRevision_nonExistingTitle() {
639 $store = MediaWikiServices::getInstance()->getRevisionStore();
640 $record = $store->newNullRevision(
641 wfGetDB( DB_MASTER ),
642 Title::newFromText( __METHOD__ . '.iDontExist!' ),
643 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
644 false,
645 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
646 );
647 $this->assertNull( $record );
648 }
649
650 /**
651 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
652 */
653 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
654 $page = $this->getTestPage();
655 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
656 /** @var Revision $rev */
657 $rev = $status->value['revision'];
658
659 $store = MediaWikiServices::getInstance()->getRevisionStore();
660 $revisionRecord = $store->getRevisionById( $rev->getId() );
661 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
662
663 $this->assertGreaterThan( 0, $result );
664 $this->assertSame(
665 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
666 $result
667 );
668 }
669
670 /**
671 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
672 */
673 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
674 // This assumes that sysops are auto patrolled
675 $sysop = $this->getTestSysop()->getUser();
676 $page = $this->getTestPage();
677 $status = $page->doEditContent(
678 new WikitextContent( __METHOD__ ),
679 __METHOD__,
680 0,
681 false,
682 $sysop
683 );
684 /** @var Revision $rev */
685 $rev = $status->value['revision'];
686
687 $store = MediaWikiServices::getInstance()->getRevisionStore();
688 $revisionRecord = $store->getRevisionById( $rev->getId() );
689 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
690
691 $this->assertSame( 0, $result );
692 }
693
694 /**
695 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
696 */
697 public function testGetRecentChange() {
698 $page = $this->getTestPage();
699 $content = new WikitextContent( __METHOD__ );
700 $status = $page->doEditContent( $content, __METHOD__ );
701 /** @var Revision $rev */
702 $rev = $status->value['revision'];
703
704 $store = MediaWikiServices::getInstance()->getRevisionStore();
705 $revRecord = $store->getRevisionById( $rev->getId() );
706 $recentChange = $store->getRecentChange( $revRecord );
707
708 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
709 $this->assertEquals( $rev->getRecentChange(), $recentChange );
710 }
711
712 /**
713 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
714 */
715 public function testGetRevisionById() {
716 $page = $this->getTestPage();
717 $content = new WikitextContent( __METHOD__ );
718 $status = $page->doEditContent( $content, __METHOD__ );
719 /** @var Revision $rev */
720 $rev = $status->value['revision'];
721
722 $store = MediaWikiServices::getInstance()->getRevisionStore();
723 $revRecord = $store->getRevisionById( $rev->getId() );
724
725 $this->assertSame( $rev->getId(), $revRecord->getId() );
726 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
727 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
728 }
729
730 /**
731 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
732 */
733 public function testGetRevisionByTitle() {
734 $page = $this->getTestPage();
735 $content = new WikitextContent( __METHOD__ );
736 $status = $page->doEditContent( $content, __METHOD__ );
737 /** @var Revision $rev */
738 $rev = $status->value['revision'];
739
740 $store = MediaWikiServices::getInstance()->getRevisionStore();
741 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
742
743 $this->assertSame( $rev->getId(), $revRecord->getId() );
744 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
745 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
746 }
747
748 /**
749 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
750 */
751 public function testGetRevisionByPageId() {
752 $page = $this->getTestPage();
753 $content = new WikitextContent( __METHOD__ );
754 $status = $page->doEditContent( $content, __METHOD__ );
755 /** @var Revision $rev */
756 $rev = $status->value['revision'];
757
758 $store = MediaWikiServices::getInstance()->getRevisionStore();
759 $revRecord = $store->getRevisionByPageId( $page->getId() );
760
761 $this->assertSame( $rev->getId(), $revRecord->getId() );
762 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
763 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
764 }
765
766 /**
767 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
768 */
769 public function testGetRevisionByTimestamp() {
770 // Make sure there is 1 second between the last revision and the rev we create...
771 // Otherwise we might not get the correct revision and the test may fail...
772 // :(
773 $page = $this->getTestPage();
774 sleep( 1 );
775 $content = new WikitextContent( __METHOD__ );
776 $status = $page->doEditContent( $content, __METHOD__ );
777 /** @var Revision $rev */
778 $rev = $status->value['revision'];
779
780 $store = MediaWikiServices::getInstance()->getRevisionStore();
781 $revRecord = $store->getRevisionByTimestamp(
782 $page->getTitle(),
783 $rev->getTimestamp()
784 );
785
786 $this->assertSame( $rev->getId(), $revRecord->getId() );
787 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
788 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
789 }
790
791 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
792 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
793 $page = WikiPage::factory( $rev->getTitle() );
794
795 $fields = [
796 'rev_id' => (string)$rev->getId(),
797 'rev_page' => (string)$rev->getPage(),
798 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
799 'rev_user_text' => (string)$rev->getUserText(),
800 'rev_user' => (string)$rev->getUser(),
801 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
802 'rev_deleted' => (string)$rev->getVisibility(),
803 'rev_len' => (string)$rev->getSize(),
804 'rev_parent_id' => (string)$rev->getParentId(),
805 'rev_sha1' => (string)$rev->getSha1(),
806 ];
807
808 if ( in_array( 'page', $options ) ) {
809 $fields += [
810 'page_namespace' => (string)$page->getTitle()->getNamespace(),
811 'page_title' => $page->getTitle()->getDBkey(),
812 'page_id' => (string)$page->getId(),
813 'page_latest' => (string)$page->getLatest(),
814 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
815 'page_len' => (string)$page->getContent()->getSize(),
816 ];
817 }
818
819 if ( in_array( 'user', $options ) ) {
820 $fields += [
821 'user_name' => (string)$rev->getUserText(),
822 ];
823 }
824
825 if ( in_array( 'comment', $options ) ) {
826 $fields += [
827 'rev_comment_text' => $rev->getComment(),
828 'rev_comment_data' => null,
829 'rev_comment_cid' => null,
830 ];
831 }
832
833 if ( $rev->getId() ) {
834 $fields += [
835 'rev_id' => (string)$rev->getId(),
836 ];
837 }
838
839 return (object)$fields;
840 }
841
842 private function assertRevisionRecordMatchesRevision(
843 Revision $rev,
844 RevisionRecord $record
845 ) {
846 $this->assertSame( $rev->getId(), $record->getId() );
847 $this->assertSame( $rev->getPage(), $record->getPageId() );
848 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
849 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
850 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
851 $this->assertSame( $rev->isMinor(), $record->isMinor() );
852 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
853 $this->assertSame( $rev->getSize(), $record->getSize() );
854 /**
855 * @note As of MW 1.31, the database schema allows the parent ID to be
856 * NULL to indicate that it is unknown.
857 */
858 $expectedParent = $rev->getParentId();
859 if ( $expectedParent === null ) {
860 $expectedParent = 0;
861 }
862 $this->assertSame( $expectedParent, $record->getParentId() );
863 $this->assertSame( $rev->getSha1(), $record->getSha1() );
864 $this->assertSame( $rev->getComment(), $record->getComment()->text );
865 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
866 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
867 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
868
869 $revRec = $rev->getRevisionRecord();
870 $revMain = $revRec->getSlot( 'main' );
871 $recMain = $record->getSlot( 'main' );
872
873 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
874 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
875 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
876
877 if ( $revMain->hasOrigin() ) {
878 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
879 }
880
881 if ( $revMain->hasAddress() ) {
882 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
883 }
884
885 if ( $revMain->hasContentId() ) {
886 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
887 }
888 }
889
890 /**
891 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
892 */
893 public function testNewRevisionFromRow_anonEdit() {
894 $page = $this->getTestPage();
895 $text = __METHOD__ . 'a-ä';
896 /** @var Revision $rev */
897 $rev = $page->doEditContent(
898 new WikitextContent( $text ),
899 __METHOD__ . 'a'
900 )->value['revision'];
901
902 $store = MediaWikiServices::getInstance()->getRevisionStore();
903 $record = $store->newRevisionFromRow(
904 $this->revisionToRow( $rev ),
905 [],
906 $page->getTitle()
907 );
908 $this->assertRevisionRecordMatchesRevision( $rev, $record );
909 $this->assertSame( $text, $rev->getContent()->serialize() );
910 }
911
912 /**
913 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
914 */
915 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
916 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
917 $this->overrideMwServices();
918 $page = $this->getTestPage();
919 $text = __METHOD__ . 'a-ä';
920 /** @var Revision $rev */
921 $rev = $page->doEditContent(
922 new WikitextContent( $text ),
923 __METHOD__. 'a'
924 )->value['revision'];
925
926 $store = MediaWikiServices::getInstance()->getRevisionStore();
927 $record = $store->newRevisionFromRow(
928 $this->revisionToRow( $rev ),
929 [],
930 $page->getTitle()
931 );
932 $this->assertRevisionRecordMatchesRevision( $rev, $record );
933 $this->assertSame( $text, $rev->getContent()->serialize() );
934 }
935
936 /**
937 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
938 */
939 public function testNewRevisionFromRow_userEdit() {
940 $page = $this->getTestPage();
941 $text = __METHOD__ . 'b-ä';
942 /** @var Revision $rev */
943 $rev = $page->doEditContent(
944 new WikitextContent( $text ),
945 __METHOD__ . 'b',
946 0,
947 false,
948 $this->getTestUser()->getUser()
949 )->value['revision'];
950
951 $store = MediaWikiServices::getInstance()->getRevisionStore();
952 $record = $store->newRevisionFromRow(
953 $this->revisionToRow( $rev ),
954 [],
955 $page->getTitle()
956 );
957 $this->assertRevisionRecordMatchesRevision( $rev, $record );
958 $this->assertSame( $text, $rev->getContent()->serialize() );
959 }
960
961 /**
962 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
963 */
964 public function testNewRevisionFromArchiveRow() {
965 $store = MediaWikiServices::getInstance()->getRevisionStore();
966 $title = Title::newFromText( __METHOD__ );
967 $text = __METHOD__ . '-bä';
968 $page = WikiPage::factory( $title );
969 /** @var Revision $orig */
970 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
971 ->value['revision'];
972 $page->doDeleteArticle( __METHOD__ );
973
974 $db = wfGetDB( DB_MASTER );
975 $arQuery = $store->getArchiveQueryInfo();
976 $res = $db->select(
977 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
978 __METHOD__, [], $arQuery['joins']
979 );
980 $this->assertTrue( is_object( $res ), 'query failed' );
981
982 $row = $res->fetchObject();
983 $res->free();
984 $record = $store->newRevisionFromArchiveRow( $row );
985
986 $this->assertRevisionRecordMatchesRevision( $orig, $record );
987 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
988 }
989
990 /**
991 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
992 */
993 public function testNewRevisionFromArchiveRow_legacyEncoding() {
994 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
995 $this->overrideMwServices();
996 $store = MediaWikiServices::getInstance()->getRevisionStore();
997 $title = Title::newFromText( __METHOD__ );
998 $text = __METHOD__ . '-bä';
999 $page = WikiPage::factory( $title );
1000 /** @var Revision $orig */
1001 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1002 ->value['revision'];
1003 $page->doDeleteArticle( __METHOD__ );
1004
1005 $db = wfGetDB( DB_MASTER );
1006 $arQuery = $store->getArchiveQueryInfo();
1007 $res = $db->select(
1008 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1009 __METHOD__, [], $arQuery['joins']
1010 );
1011 $this->assertTrue( is_object( $res ), 'query failed' );
1012
1013 $row = $res->fetchObject();
1014 $res->free();
1015 $record = $store->newRevisionFromArchiveRow( $row );
1016
1017 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1018 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1019 }
1020
1021 /**
1022 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1023 */
1024 public function testInsertRevisionOn_archive() {
1025 $store = MediaWikiServices::getInstance()->getRevisionStore();
1026 $title = Title::newFromText( __METHOD__ );
1027
1028 $page = WikiPage::factory( $title );
1029 /** @var Revision $origRev */
1030 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1031 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1032 ->value['revision'];
1033 $orig = $origRev->getRevisionRecord();
1034 $page->doDeleteArticle( __METHOD__ );
1035
1036 $db = wfGetDB( DB_MASTER );
1037 $arQuery = $store->getArchiveQueryInfo();
1038 $row = $db->selectRow(
1039 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1040 __METHOD__, [], $arQuery['joins']
1041 );
1042
1043 $record = $store->newRevisionFromArchiveRow( $row );
1044
1045 $restored = $store->insertRevisionOn( $record, $db );
1046 $this->assertSame( $orig->getPageId(), $restored->getPageId() );
1047 $this->assertSame( $orig->getId(), $restored->getId() );
1048 $this->assertSame( $orig->getComment()->text, $restored->getComment()->text );
1049
1050 $origMain = $orig->getSlot( 'main' );
1051 $restoredMain = $restored->getSlot( 'main' );
1052 $this->assertSame(
1053 $origMain->getOrigin(),
1054 $restoredMain->getOrigin()
1055 );
1056
1057 if ( $origMain->hasContentId() ) {
1058 $this->assertSame(
1059 $origMain->getContentId(),
1060 $restoredMain->getContentId()
1061 );
1062 }
1063
1064 // NOTE: we didn't restore the page row, so we can't use RevisionStore::getRevisionById
1065 $this->assertSelect(
1066 'revision',
1067 [ 'rev_id' ],
1068 [ 'rev_id' => $orig->getId() ],
1069 [ [ $orig->getId() ] ]
1070 );
1071 }
1072
1073 /**
1074 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1075 */
1076 public function testLoadRevisionFromId() {
1077 $title = Title::newFromText( __METHOD__ );
1078 $page = WikiPage::factory( $title );
1079 /** @var Revision $rev */
1080 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1081 ->value['revision'];
1082
1083 $store = MediaWikiServices::getInstance()->getRevisionStore();
1084 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1085 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1086 }
1087
1088 /**
1089 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1090 */
1091 public function testLoadRevisionFromPageId() {
1092 $title = Title::newFromText( __METHOD__ );
1093 $page = WikiPage::factory( $title );
1094 /** @var Revision $rev */
1095 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1096 ->value['revision'];
1097
1098 $store = MediaWikiServices::getInstance()->getRevisionStore();
1099 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1100 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1101 }
1102
1103 /**
1104 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1105 */
1106 public function testLoadRevisionFromTitle() {
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->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1115 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1116 }
1117
1118 /**
1119 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1120 */
1121 public function testLoadRevisionFromTimestamp() {
1122 $title = Title::newFromText( __METHOD__ );
1123 $page = WikiPage::factory( $title );
1124 /** @var Revision $revOne */
1125 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1126 ->value['revision'];
1127 // Sleep to ensure different timestamps... )(evil)
1128 sleep( 1 );
1129 /** @var Revision $revTwo */
1130 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1131 ->value['revision'];
1132
1133 $store = MediaWikiServices::getInstance()->getRevisionStore();
1134 $this->assertNull(
1135 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1136 );
1137 $this->assertSame(
1138 $revOne->getId(),
1139 $store->loadRevisionFromTimestamp(
1140 wfGetDB( DB_MASTER ),
1141 $title,
1142 $revOne->getTimestamp()
1143 )->getId()
1144 );
1145 $this->assertSame(
1146 $revTwo->getId(),
1147 $store->loadRevisionFromTimestamp(
1148 wfGetDB( DB_MASTER ),
1149 $title,
1150 $revTwo->getTimestamp()
1151 )->getId()
1152 );
1153 }
1154
1155 /**
1156 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1157 */
1158 public function testGetParentLengths() {
1159 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1160 /** @var Revision $revOne */
1161 $revOne = $page->doEditContent(
1162 new WikitextContent( __METHOD__ ), __METHOD__
1163 )->value['revision'];
1164 /** @var Revision $revTwo */
1165 $revTwo = $page->doEditContent(
1166 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1167 )->value['revision'];
1168
1169 $store = MediaWikiServices::getInstance()->getRevisionStore();
1170 $this->assertSame(
1171 [
1172 $revOne->getId() => strlen( __METHOD__ ),
1173 ],
1174 $store->listRevisionSizes(
1175 wfGetDB( DB_MASTER ),
1176 [ $revOne->getId() ]
1177 )
1178 );
1179 $this->assertSame(
1180 [
1181 $revOne->getId() => strlen( __METHOD__ ),
1182 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1183 ],
1184 $store->listRevisionSizes(
1185 wfGetDB( DB_MASTER ),
1186 [ $revOne->getId(), $revTwo->getId() ]
1187 )
1188 );
1189 }
1190
1191 /**
1192 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1193 */
1194 public function testGetPreviousRevision() {
1195 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1196 /** @var Revision $revOne */
1197 $revOne = $page->doEditContent(
1198 new WikitextContent( __METHOD__ ), __METHOD__
1199 )->value['revision'];
1200 /** @var Revision $revTwo */
1201 $revTwo = $page->doEditContent(
1202 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1203 )->value['revision'];
1204
1205 $store = MediaWikiServices::getInstance()->getRevisionStore();
1206 $this->assertNull(
1207 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1208 );
1209 $this->assertSame(
1210 $revOne->getId(),
1211 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1212 );
1213 }
1214
1215 /**
1216 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1217 */
1218 public function testGetNextRevision() {
1219 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1220 /** @var Revision $revOne */
1221 $revOne = $page->doEditContent(
1222 new WikitextContent( __METHOD__ ), __METHOD__
1223 )->value['revision'];
1224 /** @var Revision $revTwo */
1225 $revTwo = $page->doEditContent(
1226 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1227 )->value['revision'];
1228
1229 $store = MediaWikiServices::getInstance()->getRevisionStore();
1230 $this->assertSame(
1231 $revTwo->getId(),
1232 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1233 );
1234 $this->assertNull(
1235 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1236 );
1237 }
1238
1239 /**
1240 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1241 */
1242 public function testGetTimestampFromId_found() {
1243 $page = $this->getTestPage();
1244 /** @var Revision $rev */
1245 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1246 ->value['revision'];
1247
1248 $store = MediaWikiServices::getInstance()->getRevisionStore();
1249 $result = $store->getTimestampFromId(
1250 $page->getTitle(),
1251 $rev->getId()
1252 );
1253
1254 $this->assertSame( $rev->getTimestamp(), $result );
1255 }
1256
1257 /**
1258 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1259 */
1260 public function testGetTimestampFromId_notFound() {
1261 $page = $this->getTestPage();
1262 /** @var Revision $rev */
1263 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1264 ->value['revision'];
1265
1266 $store = MediaWikiServices::getInstance()->getRevisionStore();
1267 $result = $store->getTimestampFromId(
1268 $page->getTitle(),
1269 $rev->getId() + 1
1270 );
1271
1272 $this->assertFalse( $result );
1273 }
1274
1275 /**
1276 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1277 */
1278 public function testCountRevisionsByPageId() {
1279 $store = MediaWikiServices::getInstance()->getRevisionStore();
1280 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1281
1282 $this->assertSame(
1283 0,
1284 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1285 );
1286 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1287 $this->assertSame(
1288 1,
1289 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1290 );
1291 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1292 $this->assertSame(
1293 2,
1294 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1295 );
1296 }
1297
1298 /**
1299 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1300 */
1301 public function testCountRevisionsByTitle() {
1302 $store = MediaWikiServices::getInstance()->getRevisionStore();
1303 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1304
1305 $this->assertSame(
1306 0,
1307 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1308 );
1309 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1310 $this->assertSame(
1311 1,
1312 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1313 );
1314 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1315 $this->assertSame(
1316 2,
1317 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1318 );
1319 }
1320
1321 /**
1322 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1323 */
1324 public function testUserWasLastToEdit_false() {
1325 $sysop = $this->getTestSysop()->getUser();
1326 $page = $this->getTestPage();
1327 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1328
1329 $store = MediaWikiServices::getInstance()->getRevisionStore();
1330 $result = $store->userWasLastToEdit(
1331 wfGetDB( DB_MASTER ),
1332 $page->getId(),
1333 $sysop->getId(),
1334 '20160101010101'
1335 );
1336 $this->assertFalse( $result );
1337 }
1338
1339 /**
1340 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1341 */
1342 public function testUserWasLastToEdit_true() {
1343 $startTime = wfTimestampNow();
1344 $sysop = $this->getTestSysop()->getUser();
1345 $page = $this->getTestPage();
1346 $page->doEditContent(
1347 new WikitextContent( __METHOD__ ),
1348 __METHOD__,
1349 0,
1350 false,
1351 $sysop
1352 );
1353
1354 $store = MediaWikiServices::getInstance()->getRevisionStore();
1355 $result = $store->userWasLastToEdit(
1356 wfGetDB( DB_MASTER ),
1357 $page->getId(),
1358 $sysop->getId(),
1359 $startTime
1360 );
1361 $this->assertTrue( $result );
1362 }
1363
1364 /**
1365 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1366 */
1367 public function testGetKnownCurrentRevision() {
1368 $page = $this->getTestPage();
1369 /** @var Revision $rev */
1370 $rev = $page->doEditContent(
1371 new WikitextContent( __METHOD__ . 'b' ),
1372 __METHOD__ . 'b',
1373 0,
1374 false,
1375 $this->getTestUser()->getUser()
1376 )->value['revision'];
1377
1378 $store = MediaWikiServices::getInstance()->getRevisionStore();
1379 $record = $store->getKnownCurrentRevision(
1380 $page->getTitle(),
1381 $rev->getId()
1382 );
1383
1384 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1385 }
1386
1387 public function provideNewMutableRevisionFromArray() {
1388 yield 'Basic array, content object' => [
1389 [
1390 'id' => 2,
1391 'page' => 1,
1392 'timestamp' => '20171017114835',
1393 'user_text' => '111.0.1.2',
1394 'user' => 0,
1395 'minor_edit' => false,
1396 'deleted' => 0,
1397 'len' => 46,
1398 'parent_id' => 1,
1399 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1400 'comment' => 'Goat Comment!',
1401 'content' => new WikitextContent( 'Some Content' ),
1402 ]
1403 ];
1404 yield 'Basic array, serialized text' => [
1405 [
1406 'id' => 2,
1407 'page' => 1,
1408 'timestamp' => '20171017114835',
1409 'user_text' => '111.0.1.2',
1410 'user' => 0,
1411 'minor_edit' => false,
1412 'deleted' => 0,
1413 'len' => 46,
1414 'parent_id' => 1,
1415 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1416 'comment' => 'Goat Comment!',
1417 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1418 ]
1419 ];
1420 yield 'Basic array, serialized text, utf-8 flags' => [
1421 [
1422 'id' => 2,
1423 'page' => 1,
1424 'timestamp' => '20171017114835',
1425 'user_text' => '111.0.1.2',
1426 'user' => 0,
1427 'minor_edit' => false,
1428 'deleted' => 0,
1429 'len' => 46,
1430 'parent_id' => 1,
1431 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1432 'comment' => 'Goat Comment!',
1433 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1434 'flags' => 'utf-8',
1435 ]
1436 ];
1437 yield 'Basic array, with title' => [
1438 [
1439 'title' => Title::newFromText( 'SomeText' ),
1440 'timestamp' => '20171017114835',
1441 'user_text' => '111.0.1.2',
1442 'user' => 0,
1443 'minor_edit' => false,
1444 'deleted' => 0,
1445 'len' => 46,
1446 'parent_id' => 1,
1447 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1448 'comment' => 'Goat Comment!',
1449 'content' => new WikitextContent( 'Some Content' ),
1450 ]
1451 ];
1452 yield 'Basic array, no user field' => [
1453 [
1454 'id' => 2,
1455 'page' => 1,
1456 'timestamp' => '20171017114835',
1457 'user_text' => '111.0.1.3',
1458 'minor_edit' => false,
1459 'deleted' => 0,
1460 'len' => 46,
1461 'parent_id' => 1,
1462 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1463 'comment' => 'Goat Comment!',
1464 'content' => new WikitextContent( 'Some Content' ),
1465 ]
1466 ];
1467 }
1468
1469 /**
1470 * @dataProvider provideNewMutableRevisionFromArray
1471 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1472 */
1473 public function testNewMutableRevisionFromArray( array $array ) {
1474 $store = MediaWikiServices::getInstance()->getRevisionStore();
1475
1476 // HACK: if $array['page'] is given, make sure that the page exists
1477 if ( isset( $array['page'] ) ) {
1478 $t = Title::newFromID( $array['page'] );
1479 if ( !$t || !$t->exists() ) {
1480 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1481 $info = $this->insertPage( $t );
1482 $array['page'] = $info['id'];
1483 }
1484 }
1485
1486 $result = $store->newMutableRevisionFromArray( $array );
1487
1488 if ( isset( $array['id'] ) ) {
1489 $this->assertSame( $array['id'], $result->getId() );
1490 }
1491 if ( isset( $array['page'] ) ) {
1492 $this->assertSame( $array['page'], $result->getPageId() );
1493 }
1494 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1495 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1496 if ( isset( $array['user'] ) ) {
1497 $this->assertSame( $array['user'], $result->getUser()->getId() );
1498 }
1499 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1500 $this->assertSame( $array['deleted'], $result->getVisibility() );
1501 $this->assertSame( $array['len'], $result->getSize() );
1502 $this->assertSame( $array['parent_id'], $result->getParentId() );
1503 $this->assertSame( $array['sha1'], $result->getSha1() );
1504 $this->assertSame( $array['comment'], $result->getComment()->text );
1505 if ( isset( $array['content'] ) ) {
1506 foreach ( $array['content'] as $role => $content ) {
1507 $this->assertTrue(
1508 $result->getContent( $role )->equals( $content )
1509 );
1510 }
1511 } elseif ( isset( $array['text'] ) ) {
1512 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1513 } elseif ( isset( $array['content_format'] ) ) {
1514 $this->assertSame(
1515 $array['content_format'],
1516 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1517 );
1518 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1519 }
1520 }
1521
1522 /**
1523 * @dataProvider provideNewMutableRevisionFromArray
1524 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1525 */
1526 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1527 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1528 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1529 $blobStore = new SqlBlobStore( $lb, $cache );
1530 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1531
1532 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1533 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1534 ->disableOriginalConstructor()
1535 ->getMock();
1536 $factory->expects( $this->any() )
1537 ->method( 'newBlobStore' )
1538 ->willReturn( $blobStore );
1539 $factory->expects( $this->any() )
1540 ->method( 'newSqlBlobStore' )
1541 ->willReturn( $blobStore );
1542
1543 $this->setService( 'BlobStoreFactory', $factory );
1544
1545 $this->testNewMutableRevisionFromArray( $array );
1546 }
1547
1548 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1549 $fields = [
1550 'rev_id',
1551 'rev_page',
1552 'rev_timestamp',
1553 'rev_minor_edit',
1554 'rev_deleted',
1555 'rev_len',
1556 'rev_parent_id',
1557 'rev_sha1',
1558 ];
1559 if ( $returnTextIdField ) {
1560 $fields[] = 'rev_text_id';
1561 }
1562 return $fields;
1563 }
1564
1565 protected function getCommentQueryFields() {
1566 return [
1567 'rev_comment_text' => 'rev_comment',
1568 'rev_comment_data' => 'NULL',
1569 'rev_comment_cid' => 'NULL',
1570 ];
1571 }
1572
1573 protected function getActorQueryFields() {
1574 return [
1575 'rev_user' => 'rev_user',
1576 'rev_user_text' => 'rev_user_text',
1577 'rev_actor' => 'NULL',
1578 ];
1579 }
1580
1581 protected function getContentHandlerQueryFields() {
1582 return [
1583 'rev_content_format',
1584 'rev_content_model',
1585 ];
1586 }
1587
1588 abstract public function provideGetQueryInfo();
1589
1590 /**
1591 * @dataProvider provideGetQueryInfo
1592 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1593 */
1594 public function testGetQueryInfo( $options, $expected ) {
1595 $store = MediaWikiServices::getInstance()->getRevisionStore();
1596
1597 $queryInfo = $store->getQueryInfo( $options );
1598
1599 $this->assertArrayEqualsIgnoringIntKeyOrder(
1600 $expected['tables'],
1601 $queryInfo['tables']
1602 );
1603 $this->assertArrayEqualsIgnoringIntKeyOrder(
1604 $expected['fields'],
1605 $queryInfo['fields']
1606 );
1607 $this->assertArrayEqualsIgnoringIntKeyOrder(
1608 $expected['joins'],
1609 $queryInfo['joins']
1610 );
1611 }
1612
1613 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1614 $fields = [
1615 'ar_id',
1616 'ar_page_id',
1617 'ar_namespace',
1618 'ar_title',
1619 'ar_rev_id',
1620 'ar_timestamp',
1621 'ar_minor_edit',
1622 'ar_deleted',
1623 'ar_len',
1624 'ar_parent_id',
1625 'ar_sha1',
1626 ];
1627 if ( $returnTextFields ) {
1628 $fields[] = 'ar_text_id';
1629 }
1630 return $fields;
1631 }
1632
1633 abstract public function provideGetArchiveQueryInfo();
1634
1635 /**
1636 * @dataProvider provideGetArchiveQueryInfo
1637 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1638 */
1639 public function testGetArchiveQueryInfo( $expected ) {
1640 $store = MediaWikiServices::getInstance()->getRevisionStore();
1641
1642 $archiveQueryInfo = $store->getArchiveQueryInfo();
1643
1644 $this->assertArrayEqualsIgnoringIntKeyOrder(
1645 $expected['tables'],
1646 $archiveQueryInfo['tables']
1647 );
1648
1649 $this->assertArrayEqualsIgnoringIntKeyOrder(
1650 $expected['fields'],
1651 $archiveQueryInfo['fields']
1652 );
1653
1654 $this->assertArrayEqualsIgnoringIntKeyOrder(
1655 $expected['joins'],
1656 $archiveQueryInfo['joins']
1657 );
1658 }
1659
1660 abstract public function provideGetSlotsQueryInfo();
1661
1662 /**
1663 * @dataProvider provideGetSlotsQueryInfo
1664 * @covers \MediaWiki\Storage\RevisionStore::getSlotsQueryInfo
1665 */
1666 public function testGetSlotsQueryInfo( $options, $expected ) {
1667 $store = MediaWikiServices::getInstance()->getRevisionStore();
1668
1669 $archiveQueryInfo = $store->getSlotsQueryInfo( $options );
1670
1671 $this->assertArrayEqualsIgnoringIntKeyOrder(
1672 $expected['tables'],
1673 $archiveQueryInfo['tables']
1674 );
1675
1676 $this->assertArrayEqualsIgnoringIntKeyOrder(
1677 $expected['fields'],
1678 $archiveQueryInfo['fields']
1679 );
1680
1681 $this->assertArrayEqualsIgnoringIntKeyOrder(
1682 $expected['joins'],
1683 $archiveQueryInfo['joins']
1684 );
1685 }
1686
1687 /**
1688 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1689 * keys.
1690 *
1691 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1692 * split into a string key array and an int key array before assertions occur.
1693 *
1694 * @param array $expected
1695 * @param array $actual
1696 */
1697 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1698 $this->objectAssociativeSort( $expected );
1699 $this->objectAssociativeSort( $actual );
1700
1701 // Separate the int key values from the string key values so that assertion failures are
1702 // easier to understand.
1703 $expectedIntKeyValues = [];
1704 $actualIntKeyValues = [];
1705
1706 // Remove all int keys and re add them at the end after sorting by value
1707 // This will result in all int keys being in the same order with same ints at the end of
1708 // the array
1709 foreach ( $expected as $key => $value ) {
1710 if ( is_int( $key ) ) {
1711 unset( $expected[$key] );
1712 $expectedIntKeyValues[] = $value;
1713 }
1714 }
1715 foreach ( $actual as $key => $value ) {
1716 if ( is_int( $key ) ) {
1717 unset( $actual[$key] );
1718 $actualIntKeyValues[] = $value;
1719 }
1720 }
1721
1722 $this->assertArrayEquals( $expected, $actual, false, true );
1723 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );
1724 }
1725
1726 }