7 * ^--- tell jenkins this test needs the database
10 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
12 class EditPageTest
extends MediaWikiLangTestCase
{
15 * @dataProvider provideExtractSectionTitle
17 function testExtractSectionTitle( $section, $title ) {
18 $extracted = EditPage
::extractSectionTitle( $section );
19 $this->assertEquals( $title, $extracted );
22 public static function provideExtractSectionTitle() {
25 "== Test ==\n\nJust a test section.",
29 "An initial section, no header.",
33 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
37 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
41 "== Section== \t\r\n followed by whitespace (bug 35051)",
47 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
48 $dbw = wfGetDB( DB_MASTER
);
50 $dbw->update( 'revision',
51 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
52 array( 'rev_id' => $page->getLatest() ) );
58 * User input text is passed to rtrim() by edit page. This is a simple
59 * wrapper around assertEquals() which calls rrtrim() to normalize the
60 * expected and actual texts.
62 function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
63 return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
67 * Performs an edit and checks the result.
69 * @param String|Title $title The title of the page to edit
70 * @param String|null $baseText Some text to create the page with before attempting the edit.
71 * @param User|String|null $user The user to perform the edit as.
72 * @param array $edit An array of request parameters used to define the edit to perform.
73 * Some well known fields are:
74 * * wpTextbox1: the text to submit
75 * * wpSummary: the edit summary
76 * * wpEditToken: the edit token (will be inserted if not provided)
77 * * wpEdittime: timestamp of the edit's base revision (will be inserted if not provided)
78 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
79 * * wpSectionTitle: the section to edit
80 * * wpMinorEdit: mark as minor edit
81 * * wpWatchthis: whether to watch the page
82 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
83 * Set to null to skip the check. Defaults to EditPage::AS_OK.
84 * @param String|null $expectedText The text expected to be on the page after the edit.
85 * Set to null to skip the check.
86 * @param String|null $message An optional message to show along with any error message.
88 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
90 protected function assertEdit( $title, $baseText, $user = null, array $edit,
91 $expectedCode = EditPage
::AS_OK
, $expectedText = null, $message = null
93 if ( is_string( $title ) ) {
94 $ns = $this->getDefaultWikitextNS();
95 $title = Title
::newFromText( $title, $ns );
98 if ( is_string( $user ) ) {
99 $user = User
::newFromName( $user );
101 if ( $user->getId() === 0 ) {
102 $user->addToDatabase();
106 $page = WikiPage
::factory( $title );
108 if ( $baseText !== null ) {
109 $content = ContentHandler
::makeContent( $baseText, $title );
110 $page->doEditContent( $content, "base text for test" );
111 $this->forceRevisionDate( $page, '20120101000000' );
115 $currentText = ContentHandler
::getContentText( $page->getContent() );
117 # EditPage rtrim() the user input, so we alter our expected text
119 $this->assertEditedTextEquals( $baseText, $currentText );
122 if ( $user == null ) {
123 $user = $GLOBALS['wgUser'];
125 $this->setMwGlobals( 'wgUser', $user );
128 if ( !isset( $edit['wpEditToken'] ) ) {
129 $edit['wpEditToken'] = $user->getEditToken();
132 if ( !isset( $edit['wpEdittime'] ) ) {
133 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
136 if ( !isset( $edit['wpStarttime'] ) ) {
137 $edit['wpStarttime'] = wfTimestampNow();
140 $req = new FauxRequest( $edit, true ); // session ??
142 $ep = new EditPage( new Article( $title ) );
143 $ep->setContextTitle( $title );
144 $ep->importFormData( $req );
146 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
148 // this is where the edit happens!
149 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
150 // and throws exceptions like PermissionsError
151 $status = $ep->internalAttemptSave( $result, $bot );
153 if ( $expectedCode !== null ) {
155 $this->assertEquals( $expectedCode, $status->value
,
156 "Expected result code mismatch. $message" );
159 $page = WikiPage
::factory( $title );
161 if ( $expectedText !== null ) {
162 // check resulting page text
163 $content = $page->getContent();
164 $text = ContentHandler
::getContentText( $content );
166 # EditPage rtrim() the user input, so we alter our expected text
168 $this->assertEditedTextEquals( $expectedText, $text,
169 "Expected article text mismatch. $message" );
175 public function testCreatePage() {
177 'EditPageTest_testCreatePage',
181 'wpTextbox1' => "Hello World!",
183 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
185 "expected article being created"
186 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
189 'EditPageTest_testCreatePage',
195 EditPage
::AS_BLANK_ARTICLE
,
197 "expected article not being created if empty"
206 'wpTextbox1' => "Not January",
208 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
210 "expected MediaWiki: page being created"
211 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
214 'MediaWiki:EditPageTest_testCreatePage',
220 EditPage
::AS_BLANK_ARTICLE
,
222 "expected not-registered MediaWiki: page not being created if empty"
232 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
234 "expected registered MediaWiki: page being created even if empty"
235 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
238 'MediaWiki:Ipb-default-expiry',
244 EditPage
::AS_BLANK_ARTICLE
,
246 "expected registered MediaWiki: page whose default content is empty not being created if empty"
254 'wpTextbox1' => "January",
256 EditPage
::AS_BLANK_ARTICLE
,
258 "expected MediaWiki: page not being created if text equals default message"
262 public function testUpdatePage() {
265 'wpTextbox1' => $text,
266 'wpSummary' => 'first update',
269 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
270 EditPage
::AS_SUCCESS_UPDATE
, $text,
271 "expected successfull update with given text" );
273 $this->forceRevisionDate( $page, '20120101000000' );
277 'wpTextbox1' => $text,
278 'wpSummary' => 'second update',
281 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
282 EditPage
::AS_SUCCESS_UPDATE
, $text,
283 "expected successfull update with given text" );
286 public static function provideSectionEdit() {
296 $sectionOne = '== one ==
300 $newSection = '== new section ==
305 $textWithNewSectionOne = preg_replace(
306 '/== one ==.*== two ==/ms',
307 "$sectionOne\n== two ==", $text
310 $textWithNewSectionAdded = "$text\n$newSection";
325 'replace first section',
326 $textWithNewSectionOne,
334 $textWithNewSectionAdded,
340 * @dataProvider provideSectionEdit
342 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
344 'wpTextbox1' => $text,
345 'wpSummary' => $summary,
346 'wpSection' => $section,
349 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
350 EditPage
::AS_SUCCESS_UPDATE
, $expected,
351 "expected successfull update of section" );
354 public static function provideAutoMerge() {
357 $tests[] = array( #0: plain conflict
358 "Elmo", # base edit user
359 "one\n\ntwo\n\nthree\n",
362 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
366 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
368 EditPage
::AS_CONFLICT_DETECTED
, # expected code
369 "ONE\n\ntwo\n\nthree\n", # expected text
370 'expected edit conflict', # message
373 $tests[] = array( #1: successful merge
374 "Elmo", # base edit user
375 "one\n\ntwo\n\nthree\n",
378 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
382 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
384 EditPage
::AS_SUCCESS_UPDATE
, # expected code
385 "ONE\n\ntwo\n\nTHREE\n", # expected text
386 'expected automatic merge', # message
390 $text .= "== first section ==\n\n";
391 $text .= "one\n\ntwo\n\nthree\n\n";
392 $text .= "== second section ==\n\n";
393 $text .= "four\n\nfive\n\nsix\n\n";
395 // extract the first section.
396 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
398 // generate expected text after merge
399 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
401 $tests[] = array( #2: merge in section
402 "Elmo", # base edit user
406 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
411 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
414 EditPage
::AS_SUCCESS_UPDATE
, # expected code
415 $expected, # expected text
416 'expected automatic section merge', # message
419 // see whether it makes a difference who did the base edit
420 $testsWithAdam = array_map( function ( $test ) {
421 $test[0] = 'Adam'; // change base edit user
425 $testsWithBerta = array_map( function ( $test ) {
426 $test[0] = 'Berta'; // change base edit user
430 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
434 * @dataProvider provideAutoMerge
436 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
437 $expectedCode, $expectedText, $message = null
439 $this->checkHasDiff3();
442 $ns = $this->getDefaultWikitextNS();
443 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
444 $page = WikiPage
::factory( $title );
446 if ( $page->exists() ) {
447 $page->doDeleteArticle( "clean slate for testing" );
451 'wpTextbox1' => $text,
454 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
455 $baseUser, $baseEdit, null, null, __METHOD__
);
457 $this->forceRevisionDate( $page, '20120101000000' );
459 $edittime = $page->getTimestamp();
461 // start timestamps for conflict detection
462 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
463 $adamsEdit['wpStarttime'] = 1;
466 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
467 $bertasEdit['wpStarttime'] = 2;
470 $starttime = wfTimestampNow();
471 $adamsTime = wfTimestamp( TS_MW
, (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime'] );
472 $bertasTime = wfTimestamp( TS_MW
, (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime'] );
474 $adamsEdit['wpStarttime'] = $adamsTime;
475 $bertasEdit['wpStarttime'] = $bertasTime;
477 $adamsEdit['wpSummary'] = 'Adam\'s edit';
478 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
480 $adamsEdit['wpEdittime'] = $edittime;
481 $bertasEdit['wpEdittime'] = $edittime;
484 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
485 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
488 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
489 $expectedCode, $expectedText, $message );