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