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