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