27959b1d58b1a1be3b3079875df657dbaaaad644
[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 return $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 $edit = array( 'wpTextbox1' => $editText );
276 if ( $ignoreBlank ) {
277 $edit['wpIgnoreBlankArticle'] = 1;
278 }
279
280 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
281
282 if ( $expectedCode != EditPage::AS_BLANK_ARTICLE ) {
283 $page->doDeleteArticleReal( $pageTitle );
284 }
285 }
286
287 public function testUpdatePage() {
288 $text = "one";
289 $edit = array(
290 'wpTextbox1' => $text,
291 'wpSummary' => 'first update',
292 );
293
294 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
295 EditPage::AS_SUCCESS_UPDATE, $text,
296 "expected successfull update with given text" );
297
298 $this->forceRevisionDate( $page, '20120101000000' );
299
300 $text = "two";
301 $edit = array(
302 'wpTextbox1' => $text,
303 'wpSummary' => 'second update',
304 );
305
306 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
307 EditPage::AS_SUCCESS_UPDATE, $text,
308 "expected successfull update with given text" );
309 }
310
311 public static function provideSectionEdit() {
312 $text = 'Intro
313
314 == one ==
315 first section.
316
317 == two ==
318 second section.
319 ';
320
321 $sectionOne = '== one ==
322 hello
323 ';
324
325 $newSection = '== new section ==
326
327 hello
328 ';
329
330 $textWithNewSectionOne = preg_replace(
331 '/== one ==.*== two ==/ms',
332 "$sectionOne\n== two ==", $text
333 );
334
335 $textWithNewSectionAdded = "$text\n$newSection";
336
337 return array(
338 array( #0
339 $text,
340 '',
341 'hello',
342 'replace all',
343 'hello'
344 ),
345
346 array( #1
347 $text,
348 '1',
349 $sectionOne,
350 'replace first section',
351 $textWithNewSectionOne,
352 ),
353
354 array( #2
355 $text,
356 'new',
357 'hello',
358 'new section',
359 $textWithNewSectionAdded,
360 ),
361 );
362 }
363
364 /**
365 * @dataProvider provideSectionEdit
366 * @covers EditPage
367 */
368 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
369 $edit = array(
370 'wpTextbox1' => $text,
371 'wpSummary' => $summary,
372 'wpSection' => $section,
373 );
374
375 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
376 EditPage::AS_SUCCESS_UPDATE, $expected,
377 "expected successfull update of section" );
378 }
379
380 public static function provideAutoMerge() {
381 $tests = array();
382
383 $tests[] = array( #0: plain conflict
384 "Elmo", # base edit user
385 "one\n\ntwo\n\nthree\n",
386 array( #adam's edit
387 'wpStarttime' => 1,
388 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
389 ),
390 array( #berta's edit
391 'wpStarttime' => 2,
392 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
393 ),
394 EditPage::AS_CONFLICT_DETECTED, # expected code
395 "ONE\n\ntwo\n\nthree\n", # expected text
396 'expected edit conflict', # message
397 );
398
399 $tests[] = array( #1: successful merge
400 "Elmo", # base edit user
401 "one\n\ntwo\n\nthree\n",
402 array( #adam's edit
403 'wpStarttime' => 1,
404 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
405 ),
406 array( #berta's edit
407 'wpStarttime' => 2,
408 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
409 ),
410 EditPage::AS_SUCCESS_UPDATE, # expected code
411 "ONE\n\ntwo\n\nTHREE\n", # expected text
412 'expected automatic merge', # message
413 );
414
415 $text = "Intro\n\n";
416 $text .= "== first section ==\n\n";
417 $text .= "one\n\ntwo\n\nthree\n\n";
418 $text .= "== second section ==\n\n";
419 $text .= "four\n\nfive\n\nsix\n\n";
420
421 // extract the first section.
422 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
423
424 // generate expected text after merge
425 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
426
427 $tests[] = array( #2: merge in section
428 "Elmo", # base edit user
429 $text,
430 array( #adam's edit
431 'wpStarttime' => 1,
432 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
433 'wpSection' => '1'
434 ),
435 array( #berta's edit
436 'wpStarttime' => 2,
437 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
438 'wpSection' => '1'
439 ),
440 EditPage::AS_SUCCESS_UPDATE, # expected code
441 $expected, # expected text
442 'expected automatic section merge', # message
443 );
444
445 // see whether it makes a difference who did the base edit
446 $testsWithAdam = array_map( function ( $test ) {
447 $test[0] = 'Adam'; // change base edit user
448 return $test;
449 }, $tests );
450
451 $testsWithBerta = array_map( function ( $test ) {
452 $test[0] = 'Berta'; // change base edit user
453 return $test;
454 }, $tests );
455
456 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
457 }
458
459 /**
460 * @dataProvider provideAutoMerge
461 * @covers EditPage
462 */
463 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
464 $expectedCode, $expectedText, $message = null
465 ) {
466 $this->checkHasDiff3();
467
468 //create page
469 $ns = $this->getDefaultWikitextNS();
470 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
471 $page = WikiPage::factory( $title );
472
473 if ( $page->exists() ) {
474 $page->doDeleteArticle( "clean slate for testing" );
475 }
476
477 $baseEdit = array(
478 'wpTextbox1' => $text,
479 );
480
481 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
482 $baseUser, $baseEdit, null, null, __METHOD__ );
483
484 $this->forceRevisionDate( $page, '20120101000000' );
485
486 $edittime = $page->getTimestamp();
487
488 // start timestamps for conflict detection
489 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
490 $adamsEdit['wpStarttime'] = 1;
491 }
492
493 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
494 $bertasEdit['wpStarttime'] = 2;
495 }
496
497 $starttime = wfTimestampNow();
498 $adamsTime = wfTimestamp(
499 TS_MW,
500 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime']
501 );
502 $bertasTime = wfTimestamp(
503 TS_MW,
504 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime']
505 );
506
507 $adamsEdit['wpStarttime'] = $adamsTime;
508 $bertasEdit['wpStarttime'] = $bertasTime;
509
510 $adamsEdit['wpSummary'] = 'Adam\'s edit';
511 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
512
513 $adamsEdit['wpEdittime'] = $edittime;
514 $bertasEdit['wpEdittime'] = $edittime;
515
516 // first edit
517 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
518 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
519
520 // second edit
521 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
522 $expectedCode, $expectedText, $message );
523 }
524
525 /**
526 * @depends testAutoMerge
527 */
528 public function testCheckDirectEditingDisallowed_forNonTextContent() {
529 $title = Title::newFromText( 'Dummy:NonTextPageForEditPage' );
530 $page = WikiPage::factory( $title );
531
532 $article = new Article( $title );
533 $article->getContext()->setTitle( $title );
534 $ep = new EditPage( $article );
535 $ep->setContextTitle( $title );
536
537 $user = $GLOBALS['wgUser'];
538
539 $edit = array(
540 'wpTextbox1' => serialize( 'non-text content' ),
541 'wpEditToken' => $user->getEditToken(),
542 'wpEdittime' => '',
543 'wpStarttime' => wfTimestampNow()
544 );
545
546 $req = new FauxRequest( $edit, true );
547 $ep->importFormData( $req );
548
549 $this->setExpectedException(
550 'MWException',
551 'This content model is not supported: testing'
552 );
553
554 $ep->internalAttemptSave( $result, false );
555 }
556
557 }