Merge "mw.notification: fix error on notif. close with $.fx.off=true"
[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()
275 {
276 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
277 $rev = Revision::newFromId( $orig->getId() );
278
279 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
280 }
281
282 /**
283 * @covers Revision::revText
284 */
285 public function testRevText()
286 {
287 $this->hideDeprecated( 'Revision::revText' );
288 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
289 $rev = Revision::newFromId( $orig->getId() );
290
291 $this->assertEquals( 'hello hello rev.', $rev->revText() );
292 }
293
294 /**
295 * @covers Revision::getRawText
296 */
297 public function testGetRawText()
298 {
299 $this->hideDeprecated( 'Revision::getRawText' );
300
301 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
302 $rev = Revision::newFromId( $orig->getId() );
303
304 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
305 }
306
307 /**
308 * @covers Revision::getContentModel
309 */
310 public function testGetContentModel()
311 {
312 global $wgContentHandlerUseDB;
313
314 if ( !$wgContentHandlerUseDB ) {
315 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
316 }
317
318 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
319 'content_model' => CONTENT_MODEL_JAVASCRIPT ) );
320 $rev = Revision::newFromId( $orig->getId() );
321
322 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
323 }
324
325 /**
326 * @covers Revision::getContentFormat
327 */
328 public function testGetContentFormat()
329 {
330 global $wgContentHandlerUseDB;
331
332 if ( !$wgContentHandlerUseDB ) {
333 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
334 }
335
336 $orig = $this->makeRevision( array( 'text' => 'hello hello.',
337 'content_model' => CONTENT_MODEL_JAVASCRIPT,
338 'content_format' => CONTENT_FORMAT_JAVASCRIPT ) );
339 $rev = Revision::newFromId( $orig->getId() );
340
341 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
342 }
343
344 /**
345 * @covers Revision::isCurrent
346 */
347 public function testIsCurrent()
348 {
349 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum', CONTENT_MODEL_WIKITEXT );
350 $rev1 = $page->getRevision();
351
352 # @todo: find out if this should be true
353 # $this->assertTrue( $rev1->isCurrent() );
354
355 $rev1x = Revision::newFromId( $rev1->getId() );
356 $this->assertTrue( $rev1x->isCurrent() );
357
358 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ), 'second rev' );
359 $rev2 = $page->getRevision();
360
361 # @todo: find out if this should be true
362 # $this->assertTrue( $rev2->isCurrent() );
363
364 $rev1x = Revision::newFromId( $rev1->getId() );
365 $this->assertFalse( $rev1x->isCurrent() );
366
367 $rev2x = Revision::newFromId( $rev2->getId() );
368 $this->assertTrue( $rev2x->isCurrent() );
369 }
370
371 /**
372 * @covers Revision::getPrevious
373 */
374 public function testGetPrevious()
375 {
376 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious', CONTENT_MODEL_WIKITEXT );
377 $rev1 = $page->getRevision();
378
379 $this->assertNull( $rev1->getPrevious() );
380
381 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
382 'second rev testGetPrevious' );
383 $rev2 = $page->getRevision();
384
385 $this->assertNotNull( $rev2->getPrevious() );
386 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
387 }
388
389 /**
390 * @covers Revision::getNext
391 */
392 public function testGetNext()
393 {
394 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext', CONTENT_MODEL_WIKITEXT );
395 $rev1 = $page->getRevision();
396
397 $this->assertNull( $rev1->getNext() );
398
399 $page->doEditContent( ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
400 'second rev testGetNext' );
401 $rev2 = $page->getRevision();
402
403 $this->assertNotNull( $rev1->getNext() );
404 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
405 }
406
407 /**
408 * @covers Revision::newNullRevision
409 */
410 public function testNewNullRevision()
411 {
412 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text', CONTENT_MODEL_WIKITEXT );
413 $orig = $page->getRevision();
414
415 $dbw = wfGetDB( DB_MASTER );
416 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
417
418 $this->assertNotEquals( $orig->getId(), $rev->getId(),
419 'new null revision shold have a different id from the original revision' );
420 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
421 'new null revision shold have the same text id as the original revision' );
422 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
423 }
424
425 public static function provideUserWasLastToEdit() {
426 return array(
427 array( #0
428 3, true, # actually the last edit
429 ),
430 array( #1
431 2, true, # not the current edit, but still by this user
432 ),
433 array( #2
434 1, false, # edit by another user
435 ),
436 array( #3
437 0, false, # first edit, by this user, but another user edited in the mean time
438 ),
439 );
440 }
441
442 /**
443 * @dataProvider provideUserWasLastToEdit
444 */
445 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
446 $userA = \User::newFromName( "RevisionStorageTest_userA" );
447 $userB = \User::newFromName( "RevisionStorageTest_userB" );
448
449 if ( $userA->getId() === 0 ) {
450 $userA = \User::createNew( $userA->getName() );
451 }
452
453 if ( $userB->getId() === 0 ) {
454 $userB = \User::createNew( $userB->getName() );
455 }
456
457 $ns = $this->getDefaultWikitextNS();
458
459 $dbw = wfGetDB( DB_MASTER );
460 $revisions = array();
461
462 // create revisions -----------------------------
463 $page = WikiPage::factory( Title::newFromText(
464 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
465
466 # zero
467 $revisions[0] = new Revision( array(
468 'page' => $page->getId(),
469 'title' => $page->getTitle(), // we need the title to determine the page's default content model
470 'timestamp' => '20120101000000',
471 'user' => $userA->getId(),
472 'text' => 'zero',
473 'content_model' => CONTENT_MODEL_WIKITEXT,
474 'summary' => 'edit zero'
475 ) );
476 $revisions[0]->insertOn( $dbw );
477
478 # one
479 $revisions[1] = new Revision( array(
480 'page' => $page->getId(),
481 'title' => $page->getTitle(), // still need the title, because $page->getId() is 0 (there's no entry in the page table)
482 'timestamp' => '20120101000100',
483 'user' => $userA->getId(),
484 'text' => 'one',
485 'content_model' => CONTENT_MODEL_WIKITEXT,
486 'summary' => 'edit one'
487 ) );
488 $revisions[1]->insertOn( $dbw );
489
490 # two
491 $revisions[2] = new Revision( array(
492 'page' => $page->getId(),
493 'title' => $page->getTitle(),
494 'timestamp' => '20120101000200',
495 'user' => $userB->getId(),
496 'text' => 'two',
497 'content_model' => CONTENT_MODEL_WIKITEXT,
498 'summary' => 'edit two'
499 ) );
500 $revisions[2]->insertOn( $dbw );
501
502 # three
503 $revisions[3] = new Revision( array(
504 'page' => $page->getId(),
505 'title' => $page->getTitle(),
506 'timestamp' => '20120101000300',
507 'user' => $userA->getId(),
508 'text' => 'three',
509 'content_model' => CONTENT_MODEL_WIKITEXT,
510 'summary' => 'edit three'
511 ) );
512 $revisions[3]->insertOn( $dbw );
513
514 # four
515 $revisions[4] = new Revision( array(
516 'page' => $page->getId(),
517 'title' => $page->getTitle(),
518 'timestamp' => '20120101000200',
519 'user' => $userA->getId(),
520 'text' => 'zero',
521 'content_model' => CONTENT_MODEL_WIKITEXT,
522 'summary' => 'edit four'
523 ) );
524 $revisions[4]->insertOn( $dbw );
525
526 // test it ---------------------------------
527 $since = $revisions[ $sinceIdx ]->getTimestamp();
528
529 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
530
531 $this->assertEquals( $expectedLast, $wasLast );
532 }
533 }