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