4 * Tests for MediaWiki api.php?action=edit.
6 * @author Daniel Kinzler
14 class ApiEditPageTest
extends ApiTestCase
{
16 public function setUp() {
17 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
21 $wgExtraNamespaces[12312] = 'Dummy';
22 $wgExtraNamespaces[12313] = 'Dummy_talk';
24 $wgNamespaceContentModels[12312] = "testing";
25 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
27 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
28 $wgContLang->resetNamespaces(); # reset namespace cache
33 public function tearDown() {
34 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
36 unset( $wgExtraNamespaces[12312] );
37 unset( $wgExtraNamespaces[12313] );
39 unset( $wgNamespaceContentModels[12312] );
40 unset( $wgContentHandlers["testing"] );
42 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
43 $wgContLang->resetNamespaces(); # reset namespace cache
48 public function testEdit() {
49 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
51 // -- test new page --------------------------------------------
52 $apiResult = $this->doApiRequestWithToken( array(
55 'text' => 'some text',
57 $apiResult = $apiResult[0];
59 // Validate API result data
60 $this->assertArrayHasKey( 'edit', $apiResult );
61 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
62 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
64 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
65 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
67 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
69 // -- test existing page, no change ----------------------------
70 $data = $this->doApiRequestWithToken( array(
73 'text' => 'some text',
76 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
78 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
79 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
81 // -- test existing page, with change --------------------------
82 $data = $this->doApiRequestWithToken( array(
85 'text' => 'different text'
88 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
90 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
91 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
93 $this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
94 $this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
95 $this->assertNotEquals(
96 $data[0]['edit']['newrevid'],
97 $data[0]['edit']['oldrevid'],
98 "revision id should change after edit"
102 public function testNonTextEdit() {
103 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
104 $data = serialize( 'some bla bla text' );
106 // -- test new page --------------------------------------------
107 $apiResult = $this->doApiRequestWithToken( array(
110 'text' => $data, ) );
111 $apiResult = $apiResult[0];
113 // Validate API result data
114 $this->assertArrayHasKey( 'edit', $apiResult );
115 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
116 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
118 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
119 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
121 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
123 // validate resulting revision
124 $page = WikiPage
::factory( Title
::newFromText( $name ) );
125 $this->assertEquals( "testing", $page->getContentModel() );
126 $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) )->getContent( Revision
::RAW
)->getNativeData();
216 $this->assertEquals( $newtext, "==section 1==\nnew content 1\n\n==section 2==\ncontent2" );
218 // Test that we raise a 'nosuchsection' error
220 $this->doApiRequestWithToken( array(
226 $this->fail( "Should have raised a UsageException" );
227 } catch ( UsageException
$e ) {
228 $this->assertEquals( $e->getCodeString(), 'nosuchsection' );
233 * Test action=edit§ion=new
234 * Run it twice so we test adding a new section on a
235 * page that doesn't exist (bug 52830) and one that
238 public function testEditNewSection() {
239 $name = 'Help:ApiEditPageTest_testEditNewSection';
241 // Test on a page that does not already exist
242 $this->assertFalse( Title
::newFromText( $name )->exists() );
243 list( $re ) = $this->doApiRequestWithToken( array(
248 'summary' => 'header',
251 $this->assertEquals( 'Success', $re['edit']['result'] );
252 // Check the page text is correct
253 $text = WikiPage
::factory( Title
::newFromText( $name ) )->getContent( Revision
::RAW
)->getNativeData();
254 $this->assertEquals( $text, "== header ==\n\ntest" );
256 // Now on one that does
257 $this->assertTrue( Title
::newFromText( $name )->exists() );
258 list( $re2 ) = $this->doApiRequestWithToken( array(
263 'summary' => 'header',
266 $this->assertEquals( 'Success', $re2['edit']['result'] );
267 $text = WikiPage
::factory( Title
::newFromText( $name ) )->getContent( Revision
::RAW
)->getNativeData();
268 $this->assertEquals( $text, "== header ==\n\ntest\n\n== header ==\n\ntest" );
271 public function testEditConflict() {
275 // assume NS_HELP defaults to wikitext
276 $name = "Help:ApiEditPageTest_testEditConflict_$count";
277 $title = Title
::newFromText( $name );
279 $page = WikiPage
::factory( $title );
282 $page->doEditContent( new WikitextContent( "Foo" ),
283 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
284 $this->forceRevisionDate( $page, '20120101000000' );
285 $baseTime = $page->getRevision()->getTimestamp();
288 $page->doEditContent( new WikitextContent( "Foo bar" ),
289 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
290 $this->forceRevisionDate( $page, '20120101020202' );
292 // try to save edit, expect conflict
294 $this->doApiRequestWithToken( array(
297 'text' => 'nix bar!',
298 'basetimestamp' => $baseTime,
299 ), null, self
::$users['sysop']->user
);
301 $this->fail( 'edit conflict expected' );
302 } catch ( UsageException
$ex ) {
303 $this->assertEquals( 'editconflict', $ex->getCodeString() );
307 public function testEditConflict_redirect() {
311 // assume NS_HELP defaults to wikitext
312 $name = "Help:ApiEditPageTest_testEditConflict_redirect_$count";
313 $title = Title
::newFromText( $name );
314 $page = WikiPage
::factory( $title );
316 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_r$count";
317 $rtitle = Title
::newFromText( $rname );
318 $rpage = WikiPage
::factory( $rtitle );
320 // base edit for content
321 $page->doEditContent( new WikitextContent( "Foo" ),
322 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
323 $this->forceRevisionDate( $page, '20120101000000' );
324 $baseTime = $page->getRevision()->getTimestamp();
326 // base edit for redirect
327 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
328 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
329 $this->forceRevisionDate( $rpage, '20120101000000' );
331 // conflicting edit to redirect
332 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
333 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
334 $this->forceRevisionDate( $rpage, '20120101020202' );
336 // try to save edit; should work, because we follow the redirect
337 list( $re, , ) = $this->doApiRequestWithToken( array(
340 'text' => 'nix bar!',
341 'basetimestamp' => $baseTime,
343 ), null, self
::$users['sysop']->user
);
345 $this->assertEquals( 'Success', $re['edit']['result'],
346 "no edit conflict expected when following redirect" );
348 // try again, without following the redirect. Should fail.
350 $this->doApiRequestWithToken( array(
353 'text' => 'nix bar!',
354 'basetimestamp' => $baseTime,
355 ), null, self
::$users['sysop']->user
);
357 $this->fail( 'edit conflict expected' );
358 } catch ( UsageException
$ex ) {
359 $this->assertEquals( 'editconflict', $ex->getCodeString() );
363 public function testEditConflict_bug41990() {
368 * bug 41990: if the target page has a newer revision than the redirect, then editing the
369 * redirect while specifying 'redirect' and *not* specifying 'basetimestamp' erroneously
370 * caused an edit conflict to be detected.
373 // assume NS_HELP defaults to wikitext
374 $name = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_$count";
375 $title = Title
::newFromText( $name );
376 $page = WikiPage
::factory( $title );
378 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_r$count";
379 $rtitle = Title
::newFromText( $rname );
380 $rpage = WikiPage
::factory( $rtitle );
382 // base edit for content
383 $page->doEditContent( new WikitextContent( "Foo" ),
384 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
385 $this->forceRevisionDate( $page, '20120101000000' );
387 // base edit for redirect
388 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
389 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
390 $this->forceRevisionDate( $rpage, '20120101000000' );
391 $baseTime = $rpage->getRevision()->getTimestamp();
393 // new edit to content
394 $page->doEditContent( new WikitextContent( "Foo bar" ),
395 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
396 $this->forceRevisionDate( $rpage, '20120101020202' );
398 // try to save edit; should work, following the redirect.
399 list( $re, , ) = $this->doApiRequestWithToken( array(
402 'text' => 'nix bar!',
404 ), null, self
::$users['sysop']->user
);
406 $this->assertEquals( 'Success', $re['edit']['result'],
407 "no edit conflict expected here" );
410 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
411 $dbw = wfGetDB( DB_MASTER
);
413 $dbw->update( 'revision',
414 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
415 array( 'rev_id' => $page->getLatest() ) );