4 * Tests for MediaWiki api.php?action=edit.
6 * @author Daniel Kinzler
14 class ApiEditPageTest
extends ApiTestCase
{
16 protected function setUp() {
17 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
21 $this->setMwGlobals( [
22 'wgExtraNamespaces' => $wgExtraNamespaces,
23 'wgNamespaceContentModels' => $wgNamespaceContentModels,
24 'wgContentHandlers' => $wgContentHandlers,
25 'wgContLang' => $wgContLang,
28 $wgExtraNamespaces[12312] = 'Dummy';
29 $wgExtraNamespaces[12313] = 'Dummy_talk';
30 $wgExtraNamespaces[12314] = 'DummyNonText';
31 $wgExtraNamespaces[12315] = 'DummyNonText_talk';
33 $wgNamespaceContentModels[12312] = "testing";
34 $wgNamespaceContentModels[12314] = "testing-nontext";
36 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
37 $wgContentHandlers["testing-nontext"] = 'DummyNonTextContentHandler';
38 $wgContentHandlers["testing-serialize-error"] =
39 'DummySerializeErrorContentHandler';
41 MWNamespace
::clearCaches();
42 $wgContLang->resetNamespaces(); # reset namespace cache
47 protected function tearDown() {
50 MWNamespace
::clearCaches();
51 $wgContLang->resetNamespaces(); # reset namespace cache
56 public function testEdit() {
57 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
59 // -- test new page --------------------------------------------
60 $apiResult = $this->doApiRequestWithToken( [
63 'text' => 'some text',
65 $apiResult = $apiResult[0];
67 // Validate API result data
68 $this->assertArrayHasKey( 'edit', $apiResult );
69 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
70 $this->assertSame( 'Success', $apiResult['edit']['result'] );
72 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
73 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
75 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
77 // -- test existing page, no change ----------------------------
78 $data = $this->doApiRequestWithToken( [
81 'text' => 'some text',
84 $this->assertSame( 'Success', $data[0]['edit']['result'] );
86 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
87 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
89 // -- test existing page, with change --------------------------
90 $data = $this->doApiRequestWithToken( [
93 'text' => 'different text'
96 $this->assertSame( 'Success', $data[0]['edit']['result'] );
98 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
99 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
101 $this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
102 $this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
103 $this->assertNotEquals(
104 $data[0]['edit']['newrevid'],
105 $data[0]['edit']['oldrevid'],
106 "revision id should change after edit"
113 public static function provideEditAppend() {
116 'foo', 'append', 'bar', "foobar"
119 'foo', 'prepend', 'bar', "barfoo"
121 [ # 2: append to empty page
122 '', 'append', 'foo', "foo"
124 [ # 3: prepend to empty page
125 '', 'prepend', 'foo', "foo"
127 [ # 4: append to non-existing page
128 null, 'append', 'foo', "foo"
130 [ # 5: prepend to non-existing page
131 null, 'prepend', 'foo', "foo"
137 * @dataProvider provideEditAppend
139 public function testEditAppend( $text, $op, $append, $expected ) {
143 // assume NS_HELP defaults to wikitext
144 $name = "Help:ApiEditPageTest_testEditAppend_$count";
146 // -- create page (or not) -----------------------------------------
147 if ( $text !== null ) {
148 list( $re ) = $this->doApiRequestWithToken( [
151 'text' => $text, ] );
153 $this->assertSame( 'Success', $re['edit']['result'] ); // sanity
156 // -- try append/prepend --------------------------------------------
157 list( $re ) = $this->doApiRequestWithToken( [
160 $op . 'text' => $append, ] );
162 $this->assertSame( 'Success', $re['edit']['result'] );
164 // -- validate -----------------------------------------------------
165 $page = new WikiPage( Title
::newFromText( $name ) );
166 $content = $page->getContent();
167 $this->assertNotNull( $content, 'Page should have been created' );
169 $text = $content->getNativeData();
171 $this->assertSame( $expected, $text );
175 * Test editing of sections
177 public function testEditSection() {
178 $name = 'Help:ApiEditPageTest_testEditSection';
179 $page = WikiPage
::factory( Title
::newFromText( $name ) );
180 $text = "==section 1==\ncontent 1\n==section 2==\ncontent2";
181 // Preload the page with some text
182 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ), 'summary' );
184 list( $re ) = $this->doApiRequestWithToken( [
188 'text' => "==section 1==\nnew content 1",
190 $this->assertSame( 'Success', $re['edit']['result'] );
191 $newtext = WikiPage
::factory( Title
::newFromText( $name ) )
192 ->getContent( Revision
::RAW
)
194 $this->assertSame( "==section 1==\nnew content 1\n\n==section 2==\ncontent2", $newtext );
196 // Test that we raise a 'nosuchsection' error
198 $this->doApiRequestWithToken( [
204 $this->fail( "Should have raised an ApiUsageException" );
205 } catch ( ApiUsageException
$e ) {
206 $this->assertTrue( self
::apiExceptionHasCode( $e, 'nosuchsection' ) );
211 * Test action=edit§ion=new
212 * Run it twice so we test adding a new section on a
213 * page that doesn't exist (T54830) and one that
216 public function testEditNewSection() {
217 $name = 'Help:ApiEditPageTest_testEditNewSection';
219 // Test on a page that does not already exist
220 $this->assertFalse( Title
::newFromText( $name )->exists() );
221 list( $re ) = $this->doApiRequestWithToken( [
226 'summary' => 'header',
229 $this->assertSame( 'Success', $re['edit']['result'] );
230 // Check the page text is correct
231 $text = WikiPage
::factory( Title
::newFromText( $name ) )
232 ->getContent( Revision
::RAW
)
234 $this->assertSame( "== header ==\n\ntest", $text );
236 // Now on one that does
237 $this->assertTrue( Title
::newFromText( $name )->exists() );
238 list( $re2 ) = $this->doApiRequestWithToken( [
243 'summary' => 'header',
246 $this->assertSame( 'Success', $re2['edit']['result'] );
247 $text = WikiPage
::factory( Title
::newFromText( $name ) )
248 ->getContent( Revision
::RAW
)
250 $this->assertSame( "== header ==\n\ntest\n\n== header ==\n\ntest", $text );
254 * Ensure we can edit through a redirect, if adding a section
256 public function testEdit_redirect() {
260 // assume NS_HELP defaults to wikitext
261 $name = "Help:ApiEditPageTest_testEdit_redirect_$count";
262 $title = Title
::newFromText( $name );
263 $page = WikiPage
::factory( $title );
265 $rname = "Help:ApiEditPageTest_testEdit_redirect_r$count";
266 $rtitle = Title
::newFromText( $rname );
267 $rpage = WikiPage
::factory( $rtitle );
269 // base edit for content
270 $page->doEditContent( new WikitextContent( "Foo" ),
271 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
272 $this->forceRevisionDate( $page, '20120101000000' );
273 $baseTime = $page->getRevision()->getTimestamp();
275 // base edit for redirect
276 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
277 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
278 $this->forceRevisionDate( $rpage, '20120101000000' );
280 // conflicting edit to redirect
281 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
282 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->getUser() );
283 $this->forceRevisionDate( $rpage, '20120101020202' );
285 // try to save edit, following the redirect
286 list( $re, , ) = $this->doApiRequestWithToken( [
289 'text' => 'nix bar!',
290 'basetimestamp' => $baseTime,
293 ], null, self
::$users['sysop']->getUser() );
295 $this->assertSame( 'Success', $re['edit']['result'],
296 "no problems expected when following redirect" );
300 * Ensure we cannot edit through a redirect, if attempting to overwrite content
302 public function testEdit_redirectText() {
306 // assume NS_HELP defaults to wikitext
307 $name = "Help:ApiEditPageTest_testEdit_redirectText_$count";
308 $title = Title
::newFromText( $name );
309 $page = WikiPage
::factory( $title );
311 $rname = "Help:ApiEditPageTest_testEdit_redirectText_r$count";
312 $rtitle = Title
::newFromText( $rname );
313 $rpage = WikiPage
::factory( $rtitle );
315 // base edit for content
316 $page->doEditContent( new WikitextContent( "Foo" ),
317 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
318 $this->forceRevisionDate( $page, '20120101000000' );
319 $baseTime = $page->getRevision()->getTimestamp();
321 // base edit for redirect
322 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
323 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
324 $this->forceRevisionDate( $rpage, '20120101000000' );
326 // conflicting edit to redirect
327 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
328 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->getUser() );
329 $this->forceRevisionDate( $rpage, '20120101020202' );
331 // try to save edit, following the redirect but without creating a section
333 $this->doApiRequestWithToken( [
336 'text' => 'nix bar!',
337 'basetimestamp' => $baseTime,
339 ], null, self
::$users['sysop']->getUser() );
341 $this->fail( 'redirect-appendonly error expected' );
342 } catch ( ApiUsageException
$ex ) {
343 $this->assertTrue( self
::apiExceptionHasCode( $ex, 'redirect-appendonly' ) );
347 public function testEditConflict() {
351 // assume NS_HELP defaults to wikitext
352 $name = "Help:ApiEditPageTest_testEditConflict_$count";
353 $title = Title
::newFromText( $name );
355 $page = WikiPage
::factory( $title );
358 $page->doEditContent( new WikitextContent( "Foo" ),
359 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
360 $this->forceRevisionDate( $page, '20120101000000' );
361 $baseTime = $page->getRevision()->getTimestamp();
364 $page->doEditContent( new WikitextContent( "Foo bar" ),
365 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->getUser() );
366 $this->forceRevisionDate( $page, '20120101020202' );
368 // try to save edit, expect conflict
370 $this->doApiRequestWithToken( [
373 'text' => 'nix bar!',
374 'basetimestamp' => $baseTime,
375 ], null, self
::$users['sysop']->getUser() );
377 $this->fail( 'edit conflict expected' );
378 } catch ( ApiUsageException
$ex ) {
379 $this->assertTrue( self
::apiExceptionHasCode( $ex, 'editconflict' ) );
384 * Ensure that editing using section=new will prevent simple conflicts
386 public function testEditConflict_newSection() {
390 // assume NS_HELP defaults to wikitext
391 $name = "Help:ApiEditPageTest_testEditConflict_newSection_$count";
392 $title = Title
::newFromText( $name );
394 $page = WikiPage
::factory( $title );
397 $page->doEditContent( new WikitextContent( "Foo" ),
398 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
399 $this->forceRevisionDate( $page, '20120101000000' );
400 $baseTime = $page->getRevision()->getTimestamp();
403 $page->doEditContent( new WikitextContent( "Foo bar" ),
404 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->getUser() );
405 $this->forceRevisionDate( $page, '20120101020202' );
407 // try to save edit, expect no conflict
408 list( $re, , ) = $this->doApiRequestWithToken( [
411 'text' => 'nix bar!',
412 'basetimestamp' => $baseTime,
414 ], null, self
::$users['sysop']->getUser() );
416 $this->assertSame( 'Success', $re['edit']['result'],
417 "no edit conflict expected here" );
420 public function testEditConflict_bug41990() {
425 * T43990: if the target page has a newer revision than the redirect, then editing the
426 * redirect while specifying 'redirect' and *not* specifying 'basetimestamp' erroneously
427 * caused an edit conflict to be detected.
430 // assume NS_HELP defaults to wikitext
431 $name = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_$count";
432 $title = Title
::newFromText( $name );
433 $page = WikiPage
::factory( $title );
435 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_r$count";
436 $rtitle = Title
::newFromText( $rname );
437 $rpage = WikiPage
::factory( $rtitle );
439 // base edit for content
440 $page->doEditContent( new WikitextContent( "Foo" ),
441 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
442 $this->forceRevisionDate( $page, '20120101000000' );
444 // base edit for redirect
445 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
446 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->getUser() );
447 $this->forceRevisionDate( $rpage, '20120101000000' );
449 // new edit to content
450 $page->doEditContent( new WikitextContent( "Foo bar" ),
451 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->getUser() );
452 $this->forceRevisionDate( $rpage, '20120101020202' );
454 // try to save edit; should work, following the redirect.
455 list( $re, , ) = $this->doApiRequestWithToken( [
458 'text' => 'nix bar!',
461 ], null, self
::$users['sysop']->getUser() );
463 $this->assertSame( 'Success', $re['edit']['result'],
464 "no edit conflict expected here" );
468 * @param WikiPage $page
469 * @param string|int $timestamp
471 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
472 $dbw = wfGetDB( DB_MASTER
);
474 $dbw->update( 'revision',
475 [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ],
476 [ 'rev_id' => $page->getLatest() ] );
481 public function testCheckDirectApiEditingDisallowed_forNonTextContent() {
482 $this->setExpectedException(
483 ApiUsageException
::class,
484 'Direct editing via API is not supported for content model ' .
485 'testing used by Dummy:ApiEditPageTest_nonTextPageEdit'
488 $this->doApiRequestWithToken( [
490 'title' => 'Dummy:ApiEditPageTest_nonTextPageEdit',
491 'text' => '{"animals":["kittens!"]}'
495 public function testSupportsDirectApiEditing_withContentHandlerOverride() {
496 $name = 'DummyNonText:ApiEditPageTest_testNonTextEdit';
497 $data = serialize( 'some bla bla text' );
499 $result = $this->doApiRequestWithToken( [
505 $apiResult = $result[0];
507 // Validate API result data
508 $this->assertArrayHasKey( 'edit', $apiResult );
509 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
510 $this->assertSame( 'Success', $apiResult['edit']['result'] );
512 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
513 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
515 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
517 // validate resulting revision
518 $page = WikiPage
::factory( Title
::newFromText( $name ) );
519 $this->assertSame( "testing-nontext", $page->getContentModel() );
520 $this->assertSame( $data, $page->getContent()->serialize() );
524 * This test verifies that after changing the content model
525 * of a page, undoing that edit via the API will also
526 * undo the content model change.
528 public function testUndoAfterContentModelChange() {
529 $name = 'Help:' . __FUNCTION__
;
530 $uploader = self
::$users['uploader']->getUser();
531 $sysop = self
::$users['sysop']->getUser();
532 $apiResult = $this->doApiRequestWithToken( [
535 'text' => 'some text',
536 ], null, $sysop )[0];
539 $this->assertArrayHasKey( 'edit', $apiResult );
540 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
541 $this->assertSame( 'Success', $apiResult['edit']['result'] );
542 $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
543 // Content model is wikitext
544 $this->assertSame( 'wikitext', $apiResult['edit']['contentmodel'] );
546 // Convert the page to JSON
547 $apiResult = $this->doApiRequestWithToken( [
551 'contentmodel' => 'json',
552 ], null, $uploader )[0];
555 $this->assertArrayHasKey( 'edit', $apiResult );
556 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
557 $this->assertSame( 'Success', $apiResult['edit']['result'] );
558 $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
559 $this->assertSame( 'json', $apiResult['edit']['contentmodel'] );
561 $apiResult = $this->doApiRequestWithToken( [
564 'undo' => $apiResult['edit']['newrevid']
565 ], null, $sysop )[0];
568 $this->assertArrayHasKey( 'edit', $apiResult );
569 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
570 $this->assertSame( 'Success', $apiResult['edit']['result'] );
571 $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
572 // Check that the contentmodel is back to wikitext now.
573 $this->assertSame( 'wikitext', $apiResult['edit']['contentmodel'] );
576 // The tests below are mostly not commented because they do exactly what
577 // you'd expect from the name.
579 public function testCorrectContentFormat() {
580 $name = 'Help:' . ucfirst( __FUNCTION__
);
582 $this->doApiRequestWithToken( [
585 'text' => 'some text',
586 'contentmodel' => 'wikitext',
587 'contentformat' => 'text/x-wiki',
590 $this->assertTrue( Title
::newFromText( $name )->exists() );
593 public function testUnsupportedContentFormat() {
594 $name = 'Help:' . ucfirst( __FUNCTION__
);
596 $this->setExpectedException( ApiUsageException
::class,
597 'Unrecognized value for parameter "contentformat": nonexistent format.' );
600 $this->doApiRequestWithToken( [
603 'text' => 'some text',
604 'contentformat' => 'nonexistent format',
607 $this->assertFalse( Title
::newFromText( $name )->exists() );
611 public function testMismatchedContentFormat() {
612 $name = 'Help:' . ucfirst( __FUNCTION__
);
614 $this->setExpectedException( ApiUsageException
::class,
615 'The requested format text/plain is not supported for content ' .
616 "model wikitext used by $name." );
619 $this->doApiRequestWithToken( [
622 'text' => 'some text',
623 'contentmodel' => 'wikitext',
624 'contentformat' => 'text/plain',
627 $this->assertFalse( Title
::newFromText( $name )->exists() );
631 public function testUndoToInvalidRev() {
632 $name = 'Help:' . ucfirst( __FUNCTION__
);
634 $revId = $this->editPage( $name, 'Some text' )->value
['revision']
638 $this->setExpectedException( ApiUsageException
::class,
639 "There is no revision with ID $revId." );
641 $this->doApiRequestWithToken( [
649 * Tests what happens if the undo parameter is a valid revision, but
650 * the undoafter parameter doesn't refer to a revision that exists in the
653 public function testUndoAfterToInvalidRev() {
654 // We can't just pick a large number for undoafter (as in
655 // testUndoToInvalidRev above), because then MediaWiki will helpfully
656 // assume we switched around undo and undoafter and we'll test the code
657 // path for undo being invalid, not undoafter. So instead we delete
658 // the revision from the database. In real life this case could come
659 // up if a revision number was skipped, e.g., if two transactions try
660 // to insert new revision rows at once and the first one to succeed
662 $name = 'Help:' . ucfirst( __FUNCTION__
);
663 $titleObj = Title
::newFromText( $name );
665 $revId1 = $this->editPage( $name, '1' )->value
['revision']->getId();
666 $revId2 = $this->editPage( $name, '2' )->value
['revision']->getId();
667 $revId3 = $this->editPage( $name, '3' )->value
['revision']->getId();
669 // Make the middle revision disappear
670 $dbw = wfGetDB( DB_MASTER
);
671 $dbw->delete( 'revision', [ 'rev_id' => $revId2 ], __METHOD__
);
672 $dbw->update( 'revision', [ 'rev_parent_id' => $revId1 ],
673 [ 'rev_id' => $revId3 ], __METHOD__
);
675 $this->setExpectedException( ApiUsageException
::class,
676 "There is no revision with ID $revId2." );
678 $this->doApiRequestWithToken( [
682 'undoafter' => $revId2,
687 * Tests what happens if the undo parameter is a valid revision, but
688 * undoafter is hidden (rev_deleted).
690 public function testUndoAfterToHiddenRev() {
691 $name = 'Help:' . ucfirst( __FUNCTION__
);
692 $titleObj = Title
::newFromText( $name );
694 $this->editPage( $name, '0' );
696 $revId1 = $this->editPage( $name, '1' )->value
['revision']->getId();
698 $revId2 = $this->editPage( $name, '2' )->value
['revision']->getId();
700 // Hide the middle revision
701 $list = RevisionDeleter
::createList( 'revision',
702 RequestContext
::getMain(), $titleObj, [ $revId1 ] );
703 $list->setVisibility( [
704 'value' => [ Revision
::DELETED_TEXT
=> 1 ],
705 'comment' => 'Bye-bye',
708 $this->setExpectedException( ApiUsageException
::class,
709 "There is no revision with ID $revId1." );
711 $this->doApiRequestWithToken( [
715 'undoafter' => $revId1,
720 * Test undo when a revision with a higher id has an earlier timestamp.
721 * This can happen if importing an old revision.
723 public function testUndoWithSwappedRevisions() {
724 $name = 'Help:' . ucfirst( __FUNCTION__
);
725 $titleObj = Title
::newFromText( $name );
727 $this->editPage( $name, '0' );
729 $revId2 = $this->editPage( $name, '2' )->value
['revision']->getId();
731 $revId1 = $this->editPage( $name, '1' )->value
['revision']->getId();
733 // Now monkey with the timestamp
734 $dbw = wfGetDB( DB_MASTER
);
737 [ 'rev_timestamp' => wfTimestamp( TS_MW
, time() - 86400 ) ],
738 [ 'rev_id' => $revId1 ],
742 $this->doApiRequestWithToken( [
746 'undoafter' => $revId1,
749 $text = ( new WikiPage( $titleObj ) )->getContent()->getNativeData();
751 // This is wrong! It should be 1. But let's test for our incorrect
752 // behavior for now, so if someone fixes it they'll fix the test as
753 // well to expect 1. If we disabled the test, it might stay disabled
754 // even once the bug is fixed, which would be a shame.
755 $this->assertSame( '2', $text );
758 public function testUndoWithConflicts() {
759 $name = 'Help:' . ucfirst( __FUNCTION__
);
761 $this->setExpectedException( ApiUsageException
::class,
762 'The edit could not be undone due to conflicting intermediate edits.' );
764 $this->editPage( $name, '1' );
766 $revId = $this->editPage( $name, '2' )->value
['revision']->getId();
768 $this->editPage( $name, '3' );
770 $this->doApiRequestWithToken( [
776 $text = ( new WikiPage( Title
::newFromText( $name ) ) )->getContent()
778 $this->assertSame( '3', $text );
782 * undoafter is supposed to be less than undo. If not, we reverse their
783 * meaning, so that the two are effectively interchangeable.
785 public function testReversedUndoAfter() {
786 $name = 'Help:' . ucfirst( __FUNCTION__
);
788 $this->editPage( $name, '0' );
789 $revId1 = $this->editPage( $name, '1' )->value
['revision']->getId();
790 $revId2 = $this->editPage( $name, '2' )->value
['revision']->getId();
792 $this->doApiRequestWithToken( [
796 'undoafter' => $revId2,
799 $text = ( new WikiPage( Title
::newFromText( $name ) ) )->getContent()
801 $this->assertSame( '1', $text );
804 public function testUndoToRevFromDifferentPage() {
805 $name = 'Help:' . ucfirst( __FUNCTION__
);
807 $this->editPage( "$name-1", 'Some text' );
808 $revId = $this->editPage( "$name-1", 'Some more text' )
809 ->value
['revision']->getId();
811 $this->editPage( "$name-2", 'Some text' );
813 $this->setExpectedException( ApiUsageException
::class,
814 "r$revId is not a revision of $name-2." );
816 $this->doApiRequestWithToken( [
818 'title' => "$name-2",
823 public function testUndoAfterToRevFromDifferentPage() {
824 $name = 'Help:' . ucfirst( __FUNCTION__
);
826 $revId1 = $this->editPage( "$name-1", 'Some text' )
827 ->value
['revision']->getId();
829 $revId2 = $this->editPage( "$name-2", 'Some text' )
830 ->value
['revision']->getId();
832 $this->setExpectedException( ApiUsageException
::class,
833 "r$revId1 is not a revision of $name-2." );
835 $this->doApiRequestWithToken( [
837 'title' => "$name-2",
839 'undoafter' => $revId1,
843 public function testMd5Text() {
844 $name = 'Help:' . ucfirst( __FUNCTION__
);
846 $this->assertFalse( Title
::newFromText( $name )->exists() );
848 $this->doApiRequestWithToken( [
851 'text' => 'Some text',
852 'md5' => md5( 'Some text' ),
855 $this->assertTrue( Title
::newFromText( $name )->exists() );
858 public function testMd5PrependText() {
859 $name = 'Help:' . ucfirst( __FUNCTION__
);
861 $this->editPage( $name, 'Some text' );
863 $this->doApiRequestWithToken( [
866 'prependtext' => 'Alert: ',
867 'md5' => md5( 'Alert: ' ),
870 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
871 ->getContent()->getNativeData();
872 $this->assertSame( 'Alert: Some text', $text );
875 public function testMd5AppendText() {
876 $name = 'Help:' . ucfirst( __FUNCTION__
);
878 $this->editPage( $name, 'Some text' );
880 $this->doApiRequestWithToken( [
883 'appendtext' => ' is nice',
884 'md5' => md5( ' is nice' ),
887 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
888 ->getContent()->getNativeData();
889 $this->assertSame( 'Some text is nice', $text );
892 public function testMd5PrependAndAppendText() {
893 $name = 'Help:' . ucfirst( __FUNCTION__
);
895 $this->editPage( $name, 'Some text' );
897 $this->doApiRequestWithToken( [
900 'prependtext' => 'Alert: ',
901 'appendtext' => ' is nice',
902 'md5' => md5( 'Alert: is nice' ),
905 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
906 ->getContent()->getNativeData();
907 $this->assertSame( 'Alert: Some text is nice', $text );
910 public function testIncorrectMd5Text() {
911 $name = 'Help:' . ucfirst( __FUNCTION__
);
913 $this->setExpectedException( ApiUsageException
::class,
914 'The supplied MD5 hash was incorrect.' );
916 $this->doApiRequestWithToken( [
919 'text' => 'Some text',
924 public function testIncorrectMd5PrependText() {
925 $name = 'Help:' . ucfirst( __FUNCTION__
);
927 $this->setExpectedException( ApiUsageException
::class,
928 'The supplied MD5 hash was incorrect.' );
930 $this->doApiRequestWithToken( [
933 'prependtext' => 'Some ',
934 'appendtext' => 'text',
935 'md5' => md5( 'Some ' ),
939 public function testIncorrectMd5AppendText() {
940 $name = 'Help:' . ucfirst( __FUNCTION__
);
942 $this->setExpectedException( ApiUsageException
::class,
943 'The supplied MD5 hash was incorrect.' );
945 $this->doApiRequestWithToken( [
948 'prependtext' => 'Some ',
949 'appendtext' => 'text',
950 'md5' => md5( 'text' ),
954 public function testCreateOnly() {
955 $name = 'Help:' . ucfirst( __FUNCTION__
);
957 $this->setExpectedException( ApiUsageException
::class,
958 'The article you tried to create has been created already.' );
960 $this->editPage( $name, 'Some text' );
961 $this->assertTrue( Title
::newFromText( $name )->exists() );
964 $this->doApiRequestWithToken( [
967 'text' => 'Some more text',
971 // Validate that content was not changed
972 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
973 ->getContent()->getNativeData();
975 $this->assertSame( 'Some text', $text );
979 public function testNoCreate() {
980 $name = 'Help:' . ucfirst( __FUNCTION__
);
982 $this->setExpectedException( ApiUsageException
::class,
983 "The page you specified doesn't exist." );
985 $this->assertFalse( Title
::newFromText( $name )->exists() );
988 $this->doApiRequestWithToken( [
991 'text' => 'Some text',
995 $this->assertFalse( Title
::newFromText( $name )->exists() );
1000 * Appending/prepending is currently only supported for TextContent. We
1001 * test this right now, and when support is added this test should be
1002 * replaced by tests that the support is correct.
1004 public function testAppendWithNonTextContentHandler() {
1005 $name = 'MediaWiki:' . ucfirst( __FUNCTION__
);
1007 $this->setExpectedException( ApiUsageException
::class,
1008 "Can't append to pages using content model testing-nontext." );
1010 $this->setTemporaryHook( 'ContentHandlerDefaultModelFor',
1011 function ( Title
$title, &$model ) use ( $name ) {
1012 if ( $title->getPrefixedText() === $name ) {
1013 $model = 'testing-nontext';
1019 $this->doApiRequestWithToken( [
1022 'appendtext' => 'Some text',
1026 public function testAppendInMediaWikiNamespace() {
1027 $name = 'MediaWiki:' . ucfirst( __FUNCTION__
);
1029 $this->assertFalse( Title
::newFromText( $name )->exists() );
1031 $this->doApiRequestWithToken( [
1034 'appendtext' => 'Some text',
1037 $this->assertTrue( Title
::newFromText( $name )->exists() );
1040 public function testAppendInMediaWikiNamespaceWithSerializationError() {
1041 $name = 'MediaWiki:' . ucfirst( __FUNCTION__
);
1043 $this->setExpectedException( ApiUsageException
::class,
1044 'Content serialization failed: Could not unserialize content' );
1046 $this->setTemporaryHook( 'ContentHandlerDefaultModelFor',
1047 function ( Title
$title, &$model ) use ( $name ) {
1048 if ( $title->getPrefixedText() === $name ) {
1049 $model = 'testing-serialize-error';
1055 $this->doApiRequestWithToken( [
1058 'appendtext' => 'Some text',
1062 public function testAppendNewSection() {
1063 $name = 'Help:' . ucfirst( __FUNCTION__
);
1065 $this->editPage( $name, 'Initial content' );
1067 $this->doApiRequestWithToken( [
1070 'appendtext' => '== New section ==',
1074 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
1075 ->getContent()->getNativeData();
1077 $this->assertSame( "Initial content\n\n== New section ==", $text );
1080 public function testAppendNewSectionWithInvalidContentModel() {
1081 $name = 'Help:' . ucfirst( __FUNCTION__
);
1083 $this->setExpectedException( ApiUsageException
::class,
1084 'Sections are not supported for content model text.' );
1086 $this->editPage( $name, 'Initial content' );
1088 $this->doApiRequestWithToken( [
1091 'appendtext' => '== New section ==',
1093 'contentmodel' => 'text',
1097 public function testAppendNewSectionWithTitle() {
1098 $name = 'Help:' . ucfirst( __FUNCTION__
);
1100 $this->editPage( $name, 'Initial content' );
1102 $this->doApiRequestWithToken( [
1105 'sectiontitle' => 'My section',
1106 'appendtext' => 'More content',
1110 $page = new WikiPage( Title
::newFromText( $name ) );
1112 $this->assertSame( "Initial content\n\n== My section ==\n\nMore content",
1113 $page->getContent()->getNativeData() );
1114 $this->assertSame( '/* My section */ new section',
1115 $page->getRevision()->getComment() );
1118 public function testAppendNewSectionWithSummary() {
1119 $name = 'Help:' . ucfirst( __FUNCTION__
);
1121 $this->editPage( $name, 'Initial content' );
1123 $this->doApiRequestWithToken( [
1126 'appendtext' => 'More content',
1128 'summary' => 'Add new section',
1131 $page = new WikiPage( Title
::newFromText( $name ) );
1133 $this->assertSame( "Initial content\n\n== Add new section ==\n\nMore content",
1134 $page->getContent()->getNativeData() );
1135 // EditPage actually assumes the summary is the section name here
1136 $this->assertSame( '/* Add new section */ new section',
1137 $page->getRevision()->getComment() );
1140 public function testAppendNewSectionWithTitleAndSummary() {
1141 $name = 'Help:' . ucfirst( __FUNCTION__
);
1143 $this->editPage( $name, 'Initial content' );
1145 $this->doApiRequestWithToken( [
1148 'sectiontitle' => 'My section',
1149 'appendtext' => 'More content',
1151 'summary' => 'Add new section',
1154 $page = new WikiPage( Title
::newFromText( $name ) );
1156 $this->assertSame( "Initial content\n\n== My section ==\n\nMore content",
1157 $page->getContent()->getNativeData() );
1158 $this->assertSame( 'Add new section',
1159 $page->getRevision()->getComment() );
1162 public function testAppendToSection() {
1163 $name = 'Help:' . ucfirst( __FUNCTION__
);
1165 $this->editPage( $name, "== Section 1 ==\n\nContent\n\n" .
1166 "== Section 2 ==\n\nFascinating!" );
1168 $this->doApiRequestWithToken( [
1171 'appendtext' => ' and more content',
1175 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
1176 ->getContent()->getNativeData();
1178 $this->assertSame( "== Section 1 ==\n\nContent and more content\n\n" .
1179 "== Section 2 ==\n\nFascinating!", $text );
1182 public function testAppendToFirstSection() {
1183 $name = 'Help:' . ucfirst( __FUNCTION__
);
1185 $this->editPage( $name, "Content\n\n== Section 1 ==\n\nFascinating!" );
1187 $this->doApiRequestWithToken( [
1190 'appendtext' => ' and more content',
1194 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
1195 ->getContent()->getNativeData();
1197 $this->assertSame( "Content and more content\n\n== Section 1 ==\n\n" .
1198 "Fascinating!", $text );
1201 public function testAppendToNonexistentSection() {
1202 $name = 'Help:' . ucfirst( __FUNCTION__
);
1204 $this->setExpectedException( ApiUsageException
::class, 'There is no section 1.' );
1206 $this->editPage( $name, 'Content' );
1209 $this->doApiRequestWithToken( [
1212 'appendtext' => ' and more content',
1216 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
1217 ->getContent()->getNativeData();
1219 $this->assertSame( 'Content', $text );
1223 public function testEditMalformedSection() {
1224 $name = 'Help:' . ucfirst( __FUNCTION__
);
1226 $this->setExpectedException( ApiUsageException
::class,
1227 'The "section" parameter must be a valid section ID or "new".' );
1228 $this->editPage( $name, 'Content' );
1231 $this->doApiRequestWithToken( [
1234 'text' => 'Different content',
1235 'section' => 'It is unlikely that this is valid',
1238 $text = ( new WikiPage( Title
::newFromText( $name ) ) )
1239 ->getContent()->getNativeData();
1241 $this->assertSame( 'Content', $text );
1245 public function testEditWithStartTimestamp() {
1246 $name = 'Help:' . ucfirst( __FUNCTION__
);
1247 $this->setExpectedException( ApiUsageException
::class,
1248 'The page has been deleted since you fetched its timestamp.' );
1250 $startTime = MWTimestamp
::convert( TS_MW
, time() - 1 );
1252 $this->editPage( $name, 'Some text' );
1254 $pageObj = new WikiPage( Title
::newFromText( $name ) );
1255 $pageObj->doDeleteArticle( 'Bye-bye' );
1257 $this->assertFalse( $pageObj->exists() );
1260 $this->doApiRequestWithToken( [
1263 'text' => 'Different text',
1264 'starttimestamp' => $startTime,
1267 $this->assertFalse( $pageObj->exists() );
1271 public function testEditMinor() {
1272 $name = 'Help:' . ucfirst( __FUNCTION__
);
1274 $this->editPage( $name, 'Some text' );
1276 $this->doApiRequestWithToken( [
1279 'text' => 'Different text',
1283 $revisionStore = \MediaWiki\MediaWikiServices
::getInstance()->getRevisionStore();
1284 $revision = $revisionStore->getRevisionByTitle( Title
::newFromText( $name ) );
1285 $this->assertTrue( $revision->isMinor() );
1288 public function testEditRecreate() {
1289 $name = 'Help:' . ucfirst( __FUNCTION__
);
1291 $startTime = MWTimestamp
::convert( TS_MW
, time() - 1 );
1293 $this->editPage( $name, 'Some text' );
1295 $pageObj = new WikiPage( Title
::newFromText( $name ) );
1296 $pageObj->doDeleteArticle( 'Bye-bye' );
1298 $this->assertFalse( $pageObj->exists() );
1300 $this->doApiRequestWithToken( [
1303 'text' => 'Different text',
1304 'starttimestamp' => $startTime,
1308 $this->assertTrue( Title
::newFromText( $name )->exists() );
1311 public function testEditWatch() {
1312 $name = 'Help:' . ucfirst( __FUNCTION__
);
1313 $user = self
::$users['sysop']->getUser();
1315 $this->doApiRequestWithToken( [
1318 'text' => 'Some text',
1322 $this->assertTrue( Title
::newFromText( $name )->exists() );
1323 $this->assertTrue( $user->isWatched( Title
::newFromText( $name ) ) );
1326 public function testEditUnwatch() {
1327 $name = 'Help:' . ucfirst( __FUNCTION__
);
1328 $user = self
::$users['sysop']->getUser();
1329 $titleObj = Title
::newFromText( $name );
1331 $user->addWatch( $titleObj );
1333 $this->assertFalse( $titleObj->exists() );
1334 $this->assertTrue( $user->isWatched( $titleObj ) );
1336 $this->doApiRequestWithToken( [
1339 'text' => 'Some text',
1343 $this->assertTrue( $titleObj->exists() );
1344 $this->assertFalse( $user->isWatched( $titleObj ) );
1347 public function testEditWithTag() {
1348 $name = 'Help:' . ucfirst( __FUNCTION__
);
1350 ChangeTags
::defineTag( 'custom tag' );
1352 $revId = $this->doApiRequestWithToken( [
1355 'text' => 'Some text',
1356 'tags' => 'custom tag',
1357 ] )[0]['edit']['newrevid'];
1359 $dbw = wfGetDB( DB_MASTER
);
1360 $this->assertSame( 'custom tag', $dbw->selectField(
1361 'change_tag', 'ct_tag', [ 'ct_rev_id' => $revId ], __METHOD__
) );
1364 public function testEditWithoutTagPermission() {
1365 $name = 'Help:' . ucfirst( __FUNCTION__
);
1367 $this->setExpectedException( ApiUsageException
::class,
1368 'You do not have permission to apply change tags along with your changes.' );
1370 $this->assertFalse( Title
::newFromText( $name )->exists() );
1372 ChangeTags
::defineTag( 'custom tag' );
1373 $this->setMwGlobals( 'wgRevokePermissions',
1374 [ 'user' => [ 'applychangetags' => true ] ] );
1376 $this->doApiRequestWithToken( [
1379 'text' => 'Some text',
1380 'tags' => 'custom tag',
1383 $this->assertFalse( Title
::newFromText( $name )->exists() );
1387 public function testEditAbortedByHook() {
1388 $name = 'Help:' . ucfirst( __FUNCTION__
);
1390 $this->setExpectedException( ApiUsageException
::class,
1391 'The modification you tried to make was aborted by an extension.' );
1393 $this->hideDeprecated( 'APIEditBeforeSave hook (used in ' .
1394 'hook-APIEditBeforeSave-closure)' );
1396 $this->setTemporaryHook( 'APIEditBeforeSave',
1403 $this->doApiRequestWithToken( [
1406 'text' => 'Some text',
1409 $this->assertFalse( Title
::newFromText( $name )->exists() );
1413 public function testEditAbortedByHookWithCustomOutput() {
1414 $name = 'Help:' . ucfirst( __FUNCTION__
);
1416 $this->hideDeprecated( 'APIEditBeforeSave hook (used in ' .
1417 'hook-APIEditBeforeSave-closure)' );
1419 $this->setTemporaryHook( 'APIEditBeforeSave',
1420 function ( $unused1, $unused2, &$r ) {
1421 $r['msg'] = 'Some message';
1425 $result = $this->doApiRequestWithToken( [
1428 'text' => 'Some text',
1430 Wikimedia\restoreWarnings
();
1432 $this->assertSame( [ 'msg' => 'Some message', 'result' => 'Failure' ],
1433 $result[0]['edit'] );
1435 $this->assertFalse( Title
::newFromText( $name )->exists() );
1438 public function testEditAbortedByEditPageHookWithResult() {
1439 $name = 'Help:' . ucfirst( __FUNCTION__
);
1441 $this->setTemporaryHook( 'EditFilterMergedContent',
1442 function ( $unused1, $unused2, Status
$status ) {
1443 $status->apiHookResult
= [ 'msg' => 'A message for you!' ];
1447 $res = $this->doApiRequestWithToken( [
1450 'text' => 'Some text',
1453 $this->assertFalse( Title
::newFromText( $name )->exists() );
1454 $this->assertSame( [ 'edit' => [ 'msg' => 'A message for you!',
1455 'result' => 'Failure' ] ], $res[0] );
1458 public function testEditAbortedByEditPageHookWithNoResult() {
1459 $name = 'Help:' . ucfirst( __FUNCTION__
);
1461 $this->setExpectedException( ApiUsageException
::class,
1462 'The modification you tried to make was aborted by an extension.' );
1464 $this->setTemporaryHook( 'EditFilterMergedContent',
1471 $this->doApiRequestWithToken( [
1474 'text' => 'Some text',
1477 $this->assertFalse( Title
::newFromText( $name )->exists() );
1481 public function testEditWhileBlocked() {
1482 $name = 'Help:' . ucfirst( __FUNCTION__
);
1484 $this->setExpectedException( ApiUsageException
::class,
1485 'You have been blocked from editing.' );
1487 $block = new Block( [
1488 'address' => self
::$users['sysop']->getUser()->getName(),
1489 'by' => self
::$users['sysop']->getUser()->getId(),
1490 'reason' => 'Capriciousness',
1491 'timestamp' => '19370101000000',
1492 'expiry' => 'infinity',
1497 $this->doApiRequestWithToken( [
1500 'text' => 'Some text',
1504 self
::$users['sysop']->getUser()->clearInstanceCache();
1508 public function testEditWhileReadOnly() {
1509 $name = 'Help:' . ucfirst( __FUNCTION__
);
1511 $this->setExpectedException( ApiUsageException
::class,
1512 'The wiki is currently in read-only mode.' );
1514 $svc = \MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode();
1515 $svc->setReason( "Read-only for testing" );
1518 $this->doApiRequestWithToken( [
1521 'text' => 'Some text',
1524 $svc->setReason( false );
1528 public function testCreateImageRedirectAnon() {
1529 $name = 'File:' . ucfirst( __FUNCTION__
);
1531 // @todo When ApiTestCase supports anonymous users, this exception
1532 // should no longer be thrown, and the test can then be updated to test
1533 // for the actual expected behavior.
1534 $this->setExpectedException( ApiUsageException
::class,
1535 'Invalid CSRF token.' );
1537 $this->doApiRequestWithToken( [
1538 'action' => 'logout',
1541 $this->doApiRequestWithToken( [
1544 'text' => '#REDIRECT [[File:Other file.png]]',
1548 public function testCreateImageRedirectLoggedIn() {
1549 $name = 'File:' . ucfirst( __FUNCTION__
);
1551 $this->setExpectedException( ApiUsageException
::class,
1552 "You don't have permission to create image redirects." );
1554 $this->setMwGlobals( 'wgRevokePermissions',
1555 [ 'user' => [ 'upload' => true ] ] );
1557 $this->doApiRequestWithToken( [
1560 'text' => '#REDIRECT [[File:Other file.png]]',
1564 public function testTooBigEdit() {
1565 $name = 'Help:' . ucfirst( __FUNCTION__
);
1567 $this->setExpectedException( ApiUsageException
::class,
1568 'The content you supplied exceeds the article size limit of 1 kilobyte.' );
1570 $this->setMwGlobals( 'wgMaxArticleSize', 1 );
1572 $text = str_repeat( '!', 1025 );
1574 $this->doApiRequestWithToken( [
1581 public function testProhibitedAnonymousEdit() {
1582 $name = 'Help:' . ucfirst( __FUNCTION__
);
1584 // @todo See comment in testCreateImageRedirectAnon
1585 $this->setExpectedException( ApiUsageException
::class,
1586 'Invalid CSRF token.' );
1587 $this->setMwGlobals( 'wgRevokePermissions',
1588 [ '*' => [ 'edit' => true ] ] );
1590 $this->doApiRequestWithToken( [
1591 'action' => 'logout',
1594 $this->doApiRequestWithToken( [
1597 'text' => 'Some text',
1601 public function testProhibitedChangeContentModel() {
1602 $name = 'Help:' . ucfirst( __FUNCTION__
);
1604 $this->setExpectedException( ApiUsageException
::class,
1605 "You don't have permission to change the content model of a page." );
1607 $this->setMwGlobals( 'wgRevokePermissions',
1608 [ 'user' => [ 'editcontentmodel' => true ] ] );
1610 $this->doApiRequestWithToken( [
1613 'text' => 'Some text',
1614 'contentmodel' => 'json',