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 protected 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 protected 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 ) {
79 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
80 $props['text'] = 'Lorem Ipsum';
83 if ( !isset( $props['comment'] ) ) {
84 $props['comment'] = 'just a test';
87 if ( !isset( $props['page'] ) ) {
88 $props['page'] = $this->the_page
->getId();
91 $rev = new Revision( $props );
93 $dbw = wfgetDB( DB_MASTER
);
94 $rev->insertOn( $dbw );
99 protected function createPage( $page, $text, $model = null ) {
100 if ( is_string( $page ) ) {
101 if ( !preg_match( '/:/', $page ) &&
102 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
104 $ns = $this->getDefaultWikitextNS();
105 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
108 $page = Title
::newFromText( $page );
111 if ( $page instanceof Title
) {
112 $page = new WikiPage( $page );
115 if ( $page->exists() ) {
116 $page->doDeleteArticle( "done" );
119 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
120 $page->doEditContent( $content, "testing", EDIT_NEW
);
125 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
126 $this->assertNotNull( $rev, 'missing revision' );
128 $this->assertEquals( $orig->getId(), $rev->getId() );
129 $this->assertEquals( $orig->getPage(), $rev->getPage() );
130 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
131 $this->assertEquals( $orig->getUser(), $rev->getUser() );
132 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
133 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
134 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
138 * @covers Revision::__construct
140 public function testConstructFromRow() {
141 $orig = $this->makeRevision();
143 $dbr = wfgetDB( DB_SLAVE
);
144 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
145 $this->assertTrue( is_object( $res ), 'query failed' );
147 $row = $res->fetchObject();
150 $rev = new Revision( $row );
152 $this->assertRevEquals( $orig, $rev );
156 * @covers Revision::newFromRow
158 public function testNewFromRow() {
159 $orig = $this->makeRevision();
161 $dbr = wfgetDB( DB_SLAVE
);
162 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
163 $this->assertTrue( is_object( $res ), 'query failed' );
165 $row = $res->fetchObject();
168 $rev = Revision
::newFromRow( $row );
170 $this->assertRevEquals( $orig, $rev );
174 * @covers Revision::newFromArchiveRow
176 public function testNewFromArchiveRow() {
177 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT
);
178 $orig = $page->getRevision();
179 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
181 $dbr = wfgetDB( DB_SLAVE
);
182 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
183 $this->assertTrue( is_object( $res ), 'query failed' );
185 $row = $res->fetchObject();
188 $rev = Revision
::newFromArchiveRow( $row );
190 $this->assertRevEquals( $orig, $rev );
194 * @covers Revision::newFromId
196 public function testNewFromId() {
197 $orig = $this->makeRevision();
199 $rev = Revision
::newFromId( $orig->getId() );
201 $this->assertRevEquals( $orig, $rev );
205 * @covers Revision::fetchRevision
207 public function testFetchRevision() {
208 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one', CONTENT_MODEL_WIKITEXT
);
209 $id1 = $page->getRevision()->getId();
211 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
212 $id2 = $page->getRevision()->getId();
214 $res = Revision
::fetchRevision( $page->getTitle() );
216 #note: order is unspecified
218 while ( ( $row = $res->fetchObject() ) ) {
219 $rows[$row->rev_id
] = $row;
222 $row = $res->fetchObject();
223 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
224 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
228 * @covers Revision::selectFields
230 public function testSelectFields() {
231 global $wgContentHandlerUseDB;
233 $fields = Revision
::selectFields();
235 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
236 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
237 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields' );
238 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
240 if ( $wgContentHandlerUseDB ) {
241 $this->assertTrue( in_array( 'rev_content_model', $fields ),
242 'missing rev_content_model in list of fields' );
243 $this->assertTrue( in_array( 'rev_content_format', $fields ),
244 'missing rev_content_format in list of fields' );
249 * @covers Revision::getPage
251 public function testGetPage() {
252 $page = $this->the_page
;
254 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
255 $rev = Revision
::newFromId( $orig->getId() );
257 $this->assertEquals( $page->getId(), $rev->getPage() );
261 * @covers Revision::getText
263 public function testGetText() {
264 $this->hideDeprecated( 'Revision::getText' );
266 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
267 $rev = Revision
::newFromId( $orig->getId() );
269 $this->assertEquals( 'hello hello.', $rev->getText() );
273 * @covers Revision::getContent
275 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() {
294 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
295 $rev = Revision
::newFromId( $orig->getId() );
297 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
301 * @covers Revision::getRawText
303 public function testGetRawText() {
304 $this->hideDeprecated( 'Revision::getRawText' );
306 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
307 $rev = Revision
::newFromId( $orig->getId() );
309 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
313 * @covers Revision::getContentModel
315 public function testGetContentModel() {
316 global $wgContentHandlerUseDB;
318 if ( !$wgContentHandlerUseDB ) {
319 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
322 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
323 'content_model' => CONTENT_MODEL_JAVASCRIPT
) );
324 $rev = Revision
::newFromId( $orig->getId() );
326 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
330 * @covers Revision::getContentFormat
332 public function testGetContentFormat() {
333 global $wgContentHandlerUseDB;
335 if ( !$wgContentHandlerUseDB ) {
336 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
339 $orig = $this->makeRevision( array(
340 'text' => 'hello hello.',
341 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
342 'content_format' => CONTENT_FORMAT_JAVASCRIPT
344 $rev = Revision
::newFromId( $orig->getId() );
346 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
350 * @covers Revision::isCurrent
352 public function testIsCurrent() {
353 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT
);
354 $rev1 = $page->getRevision();
356 # @todo find out if this should be true
357 # $this->assertTrue( $rev1->isCurrent() );
359 $rev1x = Revision
::newFromId( $rev1->getId() );
360 $this->assertTrue( $rev1x->isCurrent() );
362 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
), 'second rev' );
363 $rev2 = $page->getRevision();
365 # @todo find out if this should be true
366 # $this->assertTrue( $rev2->isCurrent() );
368 $rev1x = Revision
::newFromId( $rev1->getId() );
369 $this->assertFalse( $rev1x->isCurrent() );
371 $rev2x = Revision
::newFromId( $rev2->getId() );
372 $this->assertTrue( $rev2x->isCurrent() );
376 * @covers Revision::getPrevious
378 public function testGetPrevious() {
379 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious', CONTENT_MODEL_WIKITEXT
);
380 $rev1 = $page->getRevision();
382 $this->assertNull( $rev1->getPrevious() );
384 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
385 'second rev testGetPrevious' );
386 $rev2 = $page->getRevision();
388 $this->assertNotNull( $rev2->getPrevious() );
389 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
393 * @covers Revision::getNext
395 public function testGetNext() {
396 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext', CONTENT_MODEL_WIKITEXT
);
397 $rev1 = $page->getRevision();
399 $this->assertNull( $rev1->getNext() );
401 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
402 'second rev testGetNext' );
403 $rev2 = $page->getRevision();
405 $this->assertNotNull( $rev1->getNext() );
406 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
410 * @covers Revision::newNullRevision
412 public function testNewNullRevision() {
413 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text', CONTENT_MODEL_WIKITEXT
);
414 $orig = $page->getRevision();
416 $dbw = wfGetDB( DB_MASTER
);
417 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
419 $this->assertNotEquals( $orig->getId(), $rev->getId(),
420 'new null revision shold have a different id from the original revision' );
421 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
422 'new null revision shold have the same text id as the original revision' );
423 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
426 public static function provideUserWasLastToEdit() {
429 3, true, # actually the last edit
432 2, true, # not the current edit, but still by this user
435 1, false, # edit by another user
438 0, false, # first edit, by this user, but another user edited in the mean time
444 * @dataProvider provideUserWasLastToEdit
446 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
447 $userA = User
::newFromName( "RevisionStorageTest_userA" );
448 $userB = User
::newFromName( "RevisionStorageTest_userB" );
450 if ( $userA->getId() === 0 ) {
451 $userA = User
::createNew( $userA->getName() );
454 if ( $userB->getId() === 0 ) {
455 $userB = User
::createNew( $userB->getName() );
458 $ns = $this->getDefaultWikitextNS();
460 $dbw = wfGetDB( DB_MASTER
);
461 $revisions = array();
463 // create revisions -----------------------------
464 $page = WikiPage
::factory( Title
::newFromText(
465 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
466 $page->insertOn( $dbw );
469 $revisions[0] = new Revision( array(
470 'page' => $page->getId(),
471 'title' => $page->getTitle(), // we need the title to determine the page's default content model
472 'timestamp' => '20120101000000',
473 'user' => $userA->getId(),
475 'content_model' => CONTENT_MODEL_WIKITEXT
,
476 'summary' => 'edit zero'
478 $revisions[0]->insertOn( $dbw );
481 $revisions[1] = new Revision( array(
482 'page' => $page->getId(),
483 'title' => $page->getTitle(), // still need the title, because $page->getId() is 0 (there's no entry in the page table)
484 'timestamp' => '20120101000100',
485 'user' => $userA->getId(),
487 'content_model' => CONTENT_MODEL_WIKITEXT
,
488 'summary' => 'edit one'
490 $revisions[1]->insertOn( $dbw );
493 $revisions[2] = new Revision( array(
494 'page' => $page->getId(),
495 'title' => $page->getTitle(),
496 'timestamp' => '20120101000200',
497 'user' => $userB->getId(),
499 'content_model' => CONTENT_MODEL_WIKITEXT
,
500 'summary' => 'edit two'
502 $revisions[2]->insertOn( $dbw );
505 $revisions[3] = new Revision( array(
506 'page' => $page->getId(),
507 'title' => $page->getTitle(),
508 'timestamp' => '20120101000300',
509 'user' => $userA->getId(),
511 'content_model' => CONTENT_MODEL_WIKITEXT
,
512 'summary' => 'edit three'
514 $revisions[3]->insertOn( $dbw );
517 $revisions[4] = new Revision( array(
518 'page' => $page->getId(),
519 'title' => $page->getTitle(),
520 'timestamp' => '20120101000200',
521 'user' => $userA->getId(),
523 'content_model' => CONTENT_MODEL_WIKITEXT
,
524 'summary' => 'edit four'
526 $revisions[4]->insertOn( $dbw );
528 // test it ---------------------------------
529 $since = $revisions[$sinceIdx]->getTimestamp();
531 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
533 $this->assertEquals( $expectedLast, $wasLast );