4 * Test class for Revision storage.
6 * @group ContentHandler
8 * ^--- important, causes temporary tables to be used instead of the real database
11 * ^--- important, causes tests not to fail with timeout
13 class RevisionStorageTest
extends MediaWikiTestCase
{
15 * @var WikiPage $the_page
19 public function __construct( $name = null, array $data = [], $dataName = '' ) {
20 parent
::__construct( $name, $data, $dataName );
22 $this->tablesUsed
= array_merge( $this->tablesUsed
,
41 protected function setUp() {
46 $this->mergeMwGlobalArrayValue(
50 12313 => 'Dummy_talk',
54 $this->mergeMwGlobalArrayValue(
55 'wgNamespaceContentModels',
61 $this->mergeMwGlobalArrayValue(
64 'DUMMY' => 'DummyContentHandlerForTesting',
68 MWNamespace
::clearCaches();
69 // Reset namespace cache
70 $wgContLang->resetNamespaces();
71 if ( !$this->the_page
) {
72 $this->the_page
= $this->createPage(
73 'RevisionStorageTest_the_page',
75 CONTENT_MODEL_WIKITEXT
79 $this->tablesUsed
[] = 'archive';
82 protected function tearDown() {
87 MWNamespace
::clearCaches();
88 // Reset namespace cache
89 $wgContLang->resetNamespaces();
92 protected function makeRevision( $props = null ) {
93 if ( $props === null ) {
97 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
98 $props['text'] = 'Lorem Ipsum';
101 if ( !isset( $props['comment'] ) ) {
102 $props['comment'] = 'just a test';
105 if ( !isset( $props['page'] ) ) {
106 $props['page'] = $this->the_page
->getId();
109 $rev = new Revision( $props );
111 $dbw = wfGetDB( DB_MASTER
);
112 $rev->insertOn( $dbw );
118 * @param string $titleString
119 * @param string $text
120 * @param string|null $model
124 protected function createPage( $titleString, $text, $model = null ) {
125 if ( !preg_match( '/:/', $titleString ) &&
126 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
128 $ns = $this->getDefaultWikitextNS();
129 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
132 $title = Title
::newFromText( $titleString );
133 $wikipage = new WikiPage( $title );
135 // Delete the article if it already exists
136 if ( $wikipage->exists() ) {
137 $wikipage->doDeleteArticle( "done" );
140 $content = ContentHandler
::makeContent( $text, $title, $model );
141 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
146 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
147 $this->assertNotNull( $rev, 'missing revision' );
149 $this->assertEquals( $orig->getId(), $rev->getId() );
150 $this->assertEquals( $orig->getPage(), $rev->getPage() );
151 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
152 $this->assertEquals( $orig->getUser(), $rev->getUser() );
153 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
154 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
155 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
159 * @covers Revision::__construct
161 public function testConstructFromRow() {
162 $orig = $this->makeRevision();
164 $dbr = wfGetDB( DB_REPLICA
);
165 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
166 $this->assertTrue( is_object( $res ), 'query failed' );
168 $row = $res->fetchObject();
171 $rev = new Revision( $row );
173 $this->assertRevEquals( $orig, $rev );
177 * @covers Revision::newFromTitle
179 public function testNewFromTitle_withoutId() {
180 $page = $this->createPage(
183 CONTENT_MODEL_WIKITEXT
185 $latestRevId = $page->getLatest();
187 $rev = Revision
::newFromTitle( $page->getTitle() );
189 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
190 $this->assertEquals( $latestRevId, $rev->getId() );
194 * @covers Revision::newFromTitle
196 public function testNewFromTitle_withId() {
197 $page = $this->createPage(
200 CONTENT_MODEL_WIKITEXT
202 $latestRevId = $page->getLatest();
204 $rev = Revision
::newFromTitle( $page->getTitle(), $latestRevId );
206 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
207 $this->assertEquals( $latestRevId, $rev->getId() );
211 * @covers Revision::newFromTitle
213 public function testNewFromTitle_withBadId() {
214 $page = $this->createPage(
217 CONTENT_MODEL_WIKITEXT
219 $latestRevId = $page->getLatest();
221 $rev = Revision
::newFromTitle( $page->getTitle(), $latestRevId +
1 );
223 $this->assertNull( $rev );
227 * @covers Revision::newFromRow
229 public function testNewFromRow() {
230 $orig = $this->makeRevision();
232 $dbr = wfGetDB( DB_REPLICA
);
233 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
234 $this->assertTrue( is_object( $res ), 'query failed' );
236 $row = $res->fetchObject();
239 $rev = Revision
::newFromRow( $row );
241 $this->assertRevEquals( $orig, $rev );
245 * @covers Revision::newFromArchiveRow
247 public function testNewFromArchiveRow() {
248 $page = $this->createPage(
249 'RevisionStorageTest_testNewFromArchiveRow',
251 CONTENT_MODEL_WIKITEXT
253 $orig = $page->getRevision();
254 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
256 $dbr = wfGetDB( DB_REPLICA
);
258 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
260 $this->assertTrue( is_object( $res ), 'query failed' );
262 $row = $res->fetchObject();
265 $rev = Revision
::newFromArchiveRow( $row );
267 $this->assertRevEquals( $orig, $rev );
271 * @covers Revision::newFromId
273 public function testNewFromId() {
274 $orig = $this->makeRevision();
276 $rev = Revision
::newFromId( $orig->getId() );
278 $this->assertRevEquals( $orig, $rev );
282 * @covers Revision::fetchRevision
284 public function testFetchRevision() {
285 $page = $this->createPage(
286 'RevisionStorageTest_testFetchRevision',
288 CONTENT_MODEL_WIKITEXT
291 // Hidden process cache assertion below
292 $page->getRevision()->getId();
294 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
295 $id = $page->getRevision()->getId();
297 $res = Revision
::fetchRevision( $page->getTitle() );
299 # note: order is unspecified
301 while ( ( $row = $res->fetchObject() ) ) {
302 $rows[$row->rev_id
] = $row;
305 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
306 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
310 * @covers Revision::selectFields
312 public function testSelectFields() {
313 global $wgContentHandlerUseDB;
315 $fields = Revision
::selectFields();
317 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
318 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
320 in_array( 'rev_timestamp', $fields ),
321 'missing rev_timestamp in list of fields'
323 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
325 if ( $wgContentHandlerUseDB ) {
326 $this->assertTrue( in_array( 'rev_content_model', $fields ),
327 'missing rev_content_model in list of fields' );
328 $this->assertTrue( in_array( 'rev_content_format', $fields ),
329 'missing rev_content_format in list of fields' );
334 * @covers Revision::getPage
336 public function testGetPage() {
337 $page = $this->the_page
;
339 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
340 $rev = Revision
::newFromId( $orig->getId() );
342 $this->assertEquals( $page->getId(), $rev->getPage() );
346 * @covers Revision::getContent
348 public function testGetContent_failure() {
349 $rev = new Revision( [
350 'page' => $this->the_page
->getId(),
351 'content_model' => $this->the_page
->getContentModel(),
352 'text_id' => 123456789, // not in the test DB
355 $this->assertNull( $rev->getContent(),
356 "getContent() should return null if the revision's text blob could not be loaded." );
358 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
359 $this->assertNull( $rev->getContent(),
360 "getContent() should return null if the revision's text blob could not be loaded." );
364 * @covers Revision::getContent
366 public function testGetContent() {
367 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
368 $rev = Revision
::newFromId( $orig->getId() );
370 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
374 * @covers Revision::getContentModel
376 public function testGetContentModel() {
377 global $wgContentHandlerUseDB;
379 if ( !$wgContentHandlerUseDB ) {
380 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
383 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
384 'content_model' => CONTENT_MODEL_JAVASCRIPT
] );
385 $rev = Revision
::newFromId( $orig->getId() );
387 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
391 * @covers Revision::getContentFormat
393 public function testGetContentFormat() {
394 global $wgContentHandlerUseDB;
396 if ( !$wgContentHandlerUseDB ) {
397 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
400 $orig = $this->makeRevision( [
401 'text' => 'hello hello.',
402 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
403 'content_format' => CONTENT_FORMAT_JAVASCRIPT
405 $rev = Revision
::newFromId( $orig->getId() );
407 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
411 * @covers Revision::isCurrent
413 public function testIsCurrent() {
414 $page = $this->createPage(
415 'RevisionStorageTest_testIsCurrent',
417 CONTENT_MODEL_WIKITEXT
419 $rev1 = $page->getRevision();
421 # @todo find out if this should be true
422 # $this->assertTrue( $rev1->isCurrent() );
424 $rev1x = Revision
::newFromId( $rev1->getId() );
425 $this->assertTrue( $rev1x->isCurrent() );
427 $page->doEditContent(
428 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
431 $rev2 = $page->getRevision();
433 # @todo find out if this should be true
434 # $this->assertTrue( $rev2->isCurrent() );
436 $rev1x = Revision
::newFromId( $rev1->getId() );
437 $this->assertFalse( $rev1x->isCurrent() );
439 $rev2x = Revision
::newFromId( $rev2->getId() );
440 $this->assertTrue( $rev2x->isCurrent() );
444 * @covers Revision::getPrevious
446 public function testGetPrevious() {
447 $page = $this->createPage(
448 'RevisionStorageTest_testGetPrevious',
449 'Lorem Ipsum testGetPrevious',
450 CONTENT_MODEL_WIKITEXT
452 $rev1 = $page->getRevision();
454 $this->assertNull( $rev1->getPrevious() );
456 $page->doEditContent(
457 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
458 'second rev testGetPrevious' );
459 $rev2 = $page->getRevision();
461 $this->assertNotNull( $rev2->getPrevious() );
462 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
466 * @covers Revision::getNext
468 public function testGetNext() {
469 $page = $this->createPage(
470 'RevisionStorageTest_testGetNext',
471 'Lorem Ipsum testGetNext',
472 CONTENT_MODEL_WIKITEXT
474 $rev1 = $page->getRevision();
476 $this->assertNull( $rev1->getNext() );
478 $page->doEditContent(
479 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
480 'second rev testGetNext'
482 $rev2 = $page->getRevision();
484 $this->assertNotNull( $rev1->getNext() );
485 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
489 * @covers Revision::newNullRevision
491 public function testNewNullRevision() {
492 $page = $this->createPage(
493 'RevisionStorageTest_testNewNullRevision',
495 CONTENT_MODEL_WIKITEXT
497 $orig = $page->getRevision();
499 $dbw = wfGetDB( DB_MASTER
);
500 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
502 $this->assertNotEquals( $orig->getId(), $rev->getId(),
503 'new null revision shold have a different id from the original revision' );
504 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
505 'new null revision shold have the same text id as the original revision' );
506 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
510 * @covers Revision::insertOn
512 public function testInsertOn() {
513 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
515 $orig = $this->makeRevision( [
519 // Make sure the revision was copied to ip_changes
520 $dbr = wfGetDB( DB_REPLICA
);
521 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
522 $row = $res->fetchObject();
524 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
525 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
528 public static function provideUserWasLastToEdit() {
529 yield
'actually the last edit' => [ 3, true ];
530 yield
'not the current edit, but still by this user' => [ 2, true ];
531 yield
'edit by another user' => [ 1, false ];
532 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
536 * @dataProvider provideUserWasLastToEdit
538 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
539 $userA = User
::newFromName( "RevisionStorageTest_userA" );
540 $userB = User
::newFromName( "RevisionStorageTest_userB" );
542 if ( $userA->getId() === 0 ) {
543 $userA = User
::createNew( $userA->getName() );
546 if ( $userB->getId() === 0 ) {
547 $userB = User
::createNew( $userB->getName() );
550 $ns = $this->getDefaultWikitextNS();
552 $dbw = wfGetDB( DB_MASTER
);
555 // create revisions -----------------------------
556 $page = WikiPage
::factory( Title
::newFromText(
557 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
558 $page->insertOn( $dbw );
560 $revisions[0] = new Revision( [
561 'page' => $page->getId(),
562 // we need the title to determine the page's default content model
563 'title' => $page->getTitle(),
564 'timestamp' => '20120101000000',
565 'user' => $userA->getId(),
567 'content_model' => CONTENT_MODEL_WIKITEXT
,
568 'summary' => 'edit zero'
570 $revisions[0]->insertOn( $dbw );
572 $revisions[1] = new Revision( [
573 'page' => $page->getId(),
574 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
575 'title' => $page->getTitle(),
576 'timestamp' => '20120101000100',
577 'user' => $userA->getId(),
579 'content_model' => CONTENT_MODEL_WIKITEXT
,
580 'summary' => 'edit one'
582 $revisions[1]->insertOn( $dbw );
584 $revisions[2] = new Revision( [
585 'page' => $page->getId(),
586 'title' => $page->getTitle(),
587 'timestamp' => '20120101000200',
588 'user' => $userB->getId(),
590 'content_model' => CONTENT_MODEL_WIKITEXT
,
591 'summary' => 'edit two'
593 $revisions[2]->insertOn( $dbw );
595 $revisions[3] = new Revision( [
596 'page' => $page->getId(),
597 'title' => $page->getTitle(),
598 'timestamp' => '20120101000300',
599 'user' => $userA->getId(),
601 'content_model' => CONTENT_MODEL_WIKITEXT
,
602 'summary' => 'edit three'
604 $revisions[3]->insertOn( $dbw );
606 $revisions[4] = new Revision( [
607 'page' => $page->getId(),
608 'title' => $page->getTitle(),
609 'timestamp' => '20120101000200',
610 'user' => $userA->getId(),
612 'content_model' => CONTENT_MODEL_WIKITEXT
,
613 'summary' => 'edit four'
615 $revisions[4]->insertOn( $dbw );
617 // test it ---------------------------------
618 $since = $revisions[$sinceIdx]->getTimestamp();
620 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
622 $this->assertEquals( $expectedLast, $wasLast );