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()
276 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
277 $rev = Revision
::newFromId( $orig->getId() );
279 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
283 * @covers Revision::revText
285 public function testRevText()
287 $this->hideDeprecated( 'Revision::revText' );
288 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
289 $rev = Revision
::newFromId( $orig->getId() );
291 $this->assertEquals( 'hello hello rev.', $rev->revText() );
295 * @covers Revision::getRawText
297 public function testGetRawText()
299 $this->hideDeprecated( 'Revision::getRawText' );
301 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
302 $rev = Revision
::newFromId( $orig->getId() );
304 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
308 * @covers Revision::getContentModel
310 public function testGetContentModel()
312 global $wgContentHandlerUseDB;
314 if ( !$wgContentHandlerUseDB ) {
315 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
318 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
319 'content_model' => CONTENT_MODEL_JAVASCRIPT
) );
320 $rev = Revision
::newFromId( $orig->getId() );
322 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
326 * @covers Revision::getContentFormat
328 public function testGetContentFormat()
330 global $wgContentHandlerUseDB;
332 if ( !$wgContentHandlerUseDB ) {
333 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
336 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
337 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
338 'content_format' => CONTENT_FORMAT_JAVASCRIPT
) );
339 $rev = Revision
::newFromId( $orig->getId() );
341 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
345 * @covers Revision::isCurrent
347 public function testIsCurrent()
349 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT
);
350 $rev1 = $page->getRevision();
352 # @todo: find out if this should be true
353 # $this->assertTrue( $rev1->isCurrent() );
355 $rev1x = Revision
::newFromId( $rev1->getId() );
356 $this->assertTrue( $rev1x->isCurrent() );
358 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
), 'second rev' );
359 $rev2 = $page->getRevision();
361 # @todo: find out if this should be true
362 # $this->assertTrue( $rev2->isCurrent() );
364 $rev1x = Revision
::newFromId( $rev1->getId() );
365 $this->assertFalse( $rev1x->isCurrent() );
367 $rev2x = Revision
::newFromId( $rev2->getId() );
368 $this->assertTrue( $rev2x->isCurrent() );
372 * @covers Revision::getPrevious
374 public function testGetPrevious()
376 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious', CONTENT_MODEL_WIKITEXT
);
377 $rev1 = $page->getRevision();
379 $this->assertNull( $rev1->getPrevious() );
381 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
382 'second rev testGetPrevious' );
383 $rev2 = $page->getRevision();
385 $this->assertNotNull( $rev2->getPrevious() );
386 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
390 * @covers Revision::getNext
392 public function testGetNext()
394 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext', CONTENT_MODEL_WIKITEXT
);
395 $rev1 = $page->getRevision();
397 $this->assertNull( $rev1->getNext() );
399 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
400 'second rev testGetNext' );
401 $rev2 = $page->getRevision();
403 $this->assertNotNull( $rev1->getNext() );
404 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
408 * @covers Revision::newNullRevision
410 public function testNewNullRevision()
412 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text', CONTENT_MODEL_WIKITEXT
);
413 $orig = $page->getRevision();
415 $dbw = wfGetDB( DB_MASTER
);
416 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
418 $this->assertNotEquals( $orig->getId(), $rev->getId(),
419 'new null revision shold have a different id from the original revision' );
420 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
421 'new null revision shold have the same text id as the original revision' );
422 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
425 public static function provideUserWasLastToEdit() {
428 3, true, # actually the last edit
431 2, true, # not the current edit, but still by this user
434 1, false, # edit by another user
437 0, false, # first edit, by this user, but another user edited in the mean time
443 * @dataProvider provideUserWasLastToEdit
445 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
446 $userA = \User
::newFromName( "RevisionStorageTest_userA" );
447 $userB = \User
::newFromName( "RevisionStorageTest_userB" );
449 if ( $userA->getId() === 0 ) {
450 $userA = \User
::createNew( $userA->getName() );
453 if ( $userB->getId() === 0 ) {
454 $userB = \User
::createNew( $userB->getName() );
457 $ns = $this->getDefaultWikitextNS();
459 $dbw = wfGetDB( DB_MASTER
);
460 $revisions = array();
462 // create revisions -----------------------------
463 $page = WikiPage
::factory( Title
::newFromText(
464 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
467 $revisions[0] = new Revision( array(
468 'page' => $page->getId(),
469 'title' => $page->getTitle(), // we need the title to determine the page's default content model
470 'timestamp' => '20120101000000',
471 'user' => $userA->getId(),
473 'content_model' => CONTENT_MODEL_WIKITEXT
,
474 'summary' => 'edit zero'
476 $revisions[0]->insertOn( $dbw );
479 $revisions[1] = new Revision( array(
480 'page' => $page->getId(),
481 'title' => $page->getTitle(), // still need the title, because $page->getId() is 0 (there's no entry in the page table)
482 'timestamp' => '20120101000100',
483 'user' => $userA->getId(),
485 'content_model' => CONTENT_MODEL_WIKITEXT
,
486 'summary' => 'edit one'
488 $revisions[1]->insertOn( $dbw );
491 $revisions[2] = new Revision( array(
492 'page' => $page->getId(),
493 'title' => $page->getTitle(),
494 'timestamp' => '20120101000200',
495 'user' => $userB->getId(),
497 'content_model' => CONTENT_MODEL_WIKITEXT
,
498 'summary' => 'edit two'
500 $revisions[2]->insertOn( $dbw );
503 $revisions[3] = new Revision( array(
504 'page' => $page->getId(),
505 'title' => $page->getTitle(),
506 'timestamp' => '20120101000300',
507 'user' => $userA->getId(),
509 'content_model' => CONTENT_MODEL_WIKITEXT
,
510 'summary' => 'edit three'
512 $revisions[3]->insertOn( $dbw );
515 $revisions[4] = new Revision( array(
516 'page' => $page->getId(),
517 'title' => $page->getTitle(),
518 'timestamp' => '20120101000200',
519 'user' => $userA->getId(),
521 'content_model' => CONTENT_MODEL_WIKITEXT
,
522 'summary' => 'edit four'
524 $revisions[4]->insertOn( $dbw );
526 // test it ---------------------------------
527 $since = $revisions[ $sinceIdx ]->getTimestamp();
529 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
531 $this->assertEquals( $expectedLast, $wasLast );