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