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