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