3 use MediaWiki\MediaWikiServices
;
9 * ^--- tell jenkins this test needs the database
12 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
14 class EditPageTest
extends MediaWikiLangTestCase
{
16 protected function setUp() {
19 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
20 $this->setContentLang( $contLang );
22 $this->setMwGlobals( [
23 'wgExtraNamespaces' => [
25 12313 => 'Dummy_talk',
27 'wgNamespaceContentModels' => [ 12312 => 'testing' ],
29 $this->mergeMwGlobalArrayValue(
31 [ 'testing' => 'DummyContentHandlerForTesting' ]
36 * @dataProvider provideExtractSectionTitle
37 * @covers EditPage::extractSectionTitle
39 public function testExtractSectionTitle( $section, $title ) {
40 $extracted = EditPage
::extractSectionTitle( $section );
41 $this->assertEquals( $title, $extracted );
44 public static function provideExtractSectionTitle() {
47 "== Test ==\n\nJust a test section.",
51 "An initial section, no header.",
55 "An initial section with a fake heder (T34617)\n\n== Test == ??\nwtf",
59 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
63 "== Section== \t\r\n followed by whitespace (T37051)",
69 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
70 $dbw = wfGetDB( DB_MASTER
);
72 $dbw->update( 'revision',
73 [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ],
74 [ 'rev_id' => $page->getLatest() ] );
80 * User input text is passed to rtrim() by edit page. This is a simple
81 * wrapper around assertEquals() which calls rrtrim() to normalize the
82 * expected and actual texts.
83 * @param string $expected
84 * @param string $actual
87 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
88 $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
92 * Performs an edit and checks the result.
94 * @param string|Title $title The title of the page to edit
95 * @param string|null $baseText Some text to create the page with before attempting the edit.
96 * @param User|string|null $user The user to perform the edit as.
97 * @param array $edit An array of request parameters used to define the edit to perform.
98 * Some well known fields are:
99 * * wpTextbox1: the text to submit
100 * * wpSummary: the edit summary
101 * * wpEditToken: the edit token (will be inserted if not provided)
102 * * wpEdittime: timestamp of the edit's base revision (will be inserted
104 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
105 * * wpSectionTitle: the section to edit
106 * * wpMinorEdit: mark as minor edit
107 * * wpWatchthis: whether to watch the page
108 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
109 * Set to null to skip the check.
110 * @param string|null $expectedText The text expected to be on the page after the edit.
111 * Set to null to skip the check.
112 * @param string|null $message An optional message to show along with any error message.
114 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
116 protected function assertEdit( $title, $baseText, $user = null, array $edit,
117 $expectedCode = null, $expectedText = null, $message = null
119 if ( is_string( $title ) ) {
120 $ns = $this->getDefaultWikitextNS();
121 $title = Title
::newFromText( $title, $ns );
123 $this->assertNotNull( $title );
125 if ( is_string( $user ) ) {
126 $user = User
::newFromName( $user );
128 if ( $user->getId() === 0 ) {
129 $user->addToDatabase();
133 $page = WikiPage
::factory( $title );
135 if ( $baseText !== null ) {
136 $content = ContentHandler
::makeContent( $baseText, $title );
137 $page->doEditContent( $content, "base text for test" );
138 $this->forceRevisionDate( $page, '20120101000000' );
142 $currentText = ContentHandler
::getContentText( $page->getContent() );
144 # EditPage rtrim() the user input, so we alter our expected text
146 $this->assertEditedTextEquals( $baseText, $currentText );
149 if ( $user == null ) {
150 $user = $GLOBALS['wgUser'];
152 $this->setMwGlobals( 'wgUser', $user );
155 if ( !isset( $edit['wpEditToken'] ) ) {
156 $edit['wpEditToken'] = $user->getEditToken();
159 if ( !isset( $edit['wpEdittime'] ) ) {
160 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
163 if ( !isset( $edit['wpStarttime'] ) ) {
164 $edit['wpStarttime'] = wfTimestampNow();
167 if ( !isset( $edit['wpUnicodeCheck'] ) ) {
168 $edit['wpUnicodeCheck'] = EditPage
::UNICODE_CHECK
;
171 $req = new FauxRequest( $edit, true ); // session ??
173 $article = new Article( $title );
174 $article->getContext()->setTitle( $title );
175 $ep = new EditPage( $article );
176 $ep->setContextTitle( $title );
177 $ep->importFormData( $req );
179 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
181 // this is where the edit happens!
182 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
183 // and throws exceptions like PermissionsError
184 $status = $ep->internalAttemptSave( $result, $bot );
186 if ( $expectedCode !== null ) {
188 $this->assertEquals( $expectedCode, $status->value
,
189 "Expected result code mismatch. $message" );
192 $page = WikiPage
::factory( $title );
194 if ( $expectedText !== null ) {
195 // check resulting page text
196 $content = $page->getContent();
197 $text = ContentHandler
::getContentText( $content );
199 # EditPage rtrim() the user input, so we alter our expected text
201 $this->assertEditedTextEquals( $expectedText, $text,
202 "Expected article text mismatch. $message" );
208 public static function provideCreatePages() {
210 [ 'expected article being created',
211 'EditPageTest_testCreatePage',
214 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
217 [ 'expected article not being created if empty',
218 'EditPageTest_testCreatePage',
221 EditPage
::AS_BLANK_ARTICLE
,
224 [ 'expected MediaWiki: page being created',
228 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
231 [ 'expected not-registered MediaWiki: page not being created if empty',
232 'MediaWiki:EditPageTest_testCreatePage',
235 EditPage
::AS_BLANK_ARTICLE
,
238 [ 'expected registered MediaWiki: page being created even if empty',
242 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
245 [ 'expected registered MediaWiki: page whose default content is empty'
246 . ' not being created if empty',
247 'MediaWiki:Ipb-default-expiry',
250 EditPage
::AS_BLANK_ARTICLE
,
253 [ 'expected MediaWiki: page not being created if text equals default message',
257 EditPage
::AS_BLANK_ARTICLE
,
260 [ 'expected empty article being created',
261 'EditPageTest_testCreatePage',
264 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
272 * @dataProvider provideCreatePages
275 public function testCreatePage(
276 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
280 $this->setMwGlobals( 'wgHooks', [
281 'PageContentInsertComplete' => [ function (
282 WikiPage
&$page, User
&$user, Content
$content,
283 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
285 // types/refs checked
287 'PageContentSaveComplete' => [ function (
288 WikiPage
&$page, User
&$user, Content
$content,
289 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
290 Status
&$status, $baseRevId
291 ) use ( &$checkId ) {
292 $checkId = $status->value
['revision']->getId();
293 // types/refs checked
297 $edit = [ 'wpTextbox1' => $editText ];
298 if ( $ignoreBlank ) {
299 $edit['wpIgnoreBlankArticle'] = 1;
302 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
304 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
305 $latest = $page->getLatest();
306 $page->doDeleteArticleReal( $pageTitle );
308 $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
309 $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
314 * @dataProvider provideCreatePages
317 public function testCreatePageTrx(
318 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
321 $this->setMwGlobals( 'wgHooks', [
322 'PageContentInsertComplete' => [ function (
323 WikiPage
&$page, User
&$user, Content
$content,
324 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
326 // types/refs checked
328 'PageContentSaveComplete' => [ function (
329 WikiPage
&$page, User
&$user, Content
$content,
330 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
331 Status
&$status, $baseRevId
332 ) use ( &$checkIds ) {
333 $checkIds[] = $status->value
['revision']->getId();
334 // types/refs checked
338 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
340 $edit = [ 'wpTextbox1' => $editText ];
341 if ( $ignoreBlank ) {
342 $edit['wpIgnoreBlankArticle'] = 1;
345 $page = $this->assertEdit(
346 $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
348 $pageTitle2 = (string)$pageTitle . '/x';
349 $page2 = $this->assertEdit(
350 $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
352 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
354 $this->assertSame( 0, DeferredUpdates
::pendingUpdatesCount(), 'No deferred updates' );
356 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
357 $latest = $page->getLatest();
358 $page->doDeleteArticleReal( $pageTitle );
360 $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
361 $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
363 $latest2 = $page2->getLatest();
364 $page2->doDeleteArticleReal( $pageTitle2 );
366 $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
367 $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
374 public function testUpdatePage() {
377 $this->setMwGlobals( 'wgHooks', [
378 'PageContentInsertComplete' => [ function (
379 WikiPage
&$page, User
&$user, Content
$content,
380 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
382 // types/refs checked
384 'PageContentSaveComplete' => [ function (
385 WikiPage
&$page, User
&$user, Content
$content,
386 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
387 Status
&$status, $baseRevId
388 ) use ( &$checkIds ) {
389 $checkIds[] = $status->value
['revision']->getId();
390 // types/refs checked
396 'wpTextbox1' => $text,
397 'wpSummary' => 'first update',
400 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
401 EditPage
::AS_SUCCESS_UPDATE
, $text,
402 "expected successful update with given text" );
403 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
405 $this->forceRevisionDate( $page, '20120101000000' );
409 'wpTextbox1' => $text,
410 'wpSummary' => 'second update',
413 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
414 EditPage
::AS_SUCCESS_UPDATE
, $text,
415 "expected successful update with given text" );
416 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
417 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
423 public function testUpdatePageTrx() {
426 'wpTextbox1' => $text,
427 'wpSummary' => 'first update',
430 $page = $this->assertEdit( 'EditPageTest_testTrxUpdatePage', "zero", null, $edit,
431 EditPage
::AS_SUCCESS_UPDATE
, $text,
432 "expected successful update with given text" );
434 $this->forceRevisionDate( $page, '20120101000000' );
437 $this->setMwGlobals( 'wgHooks', [
438 'PageContentSaveComplete' => [ function (
439 WikiPage
&$page, User
&$user, Content
$content,
440 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
441 Status
&$status, $baseRevId
442 ) use ( &$checkIds ) {
443 $checkIds[] = $status->value
['revision']->getId();
444 // types/refs checked
448 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
452 'wpTextbox1' => $text,
453 'wpSummary' => 'second update',
456 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
457 EditPage
::AS_SUCCESS_UPDATE
, $text,
458 "expected successful update with given text" );
462 'wpTextbox1' => $text,
463 'wpSummary' => 'third update',
466 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
467 EditPage
::AS_SUCCESS_UPDATE
, $text,
468 "expected successful update with given text" );
470 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
472 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
473 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
474 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
477 public static function provideSectionEdit() {
487 $sectionOne = '== one ==
491 $newSection = '== new section ==
496 $textWithNewSectionOne = preg_replace(
497 '/== one ==.*== two ==/ms',
498 "$sectionOne\n== two ==", $text
501 $textWithNewSectionAdded = "$text\n$newSection";
516 'replace first section',
517 $textWithNewSectionOne,
525 $textWithNewSectionAdded,
531 * @dataProvider provideSectionEdit
534 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
536 'wpTextbox1' => $text,
537 'wpSummary' => $summary,
538 'wpSection' => $section,
541 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
542 EditPage
::AS_SUCCESS_UPDATE
, $expected,
543 "expected successful update of section" );
546 public static function provideAutoMerge() {
549 $tests[] = [ # 0: plain conflict
550 "Elmo", # base edit user
551 "one\n\ntwo\n\nthree\n",
554 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
558 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
560 EditPage
::AS_CONFLICT_DETECTED
, # expected code
561 "ONE\n\ntwo\n\nthree\n", # expected text
562 'expected edit conflict', # message
565 $tests[] = [ # 1: successful merge
566 "Elmo", # base edit user
567 "one\n\ntwo\n\nthree\n",
570 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
574 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
576 EditPage
::AS_SUCCESS_UPDATE
, # expected code
577 "ONE\n\ntwo\n\nTHREE\n", # expected text
578 'expected automatic merge', # message
582 $text .= "== first section ==\n\n";
583 $text .= "one\n\ntwo\n\nthree\n\n";
584 $text .= "== second section ==\n\n";
585 $text .= "four\n\nfive\n\nsix\n\n";
587 // extract the first section.
588 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
590 // generate expected text after merge
591 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
593 $tests[] = [ # 2: merge in section
594 "Elmo", # base edit user
598 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
603 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
606 EditPage
::AS_SUCCESS_UPDATE
, # expected code
607 $expected, # expected text
608 'expected automatic section merge', # message
611 // see whether it makes a difference who did the base edit
612 $testsWithAdam = array_map( function ( $test ) {
613 $test[0] = 'Adam'; // change base edit user
617 $testsWithBerta = array_map( function ( $test ) {
618 $test[0] = 'Berta'; // change base edit user
622 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
626 * @dataProvider provideAutoMerge
629 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
630 $expectedCode, $expectedText, $message = null
632 $this->markTestSkippedIfNoDiff3();
635 $ns = $this->getDefaultWikitextNS();
636 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
637 $page = WikiPage
::factory( $title );
639 if ( $page->exists() ) {
640 $page->doDeleteArticle( "clean slate for testing" );
644 'wpTextbox1' => $text,
647 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
648 $baseUser, $baseEdit, null, null, __METHOD__
);
650 $this->forceRevisionDate( $page, '20120101000000' );
652 $edittime = $page->getTimestamp();
654 // start timestamps for conflict detection
655 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
656 $adamsEdit['wpStarttime'] = 1;
659 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
660 $bertasEdit['wpStarttime'] = 2;
663 $starttime = wfTimestampNow();
664 $adamsTime = wfTimestamp(
666 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
668 $bertasTime = wfTimestamp(
670 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
673 $adamsEdit['wpStarttime'] = $adamsTime;
674 $bertasEdit['wpStarttime'] = $bertasTime;
676 $adamsEdit['wpSummary'] = 'Adam\'s edit';
677 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
679 $adamsEdit['wpEdittime'] = $edittime;
680 $bertasEdit['wpEdittime'] = $edittime;
683 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
684 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successful update" );
687 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
688 $expectedCode, $expectedText, $message );
692 * @depends testAutoMerge
695 public function testCheckDirectEditingDisallowed_forNonTextContent() {
696 $user = $GLOBALS['wgUser'];
699 'wpTextbox1' => serialize( 'non-text content' ),
700 'wpEditToken' => $user->getEditToken(),
702 'wpStarttime' => wfTimestampNow(),
703 'wpUnicodeCheck' => EditPage
::UNICODE_CHECK
,
706 $this->setExpectedException(
708 'This content model is not supported: testing'
711 $this->doEditDummyNonTextPage( $edit );
714 /** @covers EditPage */
715 public function testShouldPreventChangingContentModelWhenUserCannotChangeModelForTitle() {
716 $this->setTemporaryHook( 'getUserPermissionsErrors',
717 function ( Title
$page, $user, $action, &$result ) {
718 if ( $action === 'editcontentmodel' &&
719 $page->getContentModel() === CONTENT_MODEL_WIKITEXT
) {
726 $user = $GLOBALS['wgUser'];
728 $status = $this->doEditDummyNonTextPage( [
729 'wpTextbox1' => 'some text',
730 'wpEditToken' => $user->getEditToken(),
732 'wpStarttime' => wfTimestampNow(),
733 'wpUnicodeCheck' => EditPage
::UNICODE_CHECK
,
734 'model' => CONTENT_MODEL_WIKITEXT
,
735 'format' => CONTENT_FORMAT_WIKITEXT
,
738 $this->assertFalse( $status->isOK() );
739 $this->assertEquals( EditPage
::AS_NO_CHANGE_CONTENT_MODEL
, $status->getValue() );
742 /** @covers EditPage */
743 public function testShouldPreventChangingContentModelWhenUserCannotEditTargetTitle() {
744 $this->setTemporaryHook( 'getUserPermissionsErrors',
745 function ( Title
$page, $user, $action, &$result ) {
746 if ( $action === 'edit' && $page->getContentModel() === CONTENT_MODEL_WIKITEXT
) {
752 $user = $GLOBALS['wgUser'];
754 $status = $this->doEditDummyNonTextPage( [
755 'wpTextbox1' => 'some text',
756 'wpEditToken' => $user->getEditToken(),
758 'wpStarttime' => wfTimestampNow(),
759 'wpUnicodeCheck' => EditPage
::UNICODE_CHECK
,
760 'model' => CONTENT_MODEL_WIKITEXT
,
761 'format' => CONTENT_FORMAT_WIKITEXT
,
764 $this->assertFalse( $status->isOK() );
765 $this->assertEquals( EditPage
::AS_NO_CHANGE_CONTENT_MODEL
, $status->getValue() );
768 private function doEditDummyNonTextPage( array $edit ): Status
{
769 $title = Title
::newFromText( 'Dummy:NonTextPageForEditPage' );
771 $article = new Article( $title );
772 $article->getContext()->setTitle( $title );
773 $ep = new EditPage( $article );
774 $ep->setContextTitle( $title );
776 $req = new FauxRequest( $edit, true );
777 $ep->importFormData( $req );
779 return $ep->internalAttemptSave( $result, false );