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
{
14 protected function setUp() {
15 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
19 $this->setMwGlobals( [
20 'wgExtraNamespaces' => $wgExtraNamespaces,
21 'wgNamespaceContentModels' => $wgNamespaceContentModels,
22 'wgContentHandlers' => $wgContentHandlers,
23 'wgContLang' => $wgContLang,
26 $wgExtraNamespaces[12312] = 'Dummy';
27 $wgExtraNamespaces[12313] = 'Dummy_talk';
29 $wgNamespaceContentModels[12312] = "testing";
30 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
32 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
33 $wgContLang->resetNamespaces(); # reset namespace cache
37 * @dataProvider provideExtractSectionTitle
38 * @covers EditPage::extractSectionTitle
40 public function testExtractSectionTitle( $section, $title ) {
41 $extracted = EditPage
::extractSectionTitle( $section );
42 $this->assertEquals( $title, $extracted );
45 public static function provideExtractSectionTitle() {
48 "== Test ==\n\nJust a test section.",
52 "An initial section, no header.",
56 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
60 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
64 "== Section== \t\r\n followed by whitespace (bug 35051)",
70 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
71 $dbw = wfGetDB( DB_MASTER
);
73 $dbw->update( 'revision',
74 [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ],
75 [ 'rev_id' => $page->getLatest() ] );
81 * User input text is passed to rtrim() by edit page. This is a simple
82 * wrapper around assertEquals() which calls rrtrim() to normalize the
83 * expected and actual texts.
84 * @param string $expected
85 * @param string $actual
88 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
89 $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
93 * Performs an edit and checks the result.
95 * @param string|Title $title The title of the page to edit
96 * @param string|null $baseText Some text to create the page with before attempting the edit.
97 * @param User|string|null $user The user to perform the edit as.
98 * @param array $edit An array of request parameters used to define the edit to perform.
99 * Some well known fields are:
100 * * wpTextbox1: the text to submit
101 * * wpSummary: the edit summary
102 * * wpEditToken: the edit token (will be inserted if not provided)
103 * * wpEdittime: timestamp of the edit's base revision (will be inserted
105 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
106 * * wpSectionTitle: the section to edit
107 * * wpMinorEdit: mark as minor edit
108 * * wpWatchthis: whether to watch the page
109 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
110 * Set to null to skip the check.
111 * @param string|null $expectedText The text expected to be on the page after the edit.
112 * Set to null to skip the check.
113 * @param string|null $message An optional message to show along with any error message.
115 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
117 protected function assertEdit( $title, $baseText, $user = null, array $edit,
118 $expectedCode = null, $expectedText = null, $message = null
120 if ( is_string( $title ) ) {
121 $ns = $this->getDefaultWikitextNS();
122 $title = Title
::newFromText( $title, $ns );
124 $this->assertNotNull( $title );
126 if ( is_string( $user ) ) {
127 $user = User
::newFromName( $user );
129 if ( $user->getId() === 0 ) {
130 $user->addToDatabase();
134 $page = WikiPage
::factory( $title );
136 if ( $baseText !== null ) {
137 $content = ContentHandler
::makeContent( $baseText, $title );
138 $page->doEditContent( $content, "base text for test" );
139 $this->forceRevisionDate( $page, '20120101000000' );
143 $currentText = ContentHandler
::getContentText( $page->getContent() );
145 # EditPage rtrim() the user input, so we alter our expected text
147 $this->assertEditedTextEquals( $baseText, $currentText );
150 if ( $user == null ) {
151 $user = $GLOBALS['wgUser'];
153 $this->setMwGlobals( 'wgUser', $user );
156 if ( !isset( $edit['wpEditToken'] ) ) {
157 $edit['wpEditToken'] = $user->getEditToken();
160 if ( !isset( $edit['wpEdittime'] ) ) {
161 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
164 if ( !isset( $edit['wpStarttime'] ) ) {
165 $edit['wpStarttime'] = wfTimestampNow();
168 $req = new FauxRequest( $edit, true ); // session ??
170 $article = new Article( $title );
171 $article->getContext()->setTitle( $title );
172 $ep = new EditPage( $article );
173 $ep->setContextTitle( $title );
174 $ep->importFormData( $req );
176 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
178 // this is where the edit happens!
179 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
180 // and throws exceptions like PermissionsError
181 $status = $ep->internalAttemptSave( $result, $bot );
183 if ( $expectedCode !== null ) {
185 $this->assertEquals( $expectedCode, $status->value
,
186 "Expected result code mismatch. $message" );
189 $page = WikiPage
::factory( $title );
191 if ( $expectedText !== null ) {
192 // check resulting page text
193 $content = $page->getContent();
194 $text = ContentHandler
::getContentText( $content );
196 # EditPage rtrim() the user input, so we alter our expected text
198 $this->assertEditedTextEquals( $expectedText, $text,
199 "Expected article text mismatch. $message" );
205 public static function provideCreatePages() {
207 [ 'expected article being created',
208 'EditPageTest_testCreatePage',
211 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
214 [ 'expected article not being created if empty',
215 'EditPageTest_testCreatePage',
218 EditPage
::AS_BLANK_ARTICLE
,
221 [ 'expected MediaWiki: page being created',
225 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
228 [ 'expected not-registered MediaWiki: page not being created if empty',
229 'MediaWiki:EditPageTest_testCreatePage',
232 EditPage
::AS_BLANK_ARTICLE
,
235 [ 'expected registered MediaWiki: page being created even if empty',
239 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
242 [ 'expected registered MediaWiki: page whose default content is empty'
243 . ' not being created if empty',
244 'MediaWiki:Ipb-default-expiry',
247 EditPage
::AS_BLANK_ARTICLE
,
250 [ 'expected MediaWiki: page not being created if text equals default message',
254 EditPage
::AS_BLANK_ARTICLE
,
257 [ 'expected empty article being created',
258 'EditPageTest_testCreatePage',
261 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
269 * @dataProvider provideCreatePages
272 public function testCreatePage(
273 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
277 $this->setMwGlobals( 'wgHooks', [
278 'PageContentInsertComplete' => [ function (
279 WikiPage
&$page, User
&$user, Content
$content,
280 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
282 // types/refs checked
284 'PageContentSaveComplete' => [ function (
285 WikiPage
&$page, User
&$user, Content
$content,
286 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
287 Status
&$status, $baseRevId
288 ) use ( &$checkId ) {
289 $checkId = $status->value
['revision']->getId();
290 // types/refs checked
294 $edit = [ 'wpTextbox1' => $editText ];
295 if ( $ignoreBlank ) {
296 $edit['wpIgnoreBlankArticle'] = 1;
299 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
301 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
302 $latest = $page->getLatest();
303 $page->doDeleteArticleReal( $pageTitle );
305 $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
306 $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
311 * @dataProvider provideCreatePages
314 public function testCreatePageTrx(
315 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
318 $this->setMwGlobals( 'wgHooks', [
319 'PageContentInsertComplete' => [ function (
320 WikiPage
&$page, User
&$user, Content
$content,
321 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
323 // types/refs checked
325 'PageContentSaveComplete' => [ function (
326 WikiPage
&$page, User
&$user, Content
$content,
327 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
328 Status
&$status, $baseRevId
329 ) use ( &$checkIds ) {
330 $checkIds[] = $status->value
['revision']->getId();
331 // types/refs checked
335 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
337 $edit = [ 'wpTextbox1' => $editText ];
338 if ( $ignoreBlank ) {
339 $edit['wpIgnoreBlankArticle'] = 1;
342 $page = $this->assertEdit(
343 $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
345 $pageTitle2 = (string)$pageTitle . '/x';
346 $page2 = $this->assertEdit(
347 $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
349 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
351 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
352 $latest = $page->getLatest();
353 $page->doDeleteArticleReal( $pageTitle );
355 $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
356 $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
358 $latest2 = $page2->getLatest();
359 $page2->doDeleteArticleReal( $pageTitle2 );
361 $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
362 $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
366 public function testUpdatePage() {
369 $this->setMwGlobals( 'wgHooks', [
370 'PageContentInsertComplete' => [ function (
371 WikiPage
&$page, User
&$user, Content
$content,
372 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
374 // types/refs checked
376 'PageContentSaveComplete' => [ function (
377 WikiPage
&$page, User
&$user, Content
$content,
378 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
379 Status
&$status, $baseRevId
380 ) use ( &$checkIds ) {
381 $checkIds[] = $status->value
['revision']->getId();
382 // types/refs checked
388 'wpTextbox1' => $text,
389 'wpSummary' => 'first update',
392 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
393 EditPage
::AS_SUCCESS_UPDATE
, $text,
394 "expected successfull update with given text" );
395 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
397 $this->forceRevisionDate( $page, '20120101000000' );
401 'wpTextbox1' => $text,
402 'wpSummary' => 'second update',
405 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
406 EditPage
::AS_SUCCESS_UPDATE
, $text,
407 "expected successfull update with given text" );
408 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
409 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
412 public function testUpdatePageTrx() {
415 'wpTextbox1' => $text,
416 'wpSummary' => 'first update',
419 $page = $this->assertEdit( 'EditPageTest_testTrxUpdatePage', "zero", null, $edit,
420 EditPage
::AS_SUCCESS_UPDATE
, $text,
421 "expected successfull update with given text" );
423 $this->forceRevisionDate( $page, '20120101000000' );
426 $this->setMwGlobals( 'wgHooks', [
427 'PageContentSaveComplete' => [ function (
428 WikiPage
&$page, User
&$user, Content
$content,
429 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
430 Status
&$status, $baseRevId
431 ) use ( &$checkIds ) {
432 $checkIds[] = $status->value
['revision']->getId();
433 // types/refs checked
437 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
441 'wpTextbox1' => $text,
442 'wpSummary' => 'second update',
445 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
446 EditPage
::AS_SUCCESS_UPDATE
, $text,
447 "expected successfull update with given text" );
451 'wpTextbox1' => $text,
452 'wpSummary' => 'third update',
455 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
456 EditPage
::AS_SUCCESS_UPDATE
, $text,
457 "expected successfull update with given text" );
459 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
461 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
462 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
463 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
466 public static function provideSectionEdit() {
476 $sectionOne = '== one ==
480 $newSection = '== new section ==
485 $textWithNewSectionOne = preg_replace(
486 '/== one ==.*== two ==/ms',
487 "$sectionOne\n== two ==", $text
490 $textWithNewSectionAdded = "$text\n$newSection";
505 'replace first section',
506 $textWithNewSectionOne,
514 $textWithNewSectionAdded,
520 * @dataProvider provideSectionEdit
523 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
525 'wpTextbox1' => $text,
526 'wpSummary' => $summary,
527 'wpSection' => $section,
530 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
531 EditPage
::AS_SUCCESS_UPDATE
, $expected,
532 "expected successfull update of section" );
535 public static function provideAutoMerge() {
538 $tests[] = [ # 0: plain conflict
539 "Elmo", # base edit user
540 "one\n\ntwo\n\nthree\n",
543 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
547 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
549 EditPage
::AS_CONFLICT_DETECTED
, # expected code
550 "ONE\n\ntwo\n\nthree\n", # expected text
551 'expected edit conflict', # message
554 $tests[] = [ # 1: successful merge
555 "Elmo", # base edit user
556 "one\n\ntwo\n\nthree\n",
559 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
563 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
565 EditPage
::AS_SUCCESS_UPDATE
, # expected code
566 "ONE\n\ntwo\n\nTHREE\n", # expected text
567 'expected automatic merge', # message
571 $text .= "== first section ==\n\n";
572 $text .= "one\n\ntwo\n\nthree\n\n";
573 $text .= "== second section ==\n\n";
574 $text .= "four\n\nfive\n\nsix\n\n";
576 // extract the first section.
577 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
579 // generate expected text after merge
580 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
582 $tests[] = [ # 2: merge in section
583 "Elmo", # base edit user
587 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
592 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
595 EditPage
::AS_SUCCESS_UPDATE
, # expected code
596 $expected, # expected text
597 'expected automatic section merge', # message
600 // see whether it makes a difference who did the base edit
601 $testsWithAdam = array_map( function ( $test ) {
602 $test[0] = 'Adam'; // change base edit user
606 $testsWithBerta = array_map( function ( $test ) {
607 $test[0] = 'Berta'; // change base edit user
611 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
615 * @dataProvider provideAutoMerge
618 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
619 $expectedCode, $expectedText, $message = null
621 $this->markTestSkippedIfNoDiff3();
624 $ns = $this->getDefaultWikitextNS();
625 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
626 $page = WikiPage
::factory( $title );
628 if ( $page->exists() ) {
629 $page->doDeleteArticle( "clean slate for testing" );
633 'wpTextbox1' => $text,
636 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
637 $baseUser, $baseEdit, null, null, __METHOD__
);
639 $this->forceRevisionDate( $page, '20120101000000' );
641 $edittime = $page->getTimestamp();
643 // start timestamps for conflict detection
644 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
645 $adamsEdit['wpStarttime'] = 1;
648 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
649 $bertasEdit['wpStarttime'] = 2;
652 $starttime = wfTimestampNow();
653 $adamsTime = wfTimestamp(
655 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
657 $bertasTime = wfTimestamp(
659 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
662 $adamsEdit['wpStarttime'] = $adamsTime;
663 $bertasEdit['wpStarttime'] = $bertasTime;
665 $adamsEdit['wpSummary'] = 'Adam\'s edit';
666 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
668 $adamsEdit['wpEdittime'] = $edittime;
669 $bertasEdit['wpEdittime'] = $edittime;
672 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
673 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
676 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
677 $expectedCode, $expectedText, $message );
681 * @depends testAutoMerge
683 public function testCheckDirectEditingDisallowed_forNonTextContent() {
684 $title = Title
::newFromText( 'Dummy:NonTextPageForEditPage' );
685 $page = WikiPage
::factory( $title );
687 $article = new Article( $title );
688 $article->getContext()->setTitle( $title );
689 $ep = new EditPage( $article );
690 $ep->setContextTitle( $title );
692 $user = $GLOBALS['wgUser'];
695 'wpTextbox1' => serialize( 'non-text content' ),
696 'wpEditToken' => $user->getEditToken(),
698 'wpStarttime' => wfTimestampNow()
701 $req = new FauxRequest( $edit, true );
702 $ep->importFormData( $req );
704 $this->setExpectedException(
706 'This content model is not supported: testing'
709 $ep->internalAttemptSave( $result, false );