Remove old maintenance script importTextFile.php
[mediawiki.git] / tests / phpunit / includes / EditPageTest.php
blob6b8bf2720fd50ee77643dab2dd93c66bb9d8c601
1 <?php
3 /**
4 * @group Editing
6 * @group Database
7 * ^--- tell jenkins this test needs the database
9 * @group medium
10 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
12 class EditPageTest extends MediaWikiLangTestCase {
14 /**
15 * @dataProvider provideExtractSectionTitle
16 * @covers EditPage::extractSectionTitle
18 public function testExtractSectionTitle( $section, $title ) {
19 $extracted = EditPage::extractSectionTitle( $section );
20 $this->assertEquals( $title, $extracted );
23 public static function provideExtractSectionTitle() {
24 return array(
25 array(
26 "== Test ==\n\nJust a test section.",
27 "Test"
29 array(
30 "An initial section, no header.",
31 false
33 array(
34 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
35 false
37 array(
38 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
39 "Section"
41 array(
42 "== Section== \t\r\n followed by whitespace (bug 35051)",
43 'Section',
48 protected function forceRevisionDate( WikiPage $page, $timestamp ) {
49 $dbw = wfGetDB( DB_MASTER );
51 $dbw->update( 'revision',
52 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
53 array( 'rev_id' => $page->getLatest() ) );
55 $page->clear();
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.
63 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
64 return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
67 /**
68 * Performs an edit and checks the result.
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.
90 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
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 );
99 $this->assertNotNull( $title );
101 if ( is_string( $user ) ) {
102 $user = User::newFromName( $user );
104 if ( $user->getId() === 0 ) {
105 $user->addToDatabase();
109 $page = WikiPage::factory( $title );
111 if ( $baseText !== null ) {
112 $content = ContentHandler::makeContent( $baseText, $title );
113 $page->doEditContent( $content, "base text for test" );
114 $this->forceRevisionDate( $page, '20120101000000' );
116 //sanity check
117 $page->clear();
118 $currentText = ContentHandler::getContentText( $page->getContent() );
120 # EditPage rtrim() the user input, so we alter our expected text
121 # to reflect that.
122 $this->assertEditedTextEquals( $baseText, $currentText );
125 if ( $user == null ) {
126 $user = $GLOBALS['wgUser'];
127 } else {
128 $this->setMwGlobals( 'wgUser', $user );
131 if ( !isset( $edit['wpEditToken'] ) ) {
132 $edit['wpEditToken'] = $user->getEditToken();
135 if ( !isset( $edit['wpEdittime'] ) ) {
136 $edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : '';
139 if ( !isset( $edit['wpStarttime'] ) ) {
140 $edit['wpStarttime'] = wfTimestampNow();
143 $req = new FauxRequest( $edit, true ); // session ??
145 $article = new Article( $title );
146 $article->getContext()->setTitle( $title );
147 $ep = new EditPage( $article );
148 $ep->setContextTitle( $title );
149 $ep->importFormData( $req );
151 $bot = isset( $edit['bot'] ) ? (bool)$edit['bot'] : false;
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 );
158 if ( $expectedCode !== null ) {
159 // check edit code
160 $this->assertEquals( $expectedCode, $status->value,
161 "Expected result code mismatch. $message" );
164 $page = WikiPage::factory( $title );
166 if ( $expectedText !== null ) {
167 // check resulting page text
168 $content = $page->getContent();
169 $text = ContentHandler::getContentText( $content );
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" );
177 return $page;
181 * @todo split into a dataprovider and test method
182 * @covers EditPage
184 public function testCreatePage() {
185 $this->assertEdit(
186 'EditPageTest_testCreatePage',
187 null,
188 null,
189 array(
190 'wpTextbox1' => "Hello World!",
192 EditPage::AS_SUCCESS_NEW_ARTICLE,
193 "Hello World!",
194 "expected article being created"
195 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
197 $this->assertEdit(
198 'EditPageTest_testCreatePage',
199 null,
200 null,
201 array(
202 'wpTextbox1' => "",
204 EditPage::AS_BLANK_ARTICLE,
205 null,
206 "expected article not being created if empty"
209 $this->assertEdit(
210 'MediaWiki:January',
211 null,
212 'UTSysop',
213 array(
214 'wpTextbox1' => "Not January",
216 EditPage::AS_SUCCESS_NEW_ARTICLE,
217 "Not January",
218 "expected MediaWiki: page being created"
219 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
221 $this->assertEdit(
222 'MediaWiki:EditPageTest_testCreatePage',
223 null,
224 'UTSysop',
225 array(
226 'wpTextbox1' => "",
228 EditPage::AS_BLANK_ARTICLE,
229 null,
230 "expected not-registered MediaWiki: page not being created if empty"
233 $this->assertEdit(
234 'MediaWiki:January',
235 null,
236 'UTSysop',
237 array(
238 'wpTextbox1' => "",
240 EditPage::AS_SUCCESS_NEW_ARTICLE,
242 "expected registered MediaWiki: page being created even if empty"
243 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
245 $this->assertEdit(
246 'MediaWiki:Ipb-default-expiry',
247 null,
248 'UTSysop',
249 array(
250 'wpTextbox1' => "",
252 EditPage::AS_BLANK_ARTICLE,
254 "expected registered MediaWiki: page whose default content is empty not being created if empty"
257 $this->assertEdit(
258 'MediaWiki:January',
259 null,
260 'UTSysop',
261 array(
262 'wpTextbox1' => "January",
264 EditPage::AS_BLANK_ARTICLE,
265 null,
266 "expected MediaWiki: page not being created if text equals default message"
270 public function testUpdatePage() {
271 $text = "one";
272 $edit = array(
273 'wpTextbox1' => $text,
274 'wpSummary' => 'first update',
277 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
278 EditPage::AS_SUCCESS_UPDATE, $text,
279 "expected successfull update with given text" );
281 $this->forceRevisionDate( $page, '20120101000000' );
283 $text = "two";
284 $edit = array(
285 'wpTextbox1' => $text,
286 'wpSummary' => 'second update',
289 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
290 EditPage::AS_SUCCESS_UPDATE, $text,
291 "expected successfull update with given text" );
294 public static function provideSectionEdit() {
295 $text = 'Intro
297 == one ==
298 first section.
300 == two ==
301 second section.
304 $sectionOne = '== one ==
305 hello
308 $newSection = '== new section ==
310 hello
313 $textWithNewSectionOne = preg_replace(
314 '/== one ==.*== two ==/ms',
315 "$sectionOne\n== two ==", $text
318 $textWithNewSectionAdded = "$text\n$newSection";
320 return array(
321 array( #0
322 $text,
324 'hello',
325 'replace all',
326 'hello'
329 array( #1
330 $text,
331 '1',
332 $sectionOne,
333 'replace first section',
334 $textWithNewSectionOne,
337 array( #2
338 $text,
339 'new',
340 'hello',
341 'new section',
342 $textWithNewSectionAdded,
348 * @dataProvider provideSectionEdit
349 * @covers EditPage
351 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
352 $edit = array(
353 'wpTextbox1' => $text,
354 'wpSummary' => $summary,
355 'wpSection' => $section,
358 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
359 EditPage::AS_SUCCESS_UPDATE, $expected,
360 "expected successfull update of section" );
363 public static function provideAutoMerge() {
364 $tests = array();
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",
373 array( #berta's edit
374 'wpStarttime' => 2,
375 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
377 EditPage::AS_CONFLICT_DETECTED, # expected code
378 "ONE\n\ntwo\n\nthree\n", # expected text
379 'expected edit conflict', # message
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",
389 array( #berta's edit
390 'wpStarttime' => 2,
391 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
393 EditPage::AS_SUCCESS_UPDATE, # expected code
394 "ONE\n\ntwo\n\nTHREE\n", # expected text
395 'expected automatic merge', # message
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";
404 // extract the first section.
405 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
407 // generate expected text after merge
408 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
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'
418 array( #berta's edit
419 'wpStarttime' => 2,
420 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
421 'wpSection' => '1'
423 EditPage::AS_SUCCESS_UPDATE, # expected code
424 $expected, # expected text
425 'expected automatic section merge', # message
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 );
434 $testsWithBerta = array_map( function ( $test ) {
435 $test[0] = 'Berta'; // change base edit user
436 return $test;
437 }, $tests );
439 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
443 * @dataProvider provideAutoMerge
444 * @covers EditPage
446 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
447 $expectedCode, $expectedText, $message = null
449 $this->checkHasDiff3();
451 //create page
452 $ns = $this->getDefaultWikitextNS();
453 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
454 $page = WikiPage::factory( $title );
456 if ( $page->exists() ) {
457 $page->doDeleteArticle( "clean slate for testing" );
460 $baseEdit = array(
461 'wpTextbox1' => $text,
464 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
465 $baseUser, $baseEdit, null, null, __METHOD__ );
467 $this->forceRevisionDate( $page, '20120101000000' );
469 $edittime = $page->getTimestamp();
471 // start timestamps for conflict detection
472 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
473 $adamsEdit['wpStarttime'] = 1;
476 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
477 $bertasEdit['wpStarttime'] = 2;
480 $starttime = wfTimestampNow();
481 $adamsTime = wfTimestamp(
482 TS_MW,
483 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime']
485 $bertasTime = wfTimestamp(
486 TS_MW,
487 (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime']
490 $adamsEdit['wpStarttime'] = $adamsTime;
491 $bertasEdit['wpStarttime'] = $bertasTime;
493 $adamsEdit['wpSummary'] = 'Adam\'s edit';
494 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
496 $adamsEdit['wpEdittime'] = $edittime;
497 $bertasEdit['wpEdittime'] = $edittime;
499 // first edit
500 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
501 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
503 // second edit
504 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
505 $expectedCode, $expectedText, $message );