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
::clearCaches();
33 $wgContLang->resetNamespaces(); # reset namespace cache
36 protected function tearDown() {
39 MWNamespace
::clearCaches();
40 $wgContLang->resetNamespaces(); # reset namespace cache
45 * @dataProvider provideExtractSectionTitle
46 * @covers EditPage::extractSectionTitle
48 public function testExtractSectionTitle( $section, $title ) {
49 $extracted = EditPage
::extractSectionTitle( $section );
50 $this->assertEquals( $title, $extracted );
53 public static function provideExtractSectionTitle() {
56 "== Test ==\n\nJust a test section.",
60 "An initial section, no header.",
64 "An initial section with a fake heder (T34617)\n\n== Test == ??\nwtf",
68 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
72 "== Section== \t\r\n followed by whitespace (T37051)",
78 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
79 $dbw = wfGetDB( DB_MASTER
);
81 $dbw->update( 'revision',
82 [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ],
83 [ 'rev_id' => $page->getLatest() ] );
89 * User input text is passed to rtrim() by edit page. This is a simple
90 * wrapper around assertEquals() which calls rrtrim() to normalize the
91 * expected and actual texts.
92 * @param string $expected
93 * @param string $actual
96 protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
97 $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
101 * Performs an edit and checks the result.
103 * @param string|Title $title The title of the page to edit
104 * @param string|null $baseText Some text to create the page with before attempting the edit.
105 * @param User|string|null $user The user to perform the edit as.
106 * @param array $edit An array of request parameters used to define the edit to perform.
107 * Some well known fields are:
108 * * wpTextbox1: the text to submit
109 * * wpSummary: the edit summary
110 * * wpEditToken: the edit token (will be inserted if not provided)
111 * * wpEdittime: timestamp of the edit's base revision (will be inserted
113 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
114 * * wpSectionTitle: the section to edit
115 * * wpMinorEdit: mark as minor edit
116 * * wpWatchthis: whether to watch the page
117 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
118 * Set to null to skip the check.
119 * @param string|null $expectedText The text expected to be on the page after the edit.
120 * Set to null to skip the check.
121 * @param string|null $message An optional message to show along with any error message.
123 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
125 protected function assertEdit( $title, $baseText, $user = null, array $edit,
126 $expectedCode = null, $expectedText = null, $message = null
128 if ( is_string( $title ) ) {
129 $ns = $this->getDefaultWikitextNS();
130 $title = Title
::newFromText( $title, $ns );
132 $this->assertNotNull( $title );
134 if ( is_string( $user ) ) {
135 $user = User
::newFromName( $user );
137 if ( $user->getId() === 0 ) {
138 $user->addToDatabase();
142 $page = WikiPage
::factory( $title );
144 if ( $baseText !== null ) {
145 $content = ContentHandler
::makeContent( $baseText, $title );
146 $page->doEditContent( $content, "base text for test" );
147 $this->forceRevisionDate( $page, '20120101000000' );
151 $currentText = ContentHandler
::getContentText( $page->getContent() );
153 # EditPage rtrim() the user input, so we alter our expected text
155 $this->assertEditedTextEquals( $baseText, $currentText );
158 if ( $user == null ) {
159 $user = $GLOBALS['wgUser'];
161 $this->setMwGlobals( 'wgUser', $user );
164 if ( !isset( $edit['wpEditToken'] ) ) {
165 $edit['wpEditToken'] = $user->getEditToken();
168 if ( !isset( $edit['wpEdittime'] ) ) {
169 $edit['wpEdittime'] = $page->exists() ?
$page->getTimestamp() : '';
172 if ( !isset( $edit['wpStarttime'] ) ) {
173 $edit['wpStarttime'] = wfTimestampNow();
176 $req = new FauxRequest( $edit, true ); // session ??
178 $article = new Article( $title );
179 $article->getContext()->setTitle( $title );
180 $ep = new EditPage( $article );
181 $ep->setContextTitle( $title );
182 $ep->importFormData( $req );
184 $bot = isset( $edit['bot'] ) ?
(bool)$edit['bot'] : false;
186 // this is where the edit happens!
187 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
188 // and throws exceptions like PermissionsError
189 $status = $ep->internalAttemptSave( $result, $bot );
191 if ( $expectedCode !== null ) {
193 $this->assertEquals( $expectedCode, $status->value
,
194 "Expected result code mismatch. $message" );
197 $page = WikiPage
::factory( $title );
199 if ( $expectedText !== null ) {
200 // check resulting page text
201 $content = $page->getContent();
202 $text = ContentHandler
::getContentText( $content );
204 # EditPage rtrim() the user input, so we alter our expected text
206 $this->assertEditedTextEquals( $expectedText, $text,
207 "Expected article text mismatch. $message" );
213 public static function provideCreatePages() {
215 [ 'expected article being created',
216 'EditPageTest_testCreatePage',
219 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
222 [ 'expected article not being created if empty',
223 'EditPageTest_testCreatePage',
226 EditPage
::AS_BLANK_ARTICLE
,
229 [ 'expected MediaWiki: page being created',
233 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
236 [ 'expected not-registered MediaWiki: page not being created if empty',
237 'MediaWiki:EditPageTest_testCreatePage',
240 EditPage
::AS_BLANK_ARTICLE
,
243 [ 'expected registered MediaWiki: page being created even if empty',
247 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
250 [ 'expected registered MediaWiki: page whose default content is empty'
251 . ' not being created if empty',
252 'MediaWiki:Ipb-default-expiry',
255 EditPage
::AS_BLANK_ARTICLE
,
258 [ 'expected MediaWiki: page not being created if text equals default message',
262 EditPage
::AS_BLANK_ARTICLE
,
265 [ 'expected empty article being created',
266 'EditPageTest_testCreatePage',
269 EditPage
::AS_SUCCESS_NEW_ARTICLE
,
277 * @dataProvider provideCreatePages
280 public function testCreatePage(
281 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
285 $this->setMwGlobals( 'wgHooks', [
286 'PageContentInsertComplete' => [ function (
287 WikiPage
&$page, User
&$user, Content
$content,
288 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
290 // types/refs checked
292 'PageContentSaveComplete' => [ function (
293 WikiPage
&$page, User
&$user, Content
$content,
294 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
295 Status
&$status, $baseRevId
296 ) use ( &$checkId ) {
297 $checkId = $status->value
['revision']->getId();
298 // types/refs checked
302 $edit = [ 'wpTextbox1' => $editText ];
303 if ( $ignoreBlank ) {
304 $edit['wpIgnoreBlankArticle'] = 1;
307 $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
309 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
310 $latest = $page->getLatest();
311 $page->doDeleteArticleReal( $pageTitle );
313 $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
314 $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
319 * @dataProvider provideCreatePages
322 public function testCreatePageTrx(
323 $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
326 $this->setMwGlobals( 'wgHooks', [
327 'PageContentInsertComplete' => [ function (
328 WikiPage
&$page, User
&$user, Content
$content,
329 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
331 // types/refs checked
333 'PageContentSaveComplete' => [ function (
334 WikiPage
&$page, User
&$user, Content
$content,
335 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
336 Status
&$status, $baseRevId
337 ) use ( &$checkIds ) {
338 $checkIds[] = $status->value
['revision']->getId();
339 // types/refs checked
343 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
345 $edit = [ 'wpTextbox1' => $editText ];
346 if ( $ignoreBlank ) {
347 $edit['wpIgnoreBlankArticle'] = 1;
350 $page = $this->assertEdit(
351 $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
353 $pageTitle2 = (string)$pageTitle . '/x';
354 $page2 = $this->assertEdit(
355 $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
357 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
359 $this->assertEquals( 0, DeferredUpdates
::pendingUpdatesCount(), 'No deferred updates' );
361 if ( $expectedCode != EditPage
::AS_BLANK_ARTICLE
) {
362 $latest = $page->getLatest();
363 $page->doDeleteArticleReal( $pageTitle );
365 $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
366 $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
368 $latest2 = $page2->getLatest();
369 $page2->doDeleteArticleReal( $pageTitle2 );
371 $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
372 $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
376 public function testUpdatePage() {
379 $this->setMwGlobals( 'wgHooks', [
380 'PageContentInsertComplete' => [ function (
381 WikiPage
&$page, User
&$user, Content
$content,
382 $summary, $minor, $u1, $u2, &$flags, Revision
$revision
384 // types/refs checked
386 'PageContentSaveComplete' => [ function (
387 WikiPage
&$page, User
&$user, Content
$content,
388 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
389 Status
&$status, $baseRevId
390 ) use ( &$checkIds ) {
391 $checkIds[] = $status->value
['revision']->getId();
392 // types/refs checked
398 'wpTextbox1' => $text,
399 'wpSummary' => 'first update',
402 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
403 EditPage
::AS_SUCCESS_UPDATE
, $text,
404 "expected successfull update with given text" );
405 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
407 $this->forceRevisionDate( $page, '20120101000000' );
411 'wpTextbox1' => $text,
412 'wpSummary' => 'second update',
415 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
416 EditPage
::AS_SUCCESS_UPDATE
, $text,
417 "expected successfull update with given text" );
418 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
419 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
422 public function testUpdatePageTrx() {
425 'wpTextbox1' => $text,
426 'wpSummary' => 'first update',
429 $page = $this->assertEdit( 'EditPageTest_testTrxUpdatePage', "zero", null, $edit,
430 EditPage
::AS_SUCCESS_UPDATE
, $text,
431 "expected successfull update with given text" );
433 $this->forceRevisionDate( $page, '20120101000000' );
436 $this->setMwGlobals( 'wgHooks', [
437 'PageContentSaveComplete' => [ function (
438 WikiPage
&$page, User
&$user, Content
$content,
439 $summary, $minor, $u1, $u2, &$flags, Revision
$revision,
440 Status
&$status, $baseRevId
441 ) use ( &$checkIds ) {
442 $checkIds[] = $status->value
['revision']->getId();
443 // types/refs checked
447 wfGetDB( DB_MASTER
)->begin( __METHOD__
);
451 'wpTextbox1' => $text,
452 'wpSummary' => 'second update',
455 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
456 EditPage
::AS_SUCCESS_UPDATE
, $text,
457 "expected successfull update with given text" );
461 'wpTextbox1' => $text,
462 'wpSummary' => 'third update',
465 $this->assertEdit( 'EditPageTest_testTrxUpdatePage', null, null, $edit,
466 EditPage
::AS_SUCCESS_UPDATE
, $text,
467 "expected successfull update with given text" );
469 wfGetDB( DB_MASTER
)->commit( __METHOD__
);
471 $this->assertGreaterThan( 0, $checkIds[0], "First event rev ID set" );
472 $this->assertGreaterThan( 0, $checkIds[1], "Second edit hook rev ID set" );
473 $this->assertGreaterThan( $checkIds[0], $checkIds[1], "Second event rev ID is higher" );
476 public static function provideSectionEdit() {
486 $sectionOne = '== one ==
490 $newSection = '== new section ==
495 $textWithNewSectionOne = preg_replace(
496 '/== one ==.*== two ==/ms',
497 "$sectionOne\n== two ==", $text
500 $textWithNewSectionAdded = "$text\n$newSection";
515 'replace first section',
516 $textWithNewSectionOne,
524 $textWithNewSectionAdded,
530 * @dataProvider provideSectionEdit
533 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
535 'wpTextbox1' => $text,
536 'wpSummary' => $summary,
537 'wpSection' => $section,
540 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
541 EditPage
::AS_SUCCESS_UPDATE
, $expected,
542 "expected successfull update of section" );
545 public static function provideAutoMerge() {
548 $tests[] = [ # 0: plain conflict
549 "Elmo", # base edit user
550 "one\n\ntwo\n\nthree\n",
553 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
557 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
559 EditPage
::AS_CONFLICT_DETECTED
, # expected code
560 "ONE\n\ntwo\n\nthree\n", # expected text
561 'expected edit conflict', # message
564 $tests[] = [ # 1: successful merge
565 "Elmo", # base edit user
566 "one\n\ntwo\n\nthree\n",
569 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
573 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
575 EditPage
::AS_SUCCESS_UPDATE
, # expected code
576 "ONE\n\ntwo\n\nTHREE\n", # expected text
577 'expected automatic merge', # message
581 $text .= "== first section ==\n\n";
582 $text .= "one\n\ntwo\n\nthree\n\n";
583 $text .= "== second section ==\n\n";
584 $text .= "four\n\nfive\n\nsix\n\n";
586 // extract the first section.
587 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
589 // generate expected text after merge
590 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
592 $tests[] = [ # 2: merge in section
593 "Elmo", # base edit user
597 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
602 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
605 EditPage
::AS_SUCCESS_UPDATE
, # expected code
606 $expected, # expected text
607 'expected automatic section merge', # message
610 // see whether it makes a difference who did the base edit
611 $testsWithAdam = array_map( function ( $test ) {
612 $test[0] = 'Adam'; // change base edit user
616 $testsWithBerta = array_map( function ( $test ) {
617 $test[0] = 'Berta'; // change base edit user
621 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
625 * @dataProvider provideAutoMerge
628 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
629 $expectedCode, $expectedText, $message = null
631 $this->markTestSkippedIfNoDiff3();
634 $ns = $this->getDefaultWikitextNS();
635 $title = Title
::newFromText( 'EditPageTest_testAutoMerge', $ns );
636 $page = WikiPage
::factory( $title );
638 if ( $page->exists() ) {
639 $page->doDeleteArticle( "clean slate for testing" );
643 'wpTextbox1' => $text,
646 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
647 $baseUser, $baseEdit, null, null, __METHOD__
);
649 $this->forceRevisionDate( $page, '20120101000000' );
651 $edittime = $page->getTimestamp();
653 // start timestamps for conflict detection
654 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
655 $adamsEdit['wpStarttime'] = 1;
658 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
659 $bertasEdit['wpStarttime'] = 2;
662 $starttime = wfTimestampNow();
663 $adamsTime = wfTimestamp(
665 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$adamsEdit['wpStarttime']
667 $bertasTime = wfTimestamp(
669 (int)wfTimestamp( TS_UNIX
, $starttime ) +
(int)$bertasEdit['wpStarttime']
672 $adamsEdit['wpStarttime'] = $adamsTime;
673 $bertasEdit['wpStarttime'] = $bertasTime;
675 $adamsEdit['wpSummary'] = 'Adam\'s edit';
676 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
678 $adamsEdit['wpEdittime'] = $edittime;
679 $bertasEdit['wpEdittime'] = $edittime;
682 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
683 EditPage
::AS_SUCCESS_UPDATE
, null, "expected successfull update" );
686 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
687 $expectedCode, $expectedText, $message );
691 * @depends testAutoMerge
693 public function testCheckDirectEditingDisallowed_forNonTextContent() {
694 $title = Title
::newFromText( 'Dummy:NonTextPageForEditPage' );
695 $page = WikiPage
::factory( $title );
697 $article = new Article( $title );
698 $article->getContext()->setTitle( $title );
699 $ep = new EditPage( $article );
700 $ep->setContextTitle( $title );
702 $user = $GLOBALS['wgUser'];
705 'wpTextbox1' => serialize( 'non-text content' ),
706 'wpEditToken' => $user->getEditToken(),
708 'wpStarttime' => wfTimestampNow()
711 $req = new FauxRequest( $edit, true );
712 $ep->importFormData( $req );
714 $this->setExpectedException(
716 'This content model is not supported: testing'
719 $ep->internalAttemptSave( $result, false );