RevisionStorageTest: use MediaWikiTestCase methods for setting globals
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionStorageTest.php
1 <?php
2
3 /**
4 * Test class for Revision storage.
5 *
6 * @group ContentHandler
7 * @group Database
8 * ^--- important, causes temporary tables to be used instead of the real database
9 *
10 * @group medium
11 * ^--- important, causes tests not to fail with timeout
12 */
13 class RevisionStorageTest extends MediaWikiTestCase {
14 /**
15 * @var WikiPage $the_page
16 */
17 private $the_page;
18
19 public function __construct( $name = null, array $data = [], $dataName = '' ) {
20 parent::__construct( $name, $data, $dataName );
21
22 $this->tablesUsed = array_merge( $this->tablesUsed,
23 [ 'page',
24 'revision',
25 'ip_changes',
26 'text',
27
28 'recentchanges',
29 'logging',
30
31 'page_props',
32 'pagelinks',
33 'categorylinks',
34 'langlinks',
35 'externallinks',
36 'imagelinks',
37 'templatelinks',
38 'iwlinks' ] );
39 }
40
41 protected function setUp() {
42 global $wgContLang;
43
44 parent::setUp();
45
46 $this->mergeMwGlobalArrayValue(
47 'wgExtraNamespaces',
48 [
49 12312 => 'Dummy',
50 12313 => 'Dummy_talk',
51 ]
52 );
53
54 $this->mergeMwGlobalArrayValue(
55 'wgNamespaceContentModels',
56 [
57 12312 => 'DUMMY',
58 ]
59 );
60
61 $this->mergeMwGlobalArrayValue(
62 'wgContentHandlers',
63 [
64 'DUMMY' => 'DummyContentHandlerForTesting',
65 ]
66 );
67
68 MWNamespace::clearCaches();
69 // Reset namespace cache
70 $wgContLang->resetNamespaces();
71 if ( !$this->the_page ) {
72 $this->the_page = $this->createPage(
73 'RevisionStorageTest_the_page',
74 "just a dummy page",
75 CONTENT_MODEL_WIKITEXT
76 );
77 }
78
79 $this->tablesUsed[] = 'archive';
80 }
81
82 protected function tearDown() {
83 global $wgContLang;
84
85 parent::tearDown();
86
87 MWNamespace::clearCaches();
88 // Reset namespace cache
89 $wgContLang->resetNamespaces();
90 }
91
92 protected function makeRevision( $props = null ) {
93 if ( $props === null ) {
94 $props = [];
95 }
96
97 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
98 $props['text'] = 'Lorem Ipsum';
99 }
100
101 if ( !isset( $props['comment'] ) ) {
102 $props['comment'] = 'just a test';
103 }
104
105 if ( !isset( $props['page'] ) ) {
106 $props['page'] = $this->the_page->getId();
107 }
108
109 $rev = new Revision( $props );
110
111 $dbw = wfGetDB( DB_MASTER );
112 $rev->insertOn( $dbw );
113
114 return $rev;
115 }
116
117 /**
118 * @param string $titleString
119 * @param string $text
120 * @param string|null $model
121 *
122 * @return WikiPage
123 */
124 protected function createPage( $titleString, $text, $model = null ) {
125 if ( !preg_match( '/:/', $titleString ) &&
126 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
127 ) {
128 $ns = $this->getDefaultWikitextNS();
129 $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
130 }
131
132 $title = Title::newFromText( $titleString );
133 $wikipage = new WikiPage( $title );
134
135 // Delete the article if it already exists
136 if ( $wikipage->exists() ) {
137 $wikipage->doDeleteArticle( "done" );
138 }
139
140 $content = ContentHandler::makeContent( $text, $title, $model );
141 $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
142
143 return $wikipage;
144 }
145
146 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
147 $this->assertNotNull( $rev, 'missing revision' );
148
149 $this->assertEquals( $orig->getId(), $rev->getId() );
150 $this->assertEquals( $orig->getPage(), $rev->getPage() );
151 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
152 $this->assertEquals( $orig->getUser(), $rev->getUser() );
153 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
154 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
155 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
156 }
157
158 /**
159 * @covers Revision::__construct
160 */
161 public function testConstructFromRow() {
162 $orig = $this->makeRevision();
163
164 $dbr = wfGetDB( DB_REPLICA );
165 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
166 $this->assertTrue( is_object( $res ), 'query failed' );
167
168 $row = $res->fetchObject();
169 $res->free();
170
171 $rev = new Revision( $row );
172
173 $this->assertRevEquals( $orig, $rev );
174 }
175
176 /**
177 * @covers Revision::newFromTitle
178 */
179 public function testNewFromTitle_withoutId() {
180 $page = $this->createPage(
181 __METHOD__,
182 'GOAT',
183 CONTENT_MODEL_WIKITEXT
184 );
185 $latestRevId = $page->getLatest();
186
187 $rev = Revision::newFromTitle( $page->getTitle() );
188
189 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
190 $this->assertEquals( $latestRevId, $rev->getId() );
191 }
192
193 /**
194 * @covers Revision::newFromTitle
195 */
196 public function testNewFromTitle_withId() {
197 $page = $this->createPage(
198 __METHOD__,
199 'GOAT',
200 CONTENT_MODEL_WIKITEXT
201 );
202 $latestRevId = $page->getLatest();
203
204 $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId );
205
206 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
207 $this->assertEquals( $latestRevId, $rev->getId() );
208 }
209
210 /**
211 * @covers Revision::newFromTitle
212 */
213 public function testNewFromTitle_withBadId() {
214 $page = $this->createPage(
215 __METHOD__,
216 'GOAT',
217 CONTENT_MODEL_WIKITEXT
218 );
219 $latestRevId = $page->getLatest();
220
221 $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId + 1 );
222
223 $this->assertNull( $rev );
224 }
225
226 /**
227 * @covers Revision::newFromRow
228 */
229 public function testNewFromRow() {
230 $orig = $this->makeRevision();
231
232 $dbr = wfGetDB( DB_REPLICA );
233 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
234 $this->assertTrue( is_object( $res ), 'query failed' );
235
236 $row = $res->fetchObject();
237 $res->free();
238
239 $rev = Revision::newFromRow( $row );
240
241 $this->assertRevEquals( $orig, $rev );
242 }
243
244 /**
245 * @covers Revision::newFromArchiveRow
246 */
247 public function testNewFromArchiveRow() {
248 $page = $this->createPage(
249 'RevisionStorageTest_testNewFromArchiveRow',
250 'Lorem Ipsum',
251 CONTENT_MODEL_WIKITEXT
252 );
253 $orig = $page->getRevision();
254 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
255
256 $dbr = wfGetDB( DB_REPLICA );
257 $res = $dbr->select(
258 'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
259 );
260 $this->assertTrue( is_object( $res ), 'query failed' );
261
262 $row = $res->fetchObject();
263 $res->free();
264
265 $rev = Revision::newFromArchiveRow( $row );
266
267 $this->assertRevEquals( $orig, $rev );
268 }
269
270 /**
271 * @covers Revision::newFromId
272 */
273 public function testNewFromId() {
274 $orig = $this->makeRevision();
275
276 $rev = Revision::newFromId( $orig->getId() );
277
278 $this->assertRevEquals( $orig, $rev );
279 }
280
281 /**
282 * @covers Revision::fetchRevision
283 */
284 public function testFetchRevision() {
285 $page = $this->createPage(
286 'RevisionStorageTest_testFetchRevision',
287 'one',
288 CONTENT_MODEL_WIKITEXT
289 );
290
291 // Hidden process cache assertion below
292 $page->getRevision()->getId();
293
294 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
295 $id = $page->getRevision()->getId();
296
297 $res = Revision::fetchRevision( $page->getTitle() );
298
299 # note: order is unspecified
300 $rows = [];
301 while ( ( $row = $res->fetchObject() ) ) {
302 $rows[$row->rev_id] = $row;
303 }
304
305 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
306 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
307 }
308
309 /**
310 * @covers Revision::selectFields
311 */
312 public function testSelectFields() {
313 global $wgContentHandlerUseDB;
314
315 $fields = Revision::selectFields();
316
317 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
318 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
319 $this->assertTrue(
320 in_array( 'rev_timestamp', $fields ),
321 'missing rev_timestamp in list of fields'
322 );
323 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
324
325 if ( $wgContentHandlerUseDB ) {
326 $this->assertTrue( in_array( 'rev_content_model', $fields ),
327 'missing rev_content_model in list of fields' );
328 $this->assertTrue( in_array( 'rev_content_format', $fields ),
329 'missing rev_content_format in list of fields' );
330 }
331 }
332
333 /**
334 * @covers Revision::getPage
335 */
336 public function testGetPage() {
337 $page = $this->the_page;
338
339 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
340 $rev = Revision::newFromId( $orig->getId() );
341
342 $this->assertEquals( $page->getId(), $rev->getPage() );
343 }
344
345 /**
346 * @covers Revision::getContent
347 */
348 public function testGetContent_failure() {
349 $rev = new Revision( [
350 'page' => $this->the_page->getId(),
351 'content_model' => $this->the_page->getContentModel(),
352 'text_id' => 123456789, // not in the test DB
353 ] );
354
355 $this->assertNull( $rev->getContent(),
356 "getContent() should return null if the revision's text blob could not be loaded." );
357
358 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
359 $this->assertNull( $rev->getContent(),
360 "getContent() should return null if the revision's text blob could not be loaded." );
361 }
362
363 /**
364 * @covers Revision::getContent
365 */
366 public function testGetContent() {
367 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
368 $rev = Revision::newFromId( $orig->getId() );
369
370 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
371 }
372
373 /**
374 * @covers Revision::getContentModel
375 */
376 public function testGetContentModel() {
377 global $wgContentHandlerUseDB;
378
379 if ( !$wgContentHandlerUseDB ) {
380 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
381 }
382
383 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
384 'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
385 $rev = Revision::newFromId( $orig->getId() );
386
387 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
388 }
389
390 /**
391 * @covers Revision::getContentFormat
392 */
393 public function testGetContentFormat() {
394 global $wgContentHandlerUseDB;
395
396 if ( !$wgContentHandlerUseDB ) {
397 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
398 }
399
400 $orig = $this->makeRevision( [
401 'text' => 'hello hello.',
402 'content_model' => CONTENT_MODEL_JAVASCRIPT,
403 'content_format' => CONTENT_FORMAT_JAVASCRIPT
404 ] );
405 $rev = Revision::newFromId( $orig->getId() );
406
407 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
408 }
409
410 /**
411 * @covers Revision::isCurrent
412 */
413 public function testIsCurrent() {
414 $page = $this->createPage(
415 'RevisionStorageTest_testIsCurrent',
416 'Lorem Ipsum',
417 CONTENT_MODEL_WIKITEXT
418 );
419 $rev1 = $page->getRevision();
420
421 # @todo find out if this should be true
422 # $this->assertTrue( $rev1->isCurrent() );
423
424 $rev1x = Revision::newFromId( $rev1->getId() );
425 $this->assertTrue( $rev1x->isCurrent() );
426
427 $page->doEditContent(
428 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
429 'second rev'
430 );
431 $rev2 = $page->getRevision();
432
433 # @todo find out if this should be true
434 # $this->assertTrue( $rev2->isCurrent() );
435
436 $rev1x = Revision::newFromId( $rev1->getId() );
437 $this->assertFalse( $rev1x->isCurrent() );
438
439 $rev2x = Revision::newFromId( $rev2->getId() );
440 $this->assertTrue( $rev2x->isCurrent() );
441 }
442
443 /**
444 * @covers Revision::getPrevious
445 */
446 public function testGetPrevious() {
447 $page = $this->createPage(
448 'RevisionStorageTest_testGetPrevious',
449 'Lorem Ipsum testGetPrevious',
450 CONTENT_MODEL_WIKITEXT
451 );
452 $rev1 = $page->getRevision();
453
454 $this->assertNull( $rev1->getPrevious() );
455
456 $page->doEditContent(
457 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
458 'second rev testGetPrevious' );
459 $rev2 = $page->getRevision();
460
461 $this->assertNotNull( $rev2->getPrevious() );
462 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
463 }
464
465 /**
466 * @covers Revision::getNext
467 */
468 public function testGetNext() {
469 $page = $this->createPage(
470 'RevisionStorageTest_testGetNext',
471 'Lorem Ipsum testGetNext',
472 CONTENT_MODEL_WIKITEXT
473 );
474 $rev1 = $page->getRevision();
475
476 $this->assertNull( $rev1->getNext() );
477
478 $page->doEditContent(
479 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
480 'second rev testGetNext'
481 );
482 $rev2 = $page->getRevision();
483
484 $this->assertNotNull( $rev1->getNext() );
485 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
486 }
487
488 /**
489 * @covers Revision::newNullRevision
490 */
491 public function testNewNullRevision() {
492 $page = $this->createPage(
493 'RevisionStorageTest_testNewNullRevision',
494 'some testing text',
495 CONTENT_MODEL_WIKITEXT
496 );
497 $orig = $page->getRevision();
498
499 $dbw = wfGetDB( DB_MASTER );
500 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
501
502 $this->assertNotEquals( $orig->getId(), $rev->getId(),
503 'new null revision shold have a different id from the original revision' );
504 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
505 'new null revision shold have the same text id as the original revision' );
506 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
507 }
508
509 /**
510 * @covers Revision::insertOn
511 */
512 public function testInsertOn() {
513 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
514
515 $orig = $this->makeRevision( [
516 'user_text' => $ip
517 ] );
518
519 // Make sure the revision was copied to ip_changes
520 $dbr = wfGetDB( DB_REPLICA );
521 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
522 $row = $res->fetchObject();
523
524 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
525 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
526 }
527
528 public static function provideUserWasLastToEdit() {
529 yield 'actually the last edit' => [ 3, true ];
530 yield 'not the current edit, but still by this user' => [ 2, true ];
531 yield 'edit by another user' => [ 1, false ];
532 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
533 }
534
535 /**
536 * @dataProvider provideUserWasLastToEdit
537 */
538 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
539 $userA = User::newFromName( "RevisionStorageTest_userA" );
540 $userB = User::newFromName( "RevisionStorageTest_userB" );
541
542 if ( $userA->getId() === 0 ) {
543 $userA = User::createNew( $userA->getName() );
544 }
545
546 if ( $userB->getId() === 0 ) {
547 $userB = User::createNew( $userB->getName() );
548 }
549
550 $ns = $this->getDefaultWikitextNS();
551
552 $dbw = wfGetDB( DB_MASTER );
553 $revisions = [];
554
555 // create revisions -----------------------------
556 $page = WikiPage::factory( Title::newFromText(
557 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
558 $page->insertOn( $dbw );
559
560 $revisions[0] = new Revision( [
561 'page' => $page->getId(),
562 // we need the title to determine the page's default content model
563 'title' => $page->getTitle(),
564 'timestamp' => '20120101000000',
565 'user' => $userA->getId(),
566 'text' => 'zero',
567 'content_model' => CONTENT_MODEL_WIKITEXT,
568 'summary' => 'edit zero'
569 ] );
570 $revisions[0]->insertOn( $dbw );
571
572 $revisions[1] = new Revision( [
573 'page' => $page->getId(),
574 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
575 'title' => $page->getTitle(),
576 'timestamp' => '20120101000100',
577 'user' => $userA->getId(),
578 'text' => 'one',
579 'content_model' => CONTENT_MODEL_WIKITEXT,
580 'summary' => 'edit one'
581 ] );
582 $revisions[1]->insertOn( $dbw );
583
584 $revisions[2] = new Revision( [
585 'page' => $page->getId(),
586 'title' => $page->getTitle(),
587 'timestamp' => '20120101000200',
588 'user' => $userB->getId(),
589 'text' => 'two',
590 'content_model' => CONTENT_MODEL_WIKITEXT,
591 'summary' => 'edit two'
592 ] );
593 $revisions[2]->insertOn( $dbw );
594
595 $revisions[3] = new Revision( [
596 'page' => $page->getId(),
597 'title' => $page->getTitle(),
598 'timestamp' => '20120101000300',
599 'user' => $userA->getId(),
600 'text' => 'three',
601 'content_model' => CONTENT_MODEL_WIKITEXT,
602 'summary' => 'edit three'
603 ] );
604 $revisions[3]->insertOn( $dbw );
605
606 $revisions[4] = new Revision( [
607 'page' => $page->getId(),
608 'title' => $page->getTitle(),
609 'timestamp' => '20120101000200',
610 'user' => $userA->getId(),
611 'text' => 'zero',
612 'content_model' => CONTENT_MODEL_WIKITEXT,
613 'summary' => 'edit four'
614 ] );
615 $revisions[4]->insertOn( $dbw );
616
617 // test it ---------------------------------
618 $since = $revisions[$sinceIdx]->getTimestamp();
619
620 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
621
622 $this->assertEquals( $expectedLast, $wasLast );
623 }
624 }