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
{
16 * @var WikiPage $the_page
20 function __construct( $name = null, array $data = array(), $dataName = '' ) {
21 parent
::__construct( $name, $data, $dataName );
23 $this->tablesUsed
= array_merge( $this->tablesUsed
,
41 public function setUp() {
42 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
46 $wgExtraNamespaces[ 12312 ] = 'Dummy';
47 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
49 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
50 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
52 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
53 $wgContLang->resetNamespaces(); # reset namespace cache
54 if ( !$this->the_page
) {
55 $this->the_page
= $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page", CONTENT_MODEL_WIKITEXT
);
59 public function tearDown() {
60 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
64 unset( $wgExtraNamespaces[ 12312 ] );
65 unset( $wgExtraNamespaces[ 12313 ] );
67 unset( $wgNamespaceContentModels[ 12312 ] );
68 unset( $wgContentHandlers[ 'DUMMY' ] );
70 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
71 $wgContLang->resetNamespaces(); # reset namespace cache
74 protected function makeRevision( $props = null ) {
75 if ( $props === null ) $props = array();
77 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) $props['text'] = 'Lorem Ipsum';
78 if ( !isset( $props['comment'] ) ) $props['comment'] = 'just a test';
79 if ( !isset( $props['page'] ) ) $props['page'] = $this->the_page
->getId();
81 $rev = new Revision( $props );
83 $dbw = wfgetDB( DB_MASTER
);
84 $rev->insertOn( $dbw );
89 protected function createPage( $page, $text, $model = null ) {
90 if ( is_string( $page ) ) {
91 if ( !preg_match( '/:/', $page ) &&
92 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
) ) {
94 $ns = $this->getDefaultWikitextNS();
95 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
98 $page = Title
::newFromText( $page );
101 if ( $page instanceof Title
) {
102 $page = new WikiPage( $page );
105 if ( $page->exists() ) {
106 $page->doDeleteArticle( "done" );
109 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
110 $page->doEditContent( $content, "testing", EDIT_NEW
);
115 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
116 $this->assertNotNull( $rev, 'missing revision' );
118 $this->assertEquals( $orig->getId(), $rev->getId() );
119 $this->assertEquals( $orig->getPage(), $rev->getPage() );
120 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
121 $this->assertEquals( $orig->getUser(), $rev->getUser() );
122 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
123 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
124 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
128 * @covers Revision::__construct
130 public function testConstructFromRow()
132 $orig = $this->makeRevision();
134 $dbr = wfgetDB( DB_SLAVE
);
135 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
136 $this->assertTrue( is_object( $res ), 'query failed' );
138 $row = $res->fetchObject();
141 $rev = new Revision( $row );
143 $this->assertRevEquals( $orig, $rev );
147 * @covers Revision::newFromRow
149 public function testNewFromRow()
151 $orig = $this->makeRevision();
153 $dbr = wfgetDB( DB_SLAVE
);
154 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
155 $this->assertTrue( is_object( $res ), 'query failed' );
157 $row = $res->fetchObject();
160 $rev = Revision
::newFromRow( $row );
162 $this->assertRevEquals( $orig, $rev );
167 * @covers Revision::newFromArchiveRow
169 public function testNewFromArchiveRow()
171 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT
);
172 $orig = $page->getRevision();
173 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
175 $dbr = wfgetDB( DB_SLAVE
);
176 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
177 $this->assertTrue( is_object( $res ), 'query failed' );
179 $row = $res->fetchObject();
182 $rev = Revision
::newFromArchiveRow( $row );
184 $this->assertRevEquals( $orig, $rev );
188 * @covers Revision::newFromId
190 public function testNewFromId()
192 $orig = $this->makeRevision();
194 $rev = Revision
::newFromId( $orig->getId() );
196 $this->assertRevEquals( $orig, $rev );
200 * @covers Revision::fetchRevision
202 public function testFetchRevision()
204 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one', CONTENT_MODEL_WIKITEXT
);
205 $id1 = $page->getRevision()->getId();
207 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
208 $id2 = $page->getRevision()->getId();
210 $res = Revision
::fetchRevision( $page->getTitle() );
212 #note: order is unspecified
214 while ( ( $row = $res->fetchObject() ) ) {
215 $rows[ $row->rev_id
]= $row;
218 $row = $res->fetchObject();
219 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
220 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
224 * @covers Revision::selectFields
226 public function testSelectFields()
228 global $wgContentHandlerUseDB;
230 $fields = Revision
::selectFields();
232 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
233 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
234 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
235 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
237 if ( $wgContentHandlerUseDB ) {
238 $this->assertTrue( in_array( 'rev_content_model', $fields ),
239 'missing rev_content_model in list of fields');
240 $this->assertTrue( in_array( 'rev_content_format', $fields ),
241 'missing rev_content_format in list of fields');
246 * @covers Revision::getPage
248 public function testGetPage()
250 $page = $this->the_page
;
252 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
253 $rev = Revision
::newFromId( $orig->getId() );
255 $this->assertEquals( $page->getId(), $rev->getPage() );
259 * @covers Revision::getText
261 public function testGetText()
263 $this->hideDeprecated( 'Revision::getText' );
265 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
266 $rev = Revision
::newFromId( $orig->getId() );
268 $this->assertEquals( 'hello hello.', $rev->getText() );
272 * @covers Revision::getContent
274 public function testGetContent_failure()
276 $rev = new Revision( array(
277 'page' => $this->the_page
->getId(),
278 'content_model' => $this->the_page
->getContentModel(),
279 'text_id' => 123456789, // not in the test DB
282 $this->assertNull( $rev->getContent(),
283 "getContent() should return null if the revision's text blob could not be loaded." );
285 //NOTE: check this twice, once for lazy initialization, and once with the cached value.
286 $this->assertNull( $rev->getContent(),
287 "getContent() should return null if the revision's text blob could not be loaded." );
291 * @covers Revision::getContent
293 public function testGetContent()
295 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
296 $rev = Revision
::newFromId( $orig->getId() );
298 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
302 * @covers Revision::revText
304 public function testRevText()
306 $this->hideDeprecated( 'Revision::revText' );
307 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
308 $rev = Revision
::newFromId( $orig->getId() );
310 $this->assertEquals( 'hello hello rev.', $rev->revText() );
314 * @covers Revision::getRawText
316 public function testGetRawText()
318 $this->hideDeprecated( 'Revision::getRawText' );
320 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
321 $rev = Revision
::newFromId( $orig->getId() );
323 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
327 * @covers Revision::getContentModel
329 public function testGetContentModel()
331 global $wgContentHandlerUseDB;
333 if ( !$wgContentHandlerUseDB ) {
334 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
337 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
338 'content_model' => CONTENT_MODEL_JAVASCRIPT
) );
339 $rev = Revision
::newFromId( $orig->getId() );
341 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
345 * @covers Revision::getContentFormat
347 public function testGetContentFormat()
349 global $wgContentHandlerUseDB;
351 if ( !$wgContentHandlerUseDB ) {
352 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
355 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
356 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
357 'content_format' => CONTENT_FORMAT_JAVASCRIPT
) );
358 $rev = Revision
::newFromId( $orig->getId() );
360 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
364 * @covers Revision::isCurrent
366 public function testIsCurrent()
368 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT
);
369 $rev1 = $page->getRevision();
371 # @todo: find out if this should be true
372 # $this->assertTrue( $rev1->isCurrent() );
374 $rev1x = Revision
::newFromId( $rev1->getId() );
375 $this->assertTrue( $rev1x->isCurrent() );
377 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
), 'second rev' );
378 $rev2 = $page->getRevision();
380 # @todo: find out if this should be true
381 # $this->assertTrue( $rev2->isCurrent() );
383 $rev1x = Revision
::newFromId( $rev1->getId() );
384 $this->assertFalse( $rev1x->isCurrent() );
386 $rev2x = Revision
::newFromId( $rev2->getId() );
387 $this->assertTrue( $rev2x->isCurrent() );
391 * @covers Revision::getPrevious
393 public function testGetPrevious()
395 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious', CONTENT_MODEL_WIKITEXT
);
396 $rev1 = $page->getRevision();
398 $this->assertNull( $rev1->getPrevious() );
400 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
401 'second rev testGetPrevious' );
402 $rev2 = $page->getRevision();
404 $this->assertNotNull( $rev2->getPrevious() );
405 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
409 * @covers Revision::getNext
411 public function testGetNext()
413 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext', CONTENT_MODEL_WIKITEXT
);
414 $rev1 = $page->getRevision();
416 $this->assertNull( $rev1->getNext() );
418 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
419 'second rev testGetNext' );
420 $rev2 = $page->getRevision();
422 $this->assertNotNull( $rev1->getNext() );
423 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
427 * @covers Revision::newNullRevision
429 public function testNewNullRevision()
431 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text', CONTENT_MODEL_WIKITEXT
);
432 $orig = $page->getRevision();
434 $dbw = wfGetDB( DB_MASTER
);
435 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
437 $this->assertNotEquals( $orig->getId(), $rev->getId(),
438 'new null revision shold have a different id from the original revision' );
439 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
440 'new null revision shold have the same text id as the original revision' );
441 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
444 public static function provideUserWasLastToEdit() {
447 3, true, # actually the last edit
450 2, true, # not the current edit, but still by this user
453 1, false, # edit by another user
456 0, false, # first edit, by this user, but another user edited in the mean time
462 * @dataProvider provideUserWasLastToEdit
464 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
465 $userA = \User
::newFromName( "RevisionStorageTest_userA" );
466 $userB = \User
::newFromName( "RevisionStorageTest_userB" );
468 if ( $userA->getId() === 0 ) {
469 $userA = \User
::createNew( $userA->getName() );
472 if ( $userB->getId() === 0 ) {
473 $userB = \User
::createNew( $userB->getName() );
476 $ns = $this->getDefaultWikitextNS();
478 $dbw = wfGetDB( DB_MASTER
);
479 $revisions = array();
481 // create revisions -----------------------------
482 $page = WikiPage
::factory( Title
::newFromText(
483 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
486 $revisions[0] = new Revision( array(
487 'page' => $page->getId(),
488 'title' => $page->getTitle(), // we need the title to determine the page's default content model
489 'timestamp' => '20120101000000',
490 'user' => $userA->getId(),
492 'content_model' => CONTENT_MODEL_WIKITEXT
,
493 'summary' => 'edit zero'
495 $revisions[0]->insertOn( $dbw );
498 $revisions[1] = new Revision( array(
499 'page' => $page->getId(),
500 'title' => $page->getTitle(), // still need the title, because $page->getId() is 0 (there's no entry in the page table)
501 'timestamp' => '20120101000100',
502 'user' => $userA->getId(),
504 'content_model' => CONTENT_MODEL_WIKITEXT
,
505 'summary' => 'edit one'
507 $revisions[1]->insertOn( $dbw );
510 $revisions[2] = new Revision( array(
511 'page' => $page->getId(),
512 'title' => $page->getTitle(),
513 'timestamp' => '20120101000200',
514 'user' => $userB->getId(),
516 'content_model' => CONTENT_MODEL_WIKITEXT
,
517 'summary' => 'edit two'
519 $revisions[2]->insertOn( $dbw );
522 $revisions[3] = new Revision( array(
523 'page' => $page->getId(),
524 'title' => $page->getTitle(),
525 'timestamp' => '20120101000300',
526 'user' => $userA->getId(),
528 'content_model' => CONTENT_MODEL_WIKITEXT
,
529 'summary' => 'edit three'
531 $revisions[3]->insertOn( $dbw );
534 $revisions[4] = new Revision( array(
535 'page' => $page->getId(),
536 'title' => $page->getTitle(),
537 'timestamp' => '20120101000200',
538 'user' => $userA->getId(),
540 'content_model' => CONTENT_MODEL_WIKITEXT
,
541 'summary' => 'edit four'
543 $revisions[4]->insertOn( $dbw );
545 // test it ---------------------------------
546 $since = $revisions[ $sinceIdx ]->getTimestamp();
548 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
550 $this->assertEquals( $expectedLast, $wasLast );