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