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.
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 = null, $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 'EditPageTest_testCreatePage',
275 'wpIgnoreBlankArticle' => 1,
277 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
279 "expected empty article being created"
280 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
283 public function testUpdatePage() {
286 'wpTextbox1' => $text,
287 'wpSummary' => 'first update',
290 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
291 EditPage
::AS_SUCCESS_UPDATE
, $text,
292 "expected successfull update with given text" );
294 $this->forceRevisionDate( $page, '20120101000000' );
298 'wpTextbox1' => $text,
299 'wpSummary' => 'second update',
302 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
303 EditPage
::AS_SUCCESS_UPDATE
, $text,
304 "expected successfull update with given text" );
307 public static function provideSectionEdit() {
317 $sectionOne = '== one ==
321 $newSection = '== new section ==
326 $textWithNewSectionOne = preg_replace(
327 '/== one ==.*== two ==/ms',
328 "$sectionOne\n== two ==", $text
331 $textWithNewSectionAdded = "$text\n$newSection";
346 'replace first section',
347 $textWithNewSectionOne,
355 $textWithNewSectionAdded,
361 * @dataProvider provideSectionEdit
364 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
366 'wpTextbox1' => $text,
367 'wpSummary' => $summary,
368 'wpSection' => $section,
371 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
372 EditPage
::AS_SUCCESS_UPDATE
, $expected,
373 "expected successfull update of section" );
376 public static function provideAutoMerge() {
379 $tests[] = array( #0: plain conflict
380 "Elmo", # base edit user
381 "one\n\ntwo\n\nthree\n",
384 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
388 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
390 EditPage
::AS_CONFLICT_DETECTED
, # expected code
391 "ONE\n\ntwo\n\nthree\n", # expected text
392 'expected edit conflict', # message
395 $tests[] = array( #1: successful merge
396 "Elmo", # base edit user
397 "one\n\ntwo\n\nthree\n",
400 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
404 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
406 EditPage
::AS_SUCCESS_UPDATE
, # expected code
407 "ONE\n\ntwo\n\nTHREE\n", # expected text
408 'expected automatic merge', # message
412 $text .= "== first section ==\n\n";
413 $text .= "one\n\ntwo\n\nthree\n\n";
414 $text .= "== second section ==\n\n";
415 $text .= "four\n\nfive\n\nsix\n\n";
417 // extract the first section.
418 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
420 // generate expected text after merge
421 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
423 $tests[] = array( #2: merge in section
424 "Elmo", # base edit user
428 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
433 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
436 EditPage
::AS_SUCCESS_UPDATE
, # expected code
437 $expected, # expected text
438 'expected automatic section merge', # message
441 // see whether it makes a difference who did the base edit
442 $testsWithAdam = array_map( function ( $test ) {
443 $test[0] = 'Adam'; // change base edit user
447 $testsWithBerta = array_map( function ( $test ) {
448 $test[0] = 'Berta'; // change base edit user
452 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
456 * @dataProvider provideAutoMerge
459 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
460 $expectedCode, $expectedText, $message = null
462 $this->checkHasDiff3();
465 $ns = $this->getDefaultWikitextNS();
466 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
467 $page = WikiPage
::factory( $title );
469 if ( $page->exists() ) {
470 $page->doDeleteArticle( "clean slate for testing" );
474 'wpTextbox1' => $text,
477 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
478 $baseUser, $baseEdit, null, null, __METHOD__
);
480 $this->forceRevisionDate( $page, '20120101000000' );
482 $edittime = $page->getTimestamp();
484 // start timestamps for conflict detection
485 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
486 $adamsEdit['wpStarttime'] = 1;
489 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
490 $bertasEdit['wpStarttime'] = 2;
493 $starttime = wfTimestampNow();
494 $adamsTime = wfTimestamp(
496 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
498 $bertasTime = wfTimestamp(
500 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
503 $adamsEdit['wpStarttime'] = $adamsTime;
504 $bertasEdit['wpStarttime'] = $bertasTime;
506 $adamsEdit['wpSummary'] = 'Adam\'s edit';
507 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
509 $adamsEdit['wpEdittime'] = $edittime;
510 $bertasEdit['wpEdittime'] = $edittime;
513 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
514 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
517 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
518 $expectedCode, $expectedText, $message );