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