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 );
100 if ( is_string( $user ) ) {
101 $user = User
::newFromName( $user );
103 if ( $user->getId() === 0 ) {
104 $user->addToDatabase();
108 $page = WikiPage
::factory( $title );
110 if ( $baseText !== null ) {
111 $content = ContentHandler
::makeContent( $baseText, $title );
112 $page->doEditContent( $content, "base text for test" );
113 $this->forceRevisionDate( $page, '20120101000000' );
117 $currentText = ContentHandler
::getContentText( $page->getContent() );
119 # EditPage rtrim() the user input, so we alter our expected text
121 $this->assertEditedTextEquals( $baseText, $currentText );
124 if ( $user == null ) {
125 $user = $GLOBALS['wgUser'];
127 $this->setMwGlobals( 'wgUser', $user );
130 if ( !isset( $edit['wpEditToken'] ) ) {
131 $edit['wpEditToken'] = $user->getEditToken();
134 if ( !isset( $edit['wpEdittime'] ) ) {
135 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
138 if ( !isset( $edit['wpStarttime'] ) ) {
139 $edit['wpStarttime'] = wfTimestampNow();
142 $req = new FauxRequest( $edit, true ); // session ??
144 $ep = new EditPage( new Article( $title ) );
145 $ep->setContextTitle( $title );
146 $ep->importFormData( $req );
148 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
150 // this is where the edit happens!
151 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
152 // and throws exceptions like PermissionsError
153 $status = $ep->internalAttemptSave( $result, $bot );
155 if ( $expectedCode !== null ) {
157 $this->assertEquals( $expectedCode, $status->value
,
158 "Expected result code mismatch. $message" );
161 $page = WikiPage
::factory( $title );
163 if ( $expectedText !== null ) {
164 // check resulting page text
165 $content = $page->getContent();
166 $text = ContentHandler
::getContentText( $content );
168 # EditPage rtrim() the user input, so we alter our expected text
170 $this->assertEditedTextEquals( $expectedText, $text,
171 "Expected article text mismatch. $message" );
178 * @todo split into a dataprovider and test method
181 public function testCreatePage() {
183 'EditPageTest_testCreatePage',
187 'wpTextbox1' => "Hello World!",
189 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
191 "expected article being created"
192 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
195 'EditPageTest_testCreatePage',
201 EditPage
::AS_BLANK_ARTICLE
,
203 "expected article not being created if empty"
211 'wpTextbox1' => "Not January",
213 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
215 "expected MediaWiki: page being created"
216 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
219 'MediaWiki:EditPageTest_testCreatePage',
225 EditPage
::AS_BLANK_ARTICLE
,
227 "expected not-registered MediaWiki: page not being created if empty"
237 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
239 "expected registered MediaWiki: page being created even if empty"
240 )->doDeleteArticleReal( 'EditPageTest_testCreatePage' );
243 'MediaWiki:Ipb-default-expiry',
249 EditPage
::AS_BLANK_ARTICLE
,
251 "expected registered MediaWiki: page whose default content is empty not being created if empty"
259 'wpTextbox1' => "January",
261 EditPage
::AS_BLANK_ARTICLE
,
263 "expected MediaWiki: page not being created if text equals default message"
267 public function testUpdatePage() {
270 'wpTextbox1' => $text,
271 'wpSummary' => 'first update',
274 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
275 EditPage
::AS_SUCCESS_UPDATE
, $text,
276 "expected successfull update with given text" );
278 $this->forceRevisionDate( $page, '20120101000000' );
282 'wpTextbox1' => $text,
283 'wpSummary' => 'second update',
286 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
287 EditPage
::AS_SUCCESS_UPDATE
, $text,
288 "expected successfull update with given text" );
291 public static function provideSectionEdit() {
301 $sectionOne = '== one ==
305 $newSection = '== new section ==
310 $textWithNewSectionOne = preg_replace(
311 '/== one ==.*== two ==/ms',
312 "$sectionOne\n== two ==", $text
315 $textWithNewSectionAdded = "$text\n$newSection";
330 'replace first section',
331 $textWithNewSectionOne,
339 $textWithNewSectionAdded,
345 * @dataProvider provideSectionEdit
348 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
350 'wpTextbox1' => $text,
351 'wpSummary' => $summary,
352 'wpSection' => $section,
355 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
356 EditPage
::AS_SUCCESS_UPDATE
, $expected,
357 "expected successfull update of section" );
360 public static function provideAutoMerge() {
363 $tests[] = array( #0: plain conflict
364 "Elmo", # base edit user
365 "one\n\ntwo\n\nthree\n",
368 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
372 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
374 EditPage
::AS_CONFLICT_DETECTED
, # expected code
375 "ONE\n\ntwo\n\nthree\n", # expected text
376 'expected edit conflict', # message
379 $tests[] = array( #1: successful merge
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_SUCCESS_UPDATE
, # expected code
391 "ONE\n\ntwo\n\nTHREE\n", # expected text
392 'expected automatic merge', # message
396 $text .= "== first section ==\n\n";
397 $text .= "one\n\ntwo\n\nthree\n\n";
398 $text .= "== second section ==\n\n";
399 $text .= "four\n\nfive\n\nsix\n\n";
401 // extract the first section.
402 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
404 // generate expected text after merge
405 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
407 $tests[] = array( #2: merge in section
408 "Elmo", # base edit user
412 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
417 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
420 EditPage
::AS_SUCCESS_UPDATE
, # expected code
421 $expected, # expected text
422 'expected automatic section merge', # message
425 // see whether it makes a difference who did the base edit
426 $testsWithAdam = array_map( function ( $test ) {
427 $test[0] = 'Adam'; // change base edit user
431 $testsWithBerta = array_map( function ( $test ) {
432 $test[0] = 'Berta'; // change base edit user
436 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
440 * @dataProvider provideAutoMerge
443 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
444 $expectedCode, $expectedText, $message = null
446 $this->checkHasDiff3();
449 $ns = $this->getDefaultWikitextNS();
450 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
451 $page = WikiPage
::factory( $title );
453 if ( $page->exists() ) {
454 $page->doDeleteArticle( "clean slate for testing" );
458 'wpTextbox1' => $text,
461 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
462 $baseUser, $baseEdit, null, null, __METHOD__
);
464 $this->forceRevisionDate( $page, '20120101000000' );
466 $edittime = $page->getTimestamp();
468 // start timestamps for conflict detection
469 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
470 $adamsEdit['wpStarttime'] = 1;
473 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
474 $bertasEdit['wpStarttime'] = 2;
477 $starttime = wfTimestampNow();
478 $adamsTime = wfTimestamp(
480 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
482 $bertasTime = wfTimestamp(
484 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
487 $adamsEdit['wpStarttime'] = $adamsTime;
488 $bertasEdit['wpStarttime'] = $bertasTime;
490 $adamsEdit['wpSummary'] = 'Adam\'s edit';
491 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
493 $adamsEdit['wpEdittime'] = $edittime;
494 $bertasEdit['wpEdittime'] = $edittime;
497 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
498 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
501 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
502 $expectedCode, $expectedText, $message );