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
,
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
::clearCaches();
53 $wgContLang->resetNamespaces(); # reset namespace cache
54 if ( !$this->the_page
) {
55 $this->the_page
= $this->createPage(
56 'RevisionStorageTest_the_page',
58 CONTENT_MODEL_WIKITEXT
62 $this->tablesUsed
[] = 'archive';
65 protected function tearDown() {
66 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
70 unset( $wgExtraNamespaces[12312] );
71 unset( $wgExtraNamespaces[12313] );
73 unset( $wgNamespaceContentModels[12312] );
74 unset( $wgContentHandlers['DUMMY'] );
76 MWNamespace
::clearCaches();
77 $wgContLang->resetNamespaces(); # reset namespace cache
80 protected function makeRevision( $props = null ) {
81 if ( $props === null ) {
85 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
86 $props['text'] = 'Lorem Ipsum';
89 if ( !isset( $props['comment'] ) ) {
90 $props['comment'] = 'just a test';
93 if ( !isset( $props['page'] ) ) {
94 $props['page'] = $this->the_page
->getId();
97 $rev = new Revision( $props );
99 $dbw = wfGetDB( DB_MASTER
);
100 $rev->insertOn( $dbw );
105 protected function createPage( $page, $text, $model = null ) {
106 if ( is_string( $page ) ) {
107 if ( !preg_match( '/:/', $page ) &&
108 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
110 $ns = $this->getDefaultWikitextNS();
111 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
114 $page = Title
::newFromText( $page );
117 if ( $page instanceof Title
) {
118 $page = new WikiPage( $page );
121 if ( $page->exists() ) {
122 $page->doDeleteArticle( "done" );
125 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
126 $page->doEditContent( $content, "testing", EDIT_NEW
);
131 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
132 $this->assertNotNull( $rev, 'missing revision' );
134 $this->assertEquals( $orig->getId(), $rev->getId() );
135 $this->assertEquals( $orig->getPage(), $rev->getPage() );
136 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
137 $this->assertEquals( $orig->getUser(), $rev->getUser() );
138 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
139 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
140 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
144 * @covers Revision::__construct
146 public function testConstructFromRow() {
147 $orig = $this->makeRevision();
149 $dbr = wfGetDB( DB_REPLICA
);
150 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
151 $this->assertTrue( is_object( $res ), 'query failed' );
153 $row = $res->fetchObject();
156 $rev = new Revision( $row );
158 $this->assertRevEquals( $orig, $rev );
162 * @covers Revision::newFromRow
164 public function testNewFromRow() {
165 $orig = $this->makeRevision();
167 $dbr = wfGetDB( DB_REPLICA
);
168 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
169 $this->assertTrue( is_object( $res ), 'query failed' );
171 $row = $res->fetchObject();
174 $rev = Revision
::newFromRow( $row );
176 $this->assertRevEquals( $orig, $rev );
180 * @covers Revision::newFromArchiveRow
182 public function testNewFromArchiveRow() {
183 $page = $this->createPage(
184 'RevisionStorageTest_testNewFromArchiveRow',
186 CONTENT_MODEL_WIKITEXT
188 $orig = $page->getRevision();
189 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
191 $dbr = wfGetDB( DB_REPLICA
);
193 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
195 $this->assertTrue( is_object( $res ), 'query failed' );
197 $row = $res->fetchObject();
200 $rev = Revision
::newFromArchiveRow( $row );
202 $this->assertRevEquals( $orig, $rev );
206 * @covers Revision::newFromId
208 public function testNewFromId() {
209 $orig = $this->makeRevision();
211 $rev = Revision
::newFromId( $orig->getId() );
213 $this->assertRevEquals( $orig, $rev );
217 * @covers Revision::fetchRevision
219 public function testFetchRevision() {
220 $page = $this->createPage(
221 'RevisionStorageTest_testFetchRevision',
223 CONTENT_MODEL_WIKITEXT
226 // Hidden process cache assertion below
227 $page->getRevision()->getId();
229 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
230 $id = $page->getRevision()->getId();
232 $res = Revision
::fetchRevision( $page->getTitle() );
234 # note: order is unspecified
236 while ( ( $row = $res->fetchObject() ) ) {
237 $rows[$row->rev_id
] = $row;
240 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
241 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
245 * @covers Revision::selectFields
247 public function testSelectFields() {
248 global $wgContentHandlerUseDB;
250 $fields = Revision
::selectFields();
252 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
253 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
255 in_array( 'rev_timestamp', $fields ),
256 'missing rev_timestamp in list of fields'
258 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
260 if ( $wgContentHandlerUseDB ) {
261 $this->assertTrue( in_array( 'rev_content_model', $fields ),
262 'missing rev_content_model in list of fields' );
263 $this->assertTrue( in_array( 'rev_content_format', $fields ),
264 'missing rev_content_format in list of fields' );
269 * @covers Revision::getPage
271 public function testGetPage() {
272 $page = $this->the_page
;
274 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
275 $rev = Revision
::newFromId( $orig->getId() );
277 $this->assertEquals( $page->getId(), $rev->getPage() );
281 * @covers Revision::getContent
283 public function testGetContent_failure() {
284 $rev = new Revision( [
285 'page' => $this->the_page
->getId(),
286 'content_model' => $this->the_page
->getContentModel(),
287 'text_id' => 123456789, // not in the test DB
290 $this->assertNull( $rev->getContent(),
291 "getContent() should return null if the revision's text blob could not be loaded." );
293 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
294 $this->assertNull( $rev->getContent(),
295 "getContent() should return null if the revision's text blob could not be loaded." );
299 * @covers Revision::getContent
301 public function testGetContent() {
302 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
303 $rev = Revision
::newFromId( $orig->getId() );
305 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
309 * @covers Revision::getContentModel
311 public function testGetContentModel() {
312 global $wgContentHandlerUseDB;
314 if ( !$wgContentHandlerUseDB ) {
315 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
318 $orig = $this->makeRevision( [ '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() {
329 global $wgContentHandlerUseDB;
331 if ( !$wgContentHandlerUseDB ) {
332 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
335 $orig = $this->makeRevision( [
336 'text' => 'hello hello.',
337 'content_model' => CONTENT_MODEL_JAVASCRIPT
,
338 'content_format' => CONTENT_FORMAT_JAVASCRIPT
340 $rev = Revision
::newFromId( $orig->getId() );
342 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
346 * @covers Revision::isCurrent
348 public function testIsCurrent() {
349 $page = $this->createPage(
350 'RevisionStorageTest_testIsCurrent',
352 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(
363 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
366 $rev2 = $page->getRevision();
368 # @todo find out if this should be true
369 # $this->assertTrue( $rev2->isCurrent() );
371 $rev1x = Revision
::newFromId( $rev1->getId() );
372 $this->assertFalse( $rev1x->isCurrent() );
374 $rev2x = Revision
::newFromId( $rev2->getId() );
375 $this->assertTrue( $rev2x->isCurrent() );
379 * @covers Revision::getPrevious
381 public function testGetPrevious() {
382 $page = $this->createPage(
383 'RevisionStorageTest_testGetPrevious',
384 'Lorem Ipsum testGetPrevious',
385 CONTENT_MODEL_WIKITEXT
387 $rev1 = $page->getRevision();
389 $this->assertNull( $rev1->getPrevious() );
391 $page->doEditContent(
392 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
393 'second rev testGetPrevious' );
394 $rev2 = $page->getRevision();
396 $this->assertNotNull( $rev2->getPrevious() );
397 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
401 * @covers Revision::getNext
403 public function testGetNext() {
404 $page = $this->createPage(
405 'RevisionStorageTest_testGetNext',
406 'Lorem Ipsum testGetNext',
407 CONTENT_MODEL_WIKITEXT
409 $rev1 = $page->getRevision();
411 $this->assertNull( $rev1->getNext() );
413 $page->doEditContent(
414 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
415 'second rev testGetNext'
417 $rev2 = $page->getRevision();
419 $this->assertNotNull( $rev1->getNext() );
420 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
424 * @covers Revision::newNullRevision
426 public function testNewNullRevision() {
427 $page = $this->createPage(
428 'RevisionStorageTest_testNewNullRevision',
430 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() );
445 * @covers Revision::insertOn
447 public function testInsertOn() {
448 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
450 $orig = $this->makeRevision( [
454 // Make sure the revision was copied to ip_changes
455 $dbr = wfGetDB( DB_REPLICA
);
456 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
457 $row = $res->fetchObject();
459 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
460 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
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
);
500 // create revisions -----------------------------
501 $page = WikiPage
::factory( Title
::newFromText(
502 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
503 $page->insertOn( $dbw );
506 $revisions[0] = new Revision( [
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( [
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( [
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( [
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( [
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 );