Many more function case mismatches
[mediawiki.git] / tests / phpunit / includes / RevisionStorageTest.php
blob0ac9c3c46bf191887e2be807a62d696882e78bff
1 <?php
3 /**
4 * Test class for Revision storage.
6 * @group ContentHandler
7 * @group Database
8 * ^--- important, causes temporary tables to be used instead of the real database
10 * @group medium
11 * ^--- important, causes tests not to fail with timeout
13 class RevisionStorageTest extends MediaWikiTestCase {
14 /**
15 * @var WikiPage $the_page
17 private $the_page;
19 function __construct( $name = null, array $data = [], $dataName = '' ) {
20 parent::__construct( $name, $data, $dataName );
22 $this->tablesUsed = array_merge( $this->tablesUsed,
23 [ 'page',
24 'revision',
25 'text',
27 'recentchanges',
28 'logging',
30 'page_props',
31 'pagelinks',
32 'categorylinks',
33 'langlinks',
34 'externallinks',
35 'imagelinks',
36 'templatelinks',
37 'iwlinks' ] );
40 protected function setUp() {
41 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
43 parent::setUp();
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',
56 "just a dummy page",
57 CONTENT_MODEL_WIKITEXT
61 $this->tablesUsed[] = 'archive';
64 protected function tearDown() {
65 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
67 parent::tearDown();
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 ) {
81 $props = [];
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 );
101 return $rev;
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 );
127 return $page;
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_SLAVE );
149 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
150 $this->assertTrue( is_object( $res ), 'query failed' );
152 $row = $res->fetchObject();
153 $res->free();
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_SLAVE );
167 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
168 $this->assertTrue( is_object( $res ), 'query failed' );
170 $row = $res->fetchObject();
171 $res->free();
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',
184 'Lorem Ipsum',
185 CONTENT_MODEL_WIKITEXT
187 $orig = $page->getRevision();
188 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
190 $dbr = wfGetDB( DB_SLAVE );
191 $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $orig->getId() ] );
192 $this->assertTrue( is_object( $res ), 'query failed' );
194 $row = $res->fetchObject();
195 $res->free();
197 $rev = Revision::newFromArchiveRow( $row );
199 $this->assertRevEquals( $orig, $rev );
203 * @covers Revision::newFromId
205 public function testNewFromId() {
206 $orig = $this->makeRevision();
208 $rev = Revision::newFromId( $orig->getId() );
210 $this->assertRevEquals( $orig, $rev );
214 * @covers Revision::fetchRevision
216 public function testFetchRevision() {
217 $page = $this->createPage(
218 'RevisionStorageTest_testFetchRevision',
219 'one',
220 CONTENT_MODEL_WIKITEXT
223 // Hidden process cache assertion below
224 $page->getRevision()->getId();
226 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
227 $id = $page->getRevision()->getId();
229 $res = Revision::fetchRevision( $page->getTitle() );
231 # note: order is unspecified
232 $rows = [];
233 while ( ( $row = $res->fetchObject() ) ) {
234 $rows[$row->rev_id] = $row;
237 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
238 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
242 * @covers Revision::selectFields
244 public function testSelectFields() {
245 global $wgContentHandlerUseDB;
247 $fields = Revision::selectFields();
249 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
250 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
251 $this->assertTrue(
252 in_array( 'rev_timestamp', $fields ),
253 'missing rev_timestamp in list of fields'
255 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
257 if ( $wgContentHandlerUseDB ) {
258 $this->assertTrue( in_array( 'rev_content_model', $fields ),
259 'missing rev_content_model in list of fields' );
260 $this->assertTrue( in_array( 'rev_content_format', $fields ),
261 'missing rev_content_format in list of fields' );
266 * @covers Revision::getPage
268 public function testGetPage() {
269 $page = $this->the_page;
271 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
272 $rev = Revision::newFromId( $orig->getId() );
274 $this->assertEquals( $page->getId(), $rev->getPage() );
278 * @covers Revision::getText
280 public function testGetText() {
281 $this->hideDeprecated( 'Revision::getText' );
283 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
284 $rev = Revision::newFromId( $orig->getId() );
286 $this->assertEquals( 'hello hello.', $rev->getText() );
290 * @covers Revision::getContent
292 public function testGetContent_failure() {
293 $rev = new Revision( [
294 'page' => $this->the_page->getId(),
295 'content_model' => $this->the_page->getContentModel(),
296 'text_id' => 123456789, // not in the test DB
297 ] );
299 $this->assertNull( $rev->getContent(),
300 "getContent() should return null if the revision's text blob could not be loaded." );
302 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
303 $this->assertNull( $rev->getContent(),
304 "getContent() should return null if the revision's text blob could not be loaded." );
308 * @covers Revision::getContent
310 public function testGetContent() {
311 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
312 $rev = Revision::newFromId( $orig->getId() );
314 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
318 * @covers Revision::getRawText
320 public function testGetRawText() {
321 $this->hideDeprecated( 'Revision::getRawText' );
323 $orig = $this->makeRevision( [ 'text' => 'hello hello raw.' ] );
324 $rev = Revision::newFromId( $orig->getId() );
326 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
330 * @covers Revision::getContentModel
332 public function testGetContentModel() {
333 global $wgContentHandlerUseDB;
335 if ( !$wgContentHandlerUseDB ) {
336 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
339 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
340 'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
341 $rev = Revision::newFromId( $orig->getId() );
343 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
347 * @covers Revision::getContentFormat
349 public function testGetContentFormat() {
350 global $wgContentHandlerUseDB;
352 if ( !$wgContentHandlerUseDB ) {
353 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
356 $orig = $this->makeRevision( [
357 'text' => 'hello hello.',
358 'content_model' => CONTENT_MODEL_JAVASCRIPT,
359 'content_format' => CONTENT_FORMAT_JAVASCRIPT
360 ] );
361 $rev = Revision::newFromId( $orig->getId() );
363 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
367 * @covers Revision::isCurrent
369 public function testIsCurrent() {
370 $page = $this->createPage(
371 'RevisionStorageTest_testIsCurrent',
372 'Lorem Ipsum',
373 CONTENT_MODEL_WIKITEXT
375 $rev1 = $page->getRevision();
377 # @todo find out if this should be true
378 # $this->assertTrue( $rev1->isCurrent() );
380 $rev1x = Revision::newFromId( $rev1->getId() );
381 $this->assertTrue( $rev1x->isCurrent() );
383 $page->doEditContent(
384 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
385 'second rev'
387 $rev2 = $page->getRevision();
389 # @todo find out if this should be true
390 # $this->assertTrue( $rev2->isCurrent() );
392 $rev1x = Revision::newFromId( $rev1->getId() );
393 $this->assertFalse( $rev1x->isCurrent() );
395 $rev2x = Revision::newFromId( $rev2->getId() );
396 $this->assertTrue( $rev2x->isCurrent() );
400 * @covers Revision::getPrevious
402 public function testGetPrevious() {
403 $page = $this->createPage(
404 'RevisionStorageTest_testGetPrevious',
405 'Lorem Ipsum testGetPrevious',
406 CONTENT_MODEL_WIKITEXT
408 $rev1 = $page->getRevision();
410 $this->assertNull( $rev1->getPrevious() );
412 $page->doEditContent(
413 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
414 'second rev testGetPrevious' );
415 $rev2 = $page->getRevision();
417 $this->assertNotNull( $rev2->getPrevious() );
418 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
422 * @covers Revision::getNext
424 public function testGetNext() {
425 $page = $this->createPage(
426 'RevisionStorageTest_testGetNext',
427 'Lorem Ipsum testGetNext',
428 CONTENT_MODEL_WIKITEXT
430 $rev1 = $page->getRevision();
432 $this->assertNull( $rev1->getNext() );
434 $page->doEditContent(
435 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
436 'second rev testGetNext'
438 $rev2 = $page->getRevision();
440 $this->assertNotNull( $rev1->getNext() );
441 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
445 * @covers Revision::newNullRevision
447 public function testNewNullRevision() {
448 $page = $this->createPage(
449 'RevisionStorageTest_testNewNullRevision',
450 'some testing text',
451 CONTENT_MODEL_WIKITEXT
453 $orig = $page->getRevision();
455 $dbw = wfGetDB( DB_MASTER );
456 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
458 $this->assertNotEquals( $orig->getId(), $rev->getId(),
459 'new null revision shold have a different id from the original revision' );
460 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
461 'new null revision shold have the same text id as the original revision' );
462 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
465 public static function provideUserWasLastToEdit() {
466 return [
467 [ # 0
468 3, true, # actually the last edit
470 [ # 1
471 2, true, # not the current edit, but still by this user
473 [ # 2
474 1, false, # edit by another user
476 [ # 3
477 0, false, # first edit, by this user, but another user edited in the mean time
483 * @dataProvider provideUserWasLastToEdit
485 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
486 $userA = User::newFromName( "RevisionStorageTest_userA" );
487 $userB = User::newFromName( "RevisionStorageTest_userB" );
489 if ( $userA->getId() === 0 ) {
490 $userA = User::createNew( $userA->getName() );
493 if ( $userB->getId() === 0 ) {
494 $userB = User::createNew( $userB->getName() );
497 $ns = $this->getDefaultWikitextNS();
499 $dbw = wfGetDB( DB_MASTER );
500 $revisions = [];
502 // create revisions -----------------------------
503 $page = WikiPage::factory( Title::newFromText(
504 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
505 $page->insertOn( $dbw );
507 # zero
508 $revisions[0] = new Revision( [
509 'page' => $page->getId(),
510 // we need the title to determine the page's default content model
511 'title' => $page->getTitle(),
512 'timestamp' => '20120101000000',
513 'user' => $userA->getId(),
514 'text' => 'zero',
515 'content_model' => CONTENT_MODEL_WIKITEXT,
516 'summary' => 'edit zero'
517 ] );
518 $revisions[0]->insertOn( $dbw );
520 # one
521 $revisions[1] = new Revision( [
522 'page' => $page->getId(),
523 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
524 'title' => $page->getTitle(),
525 'timestamp' => '20120101000100',
526 'user' => $userA->getId(),
527 'text' => 'one',
528 'content_model' => CONTENT_MODEL_WIKITEXT,
529 'summary' => 'edit one'
530 ] );
531 $revisions[1]->insertOn( $dbw );
533 # two
534 $revisions[2] = new Revision( [
535 'page' => $page->getId(),
536 'title' => $page->getTitle(),
537 'timestamp' => '20120101000200',
538 'user' => $userB->getId(),
539 'text' => 'two',
540 'content_model' => CONTENT_MODEL_WIKITEXT,
541 'summary' => 'edit two'
542 ] );
543 $revisions[2]->insertOn( $dbw );
545 # three
546 $revisions[3] = new Revision( [
547 'page' => $page->getId(),
548 'title' => $page->getTitle(),
549 'timestamp' => '20120101000300',
550 'user' => $userA->getId(),
551 'text' => 'three',
552 'content_model' => CONTENT_MODEL_WIKITEXT,
553 'summary' => 'edit three'
554 ] );
555 $revisions[3]->insertOn( $dbw );
557 # four
558 $revisions[4] = new Revision( [
559 'page' => $page->getId(),
560 'title' => $page->getTitle(),
561 'timestamp' => '20120101000200',
562 'user' => $userA->getId(),
563 'text' => 'zero',
564 'content_model' => CONTENT_MODEL_WIKITEXT,
565 'summary' => 'edit four'
566 ] );
567 $revisions[4]->insertOn( $dbw );
569 // test it ---------------------------------
570 $since = $revisions[$sinceIdx]->getTimestamp();
572 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
574 $this->assertEquals( $expectedLast, $wasLast );