Merge "Make Special:Email an alias for Special:EmailUser"
[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 /**
15 * @dataProvider provideExtractSectionTitle
16 * @covers EditPage::extractSectionTitle
17 */
18 public function testExtractSectionTitle( $section, $title ) {
19 $extracted = EditPage::extractSectionTitle( $section );
20 $this->assertEquals( $title, $extracted );
21 }
22
23 public static function provideExtractSectionTitle() {
24 return array(
25 array(
26 "== Test ==\n\nJust a test section.",
27 "Test"
28 ),
29 array(
30 "An initial section, no header.",
31 false
32 ),
33 array(
34 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
35 false
36 ),
37 array(
38 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
39 "Section"
40 ),
41 array(
42 "== Section== \t\r\n followed by whitespace (bug 35051)",
43 'Section',
44 ),
45 );
46 }
47
48 protected function forceRevisionDate( WikiPage $page, $timestamp ) {
49 $dbw = wfGetDB( DB_MASTER );
50
51 $dbw->update( 'revision',
52 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
53 array( 'rev_id' => $page->getLatest() ) );
54
55 $page->clear();
56 }
57
58 /**
59 * User input text is passed to rtrim() by edit page. This is a simple
60 * wrapper around assertEquals() which calls rrtrim() to normalize the
61 * expected and actual texts.
62 */
63 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
64 return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
65 }
66
67 /**
68 * Performs an edit and checks the result.
69 *
70 * @param string|Title $title The title of the page to edit
71 * @param string|null $baseText Some text to create the page with before attempting the edit.
72 * @param User|string|null $user The user to perform the edit as.
73 * @param array $edit An array of request parameters used to define the edit to perform.
74 * Some well known fields are:
75 * * wpTextbox1: the text to submit
76 * * wpSummary: the edit summary
77 * * wpEditToken: the edit token (will be inserted if not provided)
78 * * wpEdittime: timestamp of the edit's base revision (will be inserted
79 * if not provided)
80 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
81 * * wpSectionTitle: the section to edit
82 * * wpMinorEdit: mark as minor edit
83 * * wpWatchthis: whether to watch the page
84 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
85 * Set to null to skip the check. Defaults to EditPage::AS_OK.
86 * @param string|null $expectedText The text expected to be on the page after the edit.
87 * Set to null to skip the check.
88 * @param string|null $message An optional message to show along with any error message.
89 *
90 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
91 */
92 protected function assertEdit( $title, $baseText, $user = null, array $edit,
93 $expectedCode = EditPage::AS_OK, $expectedText = null, $message = null
94 ) {
95 if ( is_string( $title ) ) {
96 $ns = $this->getDefaultWikitextNS();
97 $title = Title::newFromText( $title, $ns );
98 }
99 $this->assertNotNull( $title );
100
101 if ( is_string( $user ) ) {
102 $user = User::newFromName( $user );
103
104 if ( $user->getId() === 0 ) {
105 $user->addToDatabase();
106 }
107 }
108
109 $page = WikiPage::factory( $title );
110
111 if ( $baseText !== null ) {
112 $content = ContentHandler::makeContent( $baseText, $title );
113 $page->doEditContent( $content, "base text for test" );
114 $this->forceRevisionDate( $page, '20120101000000' );
115
116 //sanity check
117 $page->clear();
118 $currentText = ContentHandler::getContentText( $page->getContent() );
119
120 # EditPage rtrim() the user input, so we alter our expected text
121 # to reflect that.
122 $this->assertEditedTextEquals( $baseText, $currentText );
123 }
124
125 if ( $user == null ) {
126 $user = $GLOBALS['wgUser'];
127 } else {
128 $this->setMwGlobals( 'wgUser', $user );
129 }
130
131 if ( !isset( $edit['wpEditToken'] ) ) {
132 $edit['wpEditToken'] = $user->getEditToken();
133 }
134
135 if ( !isset( $edit['wpEdittime'] ) ) {
136 $edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : '';
137 }
138
139 if ( !isset( $edit['wpStarttime'] ) ) {
140 $edit['wpStarttime'] = wfTimestampNow();
141 }
142
143 $req = new FauxRequest( $edit, true ); // session ??
144
145 $article = new Article( $title );
146 $article->getContext()->setTitle( $title );
147 $ep = new EditPage( $article );
148 $ep->setContextTitle( $title );
149 $ep->importFormData( $req );
150
151 $bot = isset( $edit['bot'] ) ? (bool)$edit['bot'] : false;
152
153 // this is where the edit happens!
154 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
155 // and throws exceptions like PermissionsError
156 $status = $ep->internalAttemptSave( $result, $bot );
157
158 if ( $expectedCode !== null ) {
159 // check edit code
160 $this->assertEquals( $expectedCode, $status->value,
161 "Expected result code mismatch. $message" );
162 }
163
164 $page = WikiPage::factory( $title );
165
166 if ( $expectedText !== null ) {
167 // check resulting page text
168 $content = $page->getContent();
169 $text = ContentHandler::getContentText( $content );
170
171 # EditPage rtrim() the user input, so we alter our expected text
172 # to reflect that.
173 $this->assertEditedTextEquals( $expectedText, $text,
174 "Expected article text mismatch. $message" );
175 }
176
177 return $page;
178 }
179
180 /**
181 * @todo split into a dataprovider and test method
182 * @covers EditPage
183 */
184 public function testCreatePage() {
185 $this->assertEdit(
186 'EditPageTest_testCreatePage',
187 null,
188 null,
189 array(
190 'wpTextbox1' => "Hello World!",
191 ),
192 EditPage::AS_SUCCESS_NEW_ARTICLE,
193 "Hello World!",
194 "expected article being created"
195 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
196
197 $this->assertEdit(
198 'EditPageTest_testCreatePage',
199 null,
200 null,
201 array(
202 'wpTextbox1' => "",
203 ),
204 EditPage::AS_BLANK_ARTICLE,
205 null,
206 "expected article not being created if empty"
207 );
208
209 $this->assertEdit(
210 'MediaWiki:January',
211 null,
212 'UTSysop',
213 array(
214 'wpTextbox1' => "Not January",
215 ),
216 EditPage::AS_SUCCESS_NEW_ARTICLE,
217 "Not January",
218 "expected MediaWiki: page being created"
219 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
220
221 $this->assertEdit(
222 'MediaWiki:EditPageTest_testCreatePage',
223 null,
224 'UTSysop',
225 array(
226 'wpTextbox1' => "",
227 ),
228 EditPage::AS_BLANK_ARTICLE,
229 null,
230 "expected not-registered MediaWiki: page not being created if empty"
231 );
232
233 $this->assertEdit(
234 'MediaWiki:January',
235 null,
236 'UTSysop',
237 array(
238 'wpTextbox1' => "",
239 ),
240 EditPage::AS_SUCCESS_NEW_ARTICLE,
241 "",
242 "expected registered MediaWiki: page being created even if empty"
243 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
244
245 $this->assertEdit(
246 'MediaWiki:Ipb-default-expiry',
247 null,
248 'UTSysop',
249 array(
250 'wpTextbox1' => "",
251 ),
252 EditPage::AS_BLANK_ARTICLE,
253 "",
254 "expected registered MediaWiki: page whose default content is empty not being created if empty"
255 );
256
257 $this->assertEdit(
258 'MediaWiki:January',
259 null,
260 'UTSysop',
261 array(
262 'wpTextbox1' => "January",
263 ),
264 EditPage::AS_BLANK_ARTICLE,
265 null,
266 "expected MediaWiki: page not being created if text equals default message"
267 );
268 }
269
270 public function testUpdatePage() {
271 $text = "one";
272 $edit = array(
273 'wpTextbox1' => $text,
274 'wpSummary' => 'first update',
275 );
276
277 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
278 EditPage::AS_SUCCESS_UPDATE, $text,
279 "expected successfull update with given text" );
280
281 $this->forceRevisionDate( $page, '20120101000000' );
282
283 $text = "two";
284 $edit = array(
285 'wpTextbox1' => $text,
286 'wpSummary' => 'second update',
287 );
288
289 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
290 EditPage::AS_SUCCESS_UPDATE, $text,
291 "expected successfull update with given text" );
292 }
293
294 public static function provideSectionEdit() {
295 $text = 'Intro
296
297 == one ==
298 first section.
299
300 == two ==
301 second section.
302 ';
303
304 $sectionOne = '== one ==
305 hello
306 ';
307
308 $newSection = '== new section ==
309
310 hello
311 ';
312
313 $textWithNewSectionOne = preg_replace(
314 '/== one ==.*== two ==/ms',
315 "$sectionOne\n== two ==", $text
316 );
317
318 $textWithNewSectionAdded = "$text\n$newSection";
319
320 return array(
321 array( #0
322 $text,
323 '',
324 'hello',
325 'replace all',
326 'hello'
327 ),
328
329 array( #1
330 $text,
331 '1',
332 $sectionOne,
333 'replace first section',
334 $textWithNewSectionOne,
335 ),
336
337 array( #2
338 $text,
339 'new',
340 'hello',
341 'new section',
342 $textWithNewSectionAdded,
343 ),
344 );
345 }
346
347 /**
348 * @dataProvider provideSectionEdit
349 * @covers EditPage
350 */
351 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
352 $edit = array(
353 'wpTextbox1' => $text,
354 'wpSummary' => $summary,
355 'wpSection' => $section,
356 );
357
358 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
359 EditPage::AS_SUCCESS_UPDATE, $expected,
360 "expected successfull update of section" );
361 }
362
363 public static function provideAutoMerge() {
364 $tests = array();
365
366 $tests[] = array( #0: plain conflict
367 "Elmo", # base edit user
368 "one\n\ntwo\n\nthree\n",
369 array( #adam's edit
370 'wpStarttime' => 1,
371 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
372 ),
373 array( #berta's edit
374 'wpStarttime' => 2,
375 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
376 ),
377 EditPage::AS_CONFLICT_DETECTED, # expected code
378 "ONE\n\ntwo\n\nthree\n", # expected text
379 'expected edit conflict', # message
380 );
381
382 $tests[] = array( #1: successful merge
383 "Elmo", # base edit user
384 "one\n\ntwo\n\nthree\n",
385 array( #adam's edit
386 'wpStarttime' => 1,
387 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
388 ),
389 array( #berta's edit
390 'wpStarttime' => 2,
391 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
392 ),
393 EditPage::AS_SUCCESS_UPDATE, # expected code
394 "ONE\n\ntwo\n\nTHREE\n", # expected text
395 'expected automatic merge', # message
396 );
397
398 $text = "Intro\n\n";
399 $text .= "== first section ==\n\n";
400 $text .= "one\n\ntwo\n\nthree\n\n";
401 $text .= "== second section ==\n\n";
402 $text .= "four\n\nfive\n\nsix\n\n";
403
404 // extract the first section.
405 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
406
407 // generate expected text after merge
408 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
409
410 $tests[] = array( #2: merge in section
411 "Elmo", # base edit user
412 $text,
413 array( #adam's edit
414 'wpStarttime' => 1,
415 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
416 'wpSection' => '1'
417 ),
418 array( #berta's edit
419 'wpStarttime' => 2,
420 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
421 'wpSection' => '1'
422 ),
423 EditPage::AS_SUCCESS_UPDATE, # expected code
424 $expected, # expected text
425 'expected automatic section merge', # message
426 );
427
428 // see whether it makes a difference who did the base edit
429 $testsWithAdam = array_map( function ( $test ) {
430 $test[0] = 'Adam'; // change base edit user
431 return $test;
432 }, $tests );
433
434 $testsWithBerta = array_map( function ( $test ) {
435 $test[0] = 'Berta'; // change base edit user
436 return $test;
437 }, $tests );
438
439 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
440 }
441
442 /**
443 * @dataProvider provideAutoMerge
444 * @covers EditPage
445 */
446 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
447 $expectedCode, $expectedText, $message = null
448 ) {
449 $this->checkHasDiff3();
450
451 //create page
452 $ns = $this->getDefaultWikitextNS();
453 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
454 $page = WikiPage::factory( $title );
455
456 if ( $page->exists() ) {
457 $page->doDeleteArticle( "clean slate for testing" );
458 }
459
460 $baseEdit = array(
461 'wpTextbox1' => $text,
462 );
463
464 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
465 $baseUser, $baseEdit, null, null, __METHOD__ );
466
467 $this->forceRevisionDate( $page, '20120101000000' );
468
469 $edittime = $page->getTimestamp();
470
471 // start timestamps for conflict detection
472 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
473 $adamsEdit['wpStarttime'] = 1;
474 }
475
476 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
477 $bertasEdit['wpStarttime'] = 2;
478 }
479
480 $starttime = wfTimestampNow();
481 $adamsTime = wfTimestamp(
482 TS_MW,
483 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime']
484 );
485 $bertasTime = wfTimestamp(
486 TS_MW,
487 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime']
488 );
489
490 $adamsEdit['wpStarttime'] = $adamsTime;
491 $bertasEdit['wpStarttime'] = $bertasTime;
492
493 $adamsEdit['wpSummary'] = 'Adam\'s edit';
494 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
495
496 $adamsEdit['wpEdittime'] = $edittime;
497 $bertasEdit['wpEdittime'] = $edittime;
498
499 // first edit
500 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
501 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
502
503 // second edit
504 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
505 $expectedCode, $expectedText, $message );
506 }
507 }