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