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 = [], $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
61 $this->tablesUsed
[] = 'archive';
64 protected function tearDown() {
65 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
69 unset( $wgExtraNamespaces[12312] );
70 unset( $wgExtraNamespaces[12313] );
72 unset( $wgNamespaceContentModels[12312] );
73 unset( $wgContentHandlers['DUMMY'] );
75 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
76 $wgContLang->resetNamespaces(); # reset namespace cache
79 protected function makeRevision( $props = null ) {
80 if ( $props === null ) {
84 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
85 $props['text'] = 'Lorem Ipsum';
88 if ( !isset( $props['comment'] ) ) {
89 $props['comment'] = 'just a test';
92 if ( !isset( $props['page'] ) ) {
93 $props['page'] = $this->the_page
->getId();
96 $rev = new Revision( $props );
98 $dbw = wfGetDB( DB_MASTER
);
99 $rev->insertOn( $dbw );
104 protected function createPage( $page, $text, $model = null ) {
105 if ( is_string( $page ) ) {
106 if ( !preg_match( '/:/', $page ) &&
107 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
109 $ns = $this->getDefaultWikitextNS();
110 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
113 $page = Title
::newFromText( $page );
116 if ( $page instanceof Title
) {
117 $page = new WikiPage( $page );
120 if ( $page->exists() ) {
121 $page->doDeleteArticle( "done" );
124 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
125 $page->doEditContent( $content, "testing", EDIT_NEW
);
130 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
131 $this->assertNotNull( $rev, 'missing revision' );
133 $this->assertEquals( $orig->getId(), $rev->getId() );
134 $this->assertEquals( $orig->getPage(), $rev->getPage() );
135 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
136 $this->assertEquals( $orig->getUser(), $rev->getUser() );
137 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
138 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
139 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
143 * @covers Revision::__construct
145 public function testConstructFromRow() {
146 $orig = $this->makeRevision();
148 $dbr = wfGetDB( DB_REPLICA
);
149 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
150 $this->assertTrue( is_object( $res ), 'query failed' );
152 $row = $res->fetchObject();
155 $rev = new Revision( $row );
157 $this->assertRevEquals( $orig, $rev );
161 * @covers Revision::newFromRow
163 public function testNewFromRow() {
164 $orig = $this->makeRevision();
166 $dbr = wfGetDB( DB_REPLICA
);
167 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
168 $this->assertTrue( is_object( $res ), 'query failed' );
170 $row = $res->fetchObject();
173 $rev = Revision
::newFromRow( $row );
175 $this->assertRevEquals( $orig, $rev );
179 * @covers Revision::newFromArchiveRow
181 public function testNewFromArchiveRow() {
182 $page = $this->createPage(
183 'RevisionStorageTest_testNewFromArchiveRow',
185 CONTENT_MODEL_WIKITEXT
187 $orig = $page->getRevision();
188 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
190 $dbr = wfGetDB( DB_REPLICA
);
192 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
194 $this->assertTrue( is_object( $res ), 'query failed' );
196 $row = $res->fetchObject();
199 $rev = Revision
::newFromArchiveRow( $row );
201 $this->assertRevEquals( $orig, $rev );
205 * @covers Revision::newFromId
207 public function testNewFromId() {
208 $orig = $this->makeRevision();
210 $rev = Revision
::newFromId( $orig->getId() );
212 $this->assertRevEquals( $orig, $rev );
216 * @covers Revision::fetchRevision
218 public function testFetchRevision() {
219 $page = $this->createPage(
220 'RevisionStorageTest_testFetchRevision',
222 CONTENT_MODEL_WIKITEXT
225 // Hidden process cache assertion below
226 $page->getRevision()->getId();
228 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
229 $id = $page->getRevision()->getId();
231 $res = Revision
::fetchRevision( $page->getTitle() );
233 # note: order is unspecified
235 while ( ( $row = $res->fetchObject() ) ) {
236 $rows[$row->rev_id
] = $row;
239 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
240 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
244 * @covers Revision::selectFields
246 public function testSelectFields() {
247 global $wgContentHandlerUseDB;
249 $fields = Revision
::selectFields();
251 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
252 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
254 in_array( 'rev_timestamp', $fields ),
255 'missing rev_timestamp in list of fields'
257 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
259 if ( $wgContentHandlerUseDB ) {
260 $this->assertTrue( in_array( 'rev_content_model', $fields ),
261 'missing rev_content_model in list of fields' );
262 $this->assertTrue( in_array( 'rev_content_format', $fields ),
263 'missing rev_content_format in list of fields' );
268 * @covers Revision::getPage
270 public function testGetPage() {
271 $page = $this->the_page
;
273 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
274 $rev = Revision
::newFromId( $orig->getId() );
276 $this->assertEquals( $page->getId(), $rev->getPage() );
280 * @covers Revision::getContent
282 public function testGetContent_failure() {
283 $rev = new Revision( [
284 'page' => $this->the_page
->getId(),
285 'content_model' => $this->the_page
->getContentModel(),
286 'text_id' => 123456789, // not in the test DB
289 $this->assertNull( $rev->getContent(),
290 "getContent() should return null if the revision's text blob could not be loaded." );
292 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
293 $this->assertNull( $rev->getContent(),
294 "getContent() should return null if the revision's text blob could not be loaded." );
298 * @covers Revision::getContent
300 public function testGetContent() {
301 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
302 $rev = Revision
::newFromId( $orig->getId() );
304 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
308 * @covers Revision::getContentModel
310 public function testGetContentModel() {
311 global $wgContentHandlerUseDB;
313 if ( !$wgContentHandlerUseDB ) {
314 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
317 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
318 'content_model' => CONTENT_MODEL_JAVASCRIPT
] );
319 $rev = Revision
::newFromId( $orig->getId() );
321 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
325 * @covers Revision::getContentFormat
327 public function testGetContentFormat() {
328 global $wgContentHandlerUseDB;
330 if ( !$wgContentHandlerUseDB ) {
331 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
334 $orig = $this->makeRevision( [
335 'text' => 'hello hello.',
336 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
337 '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() {
348 $page = $this->createPage(
349 'RevisionStorageTest_testIsCurrent',
351 CONTENT_MODEL_WIKITEXT
353 $rev1 = $page->getRevision();
355 # @todo find out if this should be true
356 # $this->assertTrue( $rev1->isCurrent() );
358 $rev1x = Revision
::newFromId( $rev1->getId() );
359 $this->assertTrue( $rev1x->isCurrent() );
361 $page->doEditContent(
362 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
365 $rev2 = $page->getRevision();
367 # @todo find out if this should be true
368 # $this->assertTrue( $rev2->isCurrent() );
370 $rev1x = Revision
::newFromId( $rev1->getId() );
371 $this->assertFalse( $rev1x->isCurrent() );
373 $rev2x = Revision
::newFromId( $rev2->getId() );
374 $this->assertTrue( $rev2x->isCurrent() );
378 * @covers Revision::getPrevious
380 public function testGetPrevious() {
381 $page = $this->createPage(
382 'RevisionStorageTest_testGetPrevious',
383 'Lorem Ipsum testGetPrevious',
384 CONTENT_MODEL_WIKITEXT
386 $rev1 = $page->getRevision();
388 $this->assertNull( $rev1->getPrevious() );
390 $page->doEditContent(
391 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
392 'second rev testGetPrevious' );
393 $rev2 = $page->getRevision();
395 $this->assertNotNull( $rev2->getPrevious() );
396 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
400 * @covers Revision::getNext
402 public function testGetNext() {
403 $page = $this->createPage(
404 'RevisionStorageTest_testGetNext',
405 'Lorem Ipsum testGetNext',
406 CONTENT_MODEL_WIKITEXT
408 $rev1 = $page->getRevision();
410 $this->assertNull( $rev1->getNext() );
412 $page->doEditContent(
413 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
414 'second rev testGetNext'
416 $rev2 = $page->getRevision();
418 $this->assertNotNull( $rev1->getNext() );
419 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
423 * @covers Revision::newNullRevision
425 public function testNewNullRevision() {
426 $page = $this->createPage(
427 'RevisionStorageTest_testNewNullRevision',
429 CONTENT_MODEL_WIKITEXT
431 $orig = $page->getRevision();
433 $dbw = wfGetDB( DB_MASTER
);
434 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
436 $this->assertNotEquals( $orig->getId(), $rev->getId(),
437 'new null revision shold have a different id from the original revision' );
438 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
439 'new null revision shold have the same text id as the original revision' );
440 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
443 public static function provideUserWasLastToEdit() {
446 3, true, # actually the last edit
449 2, true, # not the current edit, but still by this user
452 1, false, # edit by another user
455 0, false, # first edit, by this user, but another user edited in the mean time
461 * @dataProvider provideUserWasLastToEdit
463 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
464 $userA = User
::newFromName( "RevisionStorageTest_userA" );
465 $userB = User
::newFromName( "RevisionStorageTest_userB" );
467 if ( $userA->getId() === 0 ) {
468 $userA = User
::createNew( $userA->getName() );
471 if ( $userB->getId() === 0 ) {
472 $userB = User
::createNew( $userB->getName() );
475 $ns = $this->getDefaultWikitextNS();
477 $dbw = wfGetDB( DB_MASTER
);
480 // create revisions -----------------------------
481 $page = WikiPage
::factory( Title
::newFromText(
482 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
483 $page->insertOn( $dbw );
486 $revisions[0] = new Revision( [
487 'page' => $page->getId(),
488 // we need the title to determine the page's default content model
489 'title' => $page->getTitle(),
490 'timestamp' => '20120101000000',
491 'user' => $userA->getId(),
493 'content_model' => CONTENT_MODEL_WIKITEXT
,
494 'summary' => 'edit zero'
496 $revisions[0]->insertOn( $dbw );
499 $revisions[1] = new Revision( [
500 'page' => $page->getId(),
501 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
502 'title' => $page->getTitle(),
503 'timestamp' => '20120101000100',
504 'user' => $userA->getId(),
506 'content_model' => CONTENT_MODEL_WIKITEXT
,
507 'summary' => 'edit one'
509 $revisions[1]->insertOn( $dbw );
512 $revisions[2] = new Revision( [
513 'page' => $page->getId(),
514 'title' => $page->getTitle(),
515 'timestamp' => '20120101000200',
516 'user' => $userB->getId(),
518 'content_model' => CONTENT_MODEL_WIKITEXT
,
519 'summary' => 'edit two'
521 $revisions[2]->insertOn( $dbw );
524 $revisions[3] = new Revision( [
525 'page' => $page->getId(),
526 'title' => $page->getTitle(),
527 'timestamp' => '20120101000300',
528 'user' => $userA->getId(),
530 'content_model' => CONTENT_MODEL_WIKITEXT
,
531 'summary' => 'edit three'
533 $revisions[3]->insertOn( $dbw );
536 $revisions[4] = new Revision( [
537 'page' => $page->getId(),
538 'title' => $page->getTitle(),
539 'timestamp' => '20120101000200',
540 'user' => $userA->getId(),
542 'content_model' => CONTENT_MODEL_WIKITEXT
,
543 'summary' => 'edit four'
545 $revisions[4]->insertOn( $dbw );
547 // test it ---------------------------------
548 $since = $revisions[$sinceIdx]->getTimestamp();
550 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
552 $this->assertEquals( $expectedLast, $wasLast );