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( array(
22 'wgExtraNamespaces' => $wgExtraNamespaces,
23 'wgNamespaceContentModels' => $wgNamespaceContentModels,
24 'wgContentHandlers' => $wgContentHandlers,
25 'wgContLang' => $wgContLang,
28 $wgExtraNamespaces[12312] = 'Dummy';
29 $wgExtraNamespaces[12313] = 'Dummy_talk';
31 $wgNamespaceContentModels[12312] = "testing";
32 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
34 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
35 $wgContLang->resetNamespaces(); # reset namespace cache
40 protected function tearDown() {
41 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
45 public function testEdit() {
46 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
48 // -- test new page --------------------------------------------
49 $apiResult = $this->doApiRequestWithToken( array(
52 'text' => 'some text',
54 $apiResult = $apiResult[0];
56 // Validate API result data
57 $this->assertArrayHasKey( 'edit', $apiResult );
58 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
59 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
61 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
62 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
64 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
66 // -- test existing page, no change ----------------------------
67 $data = $this->doApiRequestWithToken( array(
70 'text' => 'some text',
73 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
75 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
76 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
78 // -- test existing page, with change --------------------------
79 $data = $this->doApiRequestWithToken( array(
82 'text' => 'different text'
85 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
87 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
88 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
90 $this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
91 $this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
92 $this->assertNotEquals(
93 $data[0]['edit']['newrevid'],
94 $data[0]['edit']['oldrevid'],
95 "revision id should change after edit"
99 public function testNonTextEdit() {
100 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
101 $data = serialize( 'some bla bla text' );
103 // -- test new page --------------------------------------------
104 $apiResult = $this->doApiRequestWithToken( array(
107 'text' => $data, ) );
108 $apiResult = $apiResult[0];
110 // Validate API result data
111 $this->assertArrayHasKey( 'edit', $apiResult );
112 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
113 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
115 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
116 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
118 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
120 // validate resulting revision
121 $page = WikiPage
::factory( Title
::newFromText( $name ) );
122 $this->assertEquals( "testing", $page->getContentModel() );
123 $this->assertEquals( $data, $page->getContent()->serialize() );
129 public static function provideEditAppend() {
132 'foo', 'append', 'bar', "foobar"
135 'foo', 'prepend', 'bar', "barfoo"
137 array( #2: append to empty page
138 '', 'append', 'foo', "foo"
140 array( #3: prepend to empty page
141 '', 'prepend', 'foo', "foo"
143 array( #4: append to non-existing page
144 null, 'append', 'foo', "foo"
146 array( #5: prepend to non-existing page
147 null, 'prepend', 'foo', "foo"
153 * @dataProvider provideEditAppend
155 public function testEditAppend( $text, $op, $append, $expected ) {
159 // assume NS_HELP defaults to wikitext
160 $name = "Help:ApiEditPageTest_testEditAppend_$count";
162 // -- create page (or not) -----------------------------------------
163 if ( $text !== null ) {
164 if ( $text === '' ) {
165 // can't create an empty page, so create it with some content
166 $this->doApiRequestWithToken( array(
169 'text' => '(dummy)', ) );
172 list( $re ) = $this->doApiRequestWithToken( array(
175 'text' => $text, ) );
177 $this->assertEquals( 'Success', $re['edit']['result'] ); // sanity
180 // -- try append/prepend --------------------------------------------
181 list( $re ) = $this->doApiRequestWithToken( array(
184 $op . 'text' => $append, ) );
186 $this->assertEquals( 'Success', $re['edit']['result'] );
188 // -- validate -----------------------------------------------------
189 $page = new WikiPage( Title
::newFromText( $name ) );
190 $content = $page->getContent();
191 $this->assertNotNull( $content, 'Page should have been created' );
193 $text = $content->getNativeData();
195 $this->assertEquals( $expected, $text );
199 * Test editing of sections
201 public function testEditSection() {
202 $name = 'Help:ApiEditPageTest_testEditSection';
203 $page = WikiPage
::factory( Title
::newFromText( $name ) );
204 $text = "==section 1==\ncontent 1\n==section 2==\ncontent2";
205 // Preload the page with some text
206 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ), 'summary' );
208 list( $re ) = $this->doApiRequestWithToken( array(
212 'text' => "==section 1==\nnew content 1",
214 $this->assertEquals( 'Success', $re['edit']['result'] );
215 $newtext = WikiPage
::factory( Title
::newFromText( $name ) )
216 ->getContent( Revision
::RAW
)
218 $this->assertEquals( "==section 1==\nnew content 1\n\n==section 2==\ncontent2", $newtext );
220 // Test that we raise a 'nosuchsection' error
222 $this->doApiRequestWithToken( array(
228 $this->fail( "Should have raised a UsageException" );
229 } catch ( UsageException
$e ) {
230 $this->assertEquals( 'nosuchsection', $e->getCodeString() );
235 * Test action=edit§ion=new
236 * Run it twice so we test adding a new section on a
237 * page that doesn't exist (bug 52830) and one that
240 public function testEditNewSection() {
241 $name = 'Help:ApiEditPageTest_testEditNewSection';
243 // Test on a page that does not already exist
244 $this->assertFalse( Title
::newFromText( $name )->exists() );
245 list( $re ) = $this->doApiRequestWithToken( array(
250 'summary' => 'header',
253 $this->assertEquals( 'Success', $re['edit']['result'] );
254 // Check the page text is correct
255 $text = WikiPage
::factory( Title
::newFromText( $name ) )
256 ->getContent( Revision
::RAW
)
258 $this->assertEquals( "== header ==\n\ntest", $text );
260 // Now on one that does
261 $this->assertTrue( Title
::newFromText( $name )->exists() );
262 list( $re2 ) = $this->doApiRequestWithToken( array(
267 'summary' => 'header',
270 $this->assertEquals( 'Success', $re2['edit']['result'] );
271 $text = WikiPage
::factory( Title
::newFromText( $name ) )
272 ->getContent( Revision
::RAW
)
274 $this->assertEquals( "== header ==\n\ntest\n\n== header ==\n\ntest", $text );
278 * Ensure we can edit through a redirect, if adding a section
280 public function testEdit_redirect() {
284 // assume NS_HELP defaults to wikitext
285 $name = "Help:ApiEditPageTest_testEdit_redirect_$count";
286 $title = Title
::newFromText( $name );
287 $page = WikiPage
::factory( $title );
289 $rname = "Help:ApiEditPageTest_testEdit_redirect_r$count";
290 $rtitle = Title
::newFromText( $rname );
291 $rpage = WikiPage
::factory( $rtitle );
293 // base edit for content
294 $page->doEditContent( new WikitextContent( "Foo" ),
295 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
296 $this->forceRevisionDate( $page, '20120101000000' );
297 $baseTime = $page->getRevision()->getTimestamp();
299 // base edit for redirect
300 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
301 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
302 $this->forceRevisionDate( $rpage, '20120101000000' );
304 // conflicting edit to redirect
305 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
306 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
307 $this->forceRevisionDate( $rpage, '20120101020202' );
309 // try to save edit, following the redirect
310 list( $re, , ) = $this->doApiRequestWithToken( array(
313 'text' => 'nix bar!',
314 'basetimestamp' => $baseTime,
317 ), null, self
::$users['sysop']->user
);
319 $this->assertEquals( 'Success', $re['edit']['result'],
320 "no problems expected when following redirect" );
324 * Ensure we cannot edit through a redirect, if attempting to overwrite content
326 public function testEdit_redirectText() {
330 // assume NS_HELP defaults to wikitext
331 $name = "Help:ApiEditPageTest_testEdit_redirectText_$count";
332 $title = Title
::newFromText( $name );
333 $page = WikiPage
::factory( $title );
335 $rname = "Help:ApiEditPageTest_testEdit_redirectText_r$count";
336 $rtitle = Title
::newFromText( $rname );
337 $rpage = WikiPage
::factory( $rtitle );
339 // base edit for content
340 $page->doEditContent( new WikitextContent( "Foo" ),
341 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
342 $this->forceRevisionDate( $page, '20120101000000' );
343 $baseTime = $page->getRevision()->getTimestamp();
345 // base edit for redirect
346 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
347 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
348 $this->forceRevisionDate( $rpage, '20120101000000' );
350 // conflicting edit to redirect
351 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
352 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
353 $this->forceRevisionDate( $rpage, '20120101020202' );
355 // try to save edit, following the redirect but without creating a section
357 $this->doApiRequestWithToken( array(
360 'text' => 'nix bar!',
361 'basetimestamp' => $baseTime,
363 ), null, self
::$users['sysop']->user
);
365 $this->fail( 'redirect-appendonly error expected' );
366 } catch ( UsageException
$ex ) {
367 $this->assertEquals( 'redirect-appendonly', $ex->getCodeString() );
371 public function testEditConflict() {
375 // assume NS_HELP defaults to wikitext
376 $name = "Help:ApiEditPageTest_testEditConflict_$count";
377 $title = Title
::newFromText( $name );
379 $page = WikiPage
::factory( $title );
382 $page->doEditContent( new WikitextContent( "Foo" ),
383 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
384 $this->forceRevisionDate( $page, '20120101000000' );
385 $baseTime = $page->getRevision()->getTimestamp();
388 $page->doEditContent( new WikitextContent( "Foo bar" ),
389 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
390 $this->forceRevisionDate( $page, '20120101020202' );
392 // try to save edit, expect conflict
394 $this->doApiRequestWithToken( array(
397 'text' => 'nix bar!',
398 'basetimestamp' => $baseTime,
399 ), null, self
::$users['sysop']->user
);
401 $this->fail( 'edit conflict expected' );
402 } catch ( UsageException
$ex ) {
403 $this->assertEquals( 'editconflict', $ex->getCodeString() );
408 * Ensure that editing using section=new will prevent simple conflicts
410 public function testEditConflict_newSection() {
414 // assume NS_HELP defaults to wikitext
415 $name = "Help:ApiEditPageTest_testEditConflict_newSection_$count";
416 $title = Title
::newFromText( $name );
418 $page = WikiPage
::factory( $title );
421 $page->doEditContent( new WikitextContent( "Foo" ),
422 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
423 $this->forceRevisionDate( $page, '20120101000000' );
424 $baseTime = $page->getRevision()->getTimestamp();
427 $page->doEditContent( new WikitextContent( "Foo bar" ),
428 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
429 $this->forceRevisionDate( $page, '20120101020202' );
431 // try to save edit, expect no conflict
432 list( $re, , ) = $this->doApiRequestWithToken( array(
435 'text' => 'nix bar!',
436 'basetimestamp' => $baseTime,
438 ), null, self
::$users['sysop']->user
);
440 $this->assertEquals( 'Success', $re['edit']['result'],
441 "no edit conflict expected here" );
444 public function testEditConflict_bug41990() {
449 * bug 41990: if the target page has a newer revision than the redirect, then editing the
450 * redirect while specifying 'redirect' and *not* specifying 'basetimestamp' erroneously
451 * caused an edit conflict to be detected.
454 // assume NS_HELP defaults to wikitext
455 $name = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_$count";
456 $title = Title
::newFromText( $name );
457 $page = WikiPage
::factory( $title );
459 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_r$count";
460 $rtitle = Title
::newFromText( $rname );
461 $rpage = WikiPage
::factory( $rtitle );
463 // base edit for content
464 $page->doEditContent( new WikitextContent( "Foo" ),
465 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
466 $this->forceRevisionDate( $page, '20120101000000' );
468 // base edit for redirect
469 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
470 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
471 $this->forceRevisionDate( $rpage, '20120101000000' );
473 // new edit to content
474 $page->doEditContent( new WikitextContent( "Foo bar" ),
475 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
476 $this->forceRevisionDate( $rpage, '20120101020202' );
478 // try to save edit; should work, following the redirect.
479 list( $re, , ) = $this->doApiRequestWithToken( array(
482 'text' => 'nix bar!',
485 ), null, self
::$users['sysop']->user
);
487 $this->assertEquals( 'Success', $re['edit']['result'],
488 "no edit conflict expected here" );
492 * @param WikiPage $page
493 * @param string|int $timestamp
495 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
496 $dbw = wfGetDB( DB_MASTER
);
498 $dbw->update( 'revision',
499 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
500 array( 'rev_id' => $page->getLatest() ) );