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