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->setContentLang( $wgContLang );
21 $this->setMwGlobals( [
22 'wgExtraNamespaces' => $wgExtraNamespaces,
23 'wgNamespaceContentModels' => $wgNamespaceContentModels,
24 'wgContentHandlers' => $wgContentHandlers,
27 $wgExtraNamespaces[12312] = 'Dummy';
28 $wgExtraNamespaces[12313] = 'Dummy_talk';
30 $wgNamespaceContentModels[12312] = "testing";
31 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
33 MWNamespace
::clearCaches();
34 $wgContLang->resetNamespaces(); # reset namespace cache
37 protected function tearDown() {
40 MWNamespace
::clearCaches();
41 $wgContLang->resetNamespaces(); # reset namespace cache
46 * @dataProvider provideExtractSectionTitle
47 * @covers EditPage::extractSectionTitle
49 public function testExtractSectionTitle( $section, $title ) {
50 $extracted = EditPage
::extractSectionTitle( $section );
51 $this->assertEquals( $title, $extracted );
54 public static function provideExtractSectionTitle() {
57 "== Test ==\n\nJust a test section.",
61 "An initial section, no header.",
65 "An initial section with a fake heder (T34617)\n\n== Test == ??\nwtf",
69 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
73 "== Section== \t\r\n followed by whitespace (T37051)",
79 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
80 $dbw = wfGetDB( DB_MASTER
);
82 $dbw->update( 'revision',
83 [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ],
84 [ 'rev_id' => $page->getLatest() ] );
90 * User input text is passed to rtrim() by edit page. This is a simple
91 * wrapper around assertEquals() which calls rrtrim() to normalize the
92 * expected and actual texts.
93 * @param string $expected
94 * @param string $actual
97 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
98 $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
102 * Performs an edit and checks the result.
104 * @param string|Title $title The title of the page to edit
105 * @param string|null $baseText Some text to create the page with before attempting the edit.
106 * @param User|string|null $user The user to perform the edit as.
107 * @param array $edit An array of request parameters used to define the edit to perform.
108 * Some well known fields are:
109 * * wpTextbox1: the text to submit
110 * * wpSummary: the edit summary
111 * * wpEditToken: the edit token (will be inserted if not provided)
112 * * wpEdittime: timestamp of the edit's base revision (will be inserted
114 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
115 * * wpSectionTitle: the section to edit
116 * * wpMinorEdit: mark as minor edit
117 * * wpWatchthis: whether to watch the page
118 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
119 * Set to null to skip the check.
120 * @param string|null $expectedText The text expected to be on the page after the edit.
121 * Set to null to skip the check.
122 * @param string|null $message An optional message to show along with any error message.
124 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
126 protected function assertEdit( $title, $baseText, $user = null, array $edit,
127 $expectedCode = null, $expectedText = null, $message = null
129 if ( is_string( $title ) ) {
130 $ns = $this->getDefaultWikitextNS();
131 $title = Title
::newFromText( $title, $ns );
133 $this->assertNotNull( $title );
135 if ( is_string( $user ) ) {
136 $user = User
::newFromName( $user );
138 if ( $user->getId() === 0 ) {
139 $user->addToDatabase();
143 $page = WikiPage
::factory( $title );
145 if ( $baseText !== null ) {
146 $content = ContentHandler
::makeContent( $baseText, $title );
147 $page->doEditContent( $content, "base text for test" );
148 $this->forceRevisionDate( $page, '20120101000000' );
152 $currentText = ContentHandler
::getContentText( $page->getContent() );
154 # EditPage rtrim() the user input, so we alter our expected text
156 $this->assertEditedTextEquals( $baseText, $currentText );
159 if ( $user == null ) {
160 $user = $GLOBALS['wgUser'];
162 $this->setMwGlobals( 'wgUser', $user );
165 if ( !isset( $edit['wpEditToken'] ) ) {
166 $edit['wpEditToken'] = $user->getEditToken();
169 if ( !isset( $edit['wpEdittime'] ) ) {
170 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
173 if ( !isset( $edit['wpStarttime'] ) ) {
174 $edit['wpStarttime'] = wfTimestampNow();
177 if ( !isset( $edit['wpUnicodeCheck'] ) ) {
178 $edit['wpUnicodeCheck'] = EditPage
::UNICODE_CHECK
;
181 $req = new FauxRequest( $edit, true ); // session ??
183 $article = new Article( $title );
184 $article->getContext()->setTitle( $title );
185 $ep = new EditPage( $article );
186 $ep->setContextTitle( $title );
187 $ep->importFormData( $req );
189 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
191 // this is where the edit happens!
192 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
193 // and throws exceptions like PermissionsError
194 $status = $ep->internalAttemptSave( $result, $bot );
196 if ( $expectedCode !== null ) {
198 $this->assertEquals( $expectedCode, $status->value
,
199 "Expected result code mismatch. $message" );
202 $page = WikiPage
::factory( $title );
204 if ( $expectedText !== null ) {
205 // check resulting page text
206 $content = $page->getContent();
207 $text = ContentHandler
::getContentText( $content );
209 # EditPage rtrim() the user input, so we alter our expected text
211 $this->assertEditedTextEquals( $expectedText, $text,
212 "Expected article text mismatch. $message" );
218 public static function provideCreatePages() {
220 [ 'expected article being created',
221 'EditPageTest_testCreatePage',
224 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
227 [ 'expected article not being created if empty',
228 'EditPageTest_testCreatePage',
231 EditPage
::AS_BLANK_ARTICLE
,
234 [ 'expected MediaWiki: page being created',
238 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
241 [ 'expected not-registered MediaWiki: page not being created if empty',
242 'MediaWiki:EditPageTest_testCreatePage',
245 EditPage
::AS_BLANK_ARTICLE
,
248 [ 'expected registered MediaWiki: page being created even if empty',
252 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
255 [ 'expected registered MediaWiki: page whose default content is empty'
256 . ' not being created if empty',
257 'MediaWiki:Ipb-default-expiry',
260 EditPage
::AS_BLANK_ARTICLE
,
263 [ 'expected MediaWiki: page not being created if text equals default message',
267 EditPage
::AS_BLANK_ARTICLE
,
270 [ 'expected empty article being created',
271 'EditPageTest_testCreatePage',
274 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
282 * @dataProvider provideCreatePages
285 public function testCreatePage(
286 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
290 $this->setMwGlobals( 'wgHooks', [
291 'PageContentInsertComplete' => [ function (
292 WikiPage
&$page, User
&$user, Content
$content,
293 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
295 // types/refs checked
297 'PageContentSaveComplete' => [ function (
298 WikiPage
&$page, User
&$user, Content
$content,
299 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
300 Status
&$status, $baseRevId
301 ) use ( &$checkId ) {
302 $checkId = $status->value
['revision']->getId();
303 // types/refs checked
307 $edit = [ 'wpTextbox1' => $editText ];
308 if ( $ignoreBlank ) {
309 $edit['wpIgnoreBlankArticle'] = 1;
312 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
314 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
315 $latest = $page->getLatest();
316 $page->doDeleteArticleReal( $pageTitle );
318 $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
319 $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
324 * @dataProvider provideCreatePages
327 public function testCreatePageTrx(
328 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
331 $this->setMwGlobals( 'wgHooks', [
332 'PageContentInsertComplete' => [ function (
333 WikiPage
&$page, User
&$user, Content
$content,
334 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
336 // types/refs checked
338 'PageContentSaveComplete' => [ function (
339 WikiPage
&$page, User
&$user, Content
$content,
340 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
341 Status
&$status, $baseRevId
342 ) use ( &$checkIds ) {
343 $checkIds[] = $status->value
['revision']->getId();
344 // types/refs checked
348 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
350 $edit = [ 'wpTextbox1' => $editText ];
351 if ( $ignoreBlank ) {
352 $edit['wpIgnoreBlankArticle'] = 1;
355 $page = $this->assertEdit(
356 $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
358 $pageTitle2 = (string)$pageTitle . '/x';
359 $page2 = $this->assertEdit(
360 $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
362 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
364 $this->assertEquals( 0, DeferredUpdates
::pendingUpdatesCount(), 'No deferred updates' );
366 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
367 $latest = $page->getLatest();
368 $page->doDeleteArticleReal( $pageTitle );
370 $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
371 $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
373 $latest2 = $page2->getLatest();
374 $page2->doDeleteArticleReal( $pageTitle2 );
376 $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
377 $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
381 public function testUpdatePage() {
384 $this->setMwGlobals( 'wgHooks', [
385 'PageContentInsertComplete' => [ function (
386 WikiPage
&$page, User
&$user, Content
$content,
387 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
389 // types/refs checked
391 'PageContentSaveComplete' => [ function (
392 WikiPage
&$page, User
&$user, Content
$content,
393 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
394 Status
&$status, $baseRevId
395 ) use ( &$checkIds ) {
396 $checkIds[] = $status->value
['revision']->getId();
397 // types/refs checked
403 'wpTextbox1' => $text,
404 'wpSummary' => 'first update',
407 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
408 EditPage
::AS_SUCCESS_UPDATE
, $text,
409 "expected successfull update with given text" );
410 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
412 $this->forceRevisionDate( $page, '20120101000000' );
416 'wpTextbox1' => $text,
417 'wpSummary' => 'second update',
420 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
421 EditPage
::AS_SUCCESS_UPDATE
, $text,
422 "expected successfull update with given text" );
423 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
424 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
427 public function testUpdatePageTrx() {
430 'wpTextbox1' => $text,
431 'wpSummary' => 'first update',
434 $page = $this->assertEdit( 'EditPageTest_testTrxUpdatePage', "zero", null, $edit,
435 EditPage
::AS_SUCCESS_UPDATE
, $text,
436 "expected successfull update with given text" );
438 $this->forceRevisionDate( $page, '20120101000000' );
441 $this->setMwGlobals( 'wgHooks', [
442 'PageContentSaveComplete' => [ function (
443 WikiPage
&$page, User
&$user, Content
$content,
444 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
445 Status
&$status, $baseRevId
446 ) use ( &$checkIds ) {
447 $checkIds[] = $status->value
['revision']->getId();
448 // types/refs checked
452 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
456 'wpTextbox1' => $text,
457 'wpSummary' => 'second update',
460 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
461 EditPage
::AS_SUCCESS_UPDATE
, $text,
462 "expected successfull update with given text" );
466 'wpTextbox1' => $text,
467 'wpSummary' => 'third update',
470 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
471 EditPage
::AS_SUCCESS_UPDATE
, $text,
472 "expected successfull update with given text" );
474 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
476 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
477 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
478 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
481 public static function provideSectionEdit() {
491 $sectionOne = '== one ==
495 $newSection = '== new section ==
500 $textWithNewSectionOne = preg_replace(
501 '/== one ==.*== two ==/ms',
502 "$sectionOne\n== two ==", $text
505 $textWithNewSectionAdded = "$text\n$newSection";
520 'replace first section',
521 $textWithNewSectionOne,
529 $textWithNewSectionAdded,
535 * @dataProvider provideSectionEdit
538 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
540 'wpTextbox1' => $text,
541 'wpSummary' => $summary,
542 'wpSection' => $section,
545 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
546 EditPage
::AS_SUCCESS_UPDATE
, $expected,
547 "expected successfull update of section" );
550 public static function provideAutoMerge() {
553 $tests[] = [ # 0: plain conflict
554 "Elmo", # base edit user
555 "one\n\ntwo\n\nthree\n",
558 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
562 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
564 EditPage
::AS_CONFLICT_DETECTED
, # expected code
565 "ONE\n\ntwo\n\nthree\n", # expected text
566 'expected edit conflict', # message
569 $tests[] = [ # 1: successful merge
570 "Elmo", # base edit user
571 "one\n\ntwo\n\nthree\n",
574 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
578 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
580 EditPage
::AS_SUCCESS_UPDATE
, # expected code
581 "ONE\n\ntwo\n\nTHREE\n", # expected text
582 'expected automatic merge', # message
586 $text .= "== first section ==\n\n";
587 $text .= "one\n\ntwo\n\nthree\n\n";
588 $text .= "== second section ==\n\n";
589 $text .= "four\n\nfive\n\nsix\n\n";
591 // extract the first section.
592 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
594 // generate expected text after merge
595 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
597 $tests[] = [ # 2: merge in section
598 "Elmo", # base edit user
602 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
607 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
610 EditPage
::AS_SUCCESS_UPDATE
, # expected code
611 $expected, # expected text
612 'expected automatic section merge', # message
615 // see whether it makes a difference who did the base edit
616 $testsWithAdam = array_map( function ( $test ) {
617 $test[0] = 'Adam'; // change base edit user
621 $testsWithBerta = array_map( function ( $test ) {
622 $test[0] = 'Berta'; // change base edit user
626 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
630 * @dataProvider provideAutoMerge
633 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
634 $expectedCode, $expectedText, $message = null
636 $this->markTestSkippedIfNoDiff3();
639 $ns = $this->getDefaultWikitextNS();
640 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
641 $page = WikiPage
::factory( $title );
643 if ( $page->exists() ) {
644 $page->doDeleteArticle( "clean slate for testing" );
648 'wpTextbox1' => $text,
651 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
652 $baseUser, $baseEdit, null, null, __METHOD__
);
654 $this->forceRevisionDate( $page, '20120101000000' );
656 $edittime = $page->getTimestamp();
658 // start timestamps for conflict detection
659 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
660 $adamsEdit['wpStarttime'] = 1;
663 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
664 $bertasEdit['wpStarttime'] = 2;
667 $starttime = wfTimestampNow();
668 $adamsTime = wfTimestamp(
670 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
672 $bertasTime = wfTimestamp(
674 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
677 $adamsEdit['wpStarttime'] = $adamsTime;
678 $bertasEdit['wpStarttime'] = $bertasTime;
680 $adamsEdit['wpSummary'] = 'Adam\'s edit';
681 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
683 $adamsEdit['wpEdittime'] = $edittime;
684 $bertasEdit['wpEdittime'] = $edittime;
687 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
688 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
691 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
692 $expectedCode, $expectedText, $message );
696 * @depends testAutoMerge
698 public function testCheckDirectEditingDisallowed_forNonTextContent() {
699 $title = Title
::newFromText( 'Dummy:NonTextPageForEditPage' );
700 $page = WikiPage
::factory( $title );
702 $article = new Article( $title );
703 $article->getContext()->setTitle( $title );
704 $ep = new EditPage( $article );
705 $ep->setContextTitle( $title );
707 $user = $GLOBALS['wgUser'];
710 'wpTextbox1' => serialize( 'non-text content' ),
711 'wpEditToken' => $user->getEditToken(),
713 'wpStarttime' => wfTimestampNow(),
714 'wpUnicodeCheck' => EditPage
::UNICODE_CHECK
,
717 $req = new FauxRequest( $edit, true );
718 $ep->importFormData( $req );
720 $this->setExpectedException(
722 'This content model is not supported: testing'
725 $ep->internalAttemptSave( $result, false );