6bf219dc7b1450739f7b9aeaa6af5779054cec2f
[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 ContentHandler;
8 use Exception;
9 use HashBagOStuff;
10 use IDBAccessObject;
11 use InvalidArgumentException;
12 use Language;
13 use MediaWiki\Linker\LinkTarget;
14 use MediaWiki\MediaWikiServices;
15 use MediaWiki\Revision\IncompleteRevisionException;
16 use MediaWiki\Revision\MutableRevisionRecord;
17 use MediaWiki\Revision\RevisionArchiveRecord;
18 use MediaWiki\Revision\RevisionRecord;
19 use MediaWiki\Revision\RevisionStoreRecord;
20 use MediaWiki\Revision\RevisionSlots;
21 use MediaWiki\Revision\RevisionStore;
22 use MediaWiki\Revision\SlotRecord;
23 use MediaWiki\Storage\BlobStoreFactory;
24 use MediaWiki\Storage\SqlBlobStore;
25 use MediaWiki\User\UserIdentityValue;
26 use MediaWikiTestCase;
27 use PHPUnit_Framework_MockObject_MockObject;
28 use Revision;
29 use TestUserRegistry;
30 use Title;
31 use WANObjectCache;
32 use Wikimedia\Rdbms\Database;
33 use Wikimedia\Rdbms\DatabaseDomain;
34 use Wikimedia\Rdbms\DatabaseSqlite;
35 use Wikimedia\Rdbms\FakeResultWrapper;
36 use Wikimedia\Rdbms\LoadBalancer;
37 use Wikimedia\Rdbms\TransactionProfiler;
38 use WikiPage;
39 use WikitextContent;
40
41 /**
42 * @group Database
43 * @group RevisionStore
44 */
45 abstract class RevisionStoreDbTestBase extends MediaWikiTestCase {
46
47 /**
48 * @var Title
49 */
50 private $testPageTitle;
51
52 /**
53 * @var WikiPage
54 */
55 private $testPage;
56
57 /**
58 * @return int
59 */
60 abstract protected function getMcrMigrationStage();
61
62 /**
63 * @return bool
64 */
65 protected function getContentHandlerUseDB() {
66 return true;
67 }
68
69 /**
70 * @return string[]
71 */
72 abstract protected function getMcrTablesToReset();
73
74 public function setUp() {
75 parent::setUp();
76 $this->tablesUsed[] = 'archive';
77 $this->tablesUsed[] = 'page';
78 $this->tablesUsed[] = 'revision';
79 $this->tablesUsed[] = 'comment';
80
81 $this->tablesUsed += $this->getMcrTablesToReset();
82
83 $this->setMwGlobals( [
84 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
85 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
86 ] );
87 }
88
89 protected function addCoreDBData() {
90 // Blank out. This would fail with a modified schema, and we don't need it.
91 }
92
93 /**
94 * @return Title
95 */
96 protected function getTestPageTitle() {
97 if ( $this->testPageTitle ) {
98 return $this->testPageTitle;
99 }
100
101 $this->testPageTitle = Title::newFromText( 'UTPage-' . __CLASS__ );
102 return $this->testPageTitle;
103 }
104
105 /**
106 * @param string|null $pageTitle whether to force-create a new page
107 * @return WikiPage
108 */
109 protected function getTestPage( $pageTitle = null ) {
110 if ( is_null( $pageTitle ) && $this->testPage ) {
111 return $this->testPage;
112 }
113
114 $title = is_null( $pageTitle ) ? $this->getTestPageTitle() : Title::newFromText( $pageTitle );
115 $page = WikiPage::factory( $title );
116
117 if ( !$page->exists() ) {
118 // Make sure we don't write to the live db.
119 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER ) );
120
121 $user = static::getTestSysop()->getUser();
122
123 $page->doEditContent(
124 new WikitextContent( 'UTContent-' . __CLASS__ ),
125 'UTPageSummary-' . __CLASS__,
126 EDIT_NEW | EDIT_SUPPRESS_RC,
127 false,
128 $user
129 );
130 }
131
132 if ( is_null( $pageTitle ) ) {
133 $this->testPage = $page;
134 }
135 return $page;
136 }
137
138 /**
139 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
140 */
141 private function getLoadBalancerMock( array $server ) {
142 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
143
144 $lb = $this->getMockBuilder( LoadBalancer::class )
145 ->setMethods( [ 'reallyOpenConnection' ] )
146 ->setConstructorArgs( [
147 [ 'servers' => [ $server ], 'localDomain' => $domain ]
148 ] )
149 ->getMock();
150
151 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
152 function ( array $server, $dbNameOverride ) {
153 return $this->getDatabaseMock( $server );
154 }
155 );
156
157 return $lb;
158 }
159
160 /**
161 * @return Database|PHPUnit_Framework_MockObject_MockObject
162 */
163 private function getDatabaseMock( array $params ) {
164 $db = $this->getMockBuilder( DatabaseSqlite::class )
165 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
166 ->setConstructorArgs( [ $params ] )
167 ->getMock();
168
169 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
170 $db->method( 'isOpen' )->willReturn( true );
171
172 return $db;
173 }
174
175 public function provideDomainCheck() {
176 yield [ false, 'test', '' ];
177 yield [ 'test', 'test', '' ];
178
179 yield [ false, 'test', 'foo_' ];
180 yield [ 'test-foo_', 'test', 'foo_' ];
181
182 yield [ false, 'dash-test', '' ];
183 yield [ 'dash-test', 'dash-test', '' ];
184
185 yield [ false, 'underscore_test', 'foo_' ];
186 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
187 }
188
189 /**
190 * @dataProvider provideDomainCheck
191 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseDomain
192 */
193 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
194 $this->setMwGlobals(
195 [
196 'wgDBname' => $dbName,
197 'wgDBprefix' => $dbPrefix,
198 ]
199 );
200
201 $loadBalancer = $this->getLoadBalancerMock(
202 [
203 'host' => '*dummy*',
204 'dbDirectory' => '*dummy*',
205 'user' => 'test',
206 'password' => 'test',
207 'flags' => 0,
208 'variables' => [],
209 'schema' => '',
210 'cliMode' => true,
211 'agent' => '',
212 'load' => 100,
213 'profiler' => null,
214 'trxProfiler' => new TransactionProfiler(),
215 'connLogger' => new \Psr\Log\NullLogger(),
216 'queryLogger' => new \Psr\Log\NullLogger(),
217 'errorLogger' => function () {
218 },
219 'deprecationLogger' => function () {
220 },
221 'type' => 'test',
222 'dbname' => $dbName,
223 'tablePrefix' => $dbPrefix,
224 ]
225 );
226 $db = $loadBalancer->getConnection( DB_REPLICA );
227
228 /** @var SqlBlobStore $blobStore */
229 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
230 ->disableOriginalConstructor()
231 ->getMock();
232
233 $store = new RevisionStore(
234 $loadBalancer,
235 $blobStore,
236 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
237 MediaWikiServices::getInstance()->getCommentStore(),
238 MediaWikiServices::getInstance()->getContentModelStore(),
239 MediaWikiServices::getInstance()->getSlotRoleStore(),
240 MediaWikiServices::getInstance()->getSlotRoleRegistry(),
241 $this->getMcrMigrationStage(),
242 MediaWikiServices::getInstance()->getActorMigration(),
243 $wikiId
244 );
245
246 $count = $store->countRevisionsByPageId( $db, 0 );
247
248 // Dummy check to make PhpUnit happy. We are really only interested in
249 // countRevisionsByPageId not failing due to the DB domain check.
250 $this->assertSame( 0, $count );
251 }
252
253 protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
254 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
255 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
256 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
257 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
258 }
259
260 protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
261 $this->assertEquals(
262 $r1->getPageAsLinkTarget()->getNamespace(),
263 $r2->getPageAsLinkTarget()->getNamespace()
264 );
265
266 $this->assertEquals(
267 $r1->getPageAsLinkTarget()->getText(),
268 $r2->getPageAsLinkTarget()->getText()
269 );
270
271 if ( $r1->getParentId() ) {
272 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
273 }
274
275 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
276 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
277 $this->assertEquals( $r1->getComment(), $r2->getComment() );
278 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
279 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
280 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
281 $this->assertEquals( $r1->getSize(), $r2->getSize() );
282 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
283 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
284 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
285 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
286 foreach ( $r1->getSlotRoles() as $role ) {
287 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
288 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
289 }
290 foreach ( [
291 RevisionRecord::DELETED_TEXT,
292 RevisionRecord::DELETED_COMMENT,
293 RevisionRecord::DELETED_USER,
294 RevisionRecord::DELETED_RESTRICTED,
295 ] as $field ) {
296 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
297 }
298 }
299
300 protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
301 $this->assertSame( $s1->getRole(), $s2->getRole() );
302 $this->assertSame( $s1->getModel(), $s2->getModel() );
303 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
304 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
305 $this->assertSame( $s1->getSize(), $s2->getSize() );
306 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
307
308 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
309 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
310 }
311
312 protected function assertRevisionCompleteness( RevisionRecord $r ) {
313 $this->assertTrue( $r->hasSlot( SlotRecord::MAIN ) );
314 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( SlotRecord::MAIN ) );
315 $this->assertInstanceOf( Content::class, $r->getContent( SlotRecord::MAIN ) );
316
317 foreach ( $r->getSlotRoles() as $role ) {
318 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
319 }
320 }
321
322 protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
323 $this->assertTrue( $slot->hasAddress() );
324 $this->assertSame( $r->getId(), $slot->getRevision() );
325
326 $this->assertInstanceOf( Content::class, $slot->getContent() );
327 }
328
329 /**
330 * @param mixed[] $details
331 *
332 * @return RevisionRecord
333 */
334 private function getRevisionRecordFromDetailsArray( $details = [] ) {
335 // Convert some values that can't be provided by dataProviders
336 if ( isset( $details['user'] ) && $details['user'] === true ) {
337 $details['user'] = $this->getTestUser()->getUser();
338 }
339 if ( isset( $details['page'] ) && $details['page'] === true ) {
340 $details['page'] = $this->getTestPage()->getId();
341 }
342 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
343 $details['parent'] = $this->getTestPage()->getLatest();
344 }
345
346 // Create the RevisionRecord with any available data
347 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
348 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
349 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
350 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
351 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
352 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
353 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
354 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
355 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
356 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
357 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
358 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
359
360 if ( isset( $details['content'] ) ) {
361 foreach ( $details['content'] as $role => $content ) {
362 $rev->setContent( $role, $content );
363 }
364 }
365
366 return $rev;
367 }
368
369 public function provideInsertRevisionOn_successes() {
370 yield 'Bare minimum revision insertion' => [
371 [
372 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
373 'page' => true,
374 'comment' => $this->getRandomCommentStoreComment(),
375 'timestamp' => '20171117010101',
376 'user' => true,
377 ],
378 ];
379 yield 'Detailed revision insertion' => [
380 [
381 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
382 'parent' => true,
383 'page' => true,
384 'comment' => $this->getRandomCommentStoreComment(),
385 'timestamp' => '20171117010101',
386 'user' => true,
387 'minor' => true,
388 'visibility' => RevisionRecord::DELETED_RESTRICTED,
389 ],
390 ];
391 }
392
393 protected function getRandomCommentStoreComment() {
394 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
395 }
396
397 /**
398 * @dataProvider provideInsertRevisionOn_successes
399 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
400 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
401 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
402 */
403 public function testInsertRevisionOn_successes(
404 array $revDetails = []
405 ) {
406 $title = $this->getTestPageTitle();
407 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
408
409 $store = MediaWikiServices::getInstance()->getRevisionStore();
410 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
411
412 // is the new revision correct?
413 $this->assertRevisionCompleteness( $return );
414 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
415 $this->assertRevisionRecordsEqual( $rev, $return );
416
417 // can we load it from the store?
418 $loaded = $store->getRevisionById( $return->getId() );
419 $this->assertRevisionCompleteness( $loaded );
420 $this->assertRevisionRecordsEqual( $return, $loaded );
421
422 // can we find it directly in the database?
423 $this->assertRevisionExistsInDatabase( $return );
424 }
425
426 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
427 $row = $this->revisionToRow( new Revision( $rev ), [] );
428
429 // unset nulled fields
430 unset( $row->rev_content_model );
431 unset( $row->rev_content_format );
432
433 // unset fake fields
434 unset( $row->rev_comment_text );
435 unset( $row->rev_comment_data );
436 unset( $row->rev_comment_cid );
437 unset( $row->rev_comment_id );
438
439 $store = MediaWikiServices::getInstance()->getRevisionStore();
440 $queryInfo = $store->getQueryInfo( [ 'user' ] );
441
442 $row = get_object_vars( $row );
443
444 // Use aliased fields from $queryInfo, e.g. rev_user
445 $keys = array_keys( $row );
446 $keys = array_combine( $keys, $keys );
447 $fields = array_intersect_key( $queryInfo['fields'], $keys ) + $keys;
448
449 // assertSelect() fails unless the orders match.
450 ksort( $fields );
451 ksort( $row );
452
453 $this->assertSelect(
454 $queryInfo['tables'],
455 $fields,
456 [ 'rev_id' => $rev->getId() ],
457 [ array_values( $row ) ],
458 [],
459 $queryInfo['joins']
460 );
461 }
462
463 /**
464 * @param SlotRecord $a
465 * @param SlotRecord $b
466 */
467 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
468 // Assert that the same blob address has been used.
469 $this->assertSame( $a->getAddress(), $b->getAddress() );
470 }
471
472 /**
473 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
474 */
475 public function testInsertRevisionOn_blobAddressExists() {
476 $title = $this->getTestPageTitle();
477 $revDetails = [
478 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
479 'parent' => true,
480 'comment' => $this->getRandomCommentStoreComment(),
481 'timestamp' => '20171117010101',
482 'user' => true,
483 ];
484
485 $store = MediaWikiServices::getInstance()->getRevisionStore();
486
487 // Insert the first revision
488 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
489 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
490 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
491 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
492
493 // Insert a second revision inheriting the same blob address
494 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( SlotRecord::MAIN ) );
495 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
496 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
497 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
498 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
499
500 $firstMainSlot = $firstReturn->getSlot( SlotRecord::MAIN );
501 $secondMainSlot = $secondReturn->getSlot( SlotRecord::MAIN );
502
503 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
504
505 // And that different revisions have been created.
506 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
507
508 // Make sure the slot rows reference the correct revision
509 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
510 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
511 }
512
513 public function provideInsertRevisionOn_failures() {
514 yield 'no slot' => [
515 [
516 'comment' => $this->getRandomCommentStoreComment(),
517 'timestamp' => '20171117010101',
518 'user' => true,
519 ],
520 new InvalidArgumentException( 'main slot must be provided' )
521 ];
522 yield 'no main slot' => [
523 [
524 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
525 'comment' => $this->getRandomCommentStoreComment(),
526 'timestamp' => '20171117010101',
527 'user' => true,
528 ],
529 new InvalidArgumentException( 'main slot must be provided' )
530 ];
531 yield 'no timestamp' => [
532 [
533 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
534 'comment' => $this->getRandomCommentStoreComment(),
535 'user' => true,
536 ],
537 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
538 ];
539 yield 'no comment' => [
540 [
541 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
542 'timestamp' => '20171117010101',
543 'user' => true,
544 ],
545 new IncompleteRevisionException( 'comment must not be NULL!' )
546 ];
547 yield 'no user' => [
548 [
549 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
550 'comment' => $this->getRandomCommentStoreComment(),
551 'timestamp' => '20171117010101',
552 ],
553 new IncompleteRevisionException( 'user must not be NULL!' )
554 ];
555 }
556
557 /**
558 * @dataProvider provideInsertRevisionOn_failures
559 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
560 */
561 public function testInsertRevisionOn_failures(
562 array $revDetails = [],
563 Exception $exception
564 ) {
565 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
566
567 $store = MediaWikiServices::getInstance()->getRevisionStore();
568
569 $this->setExpectedException(
570 get_class( $exception ),
571 $exception->getMessage(),
572 $exception->getCode()
573 );
574 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
575 }
576
577 public function provideNewNullRevision() {
578 yield [
579 Title::newFromText( 'UTPage_notAutoCreated' ),
580 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
581 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
582 true,
583 ];
584 yield [
585 Title::newFromText( 'UTPage_notAutoCreated' ),
586 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
587 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
588 false,
589 ];
590 }
591
592 /**
593 * @dataProvider provideNewNullRevision
594 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
595 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
596 */
597 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
598 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
599 $page = WikiPage::factory( $title );
600
601 if ( !$page->exists() ) {
602 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__, EDIT_NEW );
603 }
604
605 $revDetails['page'] = $page->getId();
606 $revDetails['timestamp'] = wfTimestampNow();
607 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
608 $revDetails['user'] = $user;
609
610 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
611 $store = MediaWikiServices::getInstance()->getRevisionStore();
612
613 $dbw = wfGetDB( DB_MASTER );
614 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
615 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
616
617 $record = $store->newNullRevision(
618 wfGetDB( DB_MASTER ),
619 $title,
620 $comment,
621 $minor,
622 $user
623 );
624
625 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
626 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
627 $this->assertEquals( $comment, $record->getComment() );
628 $this->assertEquals( $minor, $record->isMinor() );
629 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
630 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
631
632 $this->assertArrayEquals(
633 $baseRev->getSlotRoles(),
634 $record->getSlotRoles()
635 );
636
637 foreach ( $baseRev->getSlotRoles() as $role ) {
638 $parentSlot = $baseRev->getSlot( $role );
639 $slot = $record->getSlot( $role );
640
641 $this->assertTrue( $slot->isInherited(), 'isInherited' );
642 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
643 $this->assertSameSlotContent( $parentSlot, $slot );
644 }
645 }
646
647 /**
648 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
649 */
650 public function testNewNullRevision_nonExistingTitle() {
651 $store = MediaWikiServices::getInstance()->getRevisionStore();
652 $record = $store->newNullRevision(
653 wfGetDB( DB_MASTER ),
654 Title::newFromText( __METHOD__ . '.iDontExist!' ),
655 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
656 false,
657 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
658 );
659 $this->assertNull( $record );
660 }
661
662 /**
663 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
664 */
665 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
666 $page = $this->getTestPage();
667 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
668 /** @var Revision $rev */
669 $rev = $status->value['revision'];
670
671 $store = MediaWikiServices::getInstance()->getRevisionStore();
672 $revisionRecord = $store->getRevisionById( $rev->getId() );
673 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
674
675 $this->assertGreaterThan( 0, $result );
676 $this->assertSame(
677 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
678 $result
679 );
680 }
681
682 /**
683 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
684 */
685 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
686 // This assumes that sysops are auto patrolled
687 $sysop = $this->getTestSysop()->getUser();
688 $page = $this->getTestPage();
689 $status = $page->doEditContent(
690 new WikitextContent( __METHOD__ ),
691 __METHOD__,
692 0,
693 false,
694 $sysop
695 );
696 /** @var Revision $rev */
697 $rev = $status->value['revision'];
698
699 $store = MediaWikiServices::getInstance()->getRevisionStore();
700 $revisionRecord = $store->getRevisionById( $rev->getId() );
701 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
702
703 $this->assertSame( 0, $result );
704 }
705
706 /**
707 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
708 */
709 public function testGetRecentChange() {
710 $page = $this->getTestPage();
711 $content = new WikitextContent( __METHOD__ );
712 $status = $page->doEditContent( $content, __METHOD__ );
713 /** @var Revision $rev */
714 $rev = $status->value['revision'];
715
716 $store = MediaWikiServices::getInstance()->getRevisionStore();
717 $revRecord = $store->getRevisionById( $rev->getId() );
718 $recentChange = $store->getRecentChange( $revRecord );
719
720 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
721 $this->assertEquals( $rev->getRecentChange(), $recentChange );
722 }
723
724 /**
725 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
726 */
727 public function testGetRevisionById() {
728 $page = $this->getTestPage();
729 $content = new WikitextContent( __METHOD__ );
730 $status = $page->doEditContent( $content, __METHOD__ );
731 /** @var Revision $rev */
732 $rev = $status->value['revision'];
733
734 $store = MediaWikiServices::getInstance()->getRevisionStore();
735 $revRecord = $store->getRevisionById( $rev->getId() );
736
737 $this->assertSame( $rev->getId(), $revRecord->getId() );
738 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
739 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
740 }
741
742 /**
743 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
744 */
745 public function testGetRevisionByTitle() {
746 $page = $this->getTestPage();
747 $content = new WikitextContent( __METHOD__ );
748 $status = $page->doEditContent( $content, __METHOD__ );
749 /** @var Revision $rev */
750 $rev = $status->value['revision'];
751
752 $store = MediaWikiServices::getInstance()->getRevisionStore();
753 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
754
755 $this->assertSame( $rev->getId(), $revRecord->getId() );
756 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
757 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
758 }
759
760 /**
761 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
762 */
763 public function testGetRevisionByPageId() {
764 $page = $this->getTestPage();
765 $content = new WikitextContent( __METHOD__ );
766 $status = $page->doEditContent( $content, __METHOD__ );
767 /** @var Revision $rev */
768 $rev = $status->value['revision'];
769
770 $store = MediaWikiServices::getInstance()->getRevisionStore();
771 $revRecord = $store->getRevisionByPageId( $page->getId() );
772
773 $this->assertSame( $rev->getId(), $revRecord->getId() );
774 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
775 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
776 }
777
778 /**
779 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
780 */
781 public function testGetRevisionByTimestamp() {
782 // Make sure there is 1 second between the last revision and the rev we create...
783 // Otherwise we might not get the correct revision and the test may fail...
784 // :(
785 $page = $this->getTestPage();
786 sleep( 1 );
787 $content = new WikitextContent( __METHOD__ );
788 $status = $page->doEditContent( $content, __METHOD__ );
789 /** @var Revision $rev */
790 $rev = $status->value['revision'];
791
792 $store = MediaWikiServices::getInstance()->getRevisionStore();
793 $revRecord = $store->getRevisionByTimestamp(
794 $page->getTitle(),
795 $rev->getTimestamp()
796 );
797
798 $this->assertSame( $rev->getId(), $revRecord->getId() );
799 $this->assertTrue( $revRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
800 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
801 }
802
803 protected function revisionToRow( Revision $rev, $options = [ 'page', 'user', 'comment' ] ) {
804 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
805 $page = WikiPage::factory( $rev->getTitle() );
806
807 $fields = [
808 'rev_id' => (string)$rev->getId(),
809 'rev_page' => (string)$rev->getPage(),
810 'rev_timestamp' => $this->db->timestamp( $rev->getTimestamp() ),
811 'rev_user_text' => (string)$rev->getUserText(),
812 'rev_user' => (string)$rev->getUser() ?: null,
813 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
814 'rev_deleted' => (string)$rev->getVisibility(),
815 'rev_len' => (string)$rev->getSize(),
816 'rev_parent_id' => (string)$rev->getParentId(),
817 'rev_sha1' => (string)$rev->getSha1(),
818 ];
819
820 if ( in_array( 'page', $options ) ) {
821 $fields += [
822 'page_namespace' => (string)$page->getTitle()->getNamespace(),
823 'page_title' => $page->getTitle()->getDBkey(),
824 'page_id' => (string)$page->getId(),
825 'page_latest' => (string)$page->getLatest(),
826 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
827 'page_len' => (string)$page->getContent()->getSize(),
828 ];
829 }
830
831 if ( in_array( 'user', $options ) ) {
832 $fields += [
833 'user_name' => (string)$rev->getUserText(),
834 ];
835 }
836
837 if ( in_array( 'comment', $options ) ) {
838 $fields += [
839 'rev_comment_text' => $rev->getComment(),
840 'rev_comment_data' => null,
841 'rev_comment_cid' => null,
842 ];
843 }
844
845 if ( $rev->getId() ) {
846 $fields += [
847 'rev_id' => (string)$rev->getId(),
848 ];
849 }
850
851 return (object)$fields;
852 }
853
854 protected function assertRevisionRecordMatchesRevision(
855 Revision $rev,
856 RevisionRecord $record
857 ) {
858 $this->assertSame( $rev->getId(), $record->getId() );
859 $this->assertSame( $rev->getPage(), $record->getPageId() );
860 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
861 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
862 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
863 $this->assertSame( $rev->isMinor(), $record->isMinor() );
864 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
865 $this->assertSame( $rev->getSize(), $record->getSize() );
866 /**
867 * @note As of MW 1.31, the database schema allows the parent ID to be
868 * NULL to indicate that it is unknown.
869 */
870 $expectedParent = $rev->getParentId();
871 if ( $expectedParent === null ) {
872 $expectedParent = 0;
873 }
874 $this->assertSame( $expectedParent, $record->getParentId() );
875 $this->assertSame( $rev->getSha1(), $record->getSha1() );
876 $this->assertSame( $rev->getComment(), $record->getComment()->text );
877 $this->assertSame( $rev->getContentFormat(),
878 $record->getContent( SlotRecord::MAIN )->getDefaultFormat() );
879 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord::MAIN )->getModel() );
880 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
881
882 $revRec = $rev->getRevisionRecord();
883 $revMain = $revRec->getSlot( SlotRecord::MAIN );
884 $recMain = $record->getSlot( SlotRecord::MAIN );
885
886 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
887 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
888 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
889
890 if ( $revMain->hasOrigin() ) {
891 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
892 }
893
894 if ( $revMain->hasAddress() ) {
895 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
896 }
897
898 if ( $revMain->hasContentId() ) {
899 // XXX: the content ID value is ill-defined when SCHEMA_COMPAT_WRITE_BOTH and
900 // SCHEMA_COMPAT_READ_OLD is set, since revision insertion will report the
901 // content ID used with the new schema, while loading the revision from the
902 // old schema will report an emulated ID.
903 if ( $this->getMcrMigrationStage() & SCHEMA_COMPAT_READ_NEW ) {
904 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
905 }
906 }
907 }
908
909 /**
910 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
911 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
912 */
913 public function testNewRevisionFromRowAndSlot_getQueryInfo() {
914 $page = $this->getTestPage();
915 $text = __METHOD__ . 'o-ö';
916 /** @var Revision $rev */
917 $rev = $page->doEditContent(
918 new WikitextContent( $text ),
919 __METHOD__ . 'a'
920 )->value['revision'];
921
922 $store = MediaWikiServices::getInstance()->getRevisionStore();
923 $info = $store->getQueryInfo();
924 $row = $this->db->selectRow(
925 $info['tables'],
926 $info['fields'],
927 [ 'rev_id' => $rev->getId() ],
928 __METHOD__,
929 [],
930 $info['joins']
931 );
932
933 $info = $store->getSlotsQueryInfo( [ 'content' ] );
934 $slotRows = $this->db->select(
935 $info['tables'],
936 $info['fields'],
937 $this->getSlotRevisionConditions( $rev->getId() ),
938 __METHOD__,
939 [],
940 $info['joins']
941 );
942
943 $record = $store->newRevisionFromRowAndSlots(
944 $row,
945 iterator_to_array( $slotRows ),
946 [],
947 $page->getTitle()
948 );
949 $this->assertRevisionRecordMatchesRevision( $rev, $record );
950 $this->assertSame( $text, $rev->getContent()->serialize() );
951 }
952
953 /**
954 * Conditions to use together with getSlotsQueryInfo() when selecting slot rows for a given
955 * revision.
956 *
957 * @return array
958 */
959 abstract protected function getSlotRevisionConditions( $revId );
960
961 /**
962 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
963 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
964 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
965 */
966 public function testNewRevisionFromRow_getQueryInfo() {
967 $page = $this->getTestPage();
968 $text = __METHOD__ . 'a-ä';
969 /** @var Revision $rev */
970 $rev = $page->doEditContent(
971 new WikitextContent( $text ),
972 __METHOD__ . 'a'
973 )->value['revision'];
974
975 $store = MediaWikiServices::getInstance()->getRevisionStore();
976 $info = $store->getQueryInfo();
977 $row = $this->db->selectRow(
978 $info['tables'],
979 $info['fields'],
980 [ 'rev_id' => $rev->getId() ],
981 __METHOD__,
982 [],
983 $info['joins']
984 );
985 $record = $store->newRevisionFromRow(
986 $row,
987 [],
988 $page->getTitle()
989 );
990 $this->assertRevisionRecordMatchesRevision( $rev, $record );
991 $this->assertSame( $text, $rev->getContent()->serialize() );
992 }
993
994 /**
995 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
996 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
997 */
998 public function testNewRevisionFromRow_anonEdit() {
999 $page = $this->getTestPage();
1000 $text = __METHOD__ . 'a-ä';
1001 /** @var Revision $rev */
1002 $rev = $page->doEditContent(
1003 new WikitextContent( $text ),
1004 __METHOD__ . 'a'
1005 )->value['revision'];
1006
1007 $store = MediaWikiServices::getInstance()->getRevisionStore();
1008 $record = $store->newRevisionFromRow(
1009 $this->revisionToRow( $rev ),
1010 [],
1011 $page->getTitle()
1012 );
1013 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1014 $this->assertSame( $text, $rev->getContent()->serialize() );
1015 }
1016
1017 /**
1018 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1019 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1020 */
1021 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1022 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1023 $page = $this->getTestPage();
1024 $text = __METHOD__ . 'a-ä';
1025 /** @var Revision $rev */
1026 $rev = $page->doEditContent(
1027 new WikitextContent( $text ),
1028 __METHOD__ . 'a'
1029 )->value['revision'];
1030
1031 $store = MediaWikiServices::getInstance()->getRevisionStore();
1032 $record = $store->newRevisionFromRow(
1033 $this->revisionToRow( $rev ),
1034 [],
1035 $page->getTitle()
1036 );
1037 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1038 $this->assertSame( $text, $rev->getContent()->serialize() );
1039 }
1040
1041 /**
1042 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1043 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1044 */
1045 public function testNewRevisionFromRow_userEdit() {
1046 $page = $this->getTestPage();
1047 $text = __METHOD__ . 'b-ä';
1048 /** @var Revision $rev */
1049 $rev = $page->doEditContent(
1050 new WikitextContent( $text ),
1051 __METHOD__ . 'b',
1052 0,
1053 false,
1054 $this->getTestUser()->getUser()
1055 )->value['revision'];
1056
1057 $store = MediaWikiServices::getInstance()->getRevisionStore();
1058 $record = $store->newRevisionFromRow(
1059 $this->revisionToRow( $rev ),
1060 [],
1061 $page->getTitle()
1062 );
1063 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1064 $this->assertSame( $text, $rev->getContent()->serialize() );
1065 }
1066
1067 /**
1068 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1069 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1070 */
1071 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1072 $store = MediaWikiServices::getInstance()->getRevisionStore();
1073 $title = Title::newFromText( __METHOD__ );
1074 $text = __METHOD__ . '-bä';
1075 $page = WikiPage::factory( $title );
1076 /** @var Revision $orig */
1077 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1078 ->value['revision'];
1079 $page->doDeleteArticle( __METHOD__ );
1080
1081 $db = wfGetDB( DB_MASTER );
1082 $arQuery = $store->getArchiveQueryInfo();
1083 $res = $db->select(
1084 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1085 __METHOD__, [], $arQuery['joins']
1086 );
1087 $this->assertTrue( is_object( $res ), 'query failed' );
1088
1089 $row = $res->fetchObject();
1090 $res->free();
1091 $record = $store->newRevisionFromArchiveRow( $row );
1092
1093 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1094 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1095 }
1096
1097 /**
1098 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1099 */
1100 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1101 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1102 $store = MediaWikiServices::getInstance()->getRevisionStore();
1103 $title = Title::newFromText( __METHOD__ );
1104 $text = __METHOD__ . '-bä';
1105 $page = WikiPage::factory( $title );
1106 /** @var Revision $orig */
1107 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
1108 ->value['revision'];
1109 $page->doDeleteArticle( __METHOD__ );
1110
1111 $db = wfGetDB( DB_MASTER );
1112 $arQuery = $store->getArchiveQueryInfo();
1113 $res = $db->select(
1114 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1115 __METHOD__, [], $arQuery['joins']
1116 );
1117 $this->assertTrue( is_object( $res ), 'query failed' );
1118
1119 $row = $res->fetchObject();
1120 $res->free();
1121 $record = $store->newRevisionFromArchiveRow( $row );
1122
1123 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1124 $this->assertSame( $text, $record->getContent( SlotRecord::MAIN )->serialize() );
1125 }
1126
1127 /**
1128 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1129 */
1130 public function testNewRevisionFromArchiveRow_no_user() {
1131 $store = MediaWikiServices::getInstance()->getRevisionStore();
1132
1133 $row = (object)[
1134 'ar_id' => '1',
1135 'ar_page_id' => '2',
1136 'ar_namespace' => '0',
1137 'ar_title' => 'Something',
1138 'ar_rev_id' => '2',
1139 'ar_text_id' => '47',
1140 'ar_timestamp' => '20180528192356',
1141 'ar_minor_edit' => '0',
1142 'ar_deleted' => '0',
1143 'ar_len' => '78',
1144 'ar_parent_id' => '0',
1145 'ar_sha1' => 'deadbeef',
1146 'ar_comment_text' => 'whatever',
1147 'ar_comment_data' => null,
1148 'ar_comment_cid' => null,
1149 'ar_user' => '0',
1150 'ar_user_text' => '', // this is the important bit
1151 'ar_actor' => null,
1152 'ar_content_format' => null,
1153 'ar_content_model' => null,
1154 ];
1155
1156 \Wikimedia\suppressWarnings();
1157 $record = $store->newRevisionFromArchiveRow( $row );
1158 \Wikimedia\suppressWarnings( true );
1159
1160 $this->assertInstanceOf( RevisionRecord::class, $record );
1161 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1162 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1163 }
1164
1165 /**
1166 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1167 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1168 */
1169 public function testNewRevisionFromRow_no_user() {
1170 $store = MediaWikiServices::getInstance()->getRevisionStore();
1171 $title = Title::newFromText( __METHOD__ );
1172
1173 $row = (object)[
1174 'rev_id' => '2',
1175 'rev_page' => '2',
1176 'page_namespace' => '0',
1177 'page_title' => $title->getText(),
1178 'rev_text_id' => '47',
1179 'rev_timestamp' => '20180528192356',
1180 'rev_minor_edit' => '0',
1181 'rev_deleted' => '0',
1182 'rev_len' => '78',
1183 'rev_parent_id' => '0',
1184 'rev_sha1' => 'deadbeef',
1185 'rev_comment_text' => 'whatever',
1186 'rev_comment_data' => null,
1187 'rev_comment_cid' => null,
1188 'rev_user' => '0',
1189 'rev_user_text' => '', // this is the important bit
1190 'rev_actor' => null,
1191 'rev_content_format' => null,
1192 'rev_content_model' => null,
1193 ];
1194
1195 \Wikimedia\suppressWarnings();
1196 $record = $store->newRevisionFromRow( $row, 0, $title );
1197 \Wikimedia\suppressWarnings( true );
1198
1199 $this->assertNotNull( $record );
1200 $this->assertNotNull( $record->getUser() );
1201 $this->assertNotEmpty( $record->getUser()->getName() );
1202 }
1203
1204 /**
1205 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1206 */
1207 public function testInsertRevisionOn_archive() {
1208 // This is a round trip test for deletion and undeletion of a
1209 // revision row via the archive table.
1210
1211 $store = MediaWikiServices::getInstance()->getRevisionStore();
1212 $title = Title::newFromText( __METHOD__ );
1213
1214 $page = WikiPage::factory( $title );
1215 /** @var Revision $origRev */
1216 $page->doEditContent( new WikitextContent( "First" ), __METHOD__ . '-first' );
1217 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__ )
1218 ->value['revision'];
1219 $orig = $origRev->getRevisionRecord();
1220 $page->doDeleteArticle( __METHOD__ );
1221
1222 // re-create page, so we can later load revisions for it
1223 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__ );
1224
1225 $db = wfGetDB( DB_MASTER );
1226 $arQuery = $store->getArchiveQueryInfo();
1227 $row = $db->selectRow(
1228 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1229 __METHOD__, [], $arQuery['joins']
1230 );
1231
1232 $this->assertNotFalse( $row, 'query failed' );
1233
1234 $record = $store->newRevisionFromArchiveRow(
1235 $row,
1236 0,
1237 $title,
1238 [ 'page_id' => $title->getArticleID() ]
1239 );
1240
1241 $restored = $store->insertRevisionOn( $record, $db );
1242
1243 // is the new revision correct?
1244 $this->assertRevisionCompleteness( $restored );
1245 $this->assertRevisionRecordsEqual( $record, $restored );
1246
1247 // does the new revision use the original slot?
1248 $recMain = $record->getSlot( SlotRecord::MAIN );
1249 $restMain = $restored->getSlot( SlotRecord::MAIN );
1250 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1251 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1252 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1253 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1254
1255 // can we load it from the store?
1256 $loaded = $store->getRevisionById( $restored->getId() );
1257 $this->assertNotNull( $loaded );
1258 $this->assertRevisionCompleteness( $loaded );
1259 $this->assertRevisionRecordsEqual( $restored, $loaded );
1260
1261 // can we find it directly in the database?
1262 $this->assertRevisionExistsInDatabase( $restored );
1263 }
1264
1265 /**
1266 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1267 */
1268 public function testLoadRevisionFromId() {
1269 $title = Title::newFromText( __METHOD__ );
1270 $page = WikiPage::factory( $title );
1271 /** @var Revision $rev */
1272 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1273 ->value['revision'];
1274
1275 $store = MediaWikiServices::getInstance()->getRevisionStore();
1276 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
1277 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1278 }
1279
1280 /**
1281 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1282 */
1283 public function testLoadRevisionFromPageId() {
1284 $title = Title::newFromText( __METHOD__ );
1285 $page = WikiPage::factory( $title );
1286 /** @var Revision $rev */
1287 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1288 ->value['revision'];
1289
1290 $store = MediaWikiServices::getInstance()->getRevisionStore();
1291 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
1292 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1293 }
1294
1295 /**
1296 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1297 */
1298 public function testLoadRevisionFromTitle() {
1299 $title = Title::newFromText( __METHOD__ );
1300 $page = WikiPage::factory( $title );
1301 /** @var Revision $rev */
1302 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1303 ->value['revision'];
1304
1305 $store = MediaWikiServices::getInstance()->getRevisionStore();
1306 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
1307 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1308 }
1309
1310 /**
1311 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1312 */
1313 public function testLoadRevisionFromTimestamp() {
1314 $title = Title::newFromText( __METHOD__ );
1315 $page = WikiPage::factory( $title );
1316 /** @var Revision $revOne */
1317 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1318 ->value['revision'];
1319 // Sleep to ensure different timestamps... )(evil)
1320 sleep( 1 );
1321 /** @var Revision $revTwo */
1322 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
1323 ->value['revision'];
1324
1325 $store = MediaWikiServices::getInstance()->getRevisionStore();
1326 $this->assertNull(
1327 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
1328 );
1329 $this->assertSame(
1330 $revOne->getId(),
1331 $store->loadRevisionFromTimestamp(
1332 wfGetDB( DB_MASTER ),
1333 $title,
1334 $revOne->getTimestamp()
1335 )->getId()
1336 );
1337 $this->assertSame(
1338 $revTwo->getId(),
1339 $store->loadRevisionFromTimestamp(
1340 wfGetDB( DB_MASTER ),
1341 $title,
1342 $revTwo->getTimestamp()
1343 )->getId()
1344 );
1345 }
1346
1347 /**
1348 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1349 */
1350 public function testGetParentLengths() {
1351 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1352 /** @var Revision $revOne */
1353 $revOne = $page->doEditContent(
1354 new WikitextContent( __METHOD__ ), __METHOD__
1355 )->value['revision'];
1356 /** @var Revision $revTwo */
1357 $revTwo = $page->doEditContent(
1358 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1359 )->value['revision'];
1360
1361 $store = MediaWikiServices::getInstance()->getRevisionStore();
1362 $this->assertSame(
1363 [
1364 $revOne->getId() => strlen( __METHOD__ ),
1365 ],
1366 $store->listRevisionSizes(
1367 wfGetDB( DB_MASTER ),
1368 [ $revOne->getId() ]
1369 )
1370 );
1371 $this->assertSame(
1372 [
1373 $revOne->getId() => strlen( __METHOD__ ),
1374 $revTwo->getId() => strlen( __METHOD__ ) + 1,
1375 ],
1376 $store->listRevisionSizes(
1377 wfGetDB( DB_MASTER ),
1378 [ $revOne->getId(), $revTwo->getId() ]
1379 )
1380 );
1381 }
1382
1383 /**
1384 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1385 */
1386 public function testGetPreviousRevision() {
1387 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1388 /** @var Revision $revOne */
1389 $revOne = $page->doEditContent(
1390 new WikitextContent( __METHOD__ ), __METHOD__
1391 )->value['revision'];
1392 /** @var Revision $revTwo */
1393 $revTwo = $page->doEditContent(
1394 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1395 )->value['revision'];
1396
1397 $store = MediaWikiServices::getInstance()->getRevisionStore();
1398 $this->assertNull(
1399 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1400 );
1401 $this->assertSame(
1402 $revOne->getId(),
1403 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1404 );
1405 }
1406
1407 /**
1408 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1409 */
1410 public function testGetNextRevision() {
1411 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1412 /** @var Revision $revOne */
1413 $revOne = $page->doEditContent(
1414 new WikitextContent( __METHOD__ ), __METHOD__
1415 )->value['revision'];
1416 /** @var Revision $revTwo */
1417 $revTwo = $page->doEditContent(
1418 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1419 )->value['revision'];
1420
1421 $store = MediaWikiServices::getInstance()->getRevisionStore();
1422 $this->assertSame(
1423 $revTwo->getId(),
1424 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1425 );
1426 $this->assertNull(
1427 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1428 );
1429 }
1430
1431 public function provideNonHistoryRevision() {
1432 $title = Title::newFromText( __METHOD__ );
1433 $rev = new MutableRevisionRecord( $title );
1434 yield [ $rev ];
1435
1436 $user = new UserIdentityValue( 7, 'Frank', 0 );
1437 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1438 $row = (object)[
1439 'ar_id' => 3,
1440 'ar_rev_id' => 34567,
1441 'ar_page_id' => 5,
1442 'ar_deleted' => 0,
1443 'ar_minor_edit' => 0,
1444 'ar_timestamp' => '20180101020202',
1445 ];
1446 $slots = new RevisionSlots( [] );
1447 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1448 yield [ $rev ];
1449 }
1450
1451 /**
1452 * @dataProvider provideNonHistoryRevision
1453 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1454 */
1455 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1456 $store = MediaWikiServices::getInstance()->getRevisionStore();
1457 $this->assertNull( $store->getPreviousRevision( $rev ) );
1458 }
1459
1460 /**
1461 * @dataProvider provideNonHistoryRevision
1462 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1463 */
1464 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1465 $store = MediaWikiServices::getInstance()->getRevisionStore();
1466 $this->assertNull( $store->getNextRevision( $rev ) );
1467 }
1468
1469 /**
1470 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1471 */
1472 public function testGetTimestampFromId_found() {
1473 $page = $this->getTestPage();
1474 /** @var Revision $rev */
1475 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1476 ->value['revision'];
1477
1478 $store = MediaWikiServices::getInstance()->getRevisionStore();
1479 $result = $store->getTimestampFromId( $rev->getId() );
1480
1481 $this->assertSame( $rev->getTimestamp(), $result );
1482 }
1483
1484 /**
1485 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1486 */
1487 public function testGetTimestampFromId_notFound() {
1488 $page = $this->getTestPage();
1489 /** @var Revision $rev */
1490 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
1491 ->value['revision'];
1492
1493 $store = MediaWikiServices::getInstance()->getRevisionStore();
1494 $result = $store->getTimestampFromId( $rev->getId() + 1 );
1495
1496 $this->assertFalse( $result );
1497 }
1498
1499 /**
1500 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1501 */
1502 public function testCountRevisionsByPageId() {
1503 $store = MediaWikiServices::getInstance()->getRevisionStore();
1504 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1505
1506 $this->assertSame(
1507 0,
1508 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1509 );
1510 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1511 $this->assertSame(
1512 1,
1513 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1514 );
1515 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1516 $this->assertSame(
1517 2,
1518 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1519 );
1520 }
1521
1522 /**
1523 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1524 */
1525 public function testCountRevisionsByTitle() {
1526 $store = MediaWikiServices::getInstance()->getRevisionStore();
1527 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1528
1529 $this->assertSame(
1530 0,
1531 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1532 );
1533 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1534 $this->assertSame(
1535 1,
1536 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1537 );
1538 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1539 $this->assertSame(
1540 2,
1541 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1542 );
1543 }
1544
1545 /**
1546 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1547 */
1548 public function testUserWasLastToEdit_false() {
1549 $sysop = $this->getTestSysop()->getUser();
1550 $page = $this->getTestPage();
1551 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1552
1553 $store = MediaWikiServices::getInstance()->getRevisionStore();
1554 $result = $store->userWasLastToEdit(
1555 wfGetDB( DB_MASTER ),
1556 $page->getId(),
1557 $sysop->getId(),
1558 '20160101010101'
1559 );
1560 $this->assertFalse( $result );
1561 }
1562
1563 /**
1564 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1565 */
1566 public function testUserWasLastToEdit_true() {
1567 $startTime = wfTimestampNow();
1568 $sysop = $this->getTestSysop()->getUser();
1569 $page = $this->getTestPage();
1570 $page->doEditContent(
1571 new WikitextContent( __METHOD__ ),
1572 __METHOD__,
1573 0,
1574 false,
1575 $sysop
1576 );
1577
1578 $store = MediaWikiServices::getInstance()->getRevisionStore();
1579 $result = $store->userWasLastToEdit(
1580 wfGetDB( DB_MASTER ),
1581 $page->getId(),
1582 $sysop->getId(),
1583 $startTime
1584 );
1585 $this->assertTrue( $result );
1586 }
1587
1588 /**
1589 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1590 */
1591 public function testGetKnownCurrentRevision() {
1592 $page = $this->getTestPage();
1593 /** @var Revision $rev */
1594 $rev = $page->doEditContent(
1595 new WikitextContent( __METHOD__ . 'b' ),
1596 __METHOD__ . 'b',
1597 0,
1598 false,
1599 $this->getTestUser()->getUser()
1600 )->value['revision'];
1601
1602 $store = MediaWikiServices::getInstance()->getRevisionStore();
1603 $record = $store->getKnownCurrentRevision(
1604 $page->getTitle(),
1605 $rev->getId()
1606 );
1607
1608 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1609 }
1610
1611 public function provideNewMutableRevisionFromArray() {
1612 yield 'Basic array, content object' => [
1613 [
1614 'id' => 2,
1615 'page' => 1,
1616 'timestamp' => '20171017114835',
1617 'user_text' => '111.0.1.2',
1618 'user' => 0,
1619 'minor_edit' => false,
1620 'deleted' => 0,
1621 'len' => 46,
1622 'parent_id' => 1,
1623 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1624 'comment' => 'Goat Comment!',
1625 'content' => new WikitextContent( 'Some Content' ),
1626 ]
1627 ];
1628 yield 'Basic array, serialized text' => [
1629 [
1630 'id' => 2,
1631 'page' => 1,
1632 'timestamp' => '20171017114835',
1633 'user_text' => '111.0.1.2',
1634 'user' => 0,
1635 'minor_edit' => false,
1636 'deleted' => 0,
1637 'len' => 46,
1638 'parent_id' => 1,
1639 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1640 'comment' => 'Goat Comment!',
1641 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1642 ]
1643 ];
1644 yield 'Basic array, serialized text, utf-8 flags' => [
1645 [
1646 'id' => 2,
1647 'page' => 1,
1648 'timestamp' => '20171017114835',
1649 'user_text' => '111.0.1.2',
1650 'user' => 0,
1651 'minor_edit' => false,
1652 'deleted' => 0,
1653 'len' => 46,
1654 'parent_id' => 1,
1655 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1656 'comment' => 'Goat Comment!',
1657 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1658 'flags' => 'utf-8',
1659 ]
1660 ];
1661 yield 'Basic array, with title' => [
1662 [
1663 'title' => Title::newFromText( 'SomeText' ),
1664 'timestamp' => '20171017114835',
1665 'user_text' => '111.0.1.2',
1666 'user' => 0,
1667 'minor_edit' => false,
1668 'deleted' => 0,
1669 'len' => 46,
1670 'parent_id' => 1,
1671 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1672 'comment' => 'Goat Comment!',
1673 'content' => new WikitextContent( 'Some Content' ),
1674 ]
1675 ];
1676 yield 'Basic array, no user field' => [
1677 [
1678 'id' => 2,
1679 'page' => 1,
1680 'timestamp' => '20171017114835',
1681 'user_text' => '111.0.1.3',
1682 'minor_edit' => false,
1683 'deleted' => 0,
1684 'len' => 46,
1685 'parent_id' => 1,
1686 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1687 'comment' => 'Goat Comment!',
1688 'content' => new WikitextContent( 'Some Content' ),
1689 ]
1690 ];
1691 }
1692
1693 /**
1694 * @dataProvider provideNewMutableRevisionFromArray
1695 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1696 */
1697 public function testNewMutableRevisionFromArray( array $array ) {
1698 $store = MediaWikiServices::getInstance()->getRevisionStore();
1699
1700 // HACK: if $array['page'] is given, make sure that the page exists
1701 if ( isset( $array['page'] ) ) {
1702 $t = Title::newFromID( $array['page'] );
1703 if ( !$t || !$t->exists() ) {
1704 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1705 $info = $this->insertPage( $t );
1706 $array['page'] = $info['id'];
1707 }
1708 }
1709
1710 $result = $store->newMutableRevisionFromArray( $array );
1711
1712 if ( isset( $array['id'] ) ) {
1713 $this->assertSame( $array['id'], $result->getId() );
1714 }
1715 if ( isset( $array['page'] ) ) {
1716 $this->assertSame( $array['page'], $result->getPageId() );
1717 }
1718 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1719 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1720 if ( isset( $array['user'] ) ) {
1721 $this->assertSame( $array['user'], $result->getUser()->getId() );
1722 }
1723 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1724 $this->assertSame( $array['deleted'], $result->getVisibility() );
1725 $this->assertSame( $array['len'], $result->getSize() );
1726 $this->assertSame( $array['parent_id'], $result->getParentId() );
1727 $this->assertSame( $array['sha1'], $result->getSha1() );
1728 $this->assertSame( $array['comment'], $result->getComment()->text );
1729 if ( isset( $array['content'] ) ) {
1730 foreach ( $array['content'] as $role => $content ) {
1731 $this->assertTrue(
1732 $result->getContent( $role )->equals( $content )
1733 );
1734 }
1735 } elseif ( isset( $array['text'] ) ) {
1736 $this->assertSame( $array['text'],
1737 $result->getSlot( SlotRecord::MAIN )->getContent()->serialize() );
1738 } elseif ( isset( $array['content_format'] ) ) {
1739 $this->assertSame(
1740 $array['content_format'],
1741 $result->getSlot( SlotRecord::MAIN )->getContent()->getDefaultFormat()
1742 );
1743 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord::MAIN )->getModel() );
1744 }
1745 }
1746
1747 /**
1748 * @dataProvider provideNewMutableRevisionFromArray
1749 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1750 */
1751 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1752 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1753 $services = MediaWikiServices::getInstance();
1754 $lb = $services->getDBLoadBalancer();
1755 $access = $services->getExternalStoreAccess();
1756 $blobStore = new SqlBlobStore( $lb, $access, $cache );
1757 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1758
1759 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1760 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1761 ->disableOriginalConstructor()
1762 ->getMock();
1763 $factory->expects( $this->any() )
1764 ->method( 'newBlobStore' )
1765 ->willReturn( $blobStore );
1766 $factory->expects( $this->any() )
1767 ->method( 'newSqlBlobStore' )
1768 ->willReturn( $blobStore );
1769
1770 $this->setService( 'BlobStoreFactory', $factory );
1771
1772 $this->testNewMutableRevisionFromArray( $array );
1773 }
1774
1775 /**
1776 * Creates a new revision for testing caching behavior
1777 *
1778 * @param WikiPage $page the page for the new revision
1779 * @param RevisionStore $store store object to use for creating the revision
1780 * @return bool|RevisionStoreRecord the revision created, or false if missing
1781 */
1782 private function createRevisionStoreCacheRecord( $page, $store ) {
1783 $user = MediaWikiTestCase::getMutableTestUser()->getUser();
1784 $updater = $page->newPageUpdater( $user );
1785 $updater->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) );
1786 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1787 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1788 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1789 }
1790
1791 /**
1792 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1793 */
1794 public function testGetKnownCurrentRevision_userNameChange() {
1795 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1796 $this->setService( 'MainWANObjectCache', $cache );
1797
1798 $store = MediaWikiServices::getInstance()->getRevisionStore();
1799 $page = $this->getNonexistingTestPage();
1800 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1801
1802 // Grab the user name
1803 $userNameBefore = $rev->getUser()->getName();
1804
1805 // Change the user name in the database, "behind the back" of the cache
1806 $newUserName = "Renamed $userNameBefore";
1807 $this->db->update( 'revision',
1808 [ 'rev_user_text' => $newUserName ],
1809 [ 'rev_id' => $rev->getId() ] );
1810 $this->db->update( 'user',
1811 [ 'user_name' => $newUserName ],
1812 [ 'user_id' => $rev->getUser()->getId() ] );
1813 $this->db->update( 'actor',
1814 [ 'actor_name' => $newUserName ],
1815 [ 'actor_user' => $rev->getUser()->getId() ] );
1816
1817 // Reload the revision and regrab the user name.
1818 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1819 $userNameAfter = $revAfter->getUser()->getName();
1820
1821 // The two user names should be different.
1822 // If they are the same, we are seeing a cached value, which is bad.
1823 $this->assertNotSame( $userNameBefore, $userNameAfter );
1824
1825 // This is implied by the above assertion, but explicitly check it, for completeness
1826 $this->assertSame( $newUserName, $userNameAfter );
1827 }
1828
1829 /**
1830 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1831 */
1832 public function testGetKnownCurrentRevision_revDelete() {
1833 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1834 $this->setService( 'MainWANObjectCache', $cache );
1835
1836 $store = MediaWikiServices::getInstance()->getRevisionStore();
1837 $page = $this->getNonexistingTestPage();
1838 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1839
1840 // Grab the deleted bitmask
1841 $deletedBefore = $rev->getVisibility();
1842
1843 // Change the deleted bitmask in the database, "behind the back" of the cache
1844 $this->db->update( 'revision',
1845 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1846 [ 'rev_id' => $rev->getId() ] );
1847
1848 // Reload the revision and regrab the visibility flag.
1849 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1850 $deletedAfter = $revAfter->getVisibility();
1851
1852 // The two deleted flags should be different.
1853 // If they are the same, we are seeing a cached value, which is bad.
1854 $this->assertNotSame( $deletedBefore, $deletedAfter );
1855
1856 // This is implied by the above assertion, but explicitly check it, for completeness
1857 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1858 }
1859
1860 /**
1861 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1862 */
1863 public function testNewRevisionFromRow_userNameChange() {
1864 $page = $this->getTestPage();
1865 $text = __METHOD__;
1866 /** @var Revision $rev */
1867 $rev = $page->doEditContent(
1868 new WikitextContent( $text ),
1869 __METHOD__,
1870 0,
1871 false,
1872 $this->getMutableTestUser()->getUser()
1873 )->value['revision'];
1874
1875 $store = MediaWikiServices::getInstance()->getRevisionStore();
1876 $record = $store->newRevisionFromRow(
1877 $this->revisionToRow( $rev ),
1878 [],
1879 $page->getTitle()
1880 );
1881
1882 // Grab the user name
1883 $userNameBefore = $record->getUser()->getName();
1884
1885 // Change the user name in the database
1886 $newUserName = "Renamed $userNameBefore";
1887 $this->db->update( 'revision',
1888 [ 'rev_user_text' => $newUserName ],
1889 [ 'rev_id' => $record->getId() ] );
1890 $this->db->update( 'user',
1891 [ 'user_name' => $newUserName ],
1892 [ 'user_id' => $record->getUser()->getId() ] );
1893 $this->db->update( 'actor',
1894 [ 'actor_name' => $newUserName ],
1895 [ 'actor_user' => $record->getUser()->getId() ] );
1896
1897 // Reload the record, passing $fromCache as true to force fresh info from the db,
1898 // and regrab the user name
1899 $recordAfter = $store->newRevisionFromRow(
1900 $this->revisionToRow( $rev ),
1901 [],
1902 $page->getTitle(),
1903 true
1904 );
1905 $userNameAfter = $recordAfter->getUser()->getName();
1906
1907 // The two user names should be different.
1908 // If they are the same, we are seeing a cached value, which is bad.
1909 $this->assertNotSame( $userNameBefore, $userNameAfter );
1910
1911 // This is implied by the above assertion, but explicitly check it, for completeness
1912 $this->assertSame( $newUserName, $userNameAfter );
1913 }
1914
1915 /**
1916 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1917 */
1918 public function testNewRevisionFromRow_revDelete() {
1919 $page = $this->getTestPage();
1920 $text = __METHOD__;
1921 /** @var Revision $rev */
1922 $rev = $page->doEditContent(
1923 new WikitextContent( $text ),
1924 __METHOD__
1925 )->value['revision'];
1926
1927 $store = MediaWikiServices::getInstance()->getRevisionStore();
1928 $record = $store->newRevisionFromRow(
1929 $this->revisionToRow( $rev ),
1930 [],
1931 $page->getTitle()
1932 );
1933
1934 // Grab the deleted bitmask
1935 $deletedBefore = $record->getVisibility();
1936
1937 // Change the deleted bitmask in the database
1938 $this->db->update( 'revision',
1939 [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ],
1940 [ 'rev_id' => $record->getId() ] );
1941
1942 // Reload the record, passing $fromCache as true to force fresh info from the db,
1943 // and regrab the deleted bitmask
1944 $recordAfter = $store->newRevisionFromRow(
1945 $this->revisionToRow( $rev ),
1946 [],
1947 $page->getTitle(),
1948 true
1949 );
1950 $deletedAfter = $recordAfter->getVisibility();
1951
1952 // The two deleted flags should be different, because we modified the database.
1953 $this->assertNotSame( $deletedBefore, $deletedAfter );
1954
1955 // This is implied by the above assertion, but explicitly check it, for completeness
1956 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1957 }
1958
1959 public function provideNewRevisionsFromBatchOptions() {
1960 yield 'No preload slots or content, single page' => [
1961 null,
1962 []
1963 ];
1964 yield 'Preload slots and content, single page' => [
1965 null,
1966 [
1967 'slots' => [ SlotRecord::MAIN ],
1968 'content' => true
1969 ]
1970 ];
1971 yield 'No preload slots or content, multiple pages' => [
1972 'Other_Page',
1973 []
1974 ];
1975 yield 'Preload slots and content, multiple pages' => [
1976 'Other_Page',
1977 [
1978 'slots' => [ SlotRecord::MAIN ],
1979 'content' => true
1980 ]
1981 ];
1982 }
1983
1984 /**
1985 * @dataProvider provideNewRevisionsFromBatchOptions
1986 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
1987 * @param string|null $otherPageTitle
1988 * @param array|null $options
1989 * @throws \MWException
1990 */
1991 public function testNewRevisionsFromBatch_preloadContent(
1992 $otherPageTitle = null,
1993 array $options = []
1994 ) {
1995 $page1 = $this->getTestPage();
1996 $text = __METHOD__ . 'b-ä';
1997 $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
1998 $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
1999 /** @var Revision $rev1 */
2000 $rev1 = $editStatus->getValue()['revision'];
2001
2002 $page2 = $this->getTestPage( $otherPageTitle );
2003 $editStatus = $this->editPage( $page2->getTitle()->getPrefixedDBkey(), $text . '2' );
2004 $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 2' );
2005 /** @var Revision $rev2 */
2006 $rev2 = $editStatus->getValue()['revision'];
2007
2008 $store = MediaWikiServices::getInstance()->getRevisionStore();
2009 $result = $store->newRevisionsFromBatch(
2010 [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev2 ) ],
2011 $options
2012 );
2013 $this->assertTrue( $result->isGood() );
2014 $this->assertEmpty( $result->getErrors() );
2015 /** @var RevisionRecord[] $records */
2016 $records = $result->getValue();
2017 $this->assertRevisionRecordMatchesRevision( $rev1, $records[$rev1->getId()] );
2018 $this->assertRevisionRecordMatchesRevision( $rev2, $records[$rev2->getId()] );
2019
2020 $this->assertSame( $text . '1',
2021 ContentHandler::getContentText( $records[$rev1->getId()]->getContent( SlotRecord::MAIN ) ) );
2022 $this->assertSame( $text . '2',
2023 ContentHandler::getContentText( $records[$rev2->getId()]->getContent( SlotRecord::MAIN ) ) );
2024 $this->assertEquals( $page1->getTitle()->getDBkey(),
2025 $records[$rev1->getId()]->getPageAsLinkTarget()->getDBkey() );
2026 $this->assertEquals( $page2->getTitle()->getDBkey(),
2027 $records[$rev2->getId()]->getPageAsLinkTarget()->getDBkey() );
2028 }
2029
2030 /**
2031 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
2032 */
2033 public function testNewRevisionsFromBatch_emptyBatch() {
2034 $result = MediaWikiServices::getInstance()->getRevisionStore()
2035 ->newRevisionsFromBatch(
2036 [],
2037 [
2038 'slots' => [ SlotRecord::MAIN ],
2039 'content' => true
2040 ]
2041 );
2042 $this->assertTrue( $result->isGood() );
2043 $this->assertEmpty( $result->getValue() );
2044 $this->assertEmpty( $result->getErrors() );
2045 }
2046
2047 /**
2048 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
2049 */
2050 public function testNewRevisionsFromBatch_wrongTitle() {
2051 $page1 = $this->getTestPage();
2052 $text = __METHOD__ . 'b-ä';
2053 $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
2054 $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
2055 /** @var Revision $rev1 */
2056 $rev1 = $editStatus->getValue()['revision'];
2057
2058 $this->setExpectedException( InvalidArgumentException::class );
2059 MediaWikiServices::getInstance()->getRevisionStore()
2060 ->newRevisionsFromBatch(
2061 [ $this->revisionToRow( $rev1 ) ],
2062 [],
2063 IDBAccessObject::READ_NORMAL,
2064 $this->getTestPage( 'Title_Other_Then_The_One_Revision_Belongs_To' )->getTitle()
2065 );
2066 }
2067
2068 /**
2069 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
2070 */
2071 public function testNewRevisionsFromBatch_DuplicateRows() {
2072 $page1 = $this->getTestPage();
2073 $text = __METHOD__ . 'b-ä';
2074 $editStatus = $this->editPage( $page1->getTitle()->getPrefixedDBkey(), $text . '1' );
2075 $this->assertTrue( $editStatus->isGood(), 'Sanity: must create revision 1' );
2076 /** @var Revision $rev1 */
2077 $rev1 = $editStatus->getValue()['revision'];
2078
2079 $status = MediaWikiServices::getInstance()->getRevisionStore()
2080 ->newRevisionsFromBatch( [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev1 ) ] );
2081
2082 $this->assertFalse( $status->isGood() );
2083 $this->assertTrue( $status->hasMessage( 'internalerror' ) );
2084 }
2085 }