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