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