LocalisationCache: try harder to use LCStoreCDB
[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 = array(), $dataName = '' ) {
20 parent::__construct( $name, $data, $dataName );
21
22 $this->tablesUsed = array_merge( $this->tablesUsed,
23 array( '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
62 protected function tearDown() {
63 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
64
65 parent::tearDown();
66
67 unset( $wgExtraNamespaces[12312] );
68 unset( $wgExtraNamespaces[12313] );
69
70 unset( $wgNamespaceContentModels[12312] );
71 unset( $wgContentHandlers['DUMMY'] );
72
73 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
74 $wgContLang->resetNamespaces(); # reset namespace cache
75 }
76
77 protected function makeRevision( $props = null ) {
78 if ( $props === null ) {
79 $props = array();
80 }
81
82 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
83 $props['text'] = 'Lorem Ipsum';
84 }
85
86 if ( !isset( $props['comment'] ) ) {
87 $props['comment'] = 'just a test';
88 }
89
90 if ( !isset( $props['page'] ) ) {
91 $props['page'] = $this->the_page->getId();
92 }
93
94 $rev = new Revision( $props );
95
96 $dbw = wfgetDB( DB_MASTER );
97 $rev->insertOn( $dbw );
98
99 return $rev;
100 }
101
102 protected function createPage( $page, $text, $model = null ) {
103 if ( is_string( $page ) ) {
104 if ( !preg_match( '/:/', $page ) &&
105 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
106 ) {
107 $ns = $this->getDefaultWikitextNS();
108 $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
109 }
110
111 $page = Title::newFromText( $page );
112 }
113
114 if ( $page instanceof Title ) {
115 $page = new WikiPage( $page );
116 }
117
118 if ( $page->exists() ) {
119 $page->doDeleteArticle( "done" );
120 }
121
122 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
123 $page->doEditContent( $content, "testing", EDIT_NEW );
124
125 return $page;
126 }
127
128 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
129 $this->assertNotNull( $rev, 'missing revision' );
130
131 $this->assertEquals( $orig->getId(), $rev->getId() );
132 $this->assertEquals( $orig->getPage(), $rev->getPage() );
133 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
134 $this->assertEquals( $orig->getUser(), $rev->getUser() );
135 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
136 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
137 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
138 }
139
140 /**
141 * @covers Revision::__construct
142 */
143 public function testConstructFromRow() {
144 $orig = $this->makeRevision();
145
146 $dbr = wfgetDB( DB_SLAVE );
147 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
148 $this->assertTrue( is_object( $res ), 'query failed' );
149
150 $row = $res->fetchObject();
151 $res->free();
152
153 $rev = new Revision( $row );
154
155 $this->assertRevEquals( $orig, $rev );
156 }
157
158 /**
159 * @covers Revision::newFromRow
160 */
161 public function testNewFromRow() {
162 $orig = $this->makeRevision();
163
164 $dbr = wfgetDB( DB_SLAVE );
165 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
166 $this->assertTrue( is_object( $res ), 'query failed' );
167
168 $row = $res->fetchObject();
169 $res->free();
170
171 $rev = Revision::newFromRow( $row );
172
173 $this->assertRevEquals( $orig, $rev );
174 }
175
176 /**
177 * @covers Revision::newFromArchiveRow
178 */
179 public function testNewFromArchiveRow() {
180 $page = $this->createPage(
181 'RevisionStorageTest_testNewFromArchiveRow',
182 'Lorem Ipsum',
183 CONTENT_MODEL_WIKITEXT
184 );
185 $orig = $page->getRevision();
186 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
187
188 $dbr = wfgetDB( DB_SLAVE );
189 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
190 $this->assertTrue( is_object( $res ), 'query failed' );
191
192 $row = $res->fetchObject();
193 $res->free();
194
195 $rev = Revision::newFromArchiveRow( $row );
196
197 $this->assertRevEquals( $orig, $rev );
198 }
199
200 /**
201 * @covers Revision::newFromId
202 */
203 public function testNewFromId() {
204 $orig = $this->makeRevision();
205
206 $rev = Revision::newFromId( $orig->getId() );
207
208 $this->assertRevEquals( $orig, $rev );
209 }
210
211 /**
212 * @covers Revision::fetchRevision
213 */
214 public function testFetchRevision() {
215 $page = $this->createPage(
216 'RevisionStorageTest_testFetchRevision',
217 'one',
218 CONTENT_MODEL_WIKITEXT
219 );
220
221 // Hidden process cache assertion below
222 $page->getRevision()->getId();
223
224 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
225 $id = $page->getRevision()->getId();
226
227 $res = Revision::fetchRevision( $page->getTitle() );
228
229 # note: order is unspecified
230 $rows = array();
231 while ( ( $row = $res->fetchObject() ) ) {
232 $rows[$row->rev_id] = $row;
233 }
234
235 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
236 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
237 }
238
239 /**
240 * @covers Revision::selectFields
241 */
242 public function testSelectFields() {
243 global $wgContentHandlerUseDB;
244
245 $fields = Revision::selectFields();
246
247 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
248 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
249 $this->assertTrue(
250 in_array( 'rev_timestamp', $fields ),
251 'missing rev_timestamp in list of fields'
252 );
253 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
254
255 if ( $wgContentHandlerUseDB ) {
256 $this->assertTrue( in_array( 'rev_content_model', $fields ),
257 'missing rev_content_model in list of fields' );
258 $this->assertTrue( in_array( 'rev_content_format', $fields ),
259 'missing rev_content_format in list of fields' );
260 }
261 }
262
263 /**
264 * @covers Revision::getPage
265 */
266 public function testGetPage() {
267 $page = $this->the_page;
268
269 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
270 $rev = Revision::newFromId( $orig->getId() );
271
272 $this->assertEquals( $page->getId(), $rev->getPage() );
273 }
274
275 /**
276 * @covers Revision::getText
277 */
278 public function testGetText() {
279 $this->hideDeprecated( 'Revision::getText' );
280
281 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
282 $rev = Revision::newFromId( $orig->getId() );
283
284 $this->assertEquals( 'hello hello.', $rev->getText() );
285 }
286
287 /**
288 * @covers Revision::getContent
289 */
290 public function testGetContent_failure() {
291 $rev = new Revision( array(
292 'page' => $this->the_page->getId(),
293 'content_model' => $this->the_page->getContentModel(),
294 'text_id' => 123456789, // not in the test DB
295 ) );
296
297 $this->assertNull( $rev->getContent(),
298 "getContent() should return null if the revision's text blob could not be loaded." );
299
300 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
301 $this->assertNull( $rev->getContent(),
302 "getContent() should return null if the revision's text blob could not be loaded." );
303 }
304
305 /**
306 * @covers Revision::getContent
307 */
308 public function testGetContent() {
309 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
310 $rev = Revision::newFromId( $orig->getId() );
311
312 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
313 }
314
315 /**
316 * @covers Revision::getRawText
317 */
318 public function testGetRawText() {
319 $this->hideDeprecated( 'Revision::getRawText' );
320
321 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
322 $rev = Revision::newFromId( $orig->getId() );
323
324 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
325 }
326
327 /**
328 * @covers Revision::getContentModel
329 */
330 public function testGetContentModel() {
331 global $wgContentHandlerUseDB;
332
333 if ( !$wgContentHandlerUseDB ) {
334 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
335 }
336
337 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
338 'content_model' => CONTENT_MODEL_JAVASCRIPT ) );
339 $rev = Revision::newFromId( $orig->getId() );
340
341 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
342 }
343
344 /**
345 * @covers Revision::getContentFormat
346 */
347 public function testGetContentFormat() {
348 global $wgContentHandlerUseDB;
349
350 if ( !$wgContentHandlerUseDB ) {
351 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
352 }
353
354 $orig = $this->makeRevision( array(
355 'text' => 'hello hello.',
356 'content_model' => CONTENT_MODEL_JAVASCRIPT,
357 'content_format' => CONTENT_FORMAT_JAVASCRIPT
358 ) );
359 $rev = Revision::newFromId( $orig->getId() );
360
361 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
362 }
363
364 /**
365 * @covers Revision::isCurrent
366 */
367 public function testIsCurrent() {
368 $page = $this->createPage(
369 'RevisionStorageTest_testIsCurrent',
370 'Lorem Ipsum',
371 CONTENT_MODEL_WIKITEXT
372 );
373 $rev1 = $page->getRevision();
374
375 # @todo find out if this should be true
376 # $this->assertTrue( $rev1->isCurrent() );
377
378 $rev1x = Revision::newFromId( $rev1->getId() );
379 $this->assertTrue( $rev1x->isCurrent() );
380
381 $page->doEditContent(
382 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
383 'second rev'
384 );
385 $rev2 = $page->getRevision();
386
387 # @todo find out if this should be true
388 # $this->assertTrue( $rev2->isCurrent() );
389
390 $rev1x = Revision::newFromId( $rev1->getId() );
391 $this->assertFalse( $rev1x->isCurrent() );
392
393 $rev2x = Revision::newFromId( $rev2->getId() );
394 $this->assertTrue( $rev2x->isCurrent() );
395 }
396
397 /**
398 * @covers Revision::getPrevious
399 */
400 public function testGetPrevious() {
401 $page = $this->createPage(
402 'RevisionStorageTest_testGetPrevious',
403 'Lorem Ipsum testGetPrevious',
404 CONTENT_MODEL_WIKITEXT
405 );
406 $rev1 = $page->getRevision();
407
408 $this->assertNull( $rev1->getPrevious() );
409
410 $page->doEditContent(
411 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
412 'second rev testGetPrevious' );
413 $rev2 = $page->getRevision();
414
415 $this->assertNotNull( $rev2->getPrevious() );
416 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
417 }
418
419 /**
420 * @covers Revision::getNext
421 */
422 public function testGetNext() {
423 $page = $this->createPage(
424 'RevisionStorageTest_testGetNext',
425 'Lorem Ipsum testGetNext',
426 CONTENT_MODEL_WIKITEXT
427 );
428 $rev1 = $page->getRevision();
429
430 $this->assertNull( $rev1->getNext() );
431
432 $page->doEditContent(
433 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
434 'second rev testGetNext'
435 );
436 $rev2 = $page->getRevision();
437
438 $this->assertNotNull( $rev1->getNext() );
439 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
440 }
441
442 /**
443 * @covers Revision::newNullRevision
444 */
445 public function testNewNullRevision() {
446 $page = $this->createPage(
447 'RevisionStorageTest_testNewNullRevision',
448 'some testing text',
449 CONTENT_MODEL_WIKITEXT
450 );
451 $orig = $page->getRevision();
452
453 $dbw = wfGetDB( DB_MASTER );
454 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
455
456 $this->assertNotEquals( $orig->getId(), $rev->getId(),
457 'new null revision shold have a different id from the original revision' );
458 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
459 'new null revision shold have the same text id as the original revision' );
460 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
461 }
462
463 public static function provideUserWasLastToEdit() {
464 return array(
465 array( # 0
466 3, true, # actually the last edit
467 ),
468 array( # 1
469 2, true, # not the current edit, but still by this user
470 ),
471 array( # 2
472 1, false, # edit by another user
473 ),
474 array( # 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 = array();
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( array(
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( array(
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( array(
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( array(
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( array(
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 }