Debian Codegraph @

tests/phpunit/includes/Revision/RevisionStoreDbTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
4
5 use Exception;
6 use InvalidArgumentException;
7 use MediaWiki\CommentStore\CommentStoreComment;
8 use MediaWiki\Content\Content;
9 use MediaWiki\Content\FallbackContent;
10 use MediaWiki\Content\TextContent;
11 use MediaWiki\Content\WikitextContent;
12 use MediaWiki\Linker\LinkTarget;
13 use MediaWiki\MainConfigNames;
14 use MediaWiki\MediaWikiServices;
15 use MediaWiki\Page\PageIdentity;
16 use MediaWiki\Page\PageIdentityValue;
17 use MediaWiki\Revision\IncompleteRevisionException;
18 use MediaWiki\Revision\MutableRevisionRecord;
19 use MediaWiki\Revision\RevisionAccessException;
20 use MediaWiki\Revision\RevisionArchiveRecord;
21 use MediaWiki\Revision\RevisionRecord;
22 use MediaWiki\Revision\RevisionSlots;
23 use MediaWiki\Revision\RevisionStore;
24 use MediaWiki\Revision\RevisionStoreRecord;
25 use MediaWiki\Revision\SlotRecord;
26 use MediaWiki\Revision\SlotRoleRegistry;
27 use MediaWiki\Storage\BlobStore;
28 use MediaWiki\Storage\SqlBlobStore;
29 use MediaWiki\Tests\Unit\DummyServicesTrait;
30 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
31 use MediaWiki\Title\Title;
32 use MediaWiki\Title\TitleFactory;
33 use MediaWiki\User\User;
34 use MediaWiki\User\UserIdentityValue;
35 use MediaWiki\Utils\MWTimestamp;
36 use MediaWikiIntegrationTestCase;
37 use PHPUnit\Framework\MockObject\MockObject;
38 use StatusValue;
39 use Wikimedia\Assert\PreconditionException;
40 use Wikimedia\ObjectCache\HashBagOStuff;
41 use Wikimedia\ObjectCache\WANObjectCache;
42 use Wikimedia\Rdbms\Database;
43 use Wikimedia\Rdbms\DatabaseDomain;
44 use Wikimedia\Rdbms\DatabaseSqlite;
45 use Wikimedia\Rdbms\FakeResultWrapper;
46 use Wikimedia\Rdbms\IDBAccessObject;
47 use Wikimedia\Rdbms\ILoadBalancer;
48 use Wikimedia\Rdbms\LoadBalancer;
49 use Wikimedia\Rdbms\TransactionProfiler;
50 use Wikimedia\TestingAccessWrapper;
51 use WikiPage;
52
53 /**
54 * @group Database
55 * @group RevisionStore
56 * @covers \MediaWiki\Revision\RevisionStore
57 */
58 class RevisionStoreDbTest extends MediaWikiIntegrationTestCase {
59 use DummyServicesTrait;
60 use TempUserTestTrait;
61
62 /**
63 * @var Title
64 */
65 private $testPageTitle;
66
67 /**
68 * @var WikiPage
69 */
70 private $testPage;
71
72 /**
73 * @return Title
74 */
75 protected function getTestPageTitle() {
76 if ( $this->testPageTitle ) {
77 return $this->testPageTitle;
78 }
79
80 $this->testPageTitle = Title::newFromText( 'TestPage-' . __CLASS__ );
81 return $this->testPageTitle;
82 }
83
84 /**
85 * @param string|null $pageTitle whether to force-create a new page
86 * @return WikiPage
87 */
88 protected function getTestPage( $pageTitle = null ) {
89 if ( $pageTitle === null && $this->testPage ) {
90 return $this->testPage;
91 }
92
93 $title = $pageTitle === null ? $this->getTestPageTitle() : Title::newFromText( $pageTitle );
94 $page = $this->getExistingTestPage( $title );
95
96 if ( $pageTitle === null ) {
97 $this->testPage = $page;
98 }
99 return $page;
100 }
101
102 /**
103 * @param array $server
104 * @return LoadBalancer|MockObject
105 */
106 private function getLoadBalancerMock( array $server ) {
107 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
108
109 $lb = $this->getMockBuilder( LoadBalancer::class )
110 ->onlyMethods( [ 'reallyOpenConnection' ] )
111 ->setConstructorArgs( [
112 [ 'servers' => [ $server ], 'localDomain' => $domain ]
113 ] )
114 ->getMock();
115
116 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
117 function ( $i, DatabaseDomain $domain, array $lbInfo ) use ( $server ) {
118 $conn = $this->getDatabaseMock( $server );
119 foreach ( $lbInfo as $k => $v ) {
120 $conn->setLBInfo( $k, $v );
121 }
122
123 return $conn;
124 }
125 );
126
127 return $lb;
128 }
129
130 /**
131 * @param array $params
132 * @return Database|MockObject
133 */
134 private function getDatabaseMock( array $params ) {
135 $db = $this->getMockBuilder( DatabaseSqlite::class )
136 ->onlyMethods( [
137 'select',
138 'doSingleStatementQuery',
139 'open',
140 'closeConnection',
141 'isOpen'
142 ] )->setConstructorArgs( [ $params ] )
143 ->getMock();
144
145 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
146 $db->method( 'isOpen' )->willReturn( true );
147
148 return $db;
149 }
150
151 public static function provideDomainCheck() {
152 yield [ false, 'test', '' ];
153 yield [ 'test', 'test', '' ];
154
155 yield [ false, 'test', 'foo_' ];
156 yield [ 'test-foo_', 'test', 'foo_' ];
157
158 yield [ false, 'dash-test', '' ];
159 yield [ 'dash?htest', 'dash-test', '' ];
160
161 yield [ false, 'underscore_test', 'foo_' ];
162 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
163 }
164
165 /**
166 * @dataProvider provideDomainCheck
167 */
168 public function testDomainCheck( $dbDomain, $dbName, $dbPrefix ) {
169 $this->overrideConfigValues(
170 [
171 MainConfigNames::DBname => $dbName,
172 MainConfigNames::DBprefix => $dbPrefix,
173 ]
174 );
175
176 $loadBalancer = $this->getLoadBalancerMock(
177 [
178 'host' => '*dummy*',
179 'dbDirectory' => '*dummy*',
180 'user' => 'test',
181 'password' => 'test',
182 'flags' => 0,
183 'variables' => [],
184 'schema' => '',
185 'cliMode' => true,
186 'topologyRole' => Database::ROLE_STREAMING_MASTER,
187 'agent' => '',
188 'serverName' => '*dummy*',
189 'load' => 100,
190 'srvCache' => new HashBagOStuff(),
191 'profiler' => null,
192 'trxProfiler' => new TransactionProfiler(),
193 'errorLogger' => static function () {
194 },
195 'deprecationLogger' => static function () {
196 },
197 'type' => 'test',
198 'dbname' => $dbName,
199 'tablePrefix' => $dbPrefix,
200 ]
201 );
202 $db = $loadBalancer->getConnection( DB_REPLICA );
203
204 /** @var SqlBlobStore $blobStore */
205 $blobStore = $this->createMock( SqlBlobStore::class );
206
207 $store = new RevisionStore(
208 $loadBalancer,
209 $blobStore,
210 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
211 new HashBagOStuff(),
212 $this->getServiceContainer()->getCommentStore(),
213 $this->getServiceContainer()->getContentModelStore(),
214 $this->getServiceContainer()->getSlotRoleStore(),
215 $this->getServiceContainer()->getSlotRoleRegistry(),
216 $this->getServiceContainer()->getActorStoreFactory()->getActorStore( $dbDomain ),
217 $this->getServiceContainer()->getContentHandlerFactory(),
218 $this->getServiceContainer()->getPageStore(),
219 $this->getServiceContainer()->getTitleFactory(),
220 $this->getServiceContainer()->getHookContainer(),
221 $dbDomain
222 );
223
224 $count = $store->countRevisionsByPageId( $db, 0 );
225
226 // Dummy check to make PhpUnit happy. We are really only interested in
227 // countRevisionsByPageId not failing due to the DB domain check.
228 $this->assertSame( 0, $count );
229 }
230
231 protected function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
232 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
233 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
234 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
235 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
236 }
237
238 protected function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
239 $this->assertEquals(
240 $r1->getPageAsLinkTarget()->getNamespace(),
241 $r2->getPageAsLinkTarget()->getNamespace()
242 );
243
244 $this->assertEquals(
245 $r1->getPageAsLinkTarget()->getText(),
246 $r2->getPageAsLinkTarget()->getText()
247 );
248
249 if ( $r1->getParentId() ) {
250 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
251 }
252
253 $this->assertEquals( $r1->getUser( RevisionRecord::RAW )->getName(), $r2->getUser( RevisionRecord::RAW )->getName() );
254 $this->assertEquals( $r1->getUser( RevisionRecord::RAW )->getId(), $r2->getUser( RevisionRecord::RAW )->getId() );
255 $this->assertEquals( $r1->getComment( RevisionRecord::RAW ), $r2->getComment( RevisionRecord::RAW ) );
256 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
257 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
258 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
259 $this->assertEquals( $r1->getSize(), $r2->getSize() );
260 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
261 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
262 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
263 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
264 foreach ( $r1->getSlotRoles() as $role ) {
265 $this->assertSlotRecordsEqual( $r1->getSlot( $role, RevisionRecord::RAW ), $r2->getSlot( $role, RevisionRecord::RAW ) );
266 $this->assertTrue( $r1->getContent( $role, RevisionRecord::RAW )->equals( $r2->getContent( $role, RevisionRecord::RAW ) ) );
267 }
268 foreach ( [
269 RevisionRecord::DELETED_TEXT,
270 RevisionRecord::DELETED_COMMENT,
271 RevisionRecord::DELETED_USER,
272 RevisionRecord::DELETED_RESTRICTED,
273 ] as $field ) {
274 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
275 }
276 }
277
278 protected function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
279 $this->assertSame( $s1->getRole(), $s2->getRole() );
280 $this->assertSame( $s1->getModel(), $s2->getModel() );
281 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
282 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
283 $this->assertSame( $s1->getSize(), $s2->getSize() );
284 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
285
286 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
287 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
288 }
289
290 protected function assertRevisionCompleteness( RevisionRecord $r ) {
291 $this->assertTrue( $r->hasSlot( SlotRecord::MAIN ) );
292 $this->assertInstanceOf( SlotRecord::class, $r->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ) );
293 $this->assertInstanceOf( Content::class, $r->getContent( SlotRecord::MAIN, RevisionRecord::RAW ) );
294
295 foreach ( $r->getSlotRoles() as $role ) {
296 $this->assertSlotCompleteness( $r, $r->getSlot( $role, RevisionRecord::RAW ) );
297 }
298 }
299
300 protected function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
301 $this->assertTrue( $slot->hasAddress() );
302 $this->assertSame( $r->getId(), $slot->getRevision() );
303
304 $this->assertInstanceOf( Content::class, $slot->getContent() );
305 }
306
307 /**
308 * @param mixed[] $details
309 *
310 * @return RevisionRecord
311 */
312 private function getRevisionRecordFromDetailsArray( $details = [] ) {
313 // Convert some values that can't be provided by dataProviders
314 if ( isset( $details['user'] ) && $details['user'] === true ) {
315 $details['user'] = $this->getTestUser()->getUser();
316 }
317 if ( isset( $details['page'] ) && $details['page'] === true ) {
318 $details['page'] = $this->getTestPage()->getId();
319 }
320 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
321 $details['parent'] = $this->getTestPage()->getLatest();
322 }
323
324 // Create the RevisionRecord with any available data
325 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
326 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
327 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
328 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
329 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
330 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
331 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
332 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
333 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
334 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
335 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
336 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
337
338 if ( isset( $details['content'] ) ) {
339 foreach ( $details['content'] as $role => $content ) {
340 $rev->setContent( $role, $content );
341 }
342 }
343
344 return $rev;
345 }
346
347 public function provideInsertRevisionOn_successes() {
348 yield 'Bare minimum revision insertion' => [
349 [
350 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
351 'page' => true,
352 'comment' => $this->getRandomCommentStoreComment(),
353 'timestamp' => '20171117010101',
354 'user' => true,
355 ],
356 ];
357 yield 'Detailed revision insertion' => [
358 [
359 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
360 'parent' => true,
361 'page' => true,
362 'comment' => $this->getRandomCommentStoreComment(),
363 'timestamp' => '20171117010101',
364 'user' => true,
365 'minor' => true,
366 'visibility' => RevisionRecord::DELETED_RESTRICTED,
367 ],
368 ];
369 yield 'Multi-slot revision insertion' => [
370 [
371 'content' => [
372 SlotRecord::MAIN => new WikitextContent( 'Chicken' ),
373 'aux' => new TextContent( 'Egg' ),
374 ],
375 'page' => true,
376 'comment' => $this->getRandomCommentStoreComment(),
377 'timestamp' => '20171117010101',
378 'user' => true,
379 ],
380 ];
381 }
382
383 protected function getRandomCommentStoreComment() {
384 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
385 }
386
387 /**
388 * @dataProvider provideInsertRevisionOn_successes
389 */
390 public function testInsertRevisionOn_successes(
391 array $revDetails = []
392 ) {
393 $title = $this->getTestPageTitle();
394 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
395
396 $store = $this->getServiceContainer()->getRevisionStore();
397 $return = $store->insertRevisionOn( $rev, $this->getDb() );
398
399 // is the new revision correct?
400 $this->assertRevisionCompleteness( $return );
401 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
402 $this->assertRevisionRecordsEqual( $rev, $return );
403
404 // can we load it from the store?
405 $loaded = $store->getRevisionById( $return->getId() );
406 $this->assertRevisionCompleteness( $loaded );
407 $this->assertRevisionRecordsEqual( $return, $loaded );
408
409 // can we find it directly in the database?
410 $this->assertRevisionExistsInDatabase( $return );
411 }
412
413 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
414 $numberOfSlots = count( $rev->getSlotRoles() );
415
416 // new schema is written
417 $this->newSelectQueryBuilder()
418 ->select( 'count(*)' )
419 ->from( 'slots' )
420 ->where( [ 'slot_revision_id' => $rev->getId() ] )
421 ->assertFieldValue( $numberOfSlots );
422
423 $store = $this->getServiceContainer()->getRevisionStore();
424 $revQuery = $store->getSlotsQueryInfo( [ 'content' ] );
425
426 $this->newSelectQueryBuilder()
427 ->queryInfo( [
428 'tables' => $revQuery['tables'],
429 'joins' => $revQuery['joins']
430 ] )
431 ->select( 'count(*)' )
432 ->where( [ 'slot_revision_id' => $rev->getId() ] )
433 ->assertFieldValue( $numberOfSlots );
434
435 $row = $this->revisionRecordToRow( $rev, [] );
436
437 // unset nulled fields
438 unset( $row->rev_content_model );
439 unset( $row->rev_content_format );
440
441 // unset fake fields
442 unset( $row->rev_comment_text );
443 unset( $row->rev_comment_data );
444 unset( $row->rev_comment_cid );
445 unset( $row->rev_comment_id );
446
447 $queryInfo = $store->getQueryInfo( [ 'user' ] );
448
449 $row = get_object_vars( $row );
450
451 // Use aliased fields from $queryInfo, e.g. rev_user
452 $keys = array_keys( $row );
453 $keys = array_combine( $keys, $keys );
454 $fields = array_intersect_key( $queryInfo['fields'], $keys ) + $keys;
455
456 // assertSelect() fails unless the orders match.
457 ksort( $fields );
458 ksort( $row );
459
460 $this->newSelectQueryBuilder()
461 ->select( $fields )
462 ->queryInfo( [
463 'tables' => $queryInfo['tables'],
464 'joins' => $queryInfo['joins']
465 ] )
466 ->where( [ 'rev_id' => $rev->getId() ] )
467 ->assertResultSet( [ array_values( $row ) ] );
468 }
469
470 /**
471 * @param SlotRecord $a
472 * @param SlotRecord $b
473 */
474 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
475 // Assert that the same blob address has been used.
476 $this->assertSame( $a->getAddress(), $b->getAddress() );
477 // Assert that the same content ID has been used
478 $this->assertSame( $a->getContentId(), $b->getContentId() );
479 }
480
481 public function testInsertRevisionOn_blobAddressExists() {
482 $title = $this->getTestPageTitle();
483 $revDetails = [
484 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
485 'parent' => true,
486 'comment' => $this->getRandomCommentStoreComment(),
487 'timestamp' => '20171117010101',
488 'user' => true,
489 ];
490
491 $store = $this->getServiceContainer()->getRevisionStore();
492
493 // Insert the first revision
494 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
495 $firstReturn = $store->insertRevisionOn( $revOne, $this->getDb() );
496 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
497 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
498
499 // Insert a second revision inheriting the same blob address
500 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( SlotRecord::MAIN ) );
501 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
502 $secondReturn = $store->insertRevisionOn( $revTwo, $this->getDb() );
503 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
504 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
505
506 $firstMainSlot = $firstReturn->getSlot( SlotRecord::MAIN );
507 $secondMainSlot = $secondReturn->getSlot( SlotRecord::MAIN );
508
509 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
510
511 // And that different revisions have been created.
512 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
513
514 // Make sure the slot rows reference the correct revision
515 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
516 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
517 }
518
519 public function provideInsertRevisionOn_failures() {
520 yield 'no slot' => [
521 [
522 'comment' => $this->getRandomCommentStoreComment(),
523 'timestamp' => '20171117010101',
524 'user' => true,
525 ],
526 new IncompleteRevisionException( 'main slot must be provided' )
527 ];
528 yield 'no main slot' => [
529 [
530 'slot' => SlotRecord::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
531 'comment' => $this->getRandomCommentStoreComment(),
532 'timestamp' => '20171117010101',
533 'user' => true,
534 ],
535 new IncompleteRevisionException( 'main slot must be provided' )
536 ];
537 yield 'no timestamp' => [
538 [
539 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
540 'comment' => $this->getRandomCommentStoreComment(),
541 'user' => true,
542 ],
543 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
544 ];
545 yield 'no comment' => [
546 [
547 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
548 'timestamp' => '20171117010101',
549 'user' => true,
550 ],
551 new IncompleteRevisionException( 'comment must not be NULL!' )
552 ];
553 yield 'no user' => [
554 [
555 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
556 'comment' => $this->getRandomCommentStoreComment(),
557 'timestamp' => '20171117010101',
558 ],
559 new IncompleteRevisionException( 'user must not be NULL!' )
560 ];
561 yield 'size mismatch' => [
562 [
563 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
564 'comment' => $this->getRandomCommentStoreComment(),
565 'timestamp' => '20171117010101',
566 'user' => true,
567 'size' => 123456
568 ],
569 new PreconditionException( 'T239717' )
570 ];
571 yield 'sha1 mismatch' => [
572 [
573 'slot' => SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'Chicken' ) ),
574 'comment' => $this->getRandomCommentStoreComment(),
575 'timestamp' => '20171117010101',
576 'user' => true,
577 'sha1' => 'DEADBEEF',
578 ],
579 new PreconditionException( 'T239717' )
580 ];
581 }
582
583 /**
584 * @dataProvider provideInsertRevisionOn_failures
585 */
586 public function testInsertRevisionOn_failures(
587 array $revDetails,
588 Exception $exception
589 ) {
590 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
591
592 $store = $this->getServiceContainer()->getRevisionStore();
593
594 $this->expectException( get_class( $exception ) );
595 $this->expectExceptionMessage( $exception->getMessage() );
596 $this->expectExceptionCode( $exception->getCode() );
597 $store->insertRevisionOn( $rev, $this->getDb() );
598 }
599
600 public static function provideNewNullRevision() {
601 yield [
602 Title::newFromText( 'NewNullRevision_notAutoCreated' ),
603 [ 'content' => [ SlotRecord::MAIN => new WikitextContent( 'Flubber1' ) ] ],
604 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
605 true,
606 ];
607 yield [
608 Title::newFromText( 'NewNullRevision_notAutoCreated' ),
609 [ 'content' => [ SlotRecord::MAIN => new WikitextContent( 'Flubber2' ) ] ],
610 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
611 false,
612 ];
613 yield [
614 Title::newFromText( 'NewNullRevision_notAutoCreated' ),
615 [
616 'content' => [
617 SlotRecord::MAIN => new WikitextContent( 'Chicken' ),
618 'aux' => new WikitextContent( 'Omelet' ),
619 ],
620 ],
621 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment multi' ),
622 ];
623 }
624
625 /**
626 * @dataProvider provideNewNullRevision
627 */
628 public function testNewNullRevision( Title $title, $revDetails, $comment, $minor = false ) {
629 $user = $this->getMutableTestUser()->getUser();
630 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
631
632 if ( !$page->exists() ) {
633 $page->doUserEditContent(
634 new WikitextContent( __METHOD__ ),
635 $this->getTestSysop()->getUser(),
636 __METHOD__,
637 EDIT_NEW
638 );
639 }
640
641 $revDetails['page'] = $page->getId();
642 $revDetails['timestamp'] = wfTimestampNow();
643 $revDetails['comment'] = CommentStoreComment::newUnsavedComment( 'Base' );
644 $revDetails['user'] = $user;
645
646 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
647 $store = $this->getServiceContainer()->getRevisionStore();
648
649 $dbw = $this->getDb();
650 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
651 $page->updateRevisionOn( $dbw, $baseRev, $page->getLatest() );
652
653 $record = $store->newNullRevision(
654 $this->getDb(),
655 $title,
656 $comment,
657 $minor,
658 $user
659 );
660
661 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
662 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
663 $this->assertEquals( $comment, $record->getComment() );
664 $this->assertEquals( $minor, $record->isMinor() );
665 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
666 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
667
668 $this->assertArrayEquals(
669 $baseRev->getSlotRoles(),
670 $record->getSlotRoles()
671 );
672
673 foreach ( $baseRev->getSlotRoles() as $role ) {
674 $parentSlot = $baseRev->getSlot( $role );
675 $slot = $record->getSlot( $role );
676
677 $this->assertTrue( $slot->isInherited(), 'isInherited' );
678 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
679 $this->assertSameSlotContent( $parentSlot, $slot );
680 }
681 }
682
683 public function testNewNullRevision_nonExistingTitle() {
684 $store = $this->getServiceContainer()->getRevisionStore();
685 $record = $store->newNullRevision(
686 $this->getDb(),
687 Title::newFromText( __METHOD__ . '.iDontExist!' ),
688 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
689 false,
690 $this->getMutableTestUser()->getUser()
691 );
692 $this->assertNull( $record );
693 }
694
695 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
696 $page = $this->getTestPage();
697 $status = $page->doUserEditContent(
698 new WikitextContent( __METHOD__ ),
699 $this->getTestUser()->getUser(),
700 __METHOD__
701 );
702 $revRecord = $status->getNewRevision();
703
704 $store = $this->getServiceContainer()->getRevisionStore();
705 $storeRecord = $store->getRevisionById( $revRecord->getId() );
706 $result = $store->getRcIdIfUnpatrolled( $storeRecord );
707
708 $this->assertGreaterThan( 0, $result );
709 $this->assertSame(
710 $store->getRecentChange( $storeRecord )->getAttribute( 'rc_id' ),
711 $result
712 );
713 }
714
715 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
716 // This assumes that sysops are auto patrolled
717 $sysop = $this->getTestSysop()->getUser();
718 $page = $this->getTestPage();
719 $status = $page->doUserEditContent(
720 new WikitextContent( __METHOD__ ),
721 $sysop,
722 __METHOD__
723 );
724 $revRecord = $status->getNewRevision();
725
726 $store = $this->getServiceContainer()->getRevisionStore();
727 $storeRecord = $store->getRevisionById( $revRecord->getId() );
728 $result = $store->getRcIdIfUnpatrolled( $storeRecord );
729
730 $this->assertSame( 0, $result );
731 }
732
733 public function testGetRecentChange() {
734 $page = $this->getTestPage();
735 $content = new WikitextContent( __METHOD__ );
736 $status = $page->doUserEditContent(
737 $content,
738 $this->getTestSysop()->getUser(),
739 __METHOD__
740 );
741 $revRecord = $status->getNewRevision();
742
743 $store = $this->getServiceContainer()->getRevisionStore();
744 $storeRecord = $store->getRevisionById( $revRecord->getId() );
745 $recentChange = $store->getRecentChange( $storeRecord );
746
747 $this->assertEquals( $revRecord->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
748 }
749
750 public function testGetRevisionById() {
751 $page = $this->getTestPage();
752 $content = new WikitextContent( __METHOD__ );
753 $status = $page->doUserEditContent(
754 $content,
755 $this->getTestSysop()->getUser(),
756 __METHOD__
757 );
758 $revRecord = $status->getNewRevision();
759
760 $store = $this->getServiceContainer()->getRevisionStore();
761 $storeRecord = $store->getRevisionById( $revRecord->getId() );
762
763 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
764 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
765 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
766 }
767
768 public function testGetRevisionById_crossWiki_withPage() {
769 $page = $this->getTestPage();
770 $content = new WikitextContent( __METHOD__ );
771 $status = $page->doUserEditContent(
772 $content,
773 $this->getTestSysop()->getUser(),
774 __METHOD__
775 );
776 $revRecord = $status->getNewRevision();
777 $revId = $revRecord->getId();
778
779 // Pretend the local test DB is a sister site
780 $wikiId = $this->getDb()->getDomainID();
781 $store = $this->getServiceContainer()->getRevisionStoreFactory()
782 ->getRevisionStore( $wikiId );
783
784 // Construct a ProperPageIdentity with the sister site's wiki Id
785 $pageIdentity = new PageIdentityValue(
786 $page->getId(), $page->getNamespace(), $page->getDBkey(), $wikiId
787 );
788 $storeRecord = $store->getRevisionById( $revId, 0, $pageIdentity );
789
790 $this->assertSame( $wikiId, $storeRecord->getWikiId() );
791 $this->assertSame( $revId, $storeRecord->getId( $wikiId ) );
792 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
793 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
794 }
795
796 public function testGetRevisionById_crossWiki() {
797 $page = $this->getTestPage();
798 $content = new WikitextContent( __METHOD__ );
799 $status = $page->doUserEditContent(
800 $content,
801 $this->getTestSysop()->getUser(),
802 __METHOD__
803 );
804 $revRecord = $status->getNewRevision();
805 $revId = $revRecord->getId();
806 $pageId = $revRecord->getPageId();
807
808 // Make TitleFactory always fail, since it should not be used for the cross-wiki case.
809 $noOpTitleFactory = $this->createNoOpMock( TitleFactory::class );
810 $this->setService( 'TitleFactory', $noOpTitleFactory );
811
812 // Pretend the local test DB is a sister site
813 $wikiId = $this->getDb()->getDomainID();
814 $store = $this->getServiceContainer()->getRevisionStoreFactory()
815 ->getRevisionStore( $wikiId );
816
817 $storeRecord = $store->getRevisionById( $revId );
818
819 $this->assertSame( $wikiId, $storeRecord->getWikiId() );
820 $this->assertSame( $wikiId, $storeRecord->getPage()->getWikiId() );
821 $this->assertNotInstanceOf( Title::class, $storeRecord->getPage() );
822 $this->assertSame( $revId, $storeRecord->getId( $wikiId ) );
823 $this->assertSame( $pageId, $storeRecord->getPage()->getId( $wikiId ) );
824 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
825 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
826 }
827
828 public function testGetRevisionById_undefinedContentModel() {
829 $page = $this->getTestPage();
830 $content = new WikitextContent( __METHOD__ );
831 $status = $page->doUserEditContent(
832 $content,
833 $this->getTestSysop()->getUser(),
834 __METHOD__
835 );
836 $revRecord = $status->getNewRevision();
837
838 $mockContentHandlerFactory = $this->getDummyContentHandlerFactory();
839 $this->setService( 'ContentHandlerFactory', $mockContentHandlerFactory );
840 $store = $this->getServiceContainer()->getRevisionStore();
841
842 $storeRecord = $store->getRevisionById( $revRecord->getId() );
843
844 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
845
846 $actualContent = $storeRecord->getSlot( SlotRecord::MAIN )->getContent();
847 $this->assertInstanceOf( FallbackContent::class, $actualContent );
848 $this->assertSame( __METHOD__, $actualContent->serialize() );
849 }
850
851 /**
852 * @dataProvider provideRevisionByTitle
853 *
854 * @param callable $getTitle
855 */
856 public function testGetRevisionByTitle( $getTitle ) {
857 $page = $this->getTestPage();
858 $title = $getTitle();
859 $content = new WikitextContent( __METHOD__ );
860 $status = $page->doUserEditContent(
861 $content,
862 $this->getTestSysop()->getUser(),
863 __METHOD__
864 );
865 $revRecord = $status->getNewRevision();
866
867 $store = $this->getServiceContainer()->getRevisionStore();
868 $storeRecord = $store->getRevisionByTitle( $title );
869
870 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
871 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
872 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
873 }
874
875 public function provideRevisionByTitle() {
876 return [
877 [ function () {
878 return $this->getTestPageTitle();
879 } ],
880 [ function () {
881 return $this->getTestPageTitle()->toPageIdentity();
882 } ]
883 ];
884 }
885
886 private function executeWithForeignStore( string $dbDomain, callable $callback ) {
887 $services = $this->getServiceContainer();
888 // Configure the load balancer to route queries for the "foreign" domain to the test DB
889 $dbLoadBalancer = $services->getDBLoadBalancer();
890 $dbLoadBalancer->setDomainAliases( [ $dbDomain => $dbLoadBalancer->getLocalDomainID() ] );
891 $store = new RevisionStore(
892 $dbLoadBalancer,
893 $services->getBlobStore(),
894 $services->getMainWANObjectCache(),
895 new HashBagOStuff(),
896 $services->getCommentStore(),
897 $services->getContentModelStore(),
898 $services->getSlotRoleStore(),
899 $services->getSlotRoleRegistry(),
900 $services->getActorStoreFactory()->getActorStore( $dbDomain ),
901 $services->getContentHandlerFactory(),
902 $services->getPageStoreFactory()->getPageStore( $dbDomain ),
903 $services->getTitleFactory(),
904 $services->getHookContainer(),
905 $dbDomain
906 );
907
908 // Redefine the DBLoadBalancer service to verify Title doesn't attempt to resolve its ID
909 // via getPrimaryDatabase() etc.
910 $localLoadBalancerMock = $this->createMock( ILoadBalancer::class );
911 $localLoadBalancerMock->expects( $this->never() )
912 ->method( $this->anything() );
913
914 try {
915 $this->setService( 'DBLoadBalancer', $localLoadBalancerMock );
916 // There may be other code which indirectly uses the RevisionStore
917 // service; make sure it picks up the external store as well.
918 $this->setService( 'RevisionStore', $store );
919 $callback( $store );
920 } finally {
921 // Restore the original load balancer to make test teardown work
922 $this->setService( 'DBLoadBalancer', $dbLoadBalancer );
923 }
924 }
925
926 public function testGetLatestKnownRevision_foreigh() {
927 $page = $this->getTestPage();
928 $status = $this->editPage( $page, __METHOD__ );
929 $this->assertStatusGood( $status, 'edited a page' );
930 $revRecord = $status->getNewRevision();
931 $dbDomain = 'some_foreign_wiki';
932 $this->executeWithForeignStore(
933 $dbDomain,
934 function ( RevisionStore $store ) use ( $page, $dbDomain, $revRecord ) {
935 $storeRecord = $store->getKnownCurrentRevision(
936 new PageIdentityValue( $page->getId(), $page->getNamespace(), $page->getDBkey(), $dbDomain )
937 );
938 $this->assertSame( $dbDomain, $storeRecord->getWikiId() );
939 $this->assertSame( $revRecord->getId(), $storeRecord->getId( $dbDomain ) );
940 } );
941 }
942
943 /**
944 * getRevisionByTitle should not use the local wiki DB (T248756)
945 */
946 public function testGetRevisionByTitle_doesNotUseLocalLoadBalancerForForeignWiki() {
947 $page = $this->getTestPage();
948 $content = new WikitextContent( __METHOD__ );
949 $comment = __METHOD__;
950 $status = $page->doUserEditContent(
951 $content,
952 $this->getTestSysop()->getUser(),
953 $comment
954 );
955 $revRecord = $status->getNewRevision();
956 $dbDomain = 'some_foreign_wiki';
957 $this->executeWithForeignStore(
958 $dbDomain,
959 function ( RevisionStore $store ) use ( $page, $dbDomain, $revRecord, $content, $comment ) {
960 $storeRecord = $store->getRevisionByTitle(
961 new PageIdentityValue(
962 $page->getId(),
963 $page->getTitle()->getNamespace(),
964 $page->getTitle()->getDBkey(),
965 $dbDomain
966 )
967 );
968 $this->assertSame( $revRecord->getId(), $storeRecord->getId( $dbDomain ) );
969 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
970 $this->assertSame( $comment, $storeRecord->getComment()->text );
971 } );
972 }
973
974 public function testGetRevisionByPageId() {
975 $page = $this->getTestPage();
976 $content = new WikitextContent( __METHOD__ );
977 $status = $page->doUserEditContent(
978 $content,
979 $this->getTestSysop()->getUser(),
980 __METHOD__
981 );
982 $revRecord = $status->getNewRevision();
983
984 $store = $this->getServiceContainer()->getRevisionStore();
985 $storeRecord = $store->getRevisionByPageId( $page->getId() );
986
987 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
988 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
989 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
990
991 $storeRecord = $store->getRevisionByPageId( $page->getId(), 0, IDBAccessObject::READ_LOCKING );
992 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
993 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
994 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
995 }
996
997 /**
998 * @dataProvider provideRevisionByTitle
999 *
1000 * @param callable $getTitle
1001 */
1002 public function testGetRevisionByTimestamp( $getTitle ) {
1003 // Make sure there is 1 second between the last revision and the rev we create...
1004 // Otherwise we might not get the correct revision and the test may fail...
1005 MWTimestamp::setFakeTime( '20110401090000' );
1006 $page = $this->getTestPage();
1007 $title = $getTitle();
1008 MWTimestamp::setFakeTime( '20110401090001' );
1009 $content = new WikitextContent( __METHOD__ );
1010 $status = $page->doUserEditContent(
1011 $content,
1012 $this->getTestSysop()->getUser(),
1013 __METHOD__
1014 );
1015 $revRecord = $status->getNewRevision();
1016
1017 $store = $this->getServiceContainer()->getRevisionStore();
1018 $storeRecord = $store->getRevisionByTimestamp(
1019 $title,
1020 $revRecord->getTimestamp()
1021 );
1022
1023 $this->assertSame( $revRecord->getId(), $storeRecord->getId() );
1024 $this->assertTrue( $storeRecord->getSlot( SlotRecord::MAIN )->getContent()->equals( $content ) );
1025 $this->assertSame( __METHOD__, $storeRecord->getComment()->text );
1026 }
1027
1028 protected function revisionRecordToRow( RevisionRecord $revRecord, $options = [ 'page', 'user', 'comment' ] ) {
1029 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
1030 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $revRecord->getPage() );
1031
1032 $revUser = $revRecord->getUser();
1033 $actorId = $this->getServiceContainer()
1034 ->getActorNormalization()->findActorId( $revUser, $this->getDb() );
1035
1036 $fields = [
1037 'rev_id' => (string)$revRecord->getId(),
1038 'rev_page' => (string)$revRecord->getPageId(),
1039 'rev_timestamp' => $this->getDb()->timestamp( $revRecord->getTimestamp() ),
1040 'rev_actor' => $actorId,
1041 'rev_user_text' => $revUser ? $revUser->getName() : '',
1042 'rev_user' => (string)( $revUser ? $revUser->getId() : 0 ) ?: null,
1043 'rev_minor_edit' => $revRecord->isMinor() ? '1' : '0',
1044 'rev_deleted' => (string)$revRecord->getVisibility(),
1045 'rev_len' => (string)$revRecord->getSize(),
1046 'rev_parent_id' => (string)$revRecord->getParentId(),
1047 'rev_sha1' => (string)$revRecord->getSha1(),
1048 ];
1049
1050 if ( in_array( 'page', $options ) ) {
1051 $fields += [
1052 'page_namespace' => (string)$page->getTitle()->getNamespace(),
1053 'page_title' => $page->getTitle()->getDBkey(),
1054 'page_id' => (string)$page->getId(),
1055 'page_latest' => (string)$page->getLatest(),
1056 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
1057 'page_len' => (string)$page->getContent()->getSize(),
1058 ];
1059 }
1060
1061 if ( in_array( 'user', $options ) ) {
1062 $fields += [
1063 'user_name' => $revUser ? $revUser->getName() : ''
1064 ];
1065 }
1066
1067 if ( in_array( 'comment', $options ) ) {
1068 $revComment = $revRecord->getComment();
1069 $fields += [
1070 'rev_comment_text' => $revComment ? $revComment->text : null,
1071 'rev_comment_data' => $revComment ? $revComment->data : null,
1072 'rev_comment_cid' => $revComment ? $revComment->id : null,
1073 ];
1074 }
1075
1076 if ( $revRecord->getId() ) {
1077 $fields += [
1078 'rev_id' => (string)$revRecord->getId(),
1079 ];
1080 }
1081
1082 return (object)$fields;
1083 }
1084
1085 public function testNewRevisionFromRowAndSlots_getQueryInfo() {
1086 $page = $this->getTestPage();
1087 $text = __METHOD__ . 'o-ö';
1088 $revRecord = $page->doUserEditContent(
1089 new WikitextContent( $text ),
1090 $this->getTestSysop()->getUser(),
1091 __METHOD__ . 'a'
1092 )->getNewRevision();
1093
1094 $store = $this->getServiceContainer()->getRevisionStore();
1095 $info = $store->getQueryInfo();
1096 $row = $this->getDb()->newSelectQueryBuilder()
1097 ->queryInfo( $info )
1098 ->where( [ 'rev_id' => $revRecord->getId() ] )
1099 ->caller( __METHOD__ )
1100 ->fetchRow();
1101
1102 $info = $store->getSlotsQueryInfo( [ 'content' ] );
1103 $slotRows = $this->getDb()->newSelectQueryBuilder()
1104 ->queryInfo( $info )
1105 ->where( [ 'slot_revision_id' => $revRecord->getId() ] )
1106 ->caller( __METHOD__ )
1107 ->fetchResultSet();
1108
1109 $storeRecord = $store->newRevisionFromRowAndSlots(
1110 $row,
1111 iterator_to_array( $slotRows ),
1112 0,
1113 $page->getTitle()
1114 );
1115 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1116 $this->assertSame( $text, $revRecord->getContent( SlotRecord::MAIN )->serialize() );
1117 }
1118
1119 public function testNewRevisionFromRow_getQueryInfo() {
1120 $page = $this->getTestPage();
1121 $text = __METHOD__ . 'a-ä';
1122 $revRecord = $page->doUserEditContent(
1123 new WikitextContent( $text ),
1124 $this->getTestSysop()->getUser(),
1125 __METHOD__ . 'a'
1126 )->getNewRevision();
1127
1128 $store = $this->getServiceContainer()->getRevisionStore();
1129 $info = $store->getQueryInfo();
1130 $row = $this->getDb()->newSelectQueryBuilder()
1131 ->queryInfo( $info )
1132 ->where( [ 'rev_id' => $revRecord->getId() ] )
1133 ->caller( __METHOD__ )
1134 ->fetchRow();
1135 $storeRecord = $store->newRevisionFromRow(
1136 $row,
1137 0,
1138 $page->getTitle()
1139 );
1140 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1141 $this->assertSame( $text, $revRecord->getContent( SlotRecord::MAIN )->serialize() );
1142 }
1143
1144 public function testNewRevisionFromRow_anonEdit() {
1145 $page = $this->getTestPage();
1146 $text = __METHOD__ . 'a-ä';
1147 $revRecord = $page->doUserEditContent(
1148 new WikitextContent( $text ),
1149 $this->getTestSysop()->getUser(),
1150 __METHOD__ . 'a'
1151 )->getNewRevision();
1152
1153 $store = $this->getServiceContainer()->getRevisionStore();
1154 $storeRecord = $store->newRevisionFromRow(
1155 $this->revisionRecordToRow( $revRecord ),
1156 0,
1157 $page->getTitle()
1158 );
1159 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1160 $this->assertSame( $text, $revRecord->getContent( SlotRecord::MAIN )->serialize() );
1161 }
1162
1163 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1164 $this->overrideConfigValue( MainConfigNames::LegacyEncoding, 'windows-1252' );
1165 $page = $this->getTestPage();
1166 $text = __METHOD__ . 'a-ä';
1167 $revRecord = $page->doUserEditContent(
1168 new WikitextContent( $text ),
1169 $this->getTestSysop()->getUser(),
1170 __METHOD__ . 'a'
1171 )->getNewRevision();
1172
1173 $store = $this->getServiceContainer()->getRevisionStore();
1174 $storeRecord = $store->newRevisionFromRow(
1175 $this->revisionRecordToRow( $revRecord ),
1176 0,
1177 $page->getTitle()
1178 );
1179 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1180 $this->assertSame( $text, $revRecord->getContent( SlotRecord::MAIN )->serialize() );
1181 }
1182
1183 public function testNewRevisionFromRow_userEdit() {
1184 $page = $this->getTestPage();
1185 $text = __METHOD__ . 'b-ä';
1186 $revRecord = $page->doUserEditContent(
1187 new WikitextContent( $text ),
1188 $this->getTestUser()->getUser(),
1189 __METHOD__ . 'b'
1190 )->getNewRevision();
1191
1192 $store = $this->getServiceContainer()->getRevisionStore();
1193 $storeRecord = $store->newRevisionFromRow(
1194 $this->revisionRecordToRow( $revRecord ),
1195 0,
1196 $page->getTitle()
1197 );
1198 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1199 $this->assertSame( $text, $revRecord->getContent( SlotRecord::MAIN )->serialize() );
1200 }
1201
1202 private function buildRevisionStore( string $text, PageIdentity $pageIdentity ) {
1203 $store = $this->getServiceContainer()->getRevisionStore();
1204 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $pageIdentity );
1205 $orig = $page->doUserEditContent(
1206 new WikitextContent( $text ),
1207 $this->getTestSysop()->getUser(),
1208 __METHOD__
1209 )->getNewRevision();
1210 $this->deletePage( $page );
1211
1212 $res = $store->newArchiveSelectQueryBuilder( $this->getDb() )
1213 ->joinComment()
1214 ->where( [ 'ar_rev_id' => $orig->getId() ] )
1215 ->caller( __METHOD__ )->fetchResultSet();
1216 $this->assertIsObject( $res, 'query failed' );
1217
1218 $info = $store->getSlotsQueryInfo( [ 'content' ] );
1219 $slotRows = $this->getDb()->newSelectQueryBuilder()
1220 ->queryInfo( $info )
1221 ->where( [ 'slot_revision_id' => $orig->getId() ] )
1222 ->caller( __METHOD__ )
1223 ->fetchResultSet();
1224
1225 $row = $res->fetchObject();
1226 $res->free();
1227 return [ $store, $row, $slotRows, $orig ];
1228 }
1229
1230 public function testNewRevisionFromArchiveRowAndSlots_getArchiveQueryInfo() {
1231 $text = __METHOD__ . '-bä';
1232 $title = Title::newFromText( __METHOD__ );
1233 [ $store, $row, $slotRows, $orig ] = $this->buildRevisionStore( $text, $title );
1234 $storeRecord = $store->newRevisionFromArchiveRowAndSlots(
1235 $row,
1236 iterator_to_array( $slotRows )
1237 );
1238 $this->assertRevisionRecordsEqual( $orig, $storeRecord );
1239 $this->assertSame( $text, $storeRecord->getContent( SlotRecord::MAIN, RevisionRecord::RAW )->serialize() );
1240 }
1241
1242 public static function provideNewRevisionFromArchiveRowAndSlotsTitles() {
1243 return [
1244 [ static function () {
1245 return Title::newFromText( 'Test_NewRevisionFromArchiveRowAndSlotsTitles' );
1246 } ],
1247 [ static function () {
1248 return Title::newFromText( 'Test_NewRevisionFromArchiveRowAndSlotsTitles' )->toPageIdentity();
1249 } ]
1250 ];
1251 }
1252
1253 /**
1254 * @dataProvider provideNewRevisionFromArchiveRowAndSlotsTitles
1255 */
1256 public function testNewRevisionFromArchiveRowAndSlots_getArchiveQueryInfoWithTitle( $getPageIdentity ) {
1257 $text = __METHOD__ . '-bä';
1258 $page = $getPageIdentity();
1259 [ $store, $row, $slotRows, $orig ] = $this->buildRevisionStore( $text, $page );
1260 $storeRecord = $store->newRevisionFromArchiveRowAndSlots(
1261 $row,
1262 iterator_to_array( $slotRows ),
1263 0,
1264 $page
1265 );
1266
1267 $this->assertRevisionRecordsEqual( $orig, $storeRecord );
1268 $this->assertSame( $text, $storeRecord->getContent( SlotRecord::MAIN, RevisionRecord::RAW )->serialize() );
1269 }
1270
1271 public static function provideNewRevisionFromArchiveRowAndSlotsInArray() {
1272 return [
1273 [
1274 [
1275 'title' => Title::newFromText( 'Test_NewRevisionFromArchiveRowAndSlotsInArray' )
1276 ]
1277 ],
1278 [
1279 [
1280 'title' => Title::newFromText( 'Test_NewRevisionFromArchiveRowAndSlotsInArray' )->toPageIdentity()
1281 ]
1282 ],
1283 ];
1284 }
1285
1286 /**
1287 * @dataProvider provideNewRevisionFromArchiveRowAndSlotsInArray
1288 */
1289 public function testNewRevisionFromArchiveRowAndSlots_getArchiveQueryInfoWithTitleInArray( $array ) {
1290 $text = __METHOD__ . '-bä';
1291 $page = $array[ 'title' ];
1292 [ $store, $row, $slotRows, $orig ] = $this->buildRevisionStore( $text, $page );
1293 $storeRecord = $store->newRevisionFromArchiveRowAndSlots(
1294 $row,
1295 iterator_to_array( $slotRows ),
1296 0,
1297 null,
1298 $array
1299 );
1300
1301 $this->assertRevisionRecordsEqual( $orig, $storeRecord );
1302 $this->assertSame( $text, $storeRecord->getContent( SlotRecord::MAIN, RevisionRecord::RAW )->serialize() );
1303 }
1304
1305 /**
1306 * @covers \MediaWiki\Revision\ArchiveSelectQueryBuilder
1307 */
1308 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1309 $store = $this->getServiceContainer()->getRevisionStore();
1310 $title = Title::newFromText( __METHOD__ );
1311 $text = __METHOD__ . '-bä';
1312 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1313 $orig = $page->doUserEditContent(
1314 new WikitextContent( $text ),
1315 $this->getTestSysop()->getUser(),
1316 __METHOD__
1317 )->getNewRevision();
1318 $this->deletePage( $page );
1319
1320 $res = $store->newArchiveSelectQueryBuilder( $this->getDb() )
1321 ->joinComment()
1322 ->where( [ 'ar_rev_id' => $orig->getId() ] )
1323 ->caller( __METHOD__ )->fetchResultSet();
1324 $this->assertIsObject( $res, 'query failed' );
1325
1326 $row = $res->fetchObject();
1327 $res->free();
1328 $storeRecord = $store->newRevisionFromArchiveRow( $row );
1329
1330 $this->assertRevisionRecordsEqual( $orig, $storeRecord );
1331 $this->assertSame( $text, $storeRecord->getContent( SlotRecord::MAIN, RevisionRecord::RAW )->serialize() );
1332 }
1333
1334 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1335 $this->overrideConfigValue( MainConfigNames::LegacyEncoding, 'windows-1252' );
1336 $store = $this->getServiceContainer()->getRevisionStore();
1337 $title = Title::newFromText( __METHOD__ );
1338 $text = __METHOD__ . '-bä';
1339 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1340 $orig = $page->doUserEditContent(
1341 new WikitextContent( $text ),
1342 $this->getTestSysop()->getUser(),
1343 __METHOD__
1344 )->getNewRevision();
1345 $this->deletePage( $page );
1346
1347 $res = $store->newArchiveSelectQueryBuilder( $this->getDb() )
1348 ->joinComment()
1349 ->where( [ 'ar_rev_id' => $orig->getId() ] )
1350 ->caller( __METHOD__ )->fetchResultSet();
1351 $this->assertIsObject( $res, 'query failed' );
1352
1353 $row = $res->fetchObject();
1354 $res->free();
1355 $storeRecord = $store->newRevisionFromArchiveRow( $row );
1356
1357 $this->assertRevisionRecordsEqual( $orig, $storeRecord );
1358 $this->assertSame( $text, $storeRecord->getContent( SlotRecord::MAIN, RevisionRecord::RAW )->serialize() );
1359 }
1360
1361 public function testNewRevisionFromArchiveRow_no_user() {
1362 $store = $this->getServiceContainer()->getRevisionStore();
1363
1364 $row = (object)[
1365 'ar_id' => '1',
1366 'ar_page_id' => '2',
1367 'ar_namespace' => '0',
1368 'ar_title' => 'Something',
1369 'ar_rev_id' => '2',
1370 'ar_timestamp' => '20180528192356',
1371 'ar_minor_edit' => '0',
1372 'ar_deleted' => '0',
1373 'ar_len' => '78',
1374 'ar_parent_id' => '0',
1375 'ar_sha1' => 'deadbeef',
1376 'ar_comment_text' => 'whatever',
1377 'ar_comment_data' => null,
1378 'ar_comment_cid' => null,
1379 'ar_user' => '0',
1380 'ar_user_text' => '', // this is the important bit
1381 'ar_actor' => null,
1382 ];
1383
1384 $record = @$store->newRevisionFromArchiveRow( $row );
1385
1386 $this->assertInstanceOf( RevisionRecord::class, $record );
1387 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1388 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1389 }
1390
1391 /**
1392 * Test for T236624.
1393 */
1394 public function testNewRevisionFromArchiveRow_empty_actor() {
1395 $store = $this->getServiceContainer()->getRevisionStore();
1396
1397 $row = (object)[
1398 'ar_id' => '1',
1399 'ar_page_id' => '2',
1400 'ar_namespace' => '0',
1401 'ar_title' => 'Something',
1402 'ar_rev_id' => '2',
1403 'ar_text_id' => '47',
1404 'ar_timestamp' => '20180528192356',
1405 'ar_minor_edit' => '0',
1406 'ar_deleted' => '0',
1407 'ar_len' => '78',
1408 'ar_parent_id' => '0',
1409 'ar_sha1' => 'deadbeef',
1410 'ar_comment_text' => 'whatever',
1411 'ar_comment_data' => null,
1412 'ar_comment_cid' => null,
1413 'ar_user' => '0',
1414 'ar_user_text' => '', // this is the important bit
1415 'ar_actor' => null, // we will fill this in below
1416 'ar_content_format' => null,
1417 'ar_content_model' => null,
1418 ];
1419
1420 // create an actor row for the empty user name (see also T225469)
1421 $this->getDb()->newInsertQueryBuilder()
1422 ->insertInto( 'actor' )
1423 ->row( [
1424 'actor_user' => $row->ar_user,
1425 'actor_name' => $row->ar_user_text,
1426 ] )
1427 ->caller( __METHOD__ )
1428 ->execute();
1429
1430 $row->ar_actor = $this->getDb()->insertId();
1431
1432 $record = @$store->newRevisionFromArchiveRow( $row );
1433
1434 $this->assertInstanceOf( RevisionRecord::class, $record );
1435 $this->assertInstanceOf( UserIdentityValue::class, $record->getUser() );
1436 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1437 }
1438
1439 public function testNewRevisionFromRow_no_user() {
1440 $store = $this->getServiceContainer()->getRevisionStore();
1441 $title = Title::newFromText( __METHOD__ );
1442
1443 $row = (object)[
1444 'rev_id' => '2',
1445 'rev_page' => '2',
1446 'page_namespace' => '0',
1447 'page_title' => $title->getText(),
1448 'rev_text_id' => '47',
1449 'rev_timestamp' => '20180528192356',
1450 'rev_minor_edit' => '0',
1451 'rev_deleted' => '0',
1452 'rev_len' => '78',
1453 'rev_parent_id' => '0',
1454 'rev_sha1' => 'deadbeef',
1455 'rev_comment_text' => 'whatever',
1456 'rev_comment_data' => null,
1457 'rev_comment_cid' => null,
1458 'rev_user' => '0',
1459 'rev_user_text' => '', // this is the important bit
1460 'rev_actor' => null,
1461 'rev_content_format' => null,
1462 'rev_content_model' => null,
1463 ];
1464
1465 $record = @$store->newRevisionFromRow( $row, 0, $title );
1466 $this->assertNotNull( $record );
1467 $this->assertNotNull( $record->getUser() );
1468 $this->assertNotEmpty( $record->getUser()->getName() );
1469 }
1470
1471 public function testNewRevisionFromRow_noPage() {
1472 $store = $this->getServiceContainer()->getRevisionStore();
1473 $page = $this->getExistingTestPage();
1474
1475 $info = $store->getQueryInfo();
1476 $row = $this->getDb()->newSelectQueryBuilder()
1477 ->queryInfo( $info )
1478 ->where( [ 'rev_page' => $page->getId(), 'rev_id' => $page->getLatest() ] )
1479 ->caller( __METHOD__ )
1480 ->fetchRow();
1481
1482 $record = $store->newRevisionFromRow( $row );
1483
1484 $this->assertNotNull( $record );
1485 $this->assertTrue( $page->isSamePageAs( $record->getPage() ) );
1486
1487 // NOTE: This should return a Title object for now, until we no longer have a need
1488 // to frequently convert to Title.
1489 $this->assertInstanceOf( Title::class, $record->getPage() );
1490 }
1491
1492 public function testNewRevisionFromRow_noPage_crossWiki() {
1493 $page = $this->getExistingTestPage();
1494 // Make TitleFactory always fail, since it should not be used for the cross-wiki case. Note, it's important
1495 // to do this *after* the test page has been created.
1496 $noOpTitleFactory = $this->createNoOpMock( TitleFactory::class );
1497 $this->setService( 'TitleFactory', $noOpTitleFactory );
1498
1499 // Pretend the local test DB is a sister site
1500 $wikiId = $this->getDb()->getDomainID();
1501 $store = $this->getServiceContainer()->getRevisionStoreFactory()
1502 ->getRevisionStore( $wikiId );
1503
1504 $info = $store->getQueryInfo();
1505 $row = $this->getDb()->newSelectQueryBuilder()
1506 ->queryInfo( $info )
1507 ->where( [ 'rev_page' => $page->getId(), 'rev_id' => $page->getLatest() ] )
1508 ->caller( __METHOD__ )
1509 ->fetchRow();
1510
1511 $record = $store->newRevisionFromRow( $row );
1512
1513 $this->assertNotNull( $record );
1514 $this->assertSame( $page->getLatest(), $record->getId( $wikiId ) );
1515
1516 $this->assertNotInstanceOf( Title::class, $record->getPage() );
1517 $this->assertSame( $page->getId(), $record->getPage()->getId( $wikiId ) );
1518 }
1519
1520 /**
1521 * @dataProvider provideInsertRevisionOn
1522 *
1523 * @param callable $getPageIdentity
1524 */
1525 public function testInsertRevisionOn_archive( $getPageIdentity ) {
1526 // This is a round trip test for deletion and undeletion of a
1527 // revision row via the archive table.
1528 [ $title, $pageIdentity ] = $getPageIdentity();
1529 $store = $this->getServiceContainer()->getRevisionStore();
1530
1531 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1532 $user = $this->getTestSysop()->getUser();
1533 $page->doUserEditContent( new WikitextContent( "First" ), $user, __METHOD__ . '-first' );
1534 $orig = $page->doUserEditContent( new WikitextContent( "Foo" ), $user, __METHOD__ )
1535 ->getNewRevision();
1536 $this->deletePage( $page );
1537
1538 // re-create page, so we can later load revisions for it
1539 $page->doUserEditContent( new WikitextContent( 'Two' ), $user, __METHOD__ );
1540
1541 $db = $this->getDb();
1542 $row = $store->newArchiveSelectQueryBuilder( $db )
1543 ->joinComment()
1544 ->where( [ 'ar_rev_id' => $orig->getId() ] )
1545 ->caller( __METHOD__ )->fetchRow();
1546
1547 $this->assertNotFalse( $row, 'query failed' );
1548
1549 $record = $store->newRevisionFromArchiveRow(
1550 $row,
1551 0,
1552 $pageIdentity,
1553 [ 'page_id' => $title->getArticleID() ]
1554 );
1555
1556 $restored = $store->insertRevisionOn( $record, $db );
1557
1558 // is the new revision correct?
1559 $this->assertRevisionCompleteness( $restored );
1560 $this->assertRevisionRecordsEqual( $record, $restored );
1561
1562 // does the new revision use the original slot?
1563 $recMain = $record->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
1564 $restMain = $restored->getSlot( SlotRecord::MAIN );
1565 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1566 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1567 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1568 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1569
1570 // can we load it from the store?
1571 $loaded = $store->getRevisionById( $restored->getId() );
1572 $this->assertNotNull( $loaded );
1573 $this->assertRevisionCompleteness( $loaded );
1574 $this->assertRevisionRecordsEqual( $restored, $loaded );
1575
1576 // can we find it directly in the database?
1577 $this->assertRevisionExistsInDatabase( $restored );
1578 }
1579
1580 public static function provideInsertRevisionOn() {
1581 return [
1582 [ static function () {
1583 $pageTitle = Title::newFromText( 'Test_Insert_Revision_On' );
1584 return [ $pageTitle, $pageTitle ];
1585 } ],
1586 [ static function () {
1587 $pageTitle = Title::newFromText( 'Test_Insert_Revision_On' );
1588 return [ $pageTitle, $pageTitle->toPageIdentity() ];
1589 } ]
1590 ];
1591 }
1592
1593 public function testGetParentLengths() {
1594 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
1595 $revRecordOne = $page->doUserEditContent(
1596 new WikitextContent( __METHOD__ ),
1597 $this->getTestSysop()->getUser(),
1598 __METHOD__
1599 )->getNewRevision();
1600 $revRecordTwo = $page->doUserEditContent(
1601 new WikitextContent( __METHOD__ . '2' ),
1602 $this->getTestSysop()->getUser(),
1603 __METHOD__
1604 )->getNewRevision();
1605
1606 $store = $this->getServiceContainer()->getRevisionStore();
1607 $this->assertSame(
1608 [
1609 $revRecordOne->getId() => strlen( __METHOD__ ),
1610 ],
1611 $store->getRevisionSizes( [ $revRecordOne->getId() ] )
1612 );
1613 $this->assertSame(
1614 [
1615 $revRecordOne->getId() => strlen( __METHOD__ ),
1616 $revRecordTwo->getId() => strlen( __METHOD__ ) + 1,
1617 ],
1618 $store->getRevisionSizes(
1619 [ $revRecordOne->getId(), $revRecordTwo->getId() ]
1620 )
1621 );
1622 }
1623
1624 public function testGetPreviousRevision() {
1625 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
1626 $revRecordOne = $page->doUserEditContent(
1627 new WikitextContent( __METHOD__ ),
1628 $this->getTestSysop()->getUser(),
1629 __METHOD__
1630 )->getNewRevision();
1631 $revRecordTwo = $page->doUserEditContent(
1632 new WikitextContent( __METHOD__ . '2' ),
1633 $this->getTestSysop()->getUser(),
1634 __METHOD__
1635 )->getNewRevision();
1636
1637 $store = $this->getServiceContainer()->getRevisionStore();
1638 $this->assertNull(
1639 $store->getPreviousRevision(
1640 $store->getRevisionById( $revRecordOne->getId() )
1641 )
1642 );
1643 $this->assertSame(
1644 $revRecordOne->getId(),
1645 $store->getPreviousRevision(
1646 $store->getRevisionById( $revRecordTwo->getId() )
1647 )->getId()
1648 );
1649 }
1650
1651 public function testGetNextRevision() {
1652 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
1653 $revRecordOne = $page->doUserEditContent(
1654 new WikitextContent( __METHOD__ ),
1655 $this->getTestSysop()->getUser(),
1656 __METHOD__
1657 )->getNewRevision();
1658 $revRecordTwo = $page->doUserEditContent(
1659 new WikitextContent( __METHOD__ . '2' ),
1660 $this->getTestSysop()->getUser(),
1661 __METHOD__
1662 )->getNewRevision();
1663
1664 $store = $this->getServiceContainer()->getRevisionStore();
1665 $this->assertSame(
1666 $revRecordTwo->getId(),
1667 $store->getNextRevision(
1668 $store->getRevisionById( $revRecordOne->getId() )
1669 )->getId()
1670 );
1671 $this->assertNull(
1672 $store->getNextRevision( $store->getRevisionById( $revRecordTwo->getId() ) )
1673 );
1674 }
1675
1676 public static function provideNonHistoryRevision() {
1677 $title = Title::newFromText( __METHOD__ );
1678 $rev = new MutableRevisionRecord( $title );
1679 yield [ $rev ];
1680
1681 $user = new UserIdentityValue( 7, 'Frank' );
1682 $comment = CommentStoreComment::newUnsavedComment( 'Test' );
1683 $row = (object)[
1684 'ar_id' => 3,
1685 'ar_rev_id' => 34567,
1686 'ar_page_id' => 5,
1687 'ar_deleted' => 0,
1688 'ar_minor_edit' => 0,
1689 'ar_timestamp' => '20180101020202',
1690 ];
1691 $slots = new RevisionSlots( [] );
1692 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1693 yield [ $rev ];
1694 }
1695
1696 /**
1697 * @dataProvider provideNonHistoryRevision
1698 */
1699 public function testGetPreviousRevision_bad( RevisionRecord $rev ) {
1700 $store = $this->getServiceContainer()->getRevisionStore();
1701 $this->assertNull( $store->getPreviousRevision( $rev ) );
1702 }
1703
1704 /**
1705 * @dataProvider provideNonHistoryRevision
1706 */
1707 public function testGetNextRevision_bad( RevisionRecord $rev ) {
1708 $store = $this->getServiceContainer()->getRevisionStore();
1709 $this->assertNull( $store->getNextRevision( $rev ) );
1710 }
1711
1712 public function testGetTimestampFromId_found() {
1713 $page = $this->getTestPage();
1714 $revRecord = $page->doUserEditContent(
1715 new WikitextContent( __METHOD__ ),
1716 $this->getTestSysop()->getUser(),
1717 __METHOD__
1718 )->getNewRevision();
1719
1720 $store = $this->getServiceContainer()->getRevisionStore();
1721 $result = $store->getTimestampFromId( $revRecord->getId() );
1722
1723 $this->assertSame( $revRecord->getTimestamp(), $result );
1724 }
1725
1726 public function testGetTimestampFromId_notFound() {
1727 $page = $this->getTestPage();
1728 $revRecord = $page->doUserEditContent(
1729 new WikitextContent( __METHOD__ ),
1730 $this->getTestSysop()->getUser(),
1731 __METHOD__
1732 )->getNewRevision();
1733
1734 $store = $this->getServiceContainer()->getRevisionStore();
1735 $result = $store->getTimestampFromId( $revRecord->getId() + 1 );
1736
1737 $this->assertFalse( $result );
1738 }
1739
1740 public function testCountRevisionsByPageId() {
1741 $store = $this->getServiceContainer()->getRevisionStore();
1742 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
1743 $user = $this->getTestSysop()->getUser();
1744
1745 $this->assertSame(
1746 0,
1747 $store->countRevisionsByPageId( $this->getDb(), $page->getId() )
1748 );
1749 $page->doUserEditContent( new WikitextContent( 'a' ), $user, 'a' );
1750 $this->assertSame(
1751 1,
1752 $store->countRevisionsByPageId( $this->getDb(), $page->getId() )
1753 );
1754 $page->doUserEditContent( new WikitextContent( 'b' ), $user, 'b' );
1755 $this->assertSame(
1756 2,
1757 $store->countRevisionsByPageId( $this->getDb(), $page->getId() )
1758 );
1759 }
1760
1761 public function testCountRevisionsByTitle() {
1762 $store = $this->getServiceContainer()->getRevisionStore();
1763 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
1764 $user = $this->getTestSysop()->getUser();
1765
1766 $this->assertSame(
1767 0,
1768 $store->countRevisionsByTitle( $this->getDb(), $page->getTitle() )
1769 );
1770 $page->doUserEditContent( new WikitextContent( 'a' ), $user, 'a' );
1771 $this->assertSame(
1772 1,
1773 $store->countRevisionsByTitle( $this->getDb(), $page->getTitle() )
1774 );
1775 $page->doUserEditContent( new WikitextContent( 'b' ), $user, 'b' );
1776 $this->assertSame(
1777 2,
1778 $store->countRevisionsByTitle( $this->getDb(), $page->getTitle() )
1779 );
1780 }
1781
1782 public function testUserWasLastToEdit_false() {
1783 $sysop = $this->getTestSysop()->getUser();
1784 $page = $this->getTestPage();
1785 $page->doUserEditContent(
1786 new WikitextContent( __METHOD__ ),
1787 $this->getTestUser()->getUser(), // not the $sysop
1788 __METHOD__
1789 );
1790
1791 $store = $this->getServiceContainer()->getRevisionStore();
1792 $result = $store->userWasLastToEdit(
1793 $this->getDb(),
1794 $page->getId(),
1795 $sysop->getId(),
1796 '20160101010101'
1797 );
1798 $this->assertFalse( $result );
1799 }
1800
1801 public function testUserWasLastToEdit_true() {
1802 $startTime = wfTimestampNow();
1803 $sysop = $this->getTestSysop()->getUser();
1804 $page = $this->getTestPage();
1805 $page->doUserEditContent(
1806 new WikitextContent( __METHOD__ ),
1807 $sysop,
1808 __METHOD__
1809 );
1810
1811 $store = $this->getServiceContainer()->getRevisionStore();
1812 $result = $store->userWasLastToEdit(
1813 $this->getDb(),
1814 $page->getId(),
1815 $sysop->getId(),
1816 $startTime
1817 );
1818 $this->assertTrue( $result );
1819 }
1820
1821 /**
1822 * @dataProvider provideRevisionByTitle
1823 */
1824 public function testGetKnownCurrentRevision( $getPageIdentity ) {
1825 $page = $this->getTestPage();
1826 $revRecord = $page->doUserEditContent(
1827 new WikitextContent( __METHOD__ . 'b' ),
1828 $this->getTestUser()->getUser(),
1829 __METHOD__ . 'b'
1830 )->getNewRevision();
1831 $store = $this->getServiceContainer()->getRevisionStore();
1832 $storeRecord = $store->getKnownCurrentRevision(
1833 $getPageIdentity(),
1834 $revRecord->getId()
1835 );
1836 $this->assertRevisionRecordsEqual( $revRecord, $storeRecord );
1837 }
1838
1839 /**
1840 * Creates a new revision for testing caching behavior
1841 *
1842 * @param WikiPage $page the page for the new revision
1843 * @param RevisionStore $store store object to use for creating the revision
1844 * @return bool|RevisionStoreRecord the revision created, or false if missing
1845 */
1846 private function createRevisionStoreCacheRecord( $page, $store ) {
1847 $user = MediaWikiIntegrationTestCase::getMutableTestUser()->getUser();
1848 $summary = CommentStoreComment::newUnsavedComment( __METHOD__ );
1849 $rev = $page->newPageUpdater( $user )
1850 ->setContent( SlotRecord::MAIN, new WikitextContent( __METHOD__ ) )
1851 ->saveRevision( $summary, EDIT_NEW );
1852 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1853 }
1854
1855 public function testGetKnownCurrentRevision_userNameChange() {
1856 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1857 $this->setService( 'MainWANObjectCache', $cache );
1858
1859 $store = $this->getServiceContainer()->getRevisionStore();
1860 $page = $this->getNonexistingTestPage();
1861 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1862
1863 // Grab the user name
1864 $userNameBefore = $rev->getUser()->getName();
1865
1866 // Change the user name in the database, "behind the back" of the cache
1867 $newUserName = "Renamed $userNameBefore";
1868 $this->getDb()->newUpdateQueryBuilder()
1869 ->update( 'user' )
1870 ->set( [ 'user_name' => $newUserName ] )
1871 ->where( [ 'user_id' => $rev->getUser()->getId() ] )
1872 ->caller( __METHOD__ )
1873 ->execute();
1874 $this->getDb()->newUpdateQueryBuilder()
1875 ->update( 'actor' )
1876 ->set( [ 'actor_name' => $newUserName ] )
1877 ->where( [ 'actor_user' => $rev->getUser()->getId() ] )
1878 ->caller( __METHOD__ )
1879 ->execute();
1880
1881 // Reload the revision and regrab the user name.
1882 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1883 $userNameAfter = $revAfter->getUser()->getName();
1884
1885 // The two user names should be different.
1886 // If they are the same, we are seeing a cached value, which is bad.
1887 $this->assertNotSame( $userNameBefore, $userNameAfter );
1888
1889 // This is implied by the above assertion, but explicitly check it, for completeness
1890 $this->assertSame( $newUserName, $userNameAfter );
1891 }
1892
1893 public function testGetKnownCurrentRevision_stalePageId() {
1894 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1895 $this->setService( 'MainWANObjectCache', $cache );
1896
1897 $store = $this->getServiceContainer()->getRevisionStore();
1898 $page = $this->getNonexistingTestPage();
1899 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1900
1901 // Force bad article ID
1902 $title = $page->getTitle();
1903 $title->resetArticleID( 886655 );
1904
1905 $result = $store->getKnownCurrentRevision( $title, $rev->getId() );
1906
1907 $this->assertSame( $rev->getPageId(), $result->getPageId() );
1908 }
1909
1910 public function testGetKnownCurrentRevision_wrongTitle() {
1911 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1912 $this->setService( 'MainWANObjectCache', $cache );
1913
1914 $store = $this->getServiceContainer()->getRevisionStore();
1915 $page = $this->getNonexistingTestPage();
1916 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1917
1918 // Get title of another page
1919 $title = $this->getExistingTestPage( __FUNCTION__ )->getTitle();
1920 $result = $store->getKnownCurrentRevision( $title, $rev->getId() );
1921
1922 $this->assertSame( $rev->getPageId(), $result->getPageId() );
1923 $this->assertTrue( $rev->getPage()->isSamePageAs( $result->getPage() ) );
1924 }
1925
1926 public function testGetKnownCurrentRevision_revDelete() {
1927 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1928 $this->setService( 'MainWANObjectCache', $cache );
1929
1930 $store = $this->getServiceContainer()->getRevisionStore();
1931 $page = $this->getNonexistingTestPage();
1932 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1933
1934 // Grab the deleted bitmask
1935 $deletedBefore = $rev->getVisibility();
1936
1937 // Change the deleted bitmask in the database, "behind the back" of the cache
1938 $this->getDb()->newUpdateQueryBuilder()
1939 ->update( 'revision' )
1940 ->set( [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ] )
1941 ->where( [ 'rev_id' => $rev->getId() ] )
1942 ->caller( __METHOD__ )
1943 ->execute();
1944
1945 // Reload the revision and regrab the visibility flag.
1946 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1947 $deletedAfter = $revAfter->getVisibility();
1948
1949 // The two deleted flags should be different.
1950 // If they are the same, we are seeing a cached value, which is bad.
1951 $this->assertNotSame( $deletedBefore, $deletedAfter );
1952
1953 // This is implied by the above assertion, but explicitly check it, for completeness
1954 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
1955 }
1956
1957 public function testNewRevisionFromRow_userNameChange() {
1958 $page = $this->getTestPage();
1959 $text = __METHOD__;
1960 $revRecord = $page->doUserEditContent(
1961 new WikitextContent( $text ),
1962 $this->getMutableTestUser()->getUser(),
1963 __METHOD__
1964 )->getNewRevision();
1965
1966 $store = $this->getServiceContainer()->getRevisionStore();
1967 $storeRecord = $store->newRevisionFromRow(
1968 $this->revisionRecordToRow( $revRecord ),
1969 0,
1970 $page->getTitle()
1971 );
1972
1973 // Grab the user name
1974 $userNameBefore = $storeRecord->getUser()->getName();
1975
1976 // Change the user name in the database
1977 $newUserName = "Renamed $userNameBefore";
1978 $this->getDb()->newUpdateQueryBuilder()
1979 ->update( 'user' )
1980 ->set( [ 'user_name' => $newUserName ] )
1981 ->where( [ 'user_id' => $storeRecord->getUser()->getId() ] )
1982 ->caller( __METHOD__ )
1983 ->execute();
1984 $this->getDb()->newUpdateQueryBuilder()
1985 ->update( 'actor' )
1986 ->set( [ 'actor_name' => $newUserName ] )
1987 ->where( [ 'actor_user' => $storeRecord->getUser()->getId() ] )
1988 ->caller( __METHOD__ )
1989 ->execute();
1990
1991 // Reload the record, passing $fromCache as true to force fresh info from the db,
1992 // and regrab the user name
1993 $recordAfter = $store->newRevisionFromRow(
1994 $this->revisionRecordToRow( $revRecord ),
1995 0,
1996 $page->getTitle(),
1997 true
1998 );
1999 $userNameAfter = $recordAfter->getUser()->getName();
2000
2001 // The two user names should be different.
2002 // If they are the same, we are seeing a cached value, which is bad.
2003 $this->assertNotSame( $userNameBefore, $userNameAfter );
2004
2005 // This is implied by the above assertion, but explicitly check it, for completeness
2006 $this->assertSame( $newUserName, $userNameAfter );
2007 }
2008
2009 public function testNewRevisionFromRow_revDelete() {
2010 $page = $this->getTestPage();
2011 $text = __METHOD__;
2012 $revRecord = $page->doUserEditContent(
2013 new WikitextContent( $text ),
2014 $this->getTestSysop()->getUser(),
2015 __METHOD__
2016 )->getNewRevision();
2017
2018 $store = $this->getServiceContainer()->getRevisionStore();
2019 $storeRecord = $store->newRevisionFromRow(
2020 $this->revisionRecordToRow( $revRecord ),
2021 0,
2022 $page->getTitle()
2023 );
2024
2025 // Grab the deleted bitmask
2026 $deletedBefore = $storeRecord->getVisibility();
2027
2028 // Change the deleted bitmask in the database
2029 $this->getDb()->newUpdateQueryBuilder()
2030 ->update( 'revision' )
2031 ->set( [ 'rev_deleted' => RevisionRecord::DELETED_TEXT ] )
2032 ->where( [ 'rev_id' => $storeRecord->getId() ] )
2033 ->caller( __METHOD__ )
2034 ->execute();
2035
2036 // Reload the record, passing $fromCache as true to force fresh info from the db,
2037 // and regrab the deleted bitmask
2038 $recordAfter = $store->newRevisionFromRow(
2039 $this->revisionRecordToRow( $revRecord ),
2040 0,
2041 $page->getTitle(),
2042 true
2043 );
2044 $deletedAfter = $recordAfter->getVisibility();
2045
2046 // The two deleted flags should be different, because we modified the database.
2047 $this->assertNotSame( $deletedBefore, $deletedAfter );
2048
2049 // This is implied by the above assertion, but explicitly check it, for completeness
2050 $this->assertSame( RevisionRecord::DELETED_TEXT, $deletedAfter );
2051 }
2052
2053 public static function provideGetContentBlobsForBatchOptions() {
2054 yield 'all slots' => [ null ];
2055 yield 'no slots' => [ [] ];
2056 yield 'main slot' => [ [ SlotRecord::MAIN ] ];
2057 }
2058
2059 /**
2060 * @dataProvider provideGetContentBlobsForBatchOptions
2061 */
2062 public function testGetContentBlobsForBatch( $slots ) {
2063 $page1 = $this->getTestPage();
2064 $text = __METHOD__ . 'b-ä';
2065 $editStatus = $this->editPage( $page1, $text . '1' );
2066 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2067 $revRecord1 = $editStatus->getNewRevision();
2068
2069 $page2 = $this->getTestPage( $page1->getTitle()->getPrefixedText() . '_other' );
2070 $editStatus = $this->editPage( $page2, $text . '2' );
2071 $this->assertStatusGood( $editStatus, 'must create revision 2' );
2072 $revRecord2 = $editStatus->getNewRevision();
2073
2074 $store = $this->getServiceContainer()->getRevisionStore();
2075 $result = $store->getContentBlobsForBatch(
2076 [ $revRecord1->getId(), $revRecord2->getId() ],
2077 $slots
2078 );
2079 $this->assertStatusGood( $result );
2080
2081 $rowSetsByRevId = $result->getValue();
2082 $this->assertArrayHasKey( $revRecord1->getId(), $rowSetsByRevId );
2083 $this->assertArrayHasKey( $revRecord2->getId(), $rowSetsByRevId );
2084
2085 $rev1rows = $rowSetsByRevId[$revRecord1->getId()];
2086 $rev2rows = $rowSetsByRevId[$revRecord2->getId()];
2087
2088 if ( is_array( $slots ) && !in_array( SlotRecord::MAIN, $slots ) ) {
2089 $this->assertArrayNotHasKey( SlotRecord::MAIN, $rev1rows );
2090 $this->assertArrayNotHasKey( SlotRecord::MAIN, $rev2rows );
2091 } else {
2092 $this->assertArrayHasKey( SlotRecord::MAIN, $rev1rows );
2093 $this->assertArrayHasKey( SlotRecord::MAIN, $rev2rows );
2094
2095 $mainSlotRow1 = $rev1rows[ SlotRecord::MAIN ];
2096 $mainSlotRow2 = $rev2rows[ SlotRecord::MAIN ];
2097
2098 $this->assertSame( $text . '1', $mainSlotRow1->blob_data );
2099 $this->assertSame( $text . '2', $mainSlotRow2->blob_data );
2100 }
2101
2102 // try again, with objects instead of ids:
2103 $result2 = $store->getContentBlobsForBatch( [
2104 (object)[ 'rev_id' => $revRecord1->getId() ],
2105 (object)[ 'rev_id' => $revRecord2->getId() ],
2106 ], $slots );
2107
2108 $this->assertStatusGood( $result2 );
2109 $exp1 = var_export( $result->getValue(), true );
2110 $exp2 = var_export( $result2->getValue(), true );
2111 $this->assertSame( $exp1, $exp2 );
2112 }
2113
2114 public function testGetContentBlobsForBatch_archive() {
2115 $page1 = $this->getTestPage( __METHOD__ );
2116 $text = __METHOD__ . 'b-ä';
2117 $editStatus = $this->editPage( $page1, $text . '1' );
2118 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2119 $revRecord1 = $editStatus->getNewRevision();
2120 $this->deletePage( $page1 );
2121
2122 $page2 = $this->getTestPage( $page1->getTitle()->getPrefixedText() . '_other' );
2123 $editStatus = $this->editPage( $page2, $text . '2' );
2124 $this->assertStatusGood( $editStatus, 'must create revision 2' );
2125 $revRecord2 = $editStatus->getNewRevision();
2126 $this->deletePage( $page2 );
2127
2128 $store = $this->getServiceContainer()->getRevisionStore();
2129 $result = $store->getContentBlobsForBatch( [
2130 (object)[ 'ar_rev_id' => $revRecord1->getId() ],
2131 (object)[ 'ar_rev_id' => $revRecord2->getId() ],
2132 ] );
2133 $this->assertStatusGood( $result );
2134
2135 $rowSetsByRevId = $result->getValue();
2136 $this->assertArrayHasKey( $revRecord1->getId(), $rowSetsByRevId );
2137 $this->assertArrayHasKey( $revRecord2->getId(), $rowSetsByRevId );
2138 }
2139
2140 public function testGetContentBlobsForBatch_emptyBatch() {
2141 $rows = new FakeResultWrapper( [] );
2142 $result = $this->getServiceContainer()->getRevisionStore()
2143 ->getContentBlobsForBatch( $rows );
2144 $this->assertStatusGood( $result );
2145 $this->assertStatusValue( [], $result );
2146 }
2147
2148 public static function provideNewRevisionsFromBatchOptions() {
2149 yield 'No preload slots or content, single page' => [
2150 [ 'comment' ],
2151 static function () {
2152 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2153 return [ $pageTitle, $pageTitle ];
2154 },
2155 null,
2156 []
2157 ];
2158 yield 'No preload slots or content, single page and with PageIdentity' => [
2159 [ 'comment' ],
2160 static function () {
2161 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2162 return [ $pageTitle, $pageTitle->toPageIdentity() ];
2163 },
2164 null,
2165 []
2166 ];
2167 yield 'Preload slots and content, single page' => [
2168 [ 'comment' ],
2169 static function () {
2170 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2171 return [ $pageTitle, $pageTitle ];
2172 },
2173 null,
2174 [
2175 'slots' => [ SlotRecord::MAIN ],
2176 'content' => true
2177 ]
2178 ];
2179 yield 'Ask for no slots' => [
2180 [ 'comment' ],
2181 static function () {
2182 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2183 return [ $pageTitle, $pageTitle ];
2184 },
2185 null,
2186 [ 'slots' => [] ]
2187 ];
2188 yield 'Ask for only-defined slots' => [
2189 [ 'comment' ],
2190 static function () {
2191 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2192 return [ $pageTitle, $pageTitle ];
2193 },
2194 null,
2195 [ 'slots' => [ 'unused' ] ]
2196 ];
2197 yield 'No preload slots or content, multiple pages' => [
2198 [ 'comment' ],
2199 static function () {
2200 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2201 return [ $pageTitle, $pageTitle ];
2202 },
2203 'Other_Page',
2204 []
2205 ];
2206 yield 'Preload slots and content, multiple pages' => [
2207 [ 'comment' ],
2208 static function () {
2209 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2210 return [ $pageTitle, $pageTitle ];
2211 },
2212 'Other_Page',
2213 [
2214 'slots' => [ SlotRecord::MAIN ],
2215 'content' => true
2216 ]
2217 ];
2218 yield 'Preload slots and content, multiple pages, preload page fields' => [
2219 [ 'page', 'comment' ],
2220 static function () {
2221 $pageTitle = Title::newFromText( 'Test_New_Revision_From_Batch' );
2222 return [ $pageTitle, $pageTitle ];
2223 },
2224 'Other_Page',
2225 [
2226 'slots' => [ SlotRecord::MAIN ],
2227 'content' => true
2228 ]
2229 ];
2230 }
2231
2232 /**
2233 * @dataProvider provideNewRevisionsFromBatchOptions
2234 * @param array|null $queryOptions options to provide to revisionRecordToRow
2235 * @param callable $getPageIdentity
2236 * @param string|null $otherPageTitle
2237 * @param array|null $options
2238 */
2239 public function testNewRevisionsFromBatch_preloadContent(
2240 $queryOptions,
2241 $getPageIdentity,
2242 $otherPageTitle = null,
2243 array $options = []
2244 ) {
2245 if ( isset( $options['slots'] ) && in_array( 'unused', $options['slots'] ) ) {
2246 $this->getServiceContainer()->addServiceManipulator(
2247 'SlotRoleRegistry',
2248 static function ( SlotRoleRegistry $registry ) {
2249 $registry->defineRoleWithModel( 'unused', CONTENT_MODEL_JSON );
2250 }
2251 );
2252 }
2253
2254 $page1 = $this->getTestPage();
2255 $text = __METHOD__ . 'b-ä';
2256 $editStatus = $this->editPage( $page1, $text . '1' );
2257 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2258 $revRecord1 = $editStatus->getNewRevision();
2259
2260 $page2 = $this->getTestPage( $otherPageTitle );
2261 $editStatus = $this->editPage( $page2, $text . '2' );
2262 $this->assertStatusGood( $editStatus, 'must create revision 2' );
2263 $revRecord2 = $editStatus->getNewRevision();
2264
2265 $store = $this->getServiceContainer()->getRevisionStore();
2266 $result = $store->newRevisionsFromBatch(
2267 [
2268 $this->revisionRecordToRow( $revRecord1, $queryOptions ),
2269 $this->revisionRecordToRow( $revRecord2, $queryOptions )
2270 ],
2271 $options,
2272 0, $otherPageTitle ? null : $page1->getTitle()
2273 );
2274 $this->assertStatusGood( $result );
2275 /** @var RevisionRecord[] $records */
2276 $records = $result->getValue();
2277 $this->assertRevisionRecordsEqual( $revRecord1, $records[$revRecord1->getId()] );
2278 $this->assertRevisionRecordsEqual( $revRecord2, $records[$revRecord2->getId()] );
2279
2280 $content1 = $records[$revRecord1->getId()]->getContent( SlotRecord::MAIN );
2281 $this->assertInstanceOf( TextContent::class, $content1 );
2282 $this->assertSame(
2283 $text . '1',
2284 $content1->getText()
2285 );
2286 $content2 = $records[$revRecord2->getId()]->getContent( SlotRecord::MAIN );
2287 $this->assertInstanceOf( TextContent::class, $content2 );
2288 $this->assertSame(
2289 $text . '2',
2290 $content2->getText()
2291 );
2292 $this->assertEquals(
2293 $page1->getTitle()->getDBkey(),
2294 $records[$revRecord1->getId()]->getPageAsLinkTarget()->getDBkey()
2295 );
2296 $this->assertEquals(
2297 $page2->getTitle()->getDBkey(),
2298 $records[$revRecord2->getId()]->getPageAsLinkTarget()->getDBkey()
2299 );
2300 }
2301
2302 /**
2303 * @dataProvider provideNewRevisionsFromBatchOptions
2304 * @param array|null $queryOptions options to provide to revisionRecordToRow
2305 * @param callable $getPageIdentity
2306 * @param string|null $otherPageTitle
2307 * @param array|null $options
2308 */
2309 public function testNewRevisionsFromBatch_archive(
2310 $queryOptions,
2311 $getPageIdentity,
2312 $otherPageTitle = null,
2313 array $options = []
2314 ) {
2315 if ( isset( $options['slots'] ) && in_array( 'unused', $options['slots'] ) ) {
2316 $this->getServiceContainer()->addServiceManipulator(
2317 'SlotRoleRegistry',
2318 static function ( SlotRoleRegistry $registry ) {
2319 $registry->defineRoleWithModel( 'unused', CONTENT_MODEL_JSON );
2320 }
2321 );
2322 }
2323
2324 [ $title1, $pageIdentity ] = $getPageIdentity();
2325 $text1 = __METHOD__ . '-bä';
2326 $page1 = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title1 );
2327
2328 $title2 = $otherPageTitle ? Title::newFromText( $otherPageTitle ) : $title1;
2329 $text2 = __METHOD__ . '-bö';
2330 $page2 = $otherPageTitle ? $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title2 ) : $page1;
2331
2332 $sysop = $this->getTestSysop()->getUser();
2333 $revRecord1 = $page1->doUserEditContent(
2334 new WikitextContent( $text1 ),
2335 $sysop,
2336 __METHOD__
2337 )->getNewRevision();
2338 $revRecord2 = $page2->doUserEditContent(
2339 new WikitextContent( $text2 ),
2340 $sysop,
2341 __METHOD__
2342 )->getNewRevision();
2343 $this->deletePage( $page1 );
2344
2345 if ( $page2 !== $page1 ) {
2346 $this->deletePage( $page2 );
2347 }
2348
2349 $store = $this->getServiceContainer()->getRevisionStore();
2350
2351 $rows = $store->newArchiveSelectQueryBuilder( $this->getDb() )
2352 ->joinComment()
2353 ->where( [ 'ar_rev_id' => [ $revRecord1->getId(), $revRecord2->getId() ] ] )
2354 ->caller( __METHOD__ )->fetchResultSet();
2355
2356 $options['archive'] = true;
2357 $rows = iterator_to_array( $rows );
2358 $result = $store->newRevisionsFromBatch(
2359 $rows, $options, 0, $otherPageTitle ? null : $pageIdentity );
2360
2361 $this->assertStatusGood( $result );
2362 /** @var RevisionRecord[] $records */
2363 $records = $result->getValue();
2364 $this->assertCount( 2, $records );
2365 $this->assertRevisionRecordsEqual( $revRecord1, $records[$revRecord1->getId()] );
2366 $this->assertRevisionRecordsEqual( $revRecord2, $records[$revRecord2->getId()] );
2367
2368 $content1 = $records[$revRecord1->getId()]->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
2369 $this->assertInstanceOf( TextContent::class, $content1 );
2370 $this->assertSame(
2371 $text1,
2372 $content1->getText()
2373 );
2374 $content2 = $records[$revRecord2->getId()]->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
2375 $this->assertInstanceOf( TextContent::class, $content2 );
2376 $this->assertSame(
2377 $text2,
2378 $content2->getText()
2379 );
2380 $this->assertEquals(
2381 $page1->getTitle()->getDBkey(),
2382 $records[$revRecord1->getId()]->getPageAsLinkTarget()->getDBkey()
2383 );
2384 $this->assertEquals(
2385 $page2->getTitle()->getDBkey(),
2386 $records[$revRecord2->getId()]->getPageAsLinkTarget()->getDBkey()
2387 );
2388 }
2389
2390 public function testNewRevisionsFromBatch_emptyBatch() {
2391 $rows = new FakeResultWrapper( [] );
2392 $result = $this->getServiceContainer()->getRevisionStore()
2393 ->newRevisionsFromBatch(
2394 $rows,
2395 [
2396 'slots' => [ SlotRecord::MAIN ],
2397 'content' => true
2398 ]
2399 );
2400 $this->assertStatusGood( $result );
2401 $this->assertStatusValue( [], $result );
2402 }
2403
2404 public function testNewRevisionsFromBatch_wrongTitle() {
2405 $page1 = $this->getTestPage();
2406 $text = __METHOD__ . 'b-ä';
2407 $editStatus = $this->editPage( $page1, $text . '1' );
2408 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2409 $revRecord1 = $editStatus->getNewRevision();
2410
2411 $this->expectException( InvalidArgumentException::class );
2412 $this->getServiceContainer()->getRevisionStore()
2413 ->newRevisionsFromBatch(
2414 [ $this->revisionRecordToRow( $revRecord1 ) ],
2415 [],
2416 IDBAccessObject::READ_NORMAL,
2417 $this->getTestPage( 'Title_Other_Then_The_One_Revision_Belongs_To' )->getTitle()
2418 );
2419 }
2420
2421 public function testNewRevisionsFromBatch_DuplicateRows() {
2422 $page1 = $this->getTestPage();
2423 $text = __METHOD__ . 'b-ä';
2424 $editStatus = $this->editPage( $page1, $text . '1' );
2425 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2426 $revRecord1 = $editStatus->getNewRevision();
2427
2428 $status = $this->getServiceContainer()->getRevisionStore()
2429 ->newRevisionsFromBatch(
2430 [
2431 $this->revisionRecordToRow( $revRecord1 ),
2432 $this->revisionRecordToRow( $revRecord1 )
2433 ]
2434 );
2435
2436 $this->assertStatusWarning( 'internalerror_info', $status );
2437 }
2438
2439 public function testGetRevisionIdsBetween() {
2440 $NUM = 5;
2441 $MAX = 1;
2442 $page = $this->getTestPage( __METHOD__ );
2443 $revisions = [];
2444 $revisionIds = [];
2445 for ( $revNum = 0; $revNum < $NUM; $revNum++ ) {
2446 $editStatus = $this->editPage( $page, 'Revision ' . $revNum );
2447 $this->assertStatusGood( $editStatus, 'must create revision ' . $revNum );
2448 $newRevision = $editStatus->getNewRevision();
2449 $revisions[] = $newRevision;
2450 $revisionIds[] = $newRevision->getId();
2451 }
2452
2453 $revisionStore = $this->getServiceContainer()->getRevisionStore();
2454 $this->assertArrayEquals(
2455 [],
2456 $revisionStore->getRevisionIdsBetween( $page->getId(), $revisions[0], $revisions[0] ),
2457 false,
2458 false,
2459 'Must return an empty array if the same old and new revisions provided'
2460 );
2461 $this->assertArrayEquals(
2462 [],
2463 $revisionStore->getRevisionIdsBetween( $page->getId(), $revisions[0], $revisions[1] ),
2464 false,
2465 false,
2466 'Must return an empty array if the consecutive old and new revisions provided'
2467 );
2468 $this->assertArrayEquals(
2469 array_slice( $revisionIds, 1, -2 ),
2470 $revisionStore->getRevisionIdsBetween( $page->getId(), $revisions[0], $revisions[$NUM - 2] ),
2471 false,
2472 false,
2473 'The result is non-inclusive on both ends if both beginning and end are provided'
2474 );
2475 $this->assertArrayEquals(
2476 array_slice( $revisionIds, 1, -1 ),
2477 $revisionStore->getRevisionIdsBetween(
2478 $page->getId(),
2479 $revisions[0],
2480 $revisions[$NUM - 2],
2481 null,
2482 RevisionStore::INCLUDE_NEW
2483 ),
2484 'The inclusion string options are respected'
2485 );
2486 $this->assertArrayEquals(
2487 array_slice( $revisionIds, 0, -1 ),
2488 $revisionStore->getRevisionIdsBetween(
2489 $page->getId(),
2490 $revisions[0],
2491 $revisions[$NUM - 2],
2492 null,
2493 [ RevisionStore::INCLUDE_BOTH ]
2494 ),
2495 false,
2496 false,
2497 'The inclusion array options are respected'
2498 );
2499 $this->assertArrayEquals(
2500 array_slice( $revisionIds, 1 ),
2501 $revisionStore->getRevisionIdsBetween( $page->getId(), $revisions[0] ),
2502 false,
2503 false,
2504 'The result is inclusive on the end if the end is omitted'
2505 );
2506
2507 $this->assertArrayEquals(
2508 array_reverse( array_slice( $revisionIds, 1, -2 ) ),
2509 $revisionStore->getRevisionIdsBetween(
2510 $page->getId(),
2511 $revisions[0],
2512 $revisions[$NUM - 2],
2513 null,
2514 [],
2515 RevisionStore::ORDER_NEWEST_TO_OLDEST
2516 ),
2517 true,
2518 false,
2519 '$order parameter is respected'
2520 );
2521 $this->assertSame(
2522 $MAX + 1, // Returns array of length $max + 1 to detect truncation.
2523 count( $revisionStore->getRevisionIdsBetween(
2524 $page->getId(),
2525 $revisions[0],
2526 $revisions[$NUM - 1],
2527 $MAX
2528 ) ),
2529 '$max is incremented to detect truncation'
2530 );
2531 }
2532
2533 public function testCountRevisionsBetween() {
2534 $NUM = 5;
2535 $MAX = 1;
2536 $page = $this->getTestPage( __METHOD__ );
2537 $revisions = [];
2538 for ( $revNum = 0; $revNum < $NUM; $revNum++ ) {
2539 $editStatus = $this->editPage( $page, 'Revision ' . $revNum );
2540 $this->assertStatusGood( $editStatus, 'must create revision ' . $revNum );
2541 $revisions[] = $editStatus->getNewRevision();
2542 }
2543
2544 $revisionStore = $this->getServiceContainer()->getRevisionStore();
2545 $this->assertSame( 0,
2546 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0], $revisions[0] ),
2547 'Must return 0 if the same old and new revisions provided' );
2548 $this->assertSame( 0,
2549 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0], $revisions[1] ),
2550 'Must return 0 if the consecutive old and new revisions provided' );
2551 $this->assertEquals( $NUM - 3,
2552 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0], $revisions[$NUM - 2] ),
2553 'The count is non-inclusive on both ends if both beginning and end are provided' );
2554 $this->assertEquals( $NUM - 2,
2555 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0], $revisions[$NUM - 2],
2556 null, RevisionStore::INCLUDE_NEW ),
2557 'The count string options are respected' );
2558 $this->assertEquals( $NUM - 1,
2559 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0], $revisions[$NUM - 2],
2560 null, [ RevisionStore::INCLUDE_BOTH ] ),
2561 'The count array options are respected' );
2562 $this->assertEquals( $NUM - 1,
2563 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0] ),
2564 'The count is inclusive on the end if the end is omitted' );
2565 $this->assertEquals( $NUM + 1, // There was one revision from creating a page, thus NUM + 1
2566 $revisionStore->countRevisionsBetween( $page->getId() ),
2567 'The count is inclusive if both beginning and end are omitted' );
2568 $this->assertEquals( $MAX + 1, // Returns $max + 1 to detect truncation.
2569 $revisionStore->countRevisionsBetween( $page->getId(), $revisions[0],
2570 $revisions[$NUM - 1], $MAX ),
2571 'The $max is incremented to detect truncation' );
2572 }
2573
2574 public function testAuthorsBetween() {
2575 $this->disableAutoCreateTempUser();
2576 $NUM = 5;
2577 $page = $this->getTestPage( __METHOD__ );
2578 $users = [
2579 $this->getTestUser()->getUser(),
2580 $this->getTestUser()->getUser(),
2581 $this->getTestSysop()->getUser(),
2582 new User(),
2583 $this->getMutableTestUser()->getUser()
2584 ];
2585 $revisions = [];
2586 for ( $revNum = 0; $revNum < $NUM; $revNum++ ) {
2587 $editStatus = $this->editPage(
2588 $page,
2589 'Revision ' . $revNum,
2590 '',
2591 NS_MAIN,
2592 $users[$revNum] );
2593 $this->assertStatusGood( $editStatus, 'must create revision ' . $revNum );
2594 $revisions[] = $editStatus->getNewRevision();
2595 }
2596
2597 $revisionStore = $this->getServiceContainer()->getRevisionStore();
2598 $this->assertSame( 0,
2599 $revisionStore->countAuthorsBetween( $page->getId(), $revisions[0], $revisions[0] ),
2600 'countAuthorsBetween must return 0 if the same old and new revisions provided' );
2601 $this->assertArrayEquals( [],
2602 $revisionStore->getAuthorsBetween( $page->getId(), $revisions[0], $revisions[0] ),
2603 'getAuthorsBetween must return [] if the same old and new revisions provided' );
2604
2605 $this->assertSame( 0,
2606 $revisionStore->countAuthorsBetween( $page->getId(), $revisions[0], $revisions[1] ),
2607 'countAuthorsBetween must return 0 if the consecutive old and new revisions provided' );
2608 $this->assertArrayEquals( [],
2609 $revisionStore->getAuthorsBetween( $page->getId(), $revisions[0], $revisions[1] ),
2610 'getAuthorsBetween must return [] if the consecutive old and new revisions provided' );
2611
2612 $this->assertEquals( 2,
2613 $revisionStore->countAuthorsBetween( $page->getId(), $revisions[0], $revisions[$NUM - 2] ),
2614 'countAuthorsBetween is non-inclusive on both ends if both beginning and end are provided' );
2615 $result = $revisionStore->getAuthorsBetween( $page->getId(),
2616 $revisions[0], $revisions[$NUM - 2] );
2617 $this->assertCount( 2, $result,
2618 'getAuthorsBetween provides right number of users' );
2619 }
2620
2621 public static function provideBetweenMethodNames() {
2622 yield [ 'getRevisionIdsBetween' ];
2623 yield [ 'countRevisionsBetween' ];
2624 yield [ 'countAuthorsBetween' ];
2625 yield [ 'getAuthorsBetween' ];
2626 }
2627
2628 /**
2629 * @dataProvider provideBetweenMethodNames
2630 *
2631 * @param string $method the name of the method to test
2632 */
2633 public function testBetweenMethod_differentPages( $method ) {
2634 $page1 = $this->getTestPage( __METHOD__ );
2635 $page2 = $this->getTestPage( 'Other_Page' );
2636 $editStatus = $this->editPage( $page1, 'Revision 1' );
2637 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2638 $rev1 = $editStatus->getNewRevision();
2639 $editStatus = $this->editPage( $page2, 'Revision 1' );
2640 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2641 $rev2 = $editStatus->getNewRevision();
2642
2643 $this->expectException( InvalidArgumentException::class );
2644 $this->getServiceContainer()->getRevisionStore()
2645 ->{$method}( $page1->getId(), $rev1, $rev2 );
2646 }
2647
2648 /**
2649 * @dataProvider provideBetweenMethodNames
2650 *
2651 * @param string $method the name of the method to test
2652 */
2653 public function testBetweenMethod_unsavedRevision( $method ) {
2654 $rev1 = new MutableRevisionRecord( $this->getTestPageTitle() );
2655 $rev2 = new MutableRevisionRecord( $this->getTestPageTitle() );
2656
2657 $this->expectException( InvalidArgumentException::class );
2658 $this->getServiceContainer()->getRevisionStore()->{$method}(
2659 $this->getTestPage()->getId(), $rev1, $rev2 );
2660 }
2661
2662 /**
2663 * @dataProvider provideGetFirstRevision
2664 * @param callable $getPageIdentity
2665 */
2666 public function testGetFirstRevision( $getPageIdentity ) {
2667 [ $pageTitle, $pageIdentity ] = $getPageIdentity();
2668 $editStatus = $this->editPage( $pageTitle->getPrefixedDBkey(), 'First Revision' );
2669 $this->assertStatusGood( $editStatus, 'must create first revision' );
2670 $firstRevId = $editStatus->getNewRevision()->getId();
2671 $editStatus = $this->editPage( $pageTitle->getPrefixedText(), 'New Revision' );
2672 $this->assertStatusGood( $editStatus, 'must create new revision' );
2673 $this->assertNotSame(
2674 $firstRevId,
2675 $editStatus->getNewRevision()->getId(),
2676 'new revision must have different id'
2677 );
2678 $this->assertSame(
2679 $firstRevId,
2680 $this->getServiceContainer()
2681 ->getRevisionStore()
2682 ->getFirstRevision( $pageIdentity )
2683 ->getId()
2684 );
2685 }
2686
2687 public static function provideGetFirstRevision() {
2688 return [
2689 [ static function () {
2690 $pageTitle = Title::newFromText( 'Test_Get_First_Revision' );
2691 return [ $pageTitle, $pageTitle ];
2692 } ],
2693 [ static function () {
2694 $pageTitle = Title::newFromText( 'Test_Get_First_Revision' );
2695 return [ $pageTitle, $pageTitle->toPageIdentity() ];
2696 } ]
2697 ];
2698 }
2699
2700 public function testGetFirstRevision_nonexistent_page() {
2701 $this->assertNull(
2702 $this->getServiceContainer()
2703 ->getRevisionStore()
2704 ->getFirstRevision( $this->getNonexistingTestPage( __METHOD__ )->getTitle() )
2705 );
2706 }
2707
2708 public static function provideInsertRevisionByAnonAssignsNewActor() {
2709 yield 'User' => [ '127.1.1.0', static function ( MediaWikiServices $services, string $ip ) {
2710 return $services->getUserFactory()->newAnonymous( $ip );
2711 } ];
2712 yield 'User identity, anon' => [ '127.1.1.1', static function ( MediaWikiServices $services, string $ip ) {
2713 return new UserIdentityValue( 0, $ip );
2714 } ];
2715 }
2716
2717 /**
2718 * @dataProvider provideInsertRevisionByAnonAssignsNewActor
2719 */
2720 public function testInsertRevisionByAnonAssignsNewActor( string $ip, callable $userInitCallback ) {
2721 $this->disableAutoCreateTempUser();
2722 $user = $userInitCallback( $this->getServiceContainer(), $ip );
2723
2724 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
2725 $actorId = $actorNormalization->findActorId( $user, $this->getDb() );
2726 $this->assertNull( $actorId, 'New actor has no actor_id' );
2727
2728 $page = $this->getTestPage();
2729 $rev = new MutableRevisionRecord( $page->getTitle() );
2730 $rev->setTimestamp( '20180101000000' )
2731 ->setComment( CommentStoreComment::newUnsavedComment( 'test' ) )
2732 ->setUser( $user )
2733 ->setContent( SlotRecord::MAIN, new WikitextContent( 'Text' ) )
2734 ->setPageId( $page->getId() );
2735
2736 $return = $this->getServiceContainer()->getRevisionStore()->insertRevisionOn( $rev, $this->getDb() );
2737 $this->assertSame( $ip, $return->getUser()->getName() );
2738
2739 $actorId = $actorNormalization->findActorId( $user, $this->getDb() );
2740 $this->assertNotNull( $actorId );
2741 }
2742
2743 public function testGetTitle_successFromPageId() {
2744 $page = $this->getExistingTestPage();
2745
2746 $store = $this->getServiceContainer()->getRevisionStore();
2747 $title = $store->getTitle( $page->getId(), 0, IDBAccessObject::READ_NORMAL );
2748
2749 $this->assertTrue( $page->isSamePageAs( $title ) );
2750 }
2751
2752 public function testGetTitle_successFromRevId() {
2753 $page = $this->getExistingTestPage();
2754
2755 $store = $this->getServiceContainer()->getRevisionStore();
2756 $title = $store->getTitle( 0, $page->getLatest(), IDBAccessObject::READ_NORMAL );
2757
2758 $this->assertTrue( $page->isSamePageAs( $title ) );
2759 }
2760
2761 public function testGetTitle_failure() {
2762 $store = $this->getServiceContainer()->getRevisionStore();
2763
2764 $this->expectException( RevisionAccessException::class );
2765 $store->getTitle( 113349857, 897234779, IDBAccessObject::READ_NORMAL );
2766 }
2767
2768 public function testGetQueryInfo_NoSlotDataJoin() {
2769 $store = $this->getServiceContainer()->getRevisionStore();
2770 $queryInfo = $store->getQueryInfo();
2771
2772 // with the new schema enabled, query info should not join the main slot info
2773 $this->assertArrayNotHasKey( 'a_slot_data', $queryInfo['tables'] );
2774 $this->assertArrayNotHasKey( 'a_slot_data', $queryInfo['joins'] );
2775 }
2776
2777 public function testInsertRevisionOn_T202032() {
2778 // This test only makes sense for MySQL
2779 if ( $this->getDb()->getType() !== 'mysql' ) {
2780 $this->assertTrue( true );
2781 return;
2782 }
2783
2784 // NOTE: must be done before checking MAX(rev_id)
2785 $page = $this->getTestPage();
2786
2787 $maxRevId = $this->getDb()->newSelectQueryBuilder()
2788 ->select( 'MAX(rev_id)' )
2789 ->from( 'revision' )
2790 ->fetchField();
2791
2792 // Construct a slot row that will conflict with the insertion of the next revision ID,
2793 // to emulate the failure mode described in T202032. Nothing will ever read this row,
2794 // we just need it to trigger a primary key conflict.
2795 $this->getDb()->newInsertQueryBuilder()
2796 ->insertInto( 'slots' )
2797 ->row( [
2798 'slot_revision_id' => $maxRevId + 1,
2799 'slot_role_id' => 1,
2800 'slot_content_id' => 0,
2801 'slot_origin' => 0
2802 ] )
2803 ->caller( __METHOD__ )
2804 ->execute();
2805
2806 $rev = new MutableRevisionRecord( $page->getTitle() );
2807 $rev->setTimestamp( '20180101000000' )
2808 ->setComment( CommentStoreComment::newUnsavedComment( 'test' ) )
2809 ->setUser( $this->getTestUser()->getUser() )
2810 ->setContent( SlotRecord::MAIN, new WikitextContent( 'Text' ) )
2811 ->setPageId( $page->getId() );
2812
2813 $store = $this->getServiceContainer()->getRevisionStore();
2814 $return = $store->insertRevisionOn( $rev, $this->getDb() );
2815
2816 $this->assertSame( $maxRevId + 2, $return->getId() );
2817
2818 // is the new revision correct?
2819 $this->assertRevisionCompleteness( $return );
2820 $this->assertRevisionRecordsEqual( $rev, $return );
2821
2822 // can we find it directly in the database?
2823 $this->assertRevisionExistsInDatabase( $return );
2824
2825 // can we load it from the store?
2826 $loaded = $store->getRevisionById( $return->getId() );
2827 $this->assertRevisionCompleteness( $loaded );
2828 $this->assertRevisionRecordsEqual( $return, $loaded );
2829 }
2830
2831 public function testGetContentBlobsForBatch_error() {
2832 $page1 = $this->getTestPage();
2833 $text = __METHOD__ . 'b-ä';
2834 $editStatus = $this->editPage( $page1, $text . '1' );
2835 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2836 $revRecord1 = $editStatus->getNewRevision();
2837
2838 $contentAddress = $revRecord1->getSlot( SlotRecord::MAIN )->getAddress();
2839 $blobStatus = StatusValue::newGood( [] );
2840 $blobStatus->warning( 'internalerror_info', 'oops!' );
2841
2842 $mockBlobStore = $this->createMock( BlobStore::class );
2843 $mockBlobStore->method( 'getBlobBatch' )
2844 ->willReturn( $blobStatus );
2845
2846 $revStore = $this->getServiceContainer()
2847 ->getRevisionStoreFactory()
2848 ->getRevisionStore();
2849 $wrappedRevStore = TestingAccessWrapper::newFromObject( $revStore );
2850 $wrappedRevStore->blobStore = $mockBlobStore;
2851
2852 $result = $revStore->getContentBlobsForBatch( [ $revRecord1->getId() ] );
2853 $this->assertStatusWarning( 'internalerror_info', $result );
2854
2855 $records = $result->getValue();
2856 $this->assertArrayHasKey( $revRecord1->getId(), $records );
2857
2858 $mainRow = $records[$revRecord1->getId()][SlotRecord::MAIN];
2859 $this->assertNull( $mainRow->blob_data );
2860 $this->assertStatusMessagesExactly(
2861 StatusValue::newGood()
2862 ->warning( 'internalerror_info', 'oops!' )
2863 ->warning( 'internalerror_info', "Couldn't find blob data for rev {$revRecord1->getId()}" ),
2864 $result
2865 );
2866 }
2867
2868 public function testGetContentBlobsForBatchUsesGetBlobBatch() {
2869 $page1 = $this->getTestPage();
2870 $text = __METHOD__ . 'b-ä';
2871 $editStatus = $this->editPage( $page1, $text . '1' );
2872 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2873 $revRecord1 = $editStatus->getNewRevision();
2874
2875 $contentAddress = $revRecord1->getSlot( SlotRecord::MAIN )->getAddress();
2876 $mockBlobStore = $this->createMock( SqlBlobStore::class );
2877 $mockBlobStore
2878 ->expects( $this->once() )
2879 ->method( 'getBlobBatch' )
2880 ->with( [ $contentAddress ], $this->anything() )
2881 ->willReturn( StatusValue::newGood( [
2882 $contentAddress => 'Content_From_Mock'
2883 ] ) );
2884 $mockBlobStore
2885 ->expects( $this->never() )
2886 ->method( 'getBlob' );
2887
2888 $revStore = $this->getServiceContainer()
2889 ->getRevisionStoreFactory()
2890 ->getRevisionStore();
2891 $wrappedRevStore = TestingAccessWrapper::newFromObject( $revStore );
2892 $wrappedRevStore->blobStore = $mockBlobStore;
2893
2894 $result = $revStore->getContentBlobsForBatch(
2895 [ $revRecord1->getId() ],
2896 [ SlotRecord::MAIN ]
2897 );
2898 $this->assertStatusGood( $result );
2899 $this->assertSame( 'Content_From_Mock',
2900 $result->getValue()[$revRecord1->getId()][SlotRecord::MAIN]->blob_data );
2901 }
2902
2903 public function testNewRevisionsFromBatch_error() {
2904 $page = $this->getTestPage();
2905 $text = __METHOD__ . 'b-ä';
2906 $revRecord1 = $page->doUserEditContent(
2907 new WikitextContent( $text . '1' ),
2908 $this->getTestUser()->getUser(),
2909 __METHOD__ . 'b'
2910 )->getNewRevision();
2911
2912 $invalidRow = $this->revisionRecordToRow( $revRecord1 );
2913 $invalidRow->rev_id = 100500;
2914 $result = $this->getServiceContainer()->getRevisionStore()
2915 ->newRevisionsFromBatch(
2916 [ $this->revisionRecordToRow( $revRecord1 ), $invalidRow ],
2917 [
2918 'slots' => [ SlotRecord::MAIN ],
2919 'content' => true
2920 ]
2921 );
2922 $this->assertStatusWarning( 'internalerror_info', $result );
2923 $records = $result->getValue();
2924 $this->assertRevisionRecordsEqual( $revRecord1, $records[$revRecord1->getId()] );
2925 $this->assertSame( $text . '1',
2926 $records[$revRecord1->getId()]->getContent( SlotRecord::MAIN )->serialize() );
2927 $this->assertEquals( $page->getTitle()->getDBkey(),
2928 $records[$revRecord1->getId()]->getPageAsLinkTarget()->getDBkey() );
2929 $this->assertNull( $records[$invalidRow->rev_id] );
2930 $this->assertStatusMessagesExactly(
2931 StatusValue::newGood()
2932 ->warning( 'internalerror_info', "Couldn't find slots for rev 100500" ),
2933 $result
2934 );
2935 }
2936
2937 public function testNewRevisionFromBatchUsesGetBlobBatch() {
2938 $page1 = $this->getTestPage();
2939 $text = __METHOD__ . 'b-ä';
2940 $editStatus = $this->editPage( $page1, $text . '1' );
2941 $this->assertStatusGood( $editStatus, 'must create revision 1' );
2942 $revRecord1 = $editStatus->getNewRevision();
2943
2944 $contentAddress = $revRecord1->getSlot( SlotRecord::MAIN )->getAddress();
2945 $mockBlobStore = $this->createMock( SqlBlobStore::class );
2946 $mockBlobStore
2947 ->expects( $this->once() )
2948 ->method( 'getBlobBatch' )
2949 ->with( [ $contentAddress ], $this->anything() )
2950 ->willReturn( StatusValue::newGood( [
2951 $contentAddress => 'Content_From_Mock'
2952 ] ) );
2953 $mockBlobStore
2954 ->expects( $this->never() )
2955 ->method( 'getBlob' );
2956
2957 $revStore = $this->getServiceContainer()
2958 ->getRevisionStoreFactory()
2959 ->getRevisionStore();
2960 $wrappedRevStore = TestingAccessWrapper::newFromObject( $revStore );
2961 $wrappedRevStore->blobStore = $mockBlobStore;
2962
2963 $result = $revStore->newRevisionsFromBatch(
2964 [ $this->revisionRecordToRow( $revRecord1 ) ],
2965 [
2966 'slots' => [ SlotRecord::MAIN ],
2967 'content' => true
2968 ]
2969 );
2970 $this->assertStatusGood( $result );
2971 $content = $result->getValue()[$revRecord1->getId()]->getContent( SlotRecord::MAIN );
2972 $this->assertInstanceOf( TextContent::class, $content );
2973 $this->assertSame(
2974 'Content_From_Mock',
2975 $content->getText()
2976 );
2977 }
2978
2979 public function testFindIdenticalRevision() {
2980 // Prepare a page with 3 revisions
2981 $page = $this->getExistingTestPage( __METHOD__ );
2982 $status = $this->editPage( $page, 'Content 1' );
2983 $this->assertStatusGood( $status, 'edit 1' );
2984 $originalRev = $status->getNewRevision();
2985
2986 $this->assertStatusGood( $this->editPage( $page, 'Content 2' ), 'edit 2' );
2987
2988 $status = $this->editPage( $page, 'Content 1' );
2989 $this->assertStatusGood( $status, 'edit 3' );
2990 $latestRev = $status->getNewRevision();
2991
2992 $store = $this->getServiceContainer()->getRevisionStore();
2993
2994 $this->assertNull( $store->findIdenticalRevision( $latestRev, 0 ) );
2995 $this->assertNull( $store->findIdenticalRevision( $latestRev, 1 ) );
2996 $foundRev = $store->findIdenticalRevision( $latestRev, 1000 );
2997 $this->assertSame( $originalRev->getId(), $foundRev->getId() );
2998 }
2999 }