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