Merge "Add logic for "tags" in ApiBase"
[lhc/web/wiklou.git] / tests / phpunit / includes / EditPageTest.php
1 <?php
2
3 /**
4 * @group Editing
5 *
6 * @group Database
7 * ^--- tell jenkins this test needs the database
8 *
9 * @group medium
10 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
11 */
12 class EditPageTest extends MediaWikiLangTestCase {
13
14 protected function setUp() {
15 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
16
17 parent::setUp();
18
19 $this->setMwGlobals( array(
20 'wgExtraNamespaces' => $wgExtraNamespaces,
21 'wgNamespaceContentModels' => $wgNamespaceContentModels,
22 'wgContentHandlers' => $wgContentHandlers,
23 'wgContLang' => $wgContLang,
24 ) );
25
26 $wgExtraNamespaces[12312] = 'Dummy';
27 $wgExtraNamespaces[12313] = 'Dummy_talk';
28
29 $wgNamespaceContentModels[12312] = "testing";
30 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
31
32 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
33 $wgContLang->resetNamespaces(); # reset namespace cache
34 }
35
36 /**
37 * @dataProvider provideExtractSectionTitle
38 * @covers EditPage::extractSectionTitle
39 */
40 public function testExtractSectionTitle( $section, $title ) {
41 $extracted = EditPage::extractSectionTitle( $section );
42 $this->assertEquals( $title, $extracted );
43 }
44
45 public static function provideExtractSectionTitle() {
46 return array(
47 array(
48 "== Test ==\n\nJust a test section.",
49 "Test"
50 ),
51 array(
52 "An initial section, no header.",
53 false
54 ),
55 array(
56 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
57 false
58 ),
59 array(
60 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
61 "Section"
62 ),
63 array(
64 "== Section== \t\r\n followed by whitespace (bug 35051)",
65 'Section',
66 ),
67 );
68 }
69
70 protected function forceRevisionDate( WikiPage $page, $timestamp ) {
71 $dbw = wfGetDB( DB_MASTER );
72
73 $dbw->update( 'revision',
74 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
75 array( 'rev_id' => $page->getLatest() ) );
76
77 $page->clear();
78 }
79
80 /**
81 * User input text is passed to rtrim() by edit page. This is a simple
82 * wrapper around assertEquals() which calls rrtrim() to normalize the
83 * expected and actual texts.
84 * @param string $expected
85 * @param string $actual
86 * @param string $msg
87 */
88 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
89 $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
90 }
91
92 /**
93 * Performs an edit and checks the result.
94 *
95 * @param string|Title $title The title of the page to edit
96 * @param string|null $baseText Some text to create the page with before attempting the edit.
97 * @param User|string|null $user The user to perform the edit as.
98 * @param array $edit An array of request parameters used to define the edit to perform.
99 * Some well known fields are:
100 * * wpTextbox1: the text to submit
101 * * wpSummary: the edit summary
102 * * wpEditToken: the edit token (will be inserted if not provided)
103 * * wpEdittime: timestamp of the edit's base revision (will be inserted
104 * if not provided)
105 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
106 * * wpSectionTitle: the section to edit
107 * * wpMinorEdit: mark as minor edit
108 * * wpWatchthis: whether to watch the page
109 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
110 * Set to null to skip the check.
111 * @param string|null $expectedText The text expected to be on the page after the edit.
112 * Set to null to skip the check.
113 * @param string|null $message An optional message to show along with any error message.
114 *
115 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
116 */
117 protected function assertEdit( $title, $baseText, $user = null, array $edit,
118 $expectedCode = null, $expectedText = null, $message = null
119 ) {
120 if ( is_string( $title ) ) {
121 $ns = $this->getDefaultWikitextNS();
122 $title = Title::newFromText( $title, $ns );
123 }
124 $this->assertNotNull( $title );
125
126 if ( is_string( $user ) ) {
127 $user = User::newFromName( $user );
128
129 if ( $user->getId() === 0 ) {
130 $user->addToDatabase();
131 }
132 }
133
134 $page = WikiPage::factory( $title );
135
136 if ( $baseText !== null ) {
137 $content = ContentHandler::makeContent( $baseText, $title );
138 $page->doEditContent( $content, "base text for test" );
139 $this->forceRevisionDate( $page, '20120101000000' );
140
141 // sanity check
142 $page->clear();
143 $currentText = ContentHandler::getContentText( $page->getContent() );
144
145 # EditPage rtrim() the user input, so we alter our expected text
146 # to reflect that.
147 $this->assertEditedTextEquals( $baseText, $currentText );
148 }
149
150 if ( $user == null ) {
151 $user = $GLOBALS['wgUser'];
152 } else {
153 $this->setMwGlobals( 'wgUser', $user );
154 }
155
156 if ( !isset( $edit['wpEditToken'] ) ) {
157 $edit['wpEditToken'] = $user->getEditToken();
158 }
159
160 if ( !isset( $edit['wpEdittime'] ) ) {
161 $edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : '';
162 }
163
164 if ( !isset( $edit['wpStarttime'] ) ) {
165 $edit['wpStarttime'] = wfTimestampNow();
166 }
167
168 $req = new FauxRequest( $edit, true ); // session ??
169
170 $article = new Article( $title );
171 $article->getContext()->setTitle( $title );
172 $ep = new EditPage( $article );
173 $ep->setContextTitle( $title );
174 $ep->importFormData( $req );
175
176 $bot = isset( $edit['bot'] ) ? (bool)$edit['bot'] : false;
177
178 // this is where the edit happens!
179 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
180 // and throws exceptions like PermissionsError
181 $status = $ep->internalAttemptSave( $result, $bot );
182
183 if ( $expectedCode !== null ) {
184 // check edit code
185 $this->assertEquals( $expectedCode, $status->value,
186 "Expected result code mismatch. $message" );
187 }
188
189 $page = WikiPage::factory( $title );
190
191 if ( $expectedText !== null ) {
192 // check resulting page text
193 $content = $page->getContent();
194 $text = ContentHandler::getContentText( $content );
195
196 # EditPage rtrim() the user input, so we alter our expected text
197 # to reflect that.
198 $this->assertEditedTextEquals( $expectedText, $text,
199 "Expected article text mismatch. $message" );
200 }
201
202 return $page;
203 }
204
205 public static function provideCreatePages() {
206 return array(
207 array( 'expected article being created',
208 'EditPageTest_testCreatePage',
209 null,
210 'Hello World!',
211 EditPage::AS_SUCCESS_NEW_ARTICLE,
212 'Hello World!'
213 ),
214 array( 'expected article not being created if empty',
215 'EditPageTest_testCreatePage',
216 null,
217 '',
218 EditPage::AS_BLANK_ARTICLE,
219 null
220 ),
221 array( 'expected MediaWiki: page being created',
222 'MediaWiki:January',
223 'UTSysop',
224 'Not January',
225 EditPage::AS_SUCCESS_NEW_ARTICLE,
226 'Not January'
227 ),
228 array( 'expected not-registered MediaWiki: page not being created if empty',
229 'MediaWiki:EditPageTest_testCreatePage',
230 'UTSysop',
231 '',
232 EditPage::AS_BLANK_ARTICLE,
233 null
234 ),
235 array( 'expected registered MediaWiki: page being created even if empty',
236 'MediaWiki:January',
237 'UTSysop',
238 '',
239 EditPage::AS_SUCCESS_NEW_ARTICLE,
240 ''
241 ),
242 array( 'expected registered MediaWiki: page whose default content is empty'
243 . ' not being created if empty',
244 'MediaWiki:Ipb-default-expiry',
245 'UTSysop',
246 '',
247 EditPage::AS_BLANK_ARTICLE,
248 ''
249 ),
250 array( 'expected MediaWiki: page not being created if text equals default message',
251 'MediaWiki:January',
252 'UTSysop',
253 'January',
254 EditPage::AS_BLANK_ARTICLE,
255 null
256 ),
257 array( 'expected empty article being created',
258 'EditPageTest_testCreatePage',
259 null,
260 '',
261 EditPage::AS_SUCCESS_NEW_ARTICLE,
262 '',
263 true
264 ),
265 );
266 }
267
268 /**
269 * @dataProvider provideCreatePages
270 * @covers EditPage
271 */
272 public function testCreatePage(
273 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
274 ) {
275 $checkId = null;
276
277 $this->setMwGlobals( 'wgHooks', array(
278 'PageContentInsertComplete' => array( function (
279 WikiPage &$page, User &$user, Content $content,
280 $summary, $minor, $u1, $u2, &$flags, Revision $revision
281 ) {
282 // types/refs checked
283 } ),
284 'PageContentSaveComplete' => array( function (
285 WikiPage &$page, User &$user, Content $content,
286 $summary, $minor, $u1, $u2, &$flags, Revision $revision,
287 Status &$status, $baseRevId
288 ) use ( &$checkId ) {
289 $checkId = $status->value['revision']->getId();
290 // types/refs checked
291 } ),
292 ) );
293
294 $edit = array( 'wpTextbox1' => $editText );
295 if ( $ignoreBlank ) {
296 $edit['wpIgnoreBlankArticle'] = 1;
297 }
298
299 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
300
301 if ( $expectedCode != EditPage::AS_BLANK_ARTICLE ) {
302 $latest = $page->getLatest();
303 $page->doDeleteArticleReal( $pageTitle );
304
305 $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
306 $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
307 }
308 }
309
310 /**
311 * @dataProvider provideCreatePages
312 * @covers EditPage
313 */
314 public function testCreatePageTrx(
315 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
316 ) {
317 $checkIds = array();
318 $this->setMwGlobals( 'wgHooks', array(
319 'PageContentInsertComplete' => array( function (
320 WikiPage &$page, User &$user, Content $content,
321 $summary, $minor, $u1, $u2, &$flags, Revision $revision
322 ) {
323 // types/refs checked
324 } ),
325 'PageContentSaveComplete' => array( function (
326 WikiPage &$page, User &$user, Content $content,
327 $summary, $minor, $u1, $u2, &$flags, Revision $revision,
328 Status &$status, $baseRevId
329 ) use ( &$checkIds ) {
330 $checkIds[] = $status->value['revision']->getId();
331 // types/refs checked
332 } ),
333 ) );
334
335 wfGetDB( DB_MASTER )->begin( __METHOD__ );
336
337 $edit = array( 'wpTextbox1' => $editText );
338 if ( $ignoreBlank ) {
339 $edit['wpIgnoreBlankArticle'] = 1;
340 }
341
342 $page = $this->assertEdit(
343 $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
344
345 $pageTitle2 = (string)$pageTitle . '/x';
346 $page2 = $this->assertEdit(
347 $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
348
349 wfGetDB( DB_MASTER )->commit( __METHOD__ );
350
351 if ( $expectedCode != EditPage::AS_BLANK_ARTICLE ) {
352 $latest = $page->getLatest();
353 $page->doDeleteArticleReal( $pageTitle );
354
355 $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
356 $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
357
358 $latest2 = $page2->getLatest();
359 $page2->doDeleteArticleReal( $pageTitle2 );
360
361 $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
362 $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
363 }
364 }
365
366 public function testUpdatePage() {
367 $text = "one";
368 $edit = array(
369 'wpTextbox1' => $text,
370 'wpSummary' => 'first update',
371 );
372
373 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
374 EditPage::AS_SUCCESS_UPDATE, $text,
375 "expected successfull update with given text" );
376
377 $this->forceRevisionDate( $page, '20120101000000' );
378
379 $text = "two";
380 $edit = array(
381 'wpTextbox1' => $text,
382 'wpSummary' => 'second update',
383 );
384
385 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
386 EditPage::AS_SUCCESS_UPDATE, $text,
387 "expected successfull update with given text" );
388 }
389
390 public static function provideSectionEdit() {
391 $text = 'Intro
392
393 == one ==
394 first section.
395
396 == two ==
397 second section.
398 ';
399
400 $sectionOne = '== one ==
401 hello
402 ';
403
404 $newSection = '== new section ==
405
406 hello
407 ';
408
409 $textWithNewSectionOne = preg_replace(
410 '/== one ==.*== two ==/ms',
411 "$sectionOne\n== two ==", $text
412 );
413
414 $textWithNewSectionAdded = "$text\n$newSection";
415
416 return array(
417 array( # 0
418 $text,
419 '',
420 'hello',
421 'replace all',
422 'hello'
423 ),
424
425 array( # 1
426 $text,
427 '1',
428 $sectionOne,
429 'replace first section',
430 $textWithNewSectionOne,
431 ),
432
433 array( # 2
434 $text,
435 'new',
436 'hello',
437 'new section',
438 $textWithNewSectionAdded,
439 ),
440 );
441 }
442
443 /**
444 * @dataProvider provideSectionEdit
445 * @covers EditPage
446 */
447 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
448 $edit = array(
449 'wpTextbox1' => $text,
450 'wpSummary' => $summary,
451 'wpSection' => $section,
452 );
453
454 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
455 EditPage::AS_SUCCESS_UPDATE, $expected,
456 "expected successfull update of section" );
457 }
458
459 public static function provideAutoMerge() {
460 $tests = array();
461
462 $tests[] = array( # 0: plain conflict
463 "Elmo", # base edit user
464 "one\n\ntwo\n\nthree\n",
465 array( # adam's edit
466 'wpStarttime' => 1,
467 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
468 ),
469 array( # berta's edit
470 'wpStarttime' => 2,
471 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
472 ),
473 EditPage::AS_CONFLICT_DETECTED, # expected code
474 "ONE\n\ntwo\n\nthree\n", # expected text
475 'expected edit conflict', # message
476 );
477
478 $tests[] = array( # 1: successful merge
479 "Elmo", # base edit user
480 "one\n\ntwo\n\nthree\n",
481 array( # adam's edit
482 'wpStarttime' => 1,
483 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
484 ),
485 array( # berta's edit
486 'wpStarttime' => 2,
487 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
488 ),
489 EditPage::AS_SUCCESS_UPDATE, # expected code
490 "ONE\n\ntwo\n\nTHREE\n", # expected text
491 'expected automatic merge', # message
492 );
493
494 $text = "Intro\n\n";
495 $text .= "== first section ==\n\n";
496 $text .= "one\n\ntwo\n\nthree\n\n";
497 $text .= "== second section ==\n\n";
498 $text .= "four\n\nfive\n\nsix\n\n";
499
500 // extract the first section.
501 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
502
503 // generate expected text after merge
504 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
505
506 $tests[] = array( # 2: merge in section
507 "Elmo", # base edit user
508 $text,
509 array( # adam's edit
510 'wpStarttime' => 1,
511 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
512 'wpSection' => '1'
513 ),
514 array( # berta's edit
515 'wpStarttime' => 2,
516 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
517 'wpSection' => '1'
518 ),
519 EditPage::AS_SUCCESS_UPDATE, # expected code
520 $expected, # expected text
521 'expected automatic section merge', # message
522 );
523
524 // see whether it makes a difference who did the base edit
525 $testsWithAdam = array_map( function ( $test ) {
526 $test[0] = 'Adam'; // change base edit user
527 return $test;
528 }, $tests );
529
530 $testsWithBerta = array_map( function ( $test ) {
531 $test[0] = 'Berta'; // change base edit user
532 return $test;
533 }, $tests );
534
535 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
536 }
537
538 /**
539 * @dataProvider provideAutoMerge
540 * @covers EditPage
541 */
542 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
543 $expectedCode, $expectedText, $message = null
544 ) {
545 $this->markTestSkippedIfNoDiff3();
546
547 // create page
548 $ns = $this->getDefaultWikitextNS();
549 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
550 $page = WikiPage::factory( $title );
551
552 if ( $page->exists() ) {
553 $page->doDeleteArticle( "clean slate for testing" );
554 }
555
556 $baseEdit = array(
557 'wpTextbox1' => $text,
558 );
559
560 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
561 $baseUser, $baseEdit, null, null, __METHOD__ );
562
563 $this->forceRevisionDate( $page, '20120101000000' );
564
565 $edittime = $page->getTimestamp();
566
567 // start timestamps for conflict detection
568 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
569 $adamsEdit['wpStarttime'] = 1;
570 }
571
572 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
573 $bertasEdit['wpStarttime'] = 2;
574 }
575
576 $starttime = wfTimestampNow();
577 $adamsTime = wfTimestamp(
578 TS_MW,
579 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime']
580 );
581 $bertasTime = wfTimestamp(
582 TS_MW,
583 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime']
584 );
585
586 $adamsEdit['wpStarttime'] = $adamsTime;
587 $bertasEdit['wpStarttime'] = $bertasTime;
588
589 $adamsEdit['wpSummary'] = 'Adam\'s edit';
590 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
591
592 $adamsEdit['wpEdittime'] = $edittime;
593 $bertasEdit['wpEdittime'] = $edittime;
594
595 // first edit
596 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
597 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
598
599 // second edit
600 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
601 $expectedCode, $expectedText, $message );
602 }
603
604 /**
605 * @depends testAutoMerge
606 */
607 public function testCheckDirectEditingDisallowed_forNonTextContent() {
608 $title = Title::newFromText( 'Dummy:NonTextPageForEditPage' );
609 $page = WikiPage::factory( $title );
610
611 $article = new Article( $title );
612 $article->getContext()->setTitle( $title );
613 $ep = new EditPage( $article );
614 $ep->setContextTitle( $title );
615
616 $user = $GLOBALS['wgUser'];
617
618 $edit = array(
619 'wpTextbox1' => serialize( 'non-text content' ),
620 'wpEditToken' => $user->getEditToken(),
621 'wpEdittime' => '',
622 'wpStarttime' => wfTimestampNow()
623 );
624
625 $req = new FauxRequest( $edit, true );
626 $ep->importFormData( $req );
627
628 $this->setExpectedException(
629 'MWException',
630 'This content model is not supported: testing'
631 );
632
633 $ep->internalAttemptSave( $result, false );
634 }
635
636 }