Merge "maintenance: Document secondary purpose of --server"
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionStoreDbTest.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 Revision;
21 use TestUserRegistry;
22 use Title;
23 use WANObjectCache;
24 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\DatabaseSqlite;
26 use Wikimedia\Rdbms\FakeResultWrapper;
27 use Wikimedia\Rdbms\LoadBalancer;
28 use Wikimedia\Rdbms\TransactionProfiler;
29 use WikiPage;
30 use WikitextContent;
31
32 /**
33 * @group Database
34 */
35 class RevisionStoreDbTest extends MediaWikiTestCase {
36
37 public function setUp() {
38 parent::setUp();
39 $this->tablesUsed[] = 'archive';
40 $this->tablesUsed[] = 'page';
41 $this->tablesUsed[] = 'revision';
42 $this->tablesUsed[] = 'comment';
43 }
44
45 /**
46 * @return LoadBalancer
47 */
48 private function getLoadBalancerMock( array $server ) {
49 $lb = $this->getMockBuilder( LoadBalancer::class )
50 ->setMethods( [ 'reallyOpenConnection' ] )
51 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
52 ->getMock();
53
54 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
55 function ( array $server, $dbNameOverride ) {
56 return $this->getDatabaseMock( $server );
57 }
58 );
59
60 return $lb;
61 }
62
63 /**
64 * @return Database
65 */
66 private function getDatabaseMock( array $params ) {
67 $db = $this->getMockBuilder( DatabaseSqlite::class )
68 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
69 ->setConstructorArgs( [ $params ] )
70 ->getMock();
71
72 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
73 $db->method( 'isOpen' )->willReturn( true );
74
75 return $db;
76 }
77
78 public function provideDomainCheck() {
79 yield [ false, 'test', '' ];
80 yield [ 'test', 'test', '' ];
81
82 yield [ false, 'test', 'foo_' ];
83 yield [ 'test-foo_', 'test', 'foo_' ];
84
85 yield [ false, 'dash-test', '' ];
86 yield [ 'dash-test', 'dash-test', '' ];
87
88 yield [ false, 'underscore_test', 'foo_' ];
89 yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
90 }
91
92 /**
93 * @dataProvider provideDomainCheck
94 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
95 */
96 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
97 $this->setMwGlobals(
98 [
99 'wgDBname' => $dbName,
100 'wgDBprefix' => $dbPrefix,
101 ]
102 );
103
104 $loadBalancer = $this->getLoadBalancerMock(
105 [
106 'host' => '*dummy*',
107 'dbDirectory' => '*dummy*',
108 'user' => 'test',
109 'password' => 'test',
110 'flags' => 0,
111 'variables' => [],
112 'schema' => '',
113 'cliMode' => true,
114 'agent' => '',
115 'load' => 100,
116 'profiler' => null,
117 'trxProfiler' => new TransactionProfiler(),
118 'connLogger' => new \Psr\Log\NullLogger(),
119 'queryLogger' => new \Psr\Log\NullLogger(),
120 'errorLogger' => function () {
121 },
122 'deprecationLogger' => function () {
123 },
124 'type' => 'test',
125 'dbname' => $dbName,
126 'tablePrefix' => $dbPrefix,
127 ]
128 );
129 $db = $loadBalancer->getConnection( DB_REPLICA );
130
131 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
132 ->disableOriginalConstructor()
133 ->getMock();
134
135 $store = new RevisionStore(
136 $loadBalancer,
137 $blobStore,
138 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
139 MediaWikiServices::getInstance()->getCommentStore(),
140 MediaWikiServices::getInstance()->getActorMigration(),
141 $wikiId
142 );
143
144 $count = $store->countRevisionsByPageId( $db, 0 );
145
146 // Dummy check to make PhpUnit happy. We are really only interested in
147 // countRevisionsByPageId not failing due to the DB domain check.
148 $this->assertSame( 0, $count );
149 }
150
151 private function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
152 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
153 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
154 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
155 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
156 }
157
158 private function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
159 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
160 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
161 $this->assertEquals( $r1->getComment(), $r2->getComment() );
162 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
163 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
164 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
165 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
166 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
167 $this->assertEquals( $r1->getSize(), $r2->getSize() );
168 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
169 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
170 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
171 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
172 foreach ( $r1->getSlotRoles() as $role ) {
173 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
174 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
175 }
176 foreach ( [
177 RevisionRecord::DELETED_TEXT,
178 RevisionRecord::DELETED_COMMENT,
179 RevisionRecord::DELETED_USER,
180 RevisionRecord::DELETED_RESTRICTED,
181 ] as $field ) {
182 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
183 }
184 }
185
186 private function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) {
187 $this->assertSame( $s1->getRole(), $s2->getRole() );
188 $this->assertSame( $s1->getModel(), $s2->getModel() );
189 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
190 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
191 $this->assertSame( $s1->getSize(), $s2->getSize() );
192 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
193
194 $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
195 $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
196 }
197
198 private function assertRevisionCompleteness( RevisionRecord $r ) {
199 foreach ( $r->getSlotRoles() as $role ) {
200 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
201 }
202 }
203
204 private function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) {
205 $this->assertTrue( $slot->hasAddress() );
206 $this->assertSame( $r->getId(), $slot->getRevision() );
207 }
208
209 /**
210 * @param mixed[] $details
211 *
212 * @return RevisionRecord
213 */
214 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
215 // Convert some values that can't be provided by dataProviders
216 $page = WikiPage::factory( $title );
217 if ( isset( $details['user'] ) && $details['user'] === true ) {
218 $details['user'] = $this->getTestUser()->getUser();
219 }
220 if ( isset( $details['page'] ) && $details['page'] === true ) {
221 $details['page'] = $page->getId();
222 }
223 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
224 $details['parent'] = $page->getLatest();
225 }
226
227 // Create the RevisionRecord with any available data
228 $rev = new MutableRevisionRecord( $title );
229 isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
230 isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
231 isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
232 isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
233 isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
234 isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
235 isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
236 isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
237 isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
238 isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
239 isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
240
241 return $rev;
242 }
243
244 private function getRandomCommentStoreComment() {
245 return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
246 }
247
248 public function provideInsertRevisionOn_successes() {
249 yield 'Bare minimum revision insertion' => [
250 Title::newFromText( 'UTPage' ),
251 [
252 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
253 'parent' => true,
254 'comment' => $this->getRandomCommentStoreComment(),
255 'timestamp' => '20171117010101',
256 'user' => true,
257 ],
258 ];
259 yield 'Detailed revision insertion' => [
260 Title::newFromText( 'UTPage' ),
261 [
262 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
263 'parent' => true,
264 'page' => true,
265 'comment' => $this->getRandomCommentStoreComment(),
266 'timestamp' => '20171117010101',
267 'user' => true,
268 'minor' => true,
269 'visibility' => RevisionRecord::DELETED_RESTRICTED,
270 ],
271 ];
272 }
273
274 /**
275 * @dataProvider provideInsertRevisionOn_successes
276 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
277 */
278 public function testInsertRevisionOn_successes( Title $title, array $revDetails = [] ) {
279 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
280
281 $store = MediaWikiServices::getInstance()->getRevisionStore();
282 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
283
284 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
285 $this->assertRevisionRecordsEqual( $rev, $return );
286 $this->assertRevisionCompleteness( $return );
287 }
288
289 /**
290 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
291 */
292 public function testInsertRevisionOn_blobAddressExists() {
293 $title = Title::newFromText( 'UTPage' );
294 $revDetails = [
295 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
296 'parent' => true,
297 'comment' => $this->getRandomCommentStoreComment(),
298 'timestamp' => '20171117010101',
299 'user' => true,
300 ];
301
302 $store = MediaWikiServices::getInstance()->getRevisionStore();
303
304 // Insert the first revision
305 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
306 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
307 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
308 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
309
310 // Insert a second revision inheriting the same blob address
311 $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) );
312 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
313 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
314 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
315 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
316
317 // Assert that the same blob address has been used.
318 $this->assertEquals(
319 $firstReturn->getSlot( 'main' )->getAddress(),
320 $secondReturn->getSlot( 'main' )->getAddress()
321 );
322 // And that different revisions have been created.
323 $this->assertNotSame(
324 $firstReturn->getId(),
325 $secondReturn->getId()
326 );
327 }
328
329 public function provideInsertRevisionOn_failures() {
330 yield 'no slot' => [
331 Title::newFromText( 'UTPage' ),
332 [
333 'comment' => $this->getRandomCommentStoreComment(),
334 'timestamp' => '20171117010101',
335 'user' => true,
336 ],
337 new InvalidArgumentException( 'At least one slot needs to be defined!' )
338 ];
339 yield 'slot that is not main slot' => [
340 Title::newFromText( 'UTPage' ),
341 [
342 'slot' => SlotRecord::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
343 'comment' => $this->getRandomCommentStoreComment(),
344 'timestamp' => '20171117010101',
345 'user' => true,
346 ],
347 new InvalidArgumentException( 'Only the main slot is supported for now!' )
348 ];
349 yield 'no timestamp' => [
350 Title::newFromText( 'UTPage' ),
351 [
352 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
353 'comment' => $this->getRandomCommentStoreComment(),
354 'user' => true,
355 ],
356 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
357 ];
358 yield 'no comment' => [
359 Title::newFromText( 'UTPage' ),
360 [
361 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
362 'timestamp' => '20171117010101',
363 'user' => true,
364 ],
365 new IncompleteRevisionException( 'comment must not be NULL!' )
366 ];
367 yield 'no user' => [
368 Title::newFromText( 'UTPage' ),
369 [
370 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
371 'comment' => $this->getRandomCommentStoreComment(),
372 'timestamp' => '20171117010101',
373 ],
374 new IncompleteRevisionException( 'user must not be NULL!' )
375 ];
376 }
377
378 /**
379 * @dataProvider provideInsertRevisionOn_failures
380 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
381 */
382 public function testInsertRevisionOn_failures(
383 Title $title,
384 array $revDetails = [],
385 Exception $exception ) {
386 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
387
388 $store = MediaWikiServices::getInstance()->getRevisionStore();
389
390 $this->setExpectedException(
391 get_class( $exception ),
392 $exception->getMessage(),
393 $exception->getCode()
394 );
395 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
396 }
397
398 public function provideNewNullRevision() {
399 yield [
400 Title::newFromText( 'UTPage' ),
401 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
402 true,
403 ];
404 yield [
405 Title::newFromText( 'UTPage' ),
406 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
407 false,
408 ];
409 }
410
411 /**
412 * @dataProvider provideNewNullRevision
413 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
414 */
415 public function testNewNullRevision( Title $title, $comment, $minor ) {
416 $store = MediaWikiServices::getInstance()->getRevisionStore();
417 $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
418
419 $parent = $store->getRevisionByTitle( $title );
420 $record = $store->newNullRevision(
421 wfGetDB( DB_MASTER ),
422 $title,
423 $comment,
424 $minor,
425 $user
426 );
427
428 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
429 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
430 $this->assertEquals( $comment, $record->getComment() );
431 $this->assertEquals( $minor, $record->isMinor() );
432 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
433 $this->assertEquals( $parent->getId(), $record->getParentId() );
434
435 $parentSlot = $parent->getSlot( 'main' );
436 $slot = $record->getSlot( 'main' );
437
438 $this->assertTrue( $slot->isInherited(), 'isInherited' );
439 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
440 $this->assertSame( $parentSlot->getAddress(), $slot->getAddress(), 'getAddress' );
441 }
442
443 /**
444 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
445 */
446 public function testNewNullRevision_nonExistingTitle() {
447 $store = MediaWikiServices::getInstance()->getRevisionStore();
448 $record = $store->newNullRevision(
449 wfGetDB( DB_MASTER ),
450 Title::newFromText( __METHOD__ . '.iDontExist!' ),
451 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
452 false,
453 TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
454 );
455 $this->assertNull( $record );
456 }
457
458 /**
459 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
460 */
461 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
462 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
463 $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
464 /** @var Revision $rev */
465 $rev = $status->value['revision'];
466
467 $store = MediaWikiServices::getInstance()->getRevisionStore();
468 $revisionRecord = $store->getRevisionById( $rev->getId() );
469 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
470
471 $this->assertGreaterThan( 0, $result );
472 $this->assertSame(
473 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
474 $result
475 );
476 }
477
478 /**
479 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
480 */
481 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
482 // This assumes that sysops are auto patrolled
483 $sysop = $this->getTestSysop()->getUser();
484 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
485 $status = $page->doEditContent(
486 new WikitextContent( __METHOD__ ),
487 __METHOD__,
488 0,
489 false,
490 $sysop
491 );
492 /** @var Revision $rev */
493 $rev = $status->value['revision'];
494
495 $store = MediaWikiServices::getInstance()->getRevisionStore();
496 $revisionRecord = $store->getRevisionById( $rev->getId() );
497 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
498
499 $this->assertSame( 0, $result );
500 }
501
502 /**
503 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
504 */
505 public function testGetRecentChange() {
506 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
507 $content = new WikitextContent( __METHOD__ );
508 $status = $page->doEditContent( $content, __METHOD__ );
509 /** @var Revision $rev */
510 $rev = $status->value['revision'];
511
512 $store = MediaWikiServices::getInstance()->getRevisionStore();
513 $revRecord = $store->getRevisionById( $rev->getId() );
514 $recentChange = $store->getRecentChange( $revRecord );
515
516 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
517 $this->assertEquals( $rev->getRecentChange(), $recentChange );
518 }
519
520 /**
521 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
522 */
523 public function testGetRevisionById() {
524 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
525 $content = new WikitextContent( __METHOD__ );
526 $status = $page->doEditContent( $content, __METHOD__ );
527 /** @var Revision $rev */
528 $rev = $status->value['revision'];
529
530 $store = MediaWikiServices::getInstance()->getRevisionStore();
531 $revRecord = $store->getRevisionById( $rev->getId() );
532
533 $this->assertSame( $rev->getId(), $revRecord->getId() );
534 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
535 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
536 }
537
538 /**
539 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
540 */
541 public function testGetRevisionByTitle() {
542 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
543 $content = new WikitextContent( __METHOD__ );
544 $status = $page->doEditContent( $content, __METHOD__ );
545 /** @var Revision $rev */
546 $rev = $status->value['revision'];
547
548 $store = MediaWikiServices::getInstance()->getRevisionStore();
549 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
550
551 $this->assertSame( $rev->getId(), $revRecord->getId() );
552 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
553 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
554 }
555
556 /**
557 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
558 */
559 public function testGetRevisionByPageId() {
560 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
561 $content = new WikitextContent( __METHOD__ );
562 $status = $page->doEditContent( $content, __METHOD__ );
563 /** @var Revision $rev */
564 $rev = $status->value['revision'];
565
566 $store = MediaWikiServices::getInstance()->getRevisionStore();
567 $revRecord = $store->getRevisionByPageId( $page->getId() );
568
569 $this->assertSame( $rev->getId(), $revRecord->getId() );
570 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
571 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
572 }
573
574 /**
575 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
576 */
577 public function testGetRevisionByTimestamp() {
578 // Make sure there is 1 second between the last revision and the rev we create...
579 // Otherwise we might not get the correct revision and the test may fail...
580 // :(
581 sleep( 1 );
582 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
583 $content = new WikitextContent( __METHOD__ );
584 $status = $page->doEditContent( $content, __METHOD__ );
585 /** @var Revision $rev */
586 $rev = $status->value['revision'];
587
588 $store = MediaWikiServices::getInstance()->getRevisionStore();
589 $revRecord = $store->getRevisionByTimestamp(
590 $page->getTitle(),
591 $rev->getTimestamp()
592 );
593
594 $this->assertSame( $rev->getId(), $revRecord->getId() );
595 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
596 $this->assertSame( __METHOD__, $revRecord->getComment()->text );
597 }
598
599 private function revisionToRow( Revision $rev ) {
600 $page = WikiPage::factory( $rev->getTitle() );
601
602 return (object)[
603 'rev_id' => (string)$rev->getId(),
604 'rev_page' => (string)$rev->getPage(),
605 'rev_text_id' => (string)$rev->getTextId(),
606 'rev_timestamp' => (string)$rev->getTimestamp(),
607 'rev_user_text' => (string)$rev->getUserText(),
608 'rev_user' => (string)$rev->getUser(),
609 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
610 'rev_deleted' => (string)$rev->getVisibility(),
611 'rev_len' => (string)$rev->getSize(),
612 'rev_parent_id' => (string)$rev->getParentId(),
613 'rev_sha1' => (string)$rev->getSha1(),
614 'rev_comment_text' => $rev->getComment(),
615 'rev_comment_data' => null,
616 'rev_comment_cid' => null,
617 'rev_content_format' => $rev->getContentFormat(),
618 'rev_content_model' => $rev->getContentModel(),
619 'page_namespace' => (string)$page->getTitle()->getNamespace(),
620 'page_title' => $page->getTitle()->getDBkey(),
621 'page_id' => (string)$page->getId(),
622 'page_latest' => (string)$page->getLatest(),
623 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
624 'page_len' => (string)$page->getContent()->getSize(),
625 'user_name' => (string)$rev->getUserText(),
626 ];
627 }
628
629 private function assertRevisionRecordMatchesRevision(
630 Revision $rev,
631 RevisionRecord $record
632 ) {
633 $this->assertSame( $rev->getId(), $record->getId() );
634 $this->assertSame( $rev->getPage(), $record->getPageId() );
635 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
636 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
637 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
638 $this->assertSame( $rev->isMinor(), $record->isMinor() );
639 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
640 $this->assertSame( $rev->getSize(), $record->getSize() );
641 /**
642 * @note As of MW 1.31, the database schema allows the parent ID to be
643 * NULL to indicate that it is unknown.
644 */
645 $expectedParent = $rev->getParentId();
646 if ( $expectedParent === null ) {
647 $expectedParent = 0;
648 }
649 $this->assertSame( $expectedParent, $record->getParentId() );
650 $this->assertSame( $rev->getSha1(), $record->getSha1() );
651 $this->assertSame( $rev->getComment(), $record->getComment()->text );
652 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
653 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
654 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
655 }
656
657 /**
658 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
659 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
660 */
661 public function testNewRevisionFromRow_anonEdit() {
662 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH );
663 $this->overrideMwServices();
664
665 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
666 $text = __METHOD__ . 'a-ä';
667 /** @var Revision $rev */
668 $rev = $page->doEditContent(
669 new WikitextContent( $text ),
670 __METHOD__ . 'a'
671 )->value['revision'];
672
673 $store = MediaWikiServices::getInstance()->getRevisionStore();
674 $record = $store->newRevisionFromRow(
675 $this->revisionToRow( $rev ),
676 [],
677 $page->getTitle()
678 );
679 $this->assertRevisionRecordMatchesRevision( $rev, $record );
680 $this->assertSame( $text, $rev->getContent()->serialize() );
681 }
682
683 /**
684 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
685 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
686 */
687 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
688 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
689 $this->overrideMwServices();
690 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
691 $text = __METHOD__ . 'a-ä';
692 /** @var Revision $rev */
693 $rev = $page->doEditContent(
694 new WikitextContent( $text ),
695 __METHOD__. 'a'
696 )->value['revision'];
697
698 $store = MediaWikiServices::getInstance()->getRevisionStore();
699 $record = $store->newRevisionFromRow(
700 $this->revisionToRow( $rev ),
701 [],
702 $page->getTitle()
703 );
704 $this->assertRevisionRecordMatchesRevision( $rev, $record );
705 $this->assertSame( $text, $rev->getContent()->serialize() );
706 }
707
708 /**
709 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
710 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
711 */
712 public function testNewRevisionFromRow_userEdit() {
713 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH );
714 $this->overrideMwServices();
715
716 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
717 $text = __METHOD__ . 'b-ä';
718 /** @var Revision $rev */
719 $rev = $page->doEditContent(
720 new WikitextContent( $text ),
721 __METHOD__ . 'b',
722 0,
723 false,
724 $this->getTestUser()->getUser()
725 )->value['revision'];
726
727 $store = MediaWikiServices::getInstance()->getRevisionStore();
728 $record = $store->newRevisionFromRow(
729 $this->revisionToRow( $rev ),
730 [],
731 $page->getTitle()
732 );
733 $this->assertRevisionRecordMatchesRevision( $rev, $record );
734 $this->assertSame( $text, $rev->getContent()->serialize() );
735 }
736
737 /**
738 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
739 */
740 public function testNewRevisionFromArchiveRow() {
741 $store = MediaWikiServices::getInstance()->getRevisionStore();
742 $title = Title::newFromText( __METHOD__ );
743 $text = __METHOD__ . '-bä';
744 $page = WikiPage::factory( $title );
745 /** @var Revision $orig */
746 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
747 ->value['revision'];
748 $page->doDeleteArticle( __METHOD__ );
749
750 $db = wfGetDB( DB_MASTER );
751 $arQuery = $store->getArchiveQueryInfo();
752 $res = $db->select(
753 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
754 __METHOD__, [], $arQuery['joins']
755 );
756 $this->assertTrue( is_object( $res ), 'query failed' );
757
758 $row = $res->fetchObject();
759 $res->free();
760 $record = $store->newRevisionFromArchiveRow( $row );
761
762 $this->assertRevisionRecordMatchesRevision( $orig, $record );
763 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
764 }
765
766 /**
767 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
768 */
769 public function testNewRevisionFromArchiveRow_legacyEncoding() {
770 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
771 $this->overrideMwServices();
772 $store = MediaWikiServices::getInstance()->getRevisionStore();
773 $title = Title::newFromText( __METHOD__ );
774 $text = __METHOD__ . '-bä';
775 $page = WikiPage::factory( $title );
776 /** @var Revision $orig */
777 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ )
778 ->value['revision'];
779 $page->doDeleteArticle( __METHOD__ );
780
781 $db = wfGetDB( DB_MASTER );
782 $arQuery = $store->getArchiveQueryInfo();
783 $res = $db->select(
784 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
785 __METHOD__, [], $arQuery['joins']
786 );
787 $this->assertTrue( is_object( $res ), 'query failed' );
788
789 $row = $res->fetchObject();
790 $res->free();
791 $record = $store->newRevisionFromArchiveRow( $row );
792
793 $this->assertRevisionRecordMatchesRevision( $orig, $record );
794 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
795 }
796
797 /**
798 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
799 */
800 public function testLoadRevisionFromId() {
801 $title = Title::newFromText( __METHOD__ );
802 $page = WikiPage::factory( $title );
803 /** @var Revision $rev */
804 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
805 ->value['revision'];
806
807 $store = MediaWikiServices::getInstance()->getRevisionStore();
808 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
809 $this->assertRevisionRecordMatchesRevision( $rev, $result );
810 }
811
812 /**
813 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
814 */
815 public function testLoadRevisionFromPageId() {
816 $title = Title::newFromText( __METHOD__ );
817 $page = WikiPage::factory( $title );
818 /** @var Revision $rev */
819 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
820 ->value['revision'];
821
822 $store = MediaWikiServices::getInstance()->getRevisionStore();
823 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
824 $this->assertRevisionRecordMatchesRevision( $rev, $result );
825 }
826
827 /**
828 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
829 */
830 public function testLoadRevisionFromTitle() {
831 $title = Title::newFromText( __METHOD__ );
832 $page = WikiPage::factory( $title );
833 /** @var Revision $rev */
834 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
835 ->value['revision'];
836
837 $store = MediaWikiServices::getInstance()->getRevisionStore();
838 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
839 $this->assertRevisionRecordMatchesRevision( $rev, $result );
840 }
841
842 /**
843 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
844 */
845 public function testLoadRevisionFromTimestamp() {
846 $title = Title::newFromText( __METHOD__ );
847 $page = WikiPage::factory( $title );
848 /** @var Revision $revOne */
849 $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
850 ->value['revision'];
851 // Sleep to ensure different timestamps... )(evil)
852 sleep( 1 );
853 /** @var Revision $revTwo */
854 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
855 ->value['revision'];
856
857 $store = MediaWikiServices::getInstance()->getRevisionStore();
858 $this->assertNull(
859 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
860 );
861 $this->assertSame(
862 $revOne->getId(),
863 $store->loadRevisionFromTimestamp(
864 wfGetDB( DB_MASTER ),
865 $title,
866 $revOne->getTimestamp()
867 )->getId()
868 );
869 $this->assertSame(
870 $revTwo->getId(),
871 $store->loadRevisionFromTimestamp(
872 wfGetDB( DB_MASTER ),
873 $title,
874 $revTwo->getTimestamp()
875 )->getId()
876 );
877 }
878
879 /**
880 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
881 */
882 public function testGetParentLengths() {
883 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
884 /** @var Revision $revOne */
885 $revOne = $page->doEditContent(
886 new WikitextContent( __METHOD__ ), __METHOD__
887 )->value['revision'];
888 /** @var Revision $revTwo */
889 $revTwo = $page->doEditContent(
890 new WikitextContent( __METHOD__ . '2' ), __METHOD__
891 )->value['revision'];
892
893 $store = MediaWikiServices::getInstance()->getRevisionStore();
894 $this->assertSame(
895 [
896 $revOne->getId() => strlen( __METHOD__ ),
897 ],
898 $store->listRevisionSizes(
899 wfGetDB( DB_MASTER ),
900 [ $revOne->getId() ]
901 )
902 );
903 $this->assertSame(
904 [
905 $revOne->getId() => strlen( __METHOD__ ),
906 $revTwo->getId() => strlen( __METHOD__ ) + 1,
907 ],
908 $store->listRevisionSizes(
909 wfGetDB( DB_MASTER ),
910 [ $revOne->getId(), $revTwo->getId() ]
911 )
912 );
913 }
914
915 /**
916 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
917 */
918 public function testGetPreviousRevision() {
919 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
920 /** @var Revision $revOne */
921 $revOne = $page->doEditContent(
922 new WikitextContent( __METHOD__ ), __METHOD__
923 )->value['revision'];
924 /** @var Revision $revTwo */
925 $revTwo = $page->doEditContent(
926 new WikitextContent( __METHOD__ . '2' ), __METHOD__
927 )->value['revision'];
928
929 $store = MediaWikiServices::getInstance()->getRevisionStore();
930 $this->assertNull(
931 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
932 );
933 $this->assertSame(
934 $revOne->getId(),
935 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
936 );
937 }
938
939 /**
940 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
941 */
942 public function testGetNextRevision() {
943 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
944 /** @var Revision $revOne */
945 $revOne = $page->doEditContent(
946 new WikitextContent( __METHOD__ ), __METHOD__
947 )->value['revision'];
948 /** @var Revision $revTwo */
949 $revTwo = $page->doEditContent(
950 new WikitextContent( __METHOD__ . '2' ), __METHOD__
951 )->value['revision'];
952
953 $store = MediaWikiServices::getInstance()->getRevisionStore();
954 $this->assertSame(
955 $revTwo->getId(),
956 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
957 );
958 $this->assertNull(
959 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
960 );
961 }
962
963 /**
964 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
965 */
966 public function testGetTimestampFromId_found() {
967 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
968 /** @var Revision $rev */
969 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
970 ->value['revision'];
971
972 $store = MediaWikiServices::getInstance()->getRevisionStore();
973 $result = $store->getTimestampFromId(
974 $page->getTitle(),
975 $rev->getId()
976 );
977
978 $this->assertSame( $rev->getTimestamp(), $result );
979 }
980
981 /**
982 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
983 */
984 public function testGetTimestampFromId_notFound() {
985 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
986 /** @var Revision $rev */
987 $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
988 ->value['revision'];
989
990 $store = MediaWikiServices::getInstance()->getRevisionStore();
991 $result = $store->getTimestampFromId(
992 $page->getTitle(),
993 $rev->getId() + 1
994 );
995
996 $this->assertFalse( $result );
997 }
998
999 /**
1000 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1001 */
1002 public function testCountRevisionsByPageId() {
1003 $store = MediaWikiServices::getInstance()->getRevisionStore();
1004 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1005
1006 $this->assertSame(
1007 0,
1008 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1009 );
1010 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1011 $this->assertSame(
1012 1,
1013 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1014 );
1015 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1016 $this->assertSame(
1017 2,
1018 $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
1019 );
1020 }
1021
1022 /**
1023 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1024 */
1025 public function testCountRevisionsByTitle() {
1026 $store = MediaWikiServices::getInstance()->getRevisionStore();
1027 $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
1028
1029 $this->assertSame(
1030 0,
1031 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1032 );
1033 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1034 $this->assertSame(
1035 1,
1036 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1037 );
1038 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1039 $this->assertSame(
1040 2,
1041 $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
1042 );
1043 }
1044
1045 /**
1046 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1047 */
1048 public function testUserWasLastToEdit_false() {
1049 $sysop = $this->getTestSysop()->getUser();
1050 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
1051 $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1052
1053 $store = MediaWikiServices::getInstance()->getRevisionStore();
1054 $result = $store->userWasLastToEdit(
1055 wfGetDB( DB_MASTER ),
1056 $page->getId(),
1057 $sysop->getId(),
1058 '20160101010101'
1059 );
1060 $this->assertFalse( $result );
1061 }
1062
1063 /**
1064 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1065 */
1066 public function testUserWasLastToEdit_true() {
1067 $startTime = wfTimestampNow();
1068 $sysop = $this->getTestSysop()->getUser();
1069 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
1070 $page->doEditContent(
1071 new WikitextContent( __METHOD__ ),
1072 __METHOD__,
1073 0,
1074 false,
1075 $sysop
1076 );
1077
1078 $store = MediaWikiServices::getInstance()->getRevisionStore();
1079 $result = $store->userWasLastToEdit(
1080 wfGetDB( DB_MASTER ),
1081 $page->getId(),
1082 $sysop->getId(),
1083 $startTime
1084 );
1085 $this->assertTrue( $result );
1086 }
1087
1088 /**
1089 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1090 */
1091 public function testGetKnownCurrentRevision() {
1092 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
1093 /** @var Revision $rev */
1094 $rev = $page->doEditContent(
1095 new WikitextContent( __METHOD__ . 'b' ),
1096 __METHOD__ . 'b',
1097 0,
1098 false,
1099 $this->getTestUser()->getUser()
1100 )->value['revision'];
1101
1102 $store = MediaWikiServices::getInstance()->getRevisionStore();
1103 $record = $store->getKnownCurrentRevision(
1104 $page->getTitle(),
1105 $rev->getId()
1106 );
1107
1108 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1109 }
1110
1111 public function provideNewMutableRevisionFromArray() {
1112 yield 'Basic array, with page & id' => [
1113 [
1114 'id' => 2,
1115 'page' => 1,
1116 'text_id' => 2,
1117 'timestamp' => '20171017114835',
1118 'user_text' => '111.0.1.2',
1119 'user' => 0,
1120 'minor_edit' => false,
1121 'deleted' => 0,
1122 'len' => 46,
1123 'parent_id' => 1,
1124 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1125 'comment' => 'Goat Comment!',
1126 'content_format' => 'text/x-wiki',
1127 'content_model' => 'wikitext',
1128 ]
1129 ];
1130 yield 'Basic array, content object' => [
1131 [
1132 'id' => 2,
1133 'page' => 1,
1134 'timestamp' => '20171017114835',
1135 'user_text' => '111.0.1.2',
1136 'user' => 0,
1137 'minor_edit' => false,
1138 'deleted' => 0,
1139 'len' => 46,
1140 'parent_id' => 1,
1141 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1142 'comment' => 'Goat Comment!',
1143 'content' => new WikitextContent( 'Some Content' ),
1144 ]
1145 ];
1146 yield 'Basic array, serialized text' => [
1147 [
1148 'id' => 2,
1149 'page' => 1,
1150 'timestamp' => '20171017114835',
1151 'user_text' => '111.0.1.2',
1152 'user' => 0,
1153 'minor_edit' => false,
1154 'deleted' => 0,
1155 'len' => 46,
1156 'parent_id' => 1,
1157 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1158 'comment' => 'Goat Comment!',
1159 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1160 ]
1161 ];
1162 yield 'Basic array, serialized text, utf-8 flags' => [
1163 [
1164 'id' => 2,
1165 'page' => 1,
1166 'timestamp' => '20171017114835',
1167 'user_text' => '111.0.1.2',
1168 'user' => 0,
1169 'minor_edit' => false,
1170 'deleted' => 0,
1171 'len' => 46,
1172 'parent_id' => 1,
1173 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1174 'comment' => 'Goat Comment!',
1175 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1176 'flags' => 'utf-8',
1177 ]
1178 ];
1179 yield 'Basic array, with title' => [
1180 [
1181 'title' => Title::newFromText( 'SomeText' ),
1182 'text_id' => 2,
1183 'timestamp' => '20171017114835',
1184 'user_text' => '111.0.1.2',
1185 'user' => 0,
1186 'minor_edit' => false,
1187 'deleted' => 0,
1188 'len' => 46,
1189 'parent_id' => 1,
1190 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1191 'comment' => 'Goat Comment!',
1192 'content_format' => 'text/x-wiki',
1193 'content_model' => 'wikitext',
1194 ]
1195 ];
1196 yield 'Basic array, no user field' => [
1197 [
1198 'id' => 2,
1199 'page' => 1,
1200 'text_id' => 2,
1201 'timestamp' => '20171017114835',
1202 'user_text' => '111.0.1.3',
1203 'minor_edit' => false,
1204 'deleted' => 0,
1205 'len' => 46,
1206 'parent_id' => 1,
1207 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1208 'comment' => 'Goat Comment!',
1209 'content_format' => 'text/x-wiki',
1210 'content_model' => 'wikitext',
1211 ]
1212 ];
1213 }
1214
1215 /**
1216 * @dataProvider provideNewMutableRevisionFromArray
1217 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1218 */
1219 public function testNewMutableRevisionFromArray( array $array ) {
1220 $store = MediaWikiServices::getInstance()->getRevisionStore();
1221
1222 $result = $store->newMutableRevisionFromArray( $array );
1223
1224 if ( isset( $array['id'] ) ) {
1225 $this->assertSame( $array['id'], $result->getId() );
1226 }
1227 if ( isset( $array['page'] ) ) {
1228 $this->assertSame( $array['page'], $result->getPageId() );
1229 }
1230 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1231 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1232 if ( isset( $array['user'] ) ) {
1233 $this->assertSame( $array['user'], $result->getUser()->getId() );
1234 }
1235 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1236 $this->assertSame( $array['deleted'], $result->getVisibility() );
1237 $this->assertSame( $array['len'], $result->getSize() );
1238 $this->assertSame( $array['parent_id'], $result->getParentId() );
1239 $this->assertSame( $array['sha1'], $result->getSha1() );
1240 $this->assertSame( $array['comment'], $result->getComment()->text );
1241 if ( isset( $array['content'] ) ) {
1242 $this->assertTrue(
1243 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1244 );
1245 } elseif ( isset( $array['text'] ) ) {
1246 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1247 } else {
1248 $this->assertSame(
1249 $array['content_format'],
1250 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1251 );
1252 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1253 }
1254 }
1255
1256 /**
1257 * @dataProvider provideNewMutableRevisionFromArray
1258 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1259 */
1260 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1261 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1262 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1263 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
1264
1265 $factory = $this->getMockBuilder( BlobStoreFactory::class )
1266 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1267 ->disableOriginalConstructor()
1268 ->getMock();
1269 $factory->expects( $this->any() )
1270 ->method( 'newBlobStore' )
1271 ->willReturn( $blobStore );
1272 $factory->expects( $this->any() )
1273 ->method( 'newSqlBlobStore' )
1274 ->willReturn( $blobStore );
1275
1276 $this->setService( 'BlobStoreFactory', $factory );
1277
1278 $this->testNewMutableRevisionFromArray( $array );
1279 }
1280
1281 }