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