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 function __construct( $name = null, array $data = array(), $dataName = '' ) {
20 parent
::__construct( $name, $data, $dataName );
22 $this->tablesUsed
= array_merge( $this->tablesUsed
,
40 protected function setUp() {
41 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
45 $wgExtraNamespaces[12312] = 'Dummy';
46 $wgExtraNamespaces[12313] = 'Dummy_talk';
48 $wgNamespaceContentModels[12312] = 'DUMMY';
49 $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
51 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
52 $wgContLang->resetNamespaces(); # reset namespace cache
53 if ( !$this->the_page
) {
54 $this->the_page
= $this->createPage(
55 'RevisionStorageTest_the_page',
57 CONTENT_MODEL_WIKITEXT
62 protected function tearDown() {
63 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
67 unset( $wgExtraNamespaces[12312] );
68 unset( $wgExtraNamespaces[12313] );
70 unset( $wgNamespaceContentModels[12312] );
71 unset( $wgContentHandlers['DUMMY'] );
73 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
74 $wgContLang->resetNamespaces(); # reset namespace cache
77 protected function makeRevision( $props = null ) {
78 if ( $props === null ) {
82 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
83 $props['text'] = 'Lorem Ipsum';
86 if ( !isset( $props['comment'] ) ) {
87 $props['comment'] = 'just a test';
90 if ( !isset( $props['page'] ) ) {
91 $props['page'] = $this->the_page
->getId();
94 $rev = new Revision( $props );
96 $dbw = wfgetDB( DB_MASTER
);
97 $rev->insertOn( $dbw );
102 protected function createPage( $page, $text, $model = null ) {
103 if ( is_string( $page ) ) {
104 if ( !preg_match( '/:/', $page ) &&
105 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
107 $ns = $this->getDefaultWikitextNS();
108 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
111 $page = Title
::newFromText( $page );
114 if ( $page instanceof Title
) {
115 $page = new WikiPage( $page );
118 if ( $page->exists() ) {
119 $page->doDeleteArticle( "done" );
122 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
123 $page->doEditContent( $content, "testing", EDIT_NEW
);
128 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
129 $this->assertNotNull( $rev, 'missing revision' );
131 $this->assertEquals( $orig->getId(), $rev->getId() );
132 $this->assertEquals( $orig->getPage(), $rev->getPage() );
133 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
134 $this->assertEquals( $orig->getUser(), $rev->getUser() );
135 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
136 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
137 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
141 * @covers Revision::__construct
143 public function testConstructFromRow() {
144 $orig = $this->makeRevision();
146 $dbr = wfgetDB( DB_SLAVE
);
147 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
148 $this->assertTrue( is_object( $res ), 'query failed' );
150 $row = $res->fetchObject();
153 $rev = new Revision( $row );
155 $this->assertRevEquals( $orig, $rev );
159 * @covers Revision::newFromRow
161 public function testNewFromRow() {
162 $orig = $this->makeRevision();
164 $dbr = wfgetDB( DB_SLAVE
);
165 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
166 $this->assertTrue( is_object( $res ), 'query failed' );
168 $row = $res->fetchObject();
171 $rev = Revision
::newFromRow( $row );
173 $this->assertRevEquals( $orig, $rev );
177 * @covers Revision::newFromArchiveRow
179 public function testNewFromArchiveRow() {
180 $page = $this->createPage(
181 'RevisionStorageTest_testNewFromArchiveRow',
183 CONTENT_MODEL_WIKITEXT
185 $orig = $page->getRevision();
186 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
188 $dbr = wfgetDB( DB_SLAVE
);
189 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
190 $this->assertTrue( is_object( $res ), 'query failed' );
192 $row = $res->fetchObject();
195 $rev = Revision
::newFromArchiveRow( $row );
197 $this->assertRevEquals( $orig, $rev );
201 * @covers Revision::newFromId
203 public function testNewFromId() {
204 $orig = $this->makeRevision();
206 $rev = Revision
::newFromId( $orig->getId() );
208 $this->assertRevEquals( $orig, $rev );
212 * @covers Revision::fetchRevision
214 public function testFetchRevision() {
215 $page = $this->createPage(
216 'RevisionStorageTest_testFetchRevision',
218 CONTENT_MODEL_WIKITEXT
221 // Hidden process cache assertion below
222 $page->getRevision()->getId();
224 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
225 $id = $page->getRevision()->getId();
227 $res = Revision
::fetchRevision( $page->getTitle() );
229 # note: order is unspecified
231 while ( ( $row = $res->fetchObject() ) ) {
232 $rows[$row->rev_id
] = $row;
235 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
236 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
240 * @covers Revision::selectFields
242 public function testSelectFields() {
243 global $wgContentHandlerUseDB;
245 $fields = Revision
::selectFields();
247 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
248 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
250 in_array( 'rev_timestamp', $fields ),
251 'missing rev_timestamp in list of fields'
253 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
255 if ( $wgContentHandlerUseDB ) {
256 $this->assertTrue( in_array( 'rev_content_model', $fields ),
257 'missing rev_content_model in list of fields' );
258 $this->assertTrue( in_array( 'rev_content_format', $fields ),
259 'missing rev_content_format in list of fields' );
264 * @covers Revision::getPage
266 public function testGetPage() {
267 $page = $this->the_page
;
269 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
270 $rev = Revision
::newFromId( $orig->getId() );
272 $this->assertEquals( $page->getId(), $rev->getPage() );
276 * @covers Revision::getText
278 public function testGetText() {
279 $this->hideDeprecated( 'Revision::getText' );
281 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
282 $rev = Revision
::newFromId( $orig->getId() );
284 $this->assertEquals( 'hello hello.', $rev->getText() );
288 * @covers Revision::getContent
290 public function testGetContent_failure() {
291 $rev = new Revision( array(
292 'page' => $this->the_page
->getId(),
293 'content_model' => $this->the_page
->getContentModel(),
294 'text_id' => 123456789, // not in the test DB
297 $this->assertNull( $rev->getContent(),
298 "getContent() should return null if the revision's text blob could not be loaded." );
300 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
301 $this->assertNull( $rev->getContent(),
302 "getContent() should return null if the revision's text blob could not be loaded." );
306 * @covers Revision::getContent
308 public function testGetContent() {
309 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
310 $rev = Revision
::newFromId( $orig->getId() );
312 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
316 * @covers Revision::getRawText
318 public function testGetRawText() {
319 $this->hideDeprecated( 'Revision::getRawText' );
321 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
322 $rev = Revision
::newFromId( $orig->getId() );
324 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
328 * @covers Revision::getContentModel
330 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() {
348 global $wgContentHandlerUseDB;
350 if ( !$wgContentHandlerUseDB ) {
351 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
354 $orig = $this->makeRevision( array(
355 'text' => 'hello hello.',
356 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
357 'content_format' => CONTENT_FORMAT_JAVASCRIPT
359 $rev = Revision
::newFromId( $orig->getId() );
361 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
365 * @covers Revision::isCurrent
367 public function testIsCurrent() {
368 $page = $this->createPage(
369 'RevisionStorageTest_testIsCurrent',
371 CONTENT_MODEL_WIKITEXT
373 $rev1 = $page->getRevision();
375 # @todo find out if this should be true
376 # $this->assertTrue( $rev1->isCurrent() );
378 $rev1x = Revision
::newFromId( $rev1->getId() );
379 $this->assertTrue( $rev1x->isCurrent() );
381 $page->doEditContent(
382 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
385 $rev2 = $page->getRevision();
387 # @todo find out if this should be true
388 # $this->assertTrue( $rev2->isCurrent() );
390 $rev1x = Revision
::newFromId( $rev1->getId() );
391 $this->assertFalse( $rev1x->isCurrent() );
393 $rev2x = Revision
::newFromId( $rev2->getId() );
394 $this->assertTrue( $rev2x->isCurrent() );
398 * @covers Revision::getPrevious
400 public function testGetPrevious() {
401 $page = $this->createPage(
402 'RevisionStorageTest_testGetPrevious',
403 'Lorem Ipsum testGetPrevious',
404 CONTENT_MODEL_WIKITEXT
406 $rev1 = $page->getRevision();
408 $this->assertNull( $rev1->getPrevious() );
410 $page->doEditContent(
411 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
412 'second rev testGetPrevious' );
413 $rev2 = $page->getRevision();
415 $this->assertNotNull( $rev2->getPrevious() );
416 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
420 * @covers Revision::getNext
422 public function testGetNext() {
423 $page = $this->createPage(
424 'RevisionStorageTest_testGetNext',
425 'Lorem Ipsum testGetNext',
426 CONTENT_MODEL_WIKITEXT
428 $rev1 = $page->getRevision();
430 $this->assertNull( $rev1->getNext() );
432 $page->doEditContent(
433 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
434 'second rev testGetNext'
436 $rev2 = $page->getRevision();
438 $this->assertNotNull( $rev1->getNext() );
439 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
443 * @covers Revision::newNullRevision
445 public function testNewNullRevision() {
446 $page = $this->createPage(
447 'RevisionStorageTest_testNewNullRevision',
449 CONTENT_MODEL_WIKITEXT
451 $orig = $page->getRevision();
453 $dbw = wfGetDB( DB_MASTER
);
454 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
456 $this->assertNotEquals( $orig->getId(), $rev->getId(),
457 'new null revision shold have a different id from the original revision' );
458 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
459 'new null revision shold have the same text id as the original revision' );
460 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
463 public static function provideUserWasLastToEdit() {
466 3, true, # actually the last edit
469 2, true, # not the current edit, but still by this user
472 1, false, # edit by another user
475 0, false, # first edit, by this user, but another user edited in the mean time
481 * @dataProvider provideUserWasLastToEdit
483 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
484 $userA = User
::newFromName( "RevisionStorageTest_userA" );
485 $userB = User
::newFromName( "RevisionStorageTest_userB" );
487 if ( $userA->getId() === 0 ) {
488 $userA = User
::createNew( $userA->getName() );
491 if ( $userB->getId() === 0 ) {
492 $userB = User
::createNew( $userB->getName() );
495 $ns = $this->getDefaultWikitextNS();
497 $dbw = wfGetDB( DB_MASTER
);
498 $revisions = array();
500 // create revisions -----------------------------
501 $page = WikiPage
::factory( Title
::newFromText(
502 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
503 $page->insertOn( $dbw );
506 $revisions[0] = new Revision( array(
507 'page' => $page->getId(),
508 // we need the title to determine the page's default content model
509 'title' => $page->getTitle(),
510 'timestamp' => '20120101000000',
511 'user' => $userA->getId(),
513 'content_model' => CONTENT_MODEL_WIKITEXT
,
514 'summary' => 'edit zero'
516 $revisions[0]->insertOn( $dbw );
519 $revisions[1] = new Revision( array(
520 'page' => $page->getId(),
521 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
522 'title' => $page->getTitle(),
523 'timestamp' => '20120101000100',
524 'user' => $userA->getId(),
526 'content_model' => CONTENT_MODEL_WIKITEXT
,
527 'summary' => 'edit one'
529 $revisions[1]->insertOn( $dbw );
532 $revisions[2] = new Revision( array(
533 'page' => $page->getId(),
534 'title' => $page->getTitle(),
535 'timestamp' => '20120101000200',
536 'user' => $userB->getId(),
538 'content_model' => CONTENT_MODEL_WIKITEXT
,
539 'summary' => 'edit two'
541 $revisions[2]->insertOn( $dbw );
544 $revisions[3] = new Revision( array(
545 'page' => $page->getId(),
546 'title' => $page->getTitle(),
547 'timestamp' => '20120101000300',
548 'user' => $userA->getId(),
550 'content_model' => CONTENT_MODEL_WIKITEXT
,
551 'summary' => 'edit three'
553 $revisions[3]->insertOn( $dbw );
556 $revisions[4] = new Revision( array(
557 'page' => $page->getId(),
558 'title' => $page->getTitle(),
559 'timestamp' => '20120101000200',
560 'user' => $userA->getId(),
562 'content_model' => CONTENT_MODEL_WIKITEXT
,
563 'summary' => 'edit four'
565 $revisions[4]->insertOn( $dbw );
567 // test it ---------------------------------
568 $since = $revisions[$sinceIdx]->getTimestamp();
570 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
572 $this->assertEquals( $expectedLast, $wasLast );