38661907a5415d5106497ac7b2bcee2d6af0da4d
[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 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 'text',
26
27 'recentchanges',
28 'logging',
29
30 'page_props',
31 'pagelinks',
32 'categorylinks',
33 'langlinks',
34 'externallinks',
35 'imagelinks',
36 'templatelinks',
37 'iwlinks' ] );
38 }
39
40 protected function setUp() {
41 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
42
43 parent::setUp();
44
45 $wgExtraNamespaces[12312] = 'Dummy';
46 $wgExtraNamespaces[12313] = 'Dummy_talk';
47
48 $wgNamespaceContentModels[12312] = 'DUMMY';
49 $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
50
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
58 );
59 }
60
61 $this->tablesUsed[] = 'archive';
62 }
63
64 protected function tearDown() {
65 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
66
67 parent::tearDown();
68
69 unset( $wgExtraNamespaces[12312] );
70 unset( $wgExtraNamespaces[12313] );
71
72 unset( $wgNamespaceContentModels[12312] );
73 unset( $wgContentHandlers['DUMMY'] );
74
75 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
76 $wgContLang->resetNamespaces(); # reset namespace cache
77 }
78
79 protected function makeRevision( $props = null ) {
80 if ( $props === null ) {
81 $props = [];
82 }
83
84 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
85 $props['text'] = 'Lorem Ipsum';
86 }
87
88 if ( !isset( $props['comment'] ) ) {
89 $props['comment'] = 'just a test';
90 }
91
92 if ( !isset( $props['page'] ) ) {
93 $props['page'] = $this->the_page->getId();
94 }
95
96 $rev = new Revision( $props );
97
98 $dbw = wfGetDB( DB_MASTER );
99 $rev->insertOn( $dbw );
100
101 return $rev;
102 }
103
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 )
108 ) {
109 $ns = $this->getDefaultWikitextNS();
110 $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
111 }
112
113 $page = Title::newFromText( $page );
114 }
115
116 if ( $page instanceof Title ) {
117 $page = new WikiPage( $page );
118 }
119
120 if ( $page->exists() ) {
121 $page->doDeleteArticle( "done" );
122 }
123
124 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
125 $page->doEditContent( $content, "testing", EDIT_NEW );
126
127 return $page;
128 }
129
130 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
131 $this->assertNotNull( $rev, 'missing revision' );
132
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() );
140 }
141
142 /**
143 * @covers Revision::__construct
144 */
145 public function testConstructFromRow() {
146 $orig = $this->makeRevision();
147
148 $dbr = wfGetDB( DB_SLAVE );
149 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
150 $this->assertTrue( is_object( $res ), 'query failed' );
151
152 $row = $res->fetchObject();
153 $res->free();
154
155 $rev = new Revision( $row );
156
157 $this->assertRevEquals( $orig, $rev );
158 }
159
160 /**
161 * @covers Revision::newFromRow
162 */
163 public function testNewFromRow() {
164 $orig = $this->makeRevision();
165
166 $dbr = wfGetDB( DB_SLAVE );
167 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
168 $this->assertTrue( is_object( $res ), 'query failed' );
169
170 $row = $res->fetchObject();
171 $res->free();
172
173 $rev = Revision::newFromRow( $row );
174
175 $this->assertRevEquals( $orig, $rev );
176 }
177
178 /**
179 * @covers Revision::newFromArchiveRow
180 */
181 public function testNewFromArchiveRow() {
182 $page = $this->createPage(
183 'RevisionStorageTest_testNewFromArchiveRow',
184 'Lorem Ipsum',
185 CONTENT_MODEL_WIKITEXT
186 );
187 $orig = $page->getRevision();
188 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
189
190 $dbr = wfGetDB( DB_SLAVE );
191 $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $orig->getId() ] );
192 $this->assertTrue( is_object( $res ), 'query failed' );
193
194 $row = $res->fetchObject();
195 $res->free();
196
197 $rev = Revision::newFromArchiveRow( $row );
198
199 $this->assertRevEquals( $orig, $rev );
200 }
201
202 /**
203 * @covers Revision::newFromId
204 */
205 public function testNewFromId() {
206 $orig = $this->makeRevision();
207
208 $rev = Revision::newFromId( $orig->getId() );
209
210 $this->assertRevEquals( $orig, $rev );
211 }
212
213 /**
214 * @covers Revision::fetchRevision
215 */
216 public function testFetchRevision() {
217 $page = $this->createPage(
218 'RevisionStorageTest_testFetchRevision',
219 'one',
220 CONTENT_MODEL_WIKITEXT
221 );
222
223 // Hidden process cache assertion below
224 $page->getRevision()->getId();
225
226 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
227 $id = $page->getRevision()->getId();
228
229 $res = Revision::fetchRevision( $page->getTitle() );
230
231 # note: order is unspecified
232 $rows = [];
233 while ( ( $row = $res->fetchObject() ) ) {
234 $rows[$row->rev_id] = $row;
235 }
236
237 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
238 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
239 }
240
241 /**
242 * @covers Revision::selectFields
243 */
244 public function testSelectFields() {
245 global $wgContentHandlerUseDB;
246
247 $fields = Revision::selectFields();
248
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'
254 );
255 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
256
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' );
262 }
263 }
264
265 /**
266 * @covers Revision::getPage
267 */
268 public function testGetPage() {
269 $page = $this->the_page;
270
271 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
272 $rev = Revision::newFromId( $orig->getId() );
273
274 $this->assertEquals( $page->getId(), $rev->getPage() );
275 }
276
277 /**
278 * @covers Revision::getText
279 */
280 public function testGetText() {
281 $this->hideDeprecated( 'Revision::getText' );
282
283 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
284 $rev = Revision::newFromId( $orig->getId() );
285
286 $this->assertEquals( 'hello hello.', $rev->getText() );
287 }
288
289 /**
290 * @covers Revision::getContent
291 */
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 ] );
298
299 $this->assertNull( $rev->getContent(),
300 "getContent() should return null if the revision's text blob could not be loaded." );
301
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." );
305 }
306
307 /**
308 * @covers Revision::getContent
309 */
310 public function testGetContent() {
311 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
312 $rev = Revision::newFromId( $orig->getId() );
313
314 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
315 }
316
317 /**
318 * @covers Revision::getContentModel
319 */
320 public function testGetContentModel_default_model() {
321 global $wgContentHandlerUseDB;
322
323 if ( !$wgContentHandlerUseDB ) {
324 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
325 }
326
327 $orig = $this->makeRevision( [ 'text' => 'hello hello.', ] );
328
329 // Change namespace default content model, to make sure getContentModel() uses the
330 // page content model, not the namespace default.
331 $ns = $this->getDefaultWikitextNS();
332 $this->mergeMwGlobalArrayValue( 'wgNamespaceContentModels', [ $ns => CONTENT_MODEL_JSON ] );
333
334 $rev = Revision::newFromId( $orig->getId() );
335
336 // we should still get the correct content model from the page table
337 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $rev->getContentModel() );
338
339 // check that we don't null the revision model if it's equal to the default, but not to
340 // the current page model.
341 $second = $this->makeRevision( [
342 'text' => '{ "say":"hello" }',
343 'content_model' => CONTENT_MODEL_JAVASCRIPT
344 ] );
345
346 $rev = Revision::newFromId( $second->getId() );
347
348 // we should now get the content model from the revision table
349 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
350 }
351
352 /**
353 * @covers Revision::getContentModel
354 */
355 public function testGetContentModel_other_model() {
356 global $wgContentHandlerUseDB;
357
358 if ( !$wgContentHandlerUseDB ) {
359 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
360 }
361
362 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
363 'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
364 $rev = Revision::newFromId( $orig->getId() );
365
366 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
367 }
368
369 /**
370 * @covers Revision::getContentFormat
371 */
372 public function testGetContentFormat() {
373 global $wgContentHandlerUseDB;
374
375 if ( !$wgContentHandlerUseDB ) {
376 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
377 }
378
379 $orig = $this->makeRevision( [
380 'text' => 'hello hello.',
381 'content_model' => CONTENT_MODEL_JAVASCRIPT,
382 'content_format' => CONTENT_FORMAT_JAVASCRIPT
383 ] );
384 $rev = Revision::newFromId( $orig->getId() );
385
386 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
387 }
388
389 /**
390 * @covers Revision::isCurrent
391 */
392 public function testIsCurrent() {
393 $page = $this->createPage(
394 'RevisionStorageTest_testIsCurrent',
395 'Lorem Ipsum',
396 CONTENT_MODEL_WIKITEXT
397 );
398 $rev1 = $page->getRevision();
399
400 # @todo find out if this should be true
401 # $this->assertTrue( $rev1->isCurrent() );
402
403 $rev1x = Revision::newFromId( $rev1->getId() );
404 $this->assertTrue( $rev1x->isCurrent() );
405
406 $page->doEditContent(
407 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
408 'second rev'
409 );
410 $rev2 = $page->getRevision();
411
412 # @todo find out if this should be true
413 # $this->assertTrue( $rev2->isCurrent() );
414
415 $rev1x = Revision::newFromId( $rev1->getId() );
416 $this->assertFalse( $rev1x->isCurrent() );
417
418 $rev2x = Revision::newFromId( $rev2->getId() );
419 $this->assertTrue( $rev2x->isCurrent() );
420 }
421
422 /**
423 * @covers Revision::getPrevious
424 */
425 public function testGetPrevious() {
426 $page = $this->createPage(
427 'RevisionStorageTest_testGetPrevious',
428 'Lorem Ipsum testGetPrevious',
429 CONTENT_MODEL_WIKITEXT
430 );
431 $rev1 = $page->getRevision();
432
433 $this->assertNull( $rev1->getPrevious() );
434
435 $page->doEditContent(
436 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
437 'second rev testGetPrevious' );
438 $rev2 = $page->getRevision();
439
440 $this->assertNotNull( $rev2->getPrevious() );
441 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
442 }
443
444 /**
445 * @covers Revision::getNext
446 */
447 public function testGetNext() {
448 $page = $this->createPage(
449 'RevisionStorageTest_testGetNext',
450 'Lorem Ipsum testGetNext',
451 CONTENT_MODEL_WIKITEXT
452 );
453 $rev1 = $page->getRevision();
454
455 $this->assertNull( $rev1->getNext() );
456
457 $page->doEditContent(
458 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
459 'second rev testGetNext'
460 );
461 $rev2 = $page->getRevision();
462
463 $this->assertNotNull( $rev1->getNext() );
464 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
465 }
466
467 /**
468 * @covers Revision::newNullRevision
469 */
470 public function testNewNullRevision() {
471 $page = $this->createPage(
472 'RevisionStorageTest_testNewNullRevision',
473 'some testing text',
474 CONTENT_MODEL_WIKITEXT
475 );
476 $orig = $page->getRevision();
477
478 $dbw = wfGetDB( DB_MASTER );
479 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
480
481 $this->assertNotEquals( $orig->getId(), $rev->getId(),
482 'new null revision shold have a different id from the original revision' );
483 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
484 'new null revision shold have the same text id as the original revision' );
485 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
486 }
487
488 public static function provideUserWasLastToEdit() {
489 return [
490 [ # 0
491 3, true, # actually the last edit
492 ],
493 [ # 1
494 2, true, # not the current edit, but still by this user
495 ],
496 [ # 2
497 1, false, # edit by another user
498 ],
499 [ # 3
500 0, false, # first edit, by this user, but another user edited in the mean time
501 ],
502 ];
503 }
504
505 /**
506 * @dataProvider provideUserWasLastToEdit
507 */
508 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
509 $userA = User::newFromName( "RevisionStorageTest_userA" );
510 $userB = User::newFromName( "RevisionStorageTest_userB" );
511
512 if ( $userA->getId() === 0 ) {
513 $userA = User::createNew( $userA->getName() );
514 }
515
516 if ( $userB->getId() === 0 ) {
517 $userB = User::createNew( $userB->getName() );
518 }
519
520 $ns = $this->getDefaultWikitextNS();
521
522 $dbw = wfGetDB( DB_MASTER );
523 $revisions = [];
524
525 // create revisions -----------------------------
526 $page = WikiPage::factory( Title::newFromText(
527 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
528 $page->insertOn( $dbw );
529
530 # zero
531 $revisions[0] = new Revision( [
532 'page' => $page->getId(),
533 // we need the title to determine the page's default content model
534 'title' => $page->getTitle(),
535 'timestamp' => '20120101000000',
536 'user' => $userA->getId(),
537 'text' => 'zero',
538 'content_model' => CONTENT_MODEL_WIKITEXT,
539 'summary' => 'edit zero'
540 ] );
541 $revisions[0]->insertOn( $dbw );
542
543 # one
544 $revisions[1] = new Revision( [
545 'page' => $page->getId(),
546 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
547 'title' => $page->getTitle(),
548 'timestamp' => '20120101000100',
549 'user' => $userA->getId(),
550 'text' => 'one',
551 'content_model' => CONTENT_MODEL_WIKITEXT,
552 'summary' => 'edit one'
553 ] );
554 $revisions[1]->insertOn( $dbw );
555
556 # two
557 $revisions[2] = new Revision( [
558 'page' => $page->getId(),
559 'title' => $page->getTitle(),
560 'timestamp' => '20120101000200',
561 'user' => $userB->getId(),
562 'text' => 'two',
563 'content_model' => CONTENT_MODEL_WIKITEXT,
564 'summary' => 'edit two'
565 ] );
566 $revisions[2]->insertOn( $dbw );
567
568 # three
569 $revisions[3] = new Revision( [
570 'page' => $page->getId(),
571 'title' => $page->getTitle(),
572 'timestamp' => '20120101000300',
573 'user' => $userA->getId(),
574 'text' => 'three',
575 'content_model' => CONTENT_MODEL_WIKITEXT,
576 'summary' => 'edit three'
577 ] );
578 $revisions[3]->insertOn( $dbw );
579
580 # four
581 $revisions[4] = new Revision( [
582 'page' => $page->getId(),
583 'title' => $page->getTitle(),
584 'timestamp' => '20120101000200',
585 'user' => $userA->getId(),
586 'text' => 'zero',
587 'content_model' => CONTENT_MODEL_WIKITEXT,
588 'summary' => 'edit four'
589 ] );
590 $revisions[4]->insertOn( $dbw );
591
592 // test it ---------------------------------
593 $since = $revisions[$sinceIdx]->getTimestamp();
594
595 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
596
597 $this->assertEquals( $expectedLast, $wasLast );
598 }
599 }