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.
62 * @param string $expected
63 * @param string $actual
66 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
67 return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
71 * Performs an edit and checks the result.
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
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.
93 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
95 protected function assertEdit( $title, $baseText, $user = null, array $edit,
96 $expectedCode = null, $expectedText = null, $message = null
98 if ( is_string( $title ) ) {
99 $ns = $this->getDefaultWikitextNS();
100 $title = Title
::newFromText( $title, $ns );
102 $this->assertNotNull( $title );
104 if ( is_string( $user ) ) {
105 $user = User
::newFromName( $user );
107 if ( $user->getId() === 0 ) {
108 $user->addToDatabase();
112 $page = WikiPage
::factory( $title );
114 if ( $baseText !== null ) {
115 $content = ContentHandler
::makeContent( $baseText, $title );
116 $page->doEditContent( $content, "base text for test" );
117 $this->forceRevisionDate( $page, '20120101000000' );
121 $currentText = ContentHandler
::getContentText( $page->getContent() );
123 # EditPage rtrim() the user input, so we alter our expected text
125 $this->assertEditedTextEquals( $baseText, $currentText );
128 if ( $user == null ) {
129 $user = $GLOBALS['wgUser'];
131 $this->setMwGlobals( 'wgUser', $user );
134 if ( !isset( $edit['wpEditToken'] ) ) {
135 $edit['wpEditToken'] = $user->getEditToken();
138 if ( !isset( $edit['wpEdittime'] ) ) {
139 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
142 if ( !isset( $edit['wpStarttime'] ) ) {
143 $edit['wpStarttime'] = wfTimestampNow();
146 $req = new FauxRequest( $edit, true ); // session ??
148 $article = new Article( $title );
149 $article->getContext()->setTitle( $title );
150 $ep = new EditPage( $article );
151 $ep->setContextTitle( $title );
152 $ep->importFormData( $req );
154 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
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 );
161 if ( $expectedCode !== null ) {
163 $this->assertEquals( $expectedCode, $status->value
,
164 "Expected result code mismatch. $message" );
167 $page = WikiPage
::factory( $title );
169 if ( $expectedText !== null ) {
170 // check resulting page text
171 $content = $page->getContent();
172 $text = ContentHandler
::getContentText( $content );
174 # EditPage rtrim() the user input, so we alter our expected text
176 $this->assertEditedTextEquals( $expectedText, $text,
177 "Expected article text mismatch. $message" );
184 * @todo split into a dataprovider and test method
187 public function testCreatePage() {
189 'EditPageTest_testCreatePage',
193 'wpTextbox1' => "Hello World!",
195 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
197 "expected article being created"
198 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
201 'EditPageTest_testCreatePage',
207 EditPage
::AS_BLANK_ARTICLE
,
209 "expected article not being created if empty"
217 'wpTextbox1' => "Not January",
219 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
221 "expected MediaWiki: page being created"
222 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
225 'MediaWiki:EditPageTest_testCreatePage',
231 EditPage
::AS_BLANK_ARTICLE
,
233 "expected not-registered MediaWiki: page not being created if empty"
243 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
245 "expected registered MediaWiki: page being created even if empty"
246 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
249 'MediaWiki:Ipb-default-expiry',
255 EditPage
::AS_BLANK_ARTICLE
,
257 "expected registered MediaWiki: page whose default content is empty not being created if empty"
265 'wpTextbox1' => "January",
267 EditPage
::AS_BLANK_ARTICLE
,
269 "expected MediaWiki: page not being created if text equals default message"
273 'EditPageTest_testCreatePage',
278 'wpIgnoreBlankArticle' => 1,
280 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
282 "expected empty article being created"
283 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
286 public function testUpdatePage() {
289 'wpTextbox1' => $text,
290 'wpSummary' => 'first update',
293 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
294 EditPage
::AS_SUCCESS_UPDATE
, $text,
295 "expected successfull update with given text" );
297 $this->forceRevisionDate( $page, '20120101000000' );
301 'wpTextbox1' => $text,
302 'wpSummary' => 'second update',
305 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
306 EditPage
::AS_SUCCESS_UPDATE
, $text,
307 "expected successfull update with given text" );
310 public static function provideSectionEdit() {
320 $sectionOne = '== one ==
324 $newSection = '== new section ==
329 $textWithNewSectionOne = preg_replace(
330 '/== one ==.*== two ==/ms',
331 "$sectionOne\n== two ==", $text
334 $textWithNewSectionAdded = "$text\n$newSection";
349 'replace first section',
350 $textWithNewSectionOne,
358 $textWithNewSectionAdded,
364 * @dataProvider provideSectionEdit
367 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
369 'wpTextbox1' => $text,
370 'wpSummary' => $summary,
371 'wpSection' => $section,
374 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
375 EditPage
::AS_SUCCESS_UPDATE
, $expected,
376 "expected successfull update of section" );
379 public static function provideAutoMerge() {
382 $tests[] = array( #0: plain conflict
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_CONFLICT_DETECTED
, # expected code
394 "ONE\n\ntwo\n\nthree\n", # expected text
395 'expected edit conflict', # message
398 $tests[] = array( #1: successful merge
399 "Elmo", # base edit user
400 "one\n\ntwo\n\nthree\n",
403 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
407 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
409 EditPage
::AS_SUCCESS_UPDATE
, # expected code
410 "ONE\n\ntwo\n\nTHREE\n", # expected text
411 'expected automatic merge', # message
415 $text .= "== first section ==\n\n";
416 $text .= "one\n\ntwo\n\nthree\n\n";
417 $text .= "== second section ==\n\n";
418 $text .= "four\n\nfive\n\nsix\n\n";
420 // extract the first section.
421 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
423 // generate expected text after merge
424 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
426 $tests[] = array( #2: merge in section
427 "Elmo", # base edit user
431 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
436 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
439 EditPage
::AS_SUCCESS_UPDATE
, # expected code
440 $expected, # expected text
441 'expected automatic section merge', # message
444 // see whether it makes a difference who did the base edit
445 $testsWithAdam = array_map( function ( $test ) {
446 $test[0] = 'Adam'; // change base edit user
450 $testsWithBerta = array_map( function ( $test ) {
451 $test[0] = 'Berta'; // change base edit user
455 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
459 * @dataProvider provideAutoMerge
462 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
463 $expectedCode, $expectedText, $message = null
465 $this->checkHasDiff3();
468 $ns = $this->getDefaultWikitextNS();
469 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
470 $page = WikiPage
::factory( $title );
472 if ( $page->exists() ) {
473 $page->doDeleteArticle( "clean slate for testing" );
477 'wpTextbox1' => $text,
480 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
481 $baseUser, $baseEdit, null, null, __METHOD__
);
483 $this->forceRevisionDate( $page, '20120101000000' );
485 $edittime = $page->getTimestamp();
487 // start timestamps for conflict detection
488 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
489 $adamsEdit['wpStarttime'] = 1;
492 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
493 $bertasEdit['wpStarttime'] = 2;
496 $starttime = wfTimestampNow();
497 $adamsTime = wfTimestamp(
499 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
501 $bertasTime = wfTimestamp(
503 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
506 $adamsEdit['wpStarttime'] = $adamsTime;
507 $bertasEdit['wpStarttime'] = $bertasTime;
509 $adamsEdit['wpSummary'] = 'Adam\'s edit';
510 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
512 $adamsEdit['wpEdittime'] = $edittime;
513 $bertasEdit['wpEdittime'] = $edittime;
516 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
517 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
520 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
521 $expectedCode, $expectedText, $message );