Merge "Clean up database cloning for tests."
[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 {
132 $orig = $this->makeRevision();
133
134 $dbr = wfgetDB( DB_SLAVE );
135 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
136 $this->assertTrue( is_object( $res ), 'query failed' );
137
138 $row = $res->fetchObject();
139 $res->free();
140
141 $rev = new Revision( $row );
142
143 $this->assertRevEquals( $orig, $rev );
144 }
145
146 /**
147 * @covers Revision::newFromRow
148 */
149 public function testNewFromRow()
150 {
151 $orig = $this->makeRevision();
152
153 $dbr = wfgetDB( DB_SLAVE );
154 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
155 $this->assertTrue( is_object( $res ), 'query failed' );
156
157 $row = $res->fetchObject();
158 $res->free();
159
160 $rev = Revision::newFromRow( $row );
161
162 $this->assertRevEquals( $orig, $rev );
163 }
164
165
166 /**
167 * @covers Revision::newFromArchiveRow
168 */
169 public function testNewFromArchiveRow()
170 {
171 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT );
172 $orig = $page->getRevision();
173 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
174
175 $dbr = wfgetDB( DB_SLAVE );
176 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
177 $this->assertTrue( is_object( $res ), 'query failed' );
178
179 $row = $res->fetchObject();
180 $res->free();
181
182 $rev = Revision::newFromArchiveRow( $row );
183
184 $this->assertRevEquals( $orig, $rev );
185 }
186
187 /**
188 * @covers Revision::newFromId
189 */
190 public function testNewFromId()
191 {
192 $orig = $this->makeRevision();
193
194 $rev = Revision::newFromId( $orig->getId() );
195
196 $this->assertRevEquals( $orig, $rev );
197 }
198
199 /**
200 * @covers Revision::fetchRevision
201 */
202 public function testFetchRevision()
203 {
204 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one', CONTENT_MODEL_WIKITEXT );
205 $id1 = $page->getRevision()->getId();
206
207 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
208 $id2 = $page->getRevision()->getId();
209
210 $res = Revision::fetchRevision( $page->getTitle() );
211
212 #note: order is unspecified
213 $rows = array();
214 while ( ( $row = $res->fetchObject() ) ) {
215 $rows[ $row->rev_id ]= $row;
216 }
217
218 $row = $res->fetchObject();
219 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
220 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
221 }
222
223 /**
224 * @covers Revision::selectFields
225 */
226 public function testSelectFields()
227 {
228 global $wgContentHandlerUseDB;
229
230 $fields = Revision::selectFields();
231
232 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
233 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
234 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
235 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
236
237 if ( $wgContentHandlerUseDB ) {
238 $this->assertTrue( in_array( 'rev_content_model', $fields ),
239 'missing rev_content_model in list of fields');
240 $this->assertTrue( in_array( 'rev_content_format', $fields ),
241 'missing rev_content_format in list of fields');
242 }
243 }
244
245 /**
246 * @covers Revision::getPage
247 */
248 public function testGetPage()
249 {
250 $page = $this->the_page;
251
252 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
253 $rev = Revision::newFromId( $orig->getId() );
254
255 $this->assertEquals( $page->getId(), $rev->getPage() );
256 }
257
258 /**
259 * @covers Revision::getText
260 */
261 public function testGetText()
262 {
263 $this->hideDeprecated( 'Revision::getText' );
264
265 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
266 $rev = Revision::newFromId( $orig->getId() );
267
268 $this->assertEquals( 'hello hello.', $rev->getText() );
269 }
270
271 /**
272 * @covers Revision::getContent
273 */
274 public function testGetContent_failure()
275 {
276 $rev = new Revision( array(
277 'page' => $this->the_page->getId(),
278 'content_model' => $this->the_page->getContentModel(),
279 'text_id' => 123456789, // not in the test DB
280 ) );
281
282 $this->assertNull( $rev->getContent(),
283 "getContent() should return null if the revision's text blob could not be loaded." );
284
285 //NOTE: check this twice, once for lazy initialization, and once with the cached value.
286 $this->assertNull( $rev->getContent(),
287 "getContent() should return null if the revision's text blob could not be loaded." );
288 }
289
290 /**
291 * @covers Revision::getContent
292 */
293 public function testGetContent()
294 {
295 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
296 $rev = Revision::newFromId( $orig->getId() );
297
298 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
299 }
300
301 /**
302 * @covers Revision::revText
303 */
304 public function testRevText()
305 {
306 $this->hideDeprecated( 'Revision::revText' );
307 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
308 $rev = Revision::newFromId( $orig->getId() );
309
310 $this->assertEquals( 'hello hello rev.', $rev->revText() );
311 }
312
313 /**
314 * @covers Revision::getRawText
315 */
316 public function testGetRawText()
317 {
318 $this->hideDeprecated( 'Revision::getRawText' );
319
320 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
321 $rev = Revision::newFromId( $orig->getId() );
322
323 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
324 }
325
326 /**
327 * @covers Revision::getContentModel
328 */
329 public function testGetContentModel()
330 {
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 {
349 global $wgContentHandlerUseDB;
350
351 if ( !$wgContentHandlerUseDB ) {
352 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
353 }
354
355 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
356 'content_model' => CONTENT_MODEL_JAVASCRIPT,
357 'content_format' => CONTENT_FORMAT_JAVASCRIPT ) );
358 $rev = Revision::newFromId( $orig->getId() );
359
360 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
361 }
362
363 /**
364 * @covers Revision::isCurrent
365 */
366 public function testIsCurrent()
367 {
368 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT );
369 $rev1 = $page->getRevision();
370
371 # @todo: find out if this should be true
372 # $this->assertTrue( $rev1->isCurrent() );
373
374 $rev1x = Revision::newFromId( $rev1->getId() );
375 $this->assertTrue( $rev1x->isCurrent() );
376
377 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ), 'second rev' );
378 $rev2 = $page->getRevision();
379
380 # @todo: find out if this should be true
381 # $this->assertTrue( $rev2->isCurrent() );
382
383 $rev1x = Revision::newFromId( $rev1->getId() );
384 $this->assertFalse( $rev1x->isCurrent() );
385
386 $rev2x = Revision::newFromId( $rev2->getId() );
387 $this->assertTrue( $rev2x->isCurrent() );
388 }
389
390 /**
391 * @covers Revision::getPrevious
392 */
393 public function testGetPrevious()
394 {
395 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious', CONTENT_MODEL_WIKITEXT );
396 $rev1 = $page->getRevision();
397
398 $this->assertNull( $rev1->getPrevious() );
399
400 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
401 'second rev testGetPrevious' );
402 $rev2 = $page->getRevision();
403
404 $this->assertNotNull( $rev2->getPrevious() );
405 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
406 }
407
408 /**
409 * @covers Revision::getNext
410 */
411 public function testGetNext()
412 {
413 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext', CONTENT_MODEL_WIKITEXT );
414 $rev1 = $page->getRevision();
415
416 $this->assertNull( $rev1->getNext() );
417
418 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
419 'second rev testGetNext' );
420 $rev2 = $page->getRevision();
421
422 $this->assertNotNull( $rev1->getNext() );
423 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
424 }
425
426 /**
427 * @covers Revision::newNullRevision
428 */
429 public function testNewNullRevision()
430 {
431 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text', CONTENT_MODEL_WIKITEXT );
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 public static function provideUserWasLastToEdit() {
445 return array(
446 array( #0
447 3, true, # actually the last edit
448 ),
449 array( #1
450 2, true, # not the current edit, but still by this user
451 ),
452 array( #2
453 1, false, # edit by another user
454 ),
455 array( #3
456 0, false, # first edit, by this user, but another user edited in the mean time
457 ),
458 );
459 }
460
461 /**
462 * @dataProvider provideUserWasLastToEdit
463 */
464 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
465 $userA = \User::newFromName( "RevisionStorageTest_userA" );
466 $userB = \User::newFromName( "RevisionStorageTest_userB" );
467
468 if ( $userA->getId() === 0 ) {
469 $userA = \User::createNew( $userA->getName() );
470 }
471
472 if ( $userB->getId() === 0 ) {
473 $userB = \User::createNew( $userB->getName() );
474 }
475
476 $ns = $this->getDefaultWikitextNS();
477
478 $dbw = wfGetDB( DB_MASTER );
479 $revisions = array();
480
481 // create revisions -----------------------------
482 $page = WikiPage::factory( Title::newFromText(
483 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
484
485 # zero
486 $revisions[0] = new Revision( array(
487 'page' => $page->getId(),
488 'title' => $page->getTitle(), // we need the title to determine the page's default content model
489 'timestamp' => '20120101000000',
490 'user' => $userA->getId(),
491 'text' => 'zero',
492 'content_model' => CONTENT_MODEL_WIKITEXT,
493 'summary' => 'edit zero'
494 ) );
495 $revisions[0]->insertOn( $dbw );
496
497 # one
498 $revisions[1] = new Revision( array(
499 'page' => $page->getId(),
500 'title' => $page->getTitle(), // still need the title, because $page->getId() is 0 (there's no entry in the page table)
501 'timestamp' => '20120101000100',
502 'user' => $userA->getId(),
503 'text' => 'one',
504 'content_model' => CONTENT_MODEL_WIKITEXT,
505 'summary' => 'edit one'
506 ) );
507 $revisions[1]->insertOn( $dbw );
508
509 # two
510 $revisions[2] = new Revision( array(
511 'page' => $page->getId(),
512 'title' => $page->getTitle(),
513 'timestamp' => '20120101000200',
514 'user' => $userB->getId(),
515 'text' => 'two',
516 'content_model' => CONTENT_MODEL_WIKITEXT,
517 'summary' => 'edit two'
518 ) );
519 $revisions[2]->insertOn( $dbw );
520
521 # three
522 $revisions[3] = new Revision( array(
523 'page' => $page->getId(),
524 'title' => $page->getTitle(),
525 'timestamp' => '20120101000300',
526 'user' => $userA->getId(),
527 'text' => 'three',
528 'content_model' => CONTENT_MODEL_WIKITEXT,
529 'summary' => 'edit three'
530 ) );
531 $revisions[3]->insertOn( $dbw );
532
533 # four
534 $revisions[4] = new Revision( array(
535 'page' => $page->getId(),
536 'title' => $page->getTitle(),
537 'timestamp' => '20120101000200',
538 'user' => $userA->getId(),
539 'text' => 'zero',
540 'content_model' => CONTENT_MODEL_WIKITEXT,
541 'summary' => 'edit four'
542 ) );
543 $revisions[4]->insertOn( $dbw );
544
545 // test it ---------------------------------
546 $since = $revisions[ $sinceIdx ]->getTimestamp();
547
548 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
549
550 $this->assertEquals( $expectedLast, $wasLast );
551 }
552 }