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
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() {
26 "== Test ==\n\nJust a test section.",
30 "An initial section, no header.",
34 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
38 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
42 "== Section== \t\r\n followed by whitespace (bug 35051)",
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() ) );
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 );
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
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
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' );
118 $currentText = ContentHandler
::getContentText( $page->getContent() );
120 # EditPage rtrim() the user input, so we alter our expected text
122 $this->assertEditedTextEquals( $baseText, $currentText );
125 if ( $user == null ) {
126 $user = $GLOBALS['wgUser'];
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 ) {
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
173 $this->assertEditedTextEquals( $expectedText, $text,
174 "Expected article text mismatch. $message" );
181 * @todo split into a dataprovider and test method
184 public function testCreatePage() {
186 'EditPageTest_testCreatePage',
190 'wpTextbox1' => "Hello World!",
192 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
194 "expected article being created"
195 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
198 'EditPageTest_testCreatePage',
204 EditPage
::AS_BLANK_ARTICLE
,
206 "expected article not being created if empty"
214 'wpTextbox1' => "Not January",
216 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
218 "expected MediaWiki: page being created"
219 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
222 'MediaWiki:EditPageTest_testCreatePage',
228 EditPage
::AS_BLANK_ARTICLE
,
230 "expected not-registered MediaWiki: page not being created if empty"
240 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
242 "expected registered MediaWiki: page being created even if empty"
243 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
246 'MediaWiki:Ipb-default-expiry',
252 EditPage
::AS_BLANK_ARTICLE
,
254 "expected registered MediaWiki: page whose default content is empty not being created if empty"
262 'wpTextbox1' => "January",
264 EditPage
::AS_BLANK_ARTICLE
,
266 "expected MediaWiki: page not being created if text equals default message"
270 public function testUpdatePage() {
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' );
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() {
304 $sectionOne = '== one ==
308 $newSection = '== new section ==
313 $textWithNewSectionOne = preg_replace(
314 '/== one ==.*== two ==/ms',
315 "$sectionOne\n== two ==", $text
318 $textWithNewSectionAdded = "$text\n$newSection";
333 'replace first section',
334 $textWithNewSectionOne,
342 $textWithNewSectionAdded,
348 * @dataProvider provideSectionEdit
351 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
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() {
366 $tests[] = array( #0: plain conflict
367 "Elmo", # base edit user
368 "one\n\ntwo\n\nthree\n",
371 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
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",
387 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
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
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
415 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
420 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
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
434 $testsWithBerta = array_map( function ( $test ) {
435 $test[0] = 'Berta'; // change base edit user
439 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
443 * @dataProvider provideAutoMerge
446 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
447 $expectedCode, $expectedText, $message = null
449 $this->checkHasDiff3();
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" );
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(
483 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
485 $bertasTime = wfTimestamp(
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;
500 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
501 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
504 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
505 $expectedCode, $expectedText, $message );