5927cfc9fb2ca961df94b90dfe79e95a9c52703f
[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
737 /**
738 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
739 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
740 */
741 public function testNewRevisionFromRow_anonEdit() {
742 $page = $this->getExistingTestPage();
743
744 $text = __METHOD__ . 'a-ä';
745 /** @var Revision $rev */
746 $rev = $page->doEditContent(
747 new WikitextContent( $text ),
748 __METHOD__ . 'a'
749 )->value['revision'];
750
751 $store = MediaWikiServices::getInstance()->getRevisionStore();
752 $record = $store->newRevisionFromRow(
753 $this->revisionToRow( $rev ),
754 [],
755 $page->getTitle()
756 );
757 $this->assertRevisionRecordMatchesRevision( $rev, $record );
758 $this->assertSame( $text, $rev->getContent()->serialize() );
759 }
760
761 /**
762 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
763 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
764 */
765 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
766 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
767 $this->overrideMwServices();
768 $page = $this->getExistingTestPage();
769
770 $text = __METHOD__ . 'a-ä';
771 /** @var Revision $rev */
772 $rev = $page->doEditContent(
773 new WikitextContent( $text ),
774 __METHOD__. 'a'
775 )->value['revision'];
776
777 $store = MediaWikiServices::getInstance()->getRevisionStore();
778 $record = $store->newRevisionFromRow(
779 $this->revisionToRow( $rev ),
780 [],
781 $page->getTitle()
782 );
783 $this->assertRevisionRecordMatchesRevision( $rev, $record );
784 $this->assertSame( $text, $rev->getContent()->serialize() );
785 }
786
787 /**
788 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
789 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
790 */
791 public function testNewRevisionFromRow_userEdit() {
792 $page = $this->getExistingTestPage();
793
794 $text = __METHOD__ . 'b-ä';
795 /** @var Revision $rev */
796 $rev = $page->doEditContent(
797 new WikitextContent( $text ),
798 __METHOD__ . 'b',
799 0,
800 false,
801 $this->getTestUser()->getUser()
802 )->value['revision'];
803
804 $store = MediaWikiServices::getInstance()->getRevisionStore();
805 $record = $store->newRevisionFromRow(
806 $this->revisionToRow( $rev ),
807 [],
808 $page->getTitle()
809 );
810 $this->assertRevisionRecordMatchesRevision( $rev, $record );
811 $this->assertSame( $text, $rev->getContent()->serialize() );
812 }
813
814 /**
815 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
816 */
817 public function testNewRevisionFromArchiveRow() {
818 $store = MediaWikiServices::getInstance()->getRevisionStore();
819 $title = Title::newFromText( __METHOD__ );
820 $text = __METHOD__ . '-bä';
821 $page = $this->getExistingTestPage( $title );
822 /** @var Revision $orig */
823 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
824 ->value['revision'];
825 $page->doDeleteArticle( __METHOD__ );
826
827 $db = wfGetDB( DB_MASTER );
828 $arQuery = $store->getArchiveQueryInfo();
829 $res = $db->select(
830 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
831 __METHOD__, [], $arQuery['joins']
832 );
833 $this->assertTrue( is_object( $res ), 'query failed' );
834
835 $row = $res->fetchObject();
836 $res->free();
837 $record = $store->newRevisionFromArchiveRow( $row );
838
839 $this->assertRevisionRecordMatchesRevision( $orig, $record );
840 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
841 }
842
843 /**
844 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
845 */
846 public function testNewRevisionFromArchiveRow_legacyEncoding() {
847 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
848 $this->overrideMwServices();
849 $store = MediaWikiServices::getInstance()->getRevisionStore();
850 $title = Title::newFromText( __METHOD__ );
851 $text = __METHOD__ . '-bä';
852 $page = $this->getExistingTestPage( $title );
853 /** @var Revision $orig */
854 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
855 ->value['revision'];
856 $page->doDeleteArticle( __METHOD__ );
857
858 $db = wfGetDB( DB_MASTER );
859 $arQuery = $store->getArchiveQueryInfo();
860 $res = $db->select(
861 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
862 __METHOD__, [], $arQuery['joins']
863 );
864 $this->assertTrue( is_object( $res ), 'query failed' );
865
866 $row = $res->fetchObject();
867 $res->free();
868 $record = $store->newRevisionFromArchiveRow( $row );
869
870 $this->assertRevisionRecordMatchesRevision( $orig, $record );
871 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
872 }
873
874 /**
875 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
876 */
877 public function testLoadRevisionFromId() {
878 $title = Title::newFromText( __METHOD__ );
879 $page = $this->getExistingTestPage( $title );
880 $rev = $page->getRevision();
881
882 $store = MediaWikiServices::getInstance()->getRevisionStore();
883 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
884 $this->assertRevisionRecordMatchesRevision( $rev, $result );
885 }
886
887 /**
888 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
889 */
890 public function testLoadRevisionFromPageId() {
891 $title = Title::newFromText( __METHOD__ );
892 $page = $this->getExistingTestPage( $title );
893 $rev = $page->getRevision();
894
895 $store = MediaWikiServices::getInstance()->getRevisionStore();
896 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
897 $this->assertRevisionRecordMatchesRevision( $rev, $result );
898 }
899
900 /**
901 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
902 */
903 public function testLoadRevisionFromTitle() {
904 $title = Title::newFromText( __METHOD__ );
905 $page = $this->getExistingTestPage( $title );
906 $rev = $page->getRevision();
907
908 $store = MediaWikiServices::getInstance()->getRevisionStore();
909 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
910 $this->assertRevisionRecordMatchesRevision( $rev, $result );
911 }
912
913 /**
914 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
915 */
916 public function testLoadRevisionFromTimestamp() {
917 $page = $this->getNonexistingTestPage( __METHOD__ );
918 $title = $page->getTitle();
919 /** @var Revision $revOne */
920 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
921 ->value['revision'];
922 // Sleep to ensure different timestamps... )(evil)
923 sleep( 1 );
924 /** @var Revision $revTwo */
925 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
926 ->value['revision'];
927
928 $store = MediaWikiServices::getInstance()->getRevisionStore();
929 $this->assertNull(
930 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
931 );
932 $this->assertSame(
933 $revOne->getId(),
934 $store->loadRevisionFromTimestamp(
935 wfGetDB( DB_MASTER ),
936 $title,
937 $revOne->getTimestamp()
938 )->getId()
939 );
940 $this->assertSame(
941 $revTwo->getId(),
942 $store->loadRevisionFromTimestamp(
943 wfGetDB( DB_MASTER ),
944 $title,
945 $revTwo->getTimestamp()
946 )->getId()
947 );
948 }
949
950 /**
951 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
952 */
953 public function testGetParentLengths() {
954 $page = $this->getNonexistingTestPage( __METHOD__ );
955 /** @var Revision $revOne */
956 $revOne = $page->doEditContent(
957 new WikitextContent( __METHOD__ ), __METHOD__
958 )->value['revision'];
959 /** @var Revision $revTwo */
960 $revTwo = $page->doEditContent(
961 new WikitextContent( __METHOD__ . '2' ), __METHOD__
962 )->value['revision'];
963
964 $store = MediaWikiServices::getInstance()->getRevisionStore();
965 $this->assertSame(
966 [
967 $revOne->getId() => strlen( __METHOD__ ),
968 ],
969 $store->listRevisionSizes(
970 wfGetDB( DB_MASTER ),
971 [ $revOne->getId() ]
972 )
973 );
974 $this->assertSame(
975 [
976 $revOne->getId() => strlen( __METHOD__ ),
977 $revTwo->getId() => strlen( __METHOD__ ) + 1,
978 ],
979 $store->listRevisionSizes(
980 wfGetDB( DB_MASTER ),
981 [ $revOne->getId(), $revTwo->getId() ]
982 )
983 );
984 }
985
986 /**
987 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
988 */
989 public function testGetPreviousRevision() {
990 $page = $this->getNonexistingTestPage( __METHOD__ );
991 /** @var Revision $revOne */
992 $revOne = $page->doEditContent(
993 new WikitextContent( __METHOD__ ), __METHOD__
994 )->value['revision'];
995 /** @var Revision $revTwo */
996 $revTwo = $page->doEditContent(
997 new WikitextContent( __METHOD__ . '2' ), __METHOD__
998 )->value['revision'];
999
1000 $store = MediaWikiServices::getInstance()->getRevisionStore();
1001 $this->assertNull(
1002 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1003 );
1004 $this->assertSame(
1005 $revOne->getId(),
1006 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1007 );
1008 }
1009
1010 /**
1011 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1012 */
1013 public function testGetNextRevision() {
1014 $page = $this->getNonexistingTestPage( __METHOD__ );
1015 /** @var Revision $revOne */
1016 $revOne = $page->doEditContent(
1017 new WikitextContent( __METHOD__ ), __METHOD__
1018 )->value['revision'];
1019 /** @var Revision $revTwo */
1020 $revTwo = $page->doEditContent(
1021 new WikitextContent( __METHOD__ . '2' ), __METHOD__
1022 )->value['revision'];
1023
1024 $store = MediaWikiServices::getInstance()->getRevisionStore();
1025 $this->assertSame(
1026 $revTwo->getId(),
1027 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1028 );
1029 $this->assertNull(
1030 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1031 );
1032 }
1033
1034 /**
1035 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1036 */
1037 public function testGetTimestampFromId_found() {
1038 $page = $this->getExistingTestPage();
1039 $rev = $page->getRevision();
1040
1041 $store = MediaWikiServices::getInstance()->getRevisionStore();
1042 $result = $store->getTimestampFromId(
1043 $page->getTitle(),
1044 $rev->getId()
1045 );
1046
1047 $this->assertSame( $rev->getTimestamp(), $result );
1048 }
1049
1050 /**
1051 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1052 */
1053 public function testGetTimestampFromId_notFound() {
1054 $page = $this->getExistingTestPage();
1055 $rev = $page->getRevision();
1056
1057 $store = MediaWikiServices::getInstance()->getRevisionStore();
1058 $result = $store->getTimestampFromId(
1059 $page->getTitle(),
1060 $rev->getId() + 1
1061 );
1062
1063 $this->assertFalse( $result );
1064 }
1065
1066 /**
1067 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1068 */
1069 public function testCountRevisionsByPageId() {
1070 $store = MediaWikiServices::getInstance()->getRevisionStore();
1071 $page = $this->getNonexistingTestPage( __METHOD__ );
1072
1073 $this->assertSame(
1074 0,
1075 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1076 );
1077 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1078 $this->assertSame(
1079 1,
1080 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1081 );
1082 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1083 $this->assertSame(
1084 2,
1085 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1086 );
1087 }
1088
1089 /**
1090 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1091 */
1092 public function testCountRevisionsByTitle() {
1093 $store = MediaWikiServices::getInstance()->getRevisionStore();
1094 $page = $this->getNonexistingTestPage( __METHOD__ );
1095
1096 $this->assertSame(
1097 0,
1098 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1099 );
1100 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1101 $this->assertSame(
1102 1,
1103 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1104 );
1105 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1106 $this->assertSame(
1107 2,
1108 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1109 );
1110 }
1111
1112 /**
1113 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1114 */
1115 public function testUserWasLastToEdit_false() {
1116 $sysop = $this->getTestSysop()->getUser();
1117 $page = $this->getExistingTestPage();
1118 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1119
1120 $store = MediaWikiServices::getInstance()->getRevisionStore();
1121 $result = $store->userWasLastToEdit(
1122 wfGetDB( DB_MASTER ),
1123 $page->getId(),
1124 $sysop->getId(),
1125 '20160101010101'
1126 );
1127 $this->assertFalse( $result );
1128 }
1129
1130 /**
1131 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1132 */
1133 public function testUserWasLastToEdit_true() {
1134 $startTime = wfTimestampNow();
1135 $sysop = $this->getTestSysop()->getUser();
1136 $page = $this->getExistingTestPage();
1137 $page->doEditContent(
1138 new WikitextContent( __METHOD__ ),
1139 __METHOD__,
1140 0,
1141 false,
1142 $sysop
1143 );
1144
1145 $store = MediaWikiServices::getInstance()->getRevisionStore();
1146 $result = $store->userWasLastToEdit(
1147 wfGetDB( DB_MASTER ),
1148 $page->getId(),
1149 $sysop->getId(),
1150 $startTime
1151 );
1152 $this->assertTrue( $result );
1153 }
1154
1155 /**
1156 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1157 */
1158 public function testGetKnownCurrentRevision() {
1159 $page = $this->getExistingTestPage();
1160 /** @var Revision $rev */
1161 $rev = $page->doEditContent(
1162 new WikitextContent( __METHOD__ . 'b' ),
1163 __METHOD__ . 'b',
1164 0,
1165 false,
1166 $this->getTestUser()->getUser()
1167 )->value['revision'];
1168
1169 $store = MediaWikiServices::getInstance()->getRevisionStore();
1170 $record = $store->getKnownCurrentRevision(
1171 $page->getTitle(),
1172 $rev->getId()
1173 );
1174
1175 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1176 }
1177
1178 public function provideNewMutableRevisionFromArray() {
1179 yield 'Basic array, with page & id' => [
1180 [
1181 'id' => 2,
1182 'page' => 1,
1183 'text_id' => 2,
1184 'timestamp' => '20171017114835',
1185 'user_text' => '111.0.1.2',
1186 'user' => 0,
1187 'minor_edit' => false,
1188 'deleted' => 0,
1189 'len' => 46,
1190 'parent_id' => 1,
1191 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1192 'comment' => 'Goat Comment!',
1193 'content_format' => 'text/x-wiki',
1194 'content_model' => 'wikitext',
1195 ]
1196 ];
1197 yield 'Basic array, content object' => [
1198 [
1199 'id' => 2,
1200 'page' => 1,
1201 'timestamp' => '20171017114835',
1202 'user_text' => '111.0.1.2',
1203 'user' => 0,
1204 'minor_edit' => false,
1205 'deleted' => 0,
1206 'len' => 46,
1207 'parent_id' => 1,
1208 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1209 'comment' => 'Goat Comment!',
1210 'content' => new WikitextContent( 'Some Content' ),
1211 ]
1212 ];
1213 yield 'Basic array, serialized text' => [
1214 [
1215 'id' => 2,
1216 'page' => 1,
1217 'timestamp' => '20171017114835',
1218 'user_text' => '111.0.1.2',
1219 'user' => 0,
1220 'minor_edit' => false,
1221 'deleted' => 0,
1222 'len' => 46,
1223 'parent_id' => 1,
1224 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1225 'comment' => 'Goat Comment!',
1226 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1227 ]
1228 ];
1229 yield 'Basic array, serialized text, utf-8 flags' => [
1230 [
1231 'id' => 2,
1232 'page' => 1,
1233 'timestamp' => '20171017114835',
1234 'user_text' => '111.0.1.2',
1235 'user' => 0,
1236 'minor_edit' => false,
1237 'deleted' => 0,
1238 'len' => 46,
1239 'parent_id' => 1,
1240 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1241 'comment' => 'Goat Comment!',
1242 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1243 'flags' => 'utf-8',
1244 ]
1245 ];
1246 yield 'Basic array, with title' => [
1247 [
1248 'title' => Title::newFromText( 'SomeText' ),
1249 'text_id' => 2,
1250 'timestamp' => '20171017114835',
1251 'user_text' => '111.0.1.2',
1252 'user' => 0,
1253 'minor_edit' => false,
1254 'deleted' => 0,
1255 'len' => 46,
1256 'parent_id' => 1,
1257 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1258 'comment' => 'Goat Comment!',
1259 'content_format' => 'text/x-wiki',
1260 'content_model' => 'wikitext',
1261 ]
1262 ];
1263 yield 'Basic array, no user field' => [
1264 [
1265 'id' => 2,
1266 'page' => 1,
1267 'text_id' => 2,
1268 'timestamp' => '20171017114835',
1269 'user_text' => '111.0.1.3',
1270 'minor_edit' => false,
1271 'deleted' => 0,
1272 'len' => 46,
1273 'parent_id' => 1,
1274 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1275 'comment' => 'Goat Comment!',
1276 'content_format' => 'text/x-wiki',
1277 'content_model' => 'wikitext',
1278 ]
1279 ];
1280 }
1281
1282 /**
1283 * @dataProvider provideNewMutableRevisionFromArray
1284 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1285 */
1286 public function testNewMutableRevisionFromArray( array $array ) {
1287 $store = MediaWikiServices::getInstance()->getRevisionStore();
1288
1289 // HACK: if $array['page'] is given, make sure that the page exists
1290 if ( isset( $array['page'] ) ) {
1291 $t = Title::newFromID( $array['page'] );
1292 if ( !$t || !$t->exists() ) {
1293 $t = Title::makeTitle( NS_MAIN, __METHOD__ );
1294 $info = $this->insertPage( $t );
1295 $array['page'] = $info['id'];
1296 }
1297 }
1298
1299 $result = $store->newMutableRevisionFromArray( $array );
1300
1301 if ( isset( $array['id'] ) ) {
1302 $this->assertSame( $array['id'], $result->getId() );
1303 }
1304 if ( isset( $array['page'] ) ) {
1305 $this->assertSame( $array['page'], $result->getPageId() );
1306 }
1307 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1308 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1309 if ( isset( $array['user'] ) ) {
1310 $this->assertSame( $array['user'], $result->getUser()->getId() );
1311 }
1312 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1313 $this->assertSame( $array['deleted'], $result->getVisibility() );
1314 $this->assertSame( $array['len'], $result->getSize() );
1315 $this->assertSame( $array['parent_id'], $result->getParentId() );
1316 $this->assertSame( $array['sha1'], $result->getSha1() );
1317 $this->assertSame( $array['comment'], $result->getComment()->text );
1318 if ( isset( $array['content'] ) ) {
1319 $this->assertTrue(
1320 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1321 );
1322 } elseif ( isset( $array['text'] ) ) {
1323 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1324 } else {
1325 $this->assertSame(
1326 $array['content_format'],
1327 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1328 );
1329 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1330 }
1331 }
1332
1333 /**
1334 * @dataProvider provideNewMutableRevisionFromArray
1335 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1336 */
1337 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1338 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1339 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
1340 $blobStore = new SqlBlobStore( $lb, $cache );
1341 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1342
1343 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1344 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1345 ->disableOriginalConstructor()
1346 ->getMock();
1347 $factory->expects( $this->any() )
1348 ->method( 'newBlobStore' )
1349 ->willReturn( $blobStore );
1350 $factory->expects( $this->any() )
1351 ->method( 'newSqlBlobStore' )
1352 ->willReturn( $blobStore );
1353
1354 $this->setService( 'BlobStoreFactory', $factory );
1355
1356 $this->testNewMutableRevisionFromArray( $array );
1357 }
1358
1359 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1360 $fields = [
1361 'rev_id',
1362 'rev_page',
1363 'rev_timestamp',
1364 'rev_minor_edit',
1365 'rev_deleted',
1366 'rev_len',
1367 'rev_parent_id',
1368 'rev_sha1',
1369 ];
1370 if ( $returnTextIdField ) {
1371 $fields[] = 'rev_text_id';
1372 }
1373 return $fields;
1374 }
1375
1376 protected function getCommentQueryFields() {
1377 return [
1378 'rev_comment_text' => 'rev_comment',
1379 'rev_comment_data' => 'NULL',
1380 'rev_comment_cid' => 'NULL',
1381 ];
1382 }
1383
1384 protected function getActorQueryFields() {
1385 return [
1386 'rev_user' => 'rev_user',
1387 'rev_user_text' => 'rev_user_text',
1388 'rev_actor' => 'NULL',
1389 ];
1390 }
1391
1392 protected function getContentHandlerQueryFields() {
1393 return [
1394 'rev_content_format',
1395 'rev_content_model',
1396 ];
1397 }
1398
1399 abstract public function provideGetQueryInfo();
1400
1401 /**
1402 * @dataProvider provideGetQueryInfo
1403 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1404 */
1405 public function testGetQueryInfo( $options, $expected ) {
1406 $store = MediaWikiServices::getInstance()->getRevisionStore();
1407
1408 $queryInfo = $store->getQueryInfo( $options );
1409
1410 $this->assertArrayEqualsIgnoringIntKeyOrder(
1411 $expected['tables'],
1412 $queryInfo['tables']
1413 );
1414 $this->assertArrayEqualsIgnoringIntKeyOrder(
1415 $expected['fields'],
1416 $queryInfo['fields']
1417 );
1418 $this->assertArrayEqualsIgnoringIntKeyOrder(
1419 $expected['joins'],
1420 $queryInfo['joins']
1421 );
1422 }
1423
1424 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1425 $fields = [
1426 'ar_id',
1427 'ar_page_id',
1428 'ar_namespace',
1429 'ar_title',
1430 'ar_rev_id',
1431 'ar_timestamp',
1432 'ar_minor_edit',
1433 'ar_deleted',
1434 'ar_len',
1435 'ar_parent_id',
1436 'ar_sha1',
1437 ];
1438 if ( $returnTextFields ) {
1439 $fields[] = 'ar_text_id';
1440 }
1441 return $fields;
1442 }
1443
1444 abstract public function provideGetArchiveQueryInfo();
1445
1446 /**
1447 * @dataProvider provideGetArchiveQueryInfo
1448 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1449 */
1450 public function testGetArchiveQueryInfo( $expected ) {
1451 $store = MediaWikiServices::getInstance()->getRevisionStore();
1452
1453 $archiveQueryInfo = $store->getArchiveQueryInfo();
1454
1455 $this->assertArrayEqualsIgnoringIntKeyOrder(
1456 $expected['tables'],
1457 $archiveQueryInfo['tables']
1458 );
1459
1460 $this->assertArrayEqualsIgnoringIntKeyOrder(
1461 $expected['fields'],
1462 $archiveQueryInfo['fields']
1463 );
1464
1465 $this->assertArrayEqualsIgnoringIntKeyOrder(
1466 $expected['joins'],
1467 $archiveQueryInfo['joins']
1468 );
1469 }
1470
1471 /**
1472 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1473 * keys.
1474 *
1475 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1476 * split into a string key array and an int key array before assertions occur.
1477 *
1478 * @param array $expected
1479 * @param array $actual
1480 */
1481 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1482 $this->objectAssociativeSort( $expected );
1483 $this->objectAssociativeSort( $actual );
1484
1485 // Separate the int key values from the string key values so that assertion failures are
1486 // easier to understand.
1487 $expectedIntKeyValues = [];
1488 $actualIntKeyValues = [];
1489
1490 // Remove all int keys and re add them at the end after sorting by value
1491 // This will result in all int keys being in the same order with same ints at the end of
1492 // the array
1493 foreach ( $expected as $key => $value ) {
1494 if ( is_int( $key ) ) {
1495 unset( $expected[$key] );
1496 $expectedIntKeyValues[] = $value;
1497 }
1498 }
1499 foreach ( $actual as $key => $value ) {
1500 if ( is_int( $key ) ) {
1501 unset( $actual[$key] );
1502 $actualIntKeyValues[] = $value;
1503 }
1504 }
1505
1506 $this->assertArrayEquals( $expected, $actual, false, true );
1507 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );
1508 }
1509
1510 }