4 * Tests for MediaWiki api.php?action=edit.
6 * @author Daniel Kinzler
12 class ApiEditPageTest
extends ApiTestCase
{
14 public function setup() {
15 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
19 $wgExtraNamespaces[12312] = 'Dummy';
20 $wgExtraNamespaces[12313] = 'Dummy_talk';
22 $wgNamespaceContentModels[12312] = "testing";
23 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
25 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
26 $wgContLang->resetNamespaces(); # reset namespace cache
31 public function teardown() {
32 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
34 unset( $wgExtraNamespaces[12312] );
35 unset( $wgExtraNamespaces[12313] );
37 unset( $wgNamespaceContentModels[12312] );
38 unset( $wgContentHandlers["testing"] );
40 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
41 $wgContLang->resetNamespaces(); # reset namespace cache
46 function testEdit( ) {
47 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
49 // -- test new page --------------------------------------------
50 $apiResult = $this->doApiRequestWithToken( array(
53 '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', ) );
72 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
74 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
75 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
77 // -- test existing page, with change --------------------------
78 $data = $this->doApiRequestWithToken( array(
81 'text' => 'different text' ) );
83 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
85 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
86 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
88 $this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
89 $this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
90 $this->assertNotEquals(
91 $data[0]['edit']['newrevid'],
92 $data[0]['edit']['oldrevid'],
93 "revision id should change after edit"
97 function testNonTextEdit( ) {
98 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
99 $data = serialize( 'some bla bla text' );
101 // -- test new page --------------------------------------------
102 $apiResult = $this->doApiRequestWithToken( array(
105 'text' => $data, ) );
106 $apiResult = $apiResult[0];
108 // Validate API result data
109 $this->assertArrayHasKey( 'edit', $apiResult );
110 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
111 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
113 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
114 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
116 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
118 // validate resulting revision
119 $page = WikiPage
::factory( Title
::newFromText( $name ) );
120 $this->assertEquals( "testing", $page->getContentModel() );
121 $this->assertEquals( $data, $page->getContent()->serialize() );
124 static function provideEditAppend() {
127 'foo', 'append', 'bar', "foobar"
130 'foo', 'prepend', 'bar', "barfoo"
132 array( #2: append to empty page
133 '', 'append', 'foo', "foo"
135 array( #3: prepend to empty page
136 '', 'prepend', 'foo', "foo"
138 array( #4: append to non-existing page
139 null, 'append', 'foo', "foo"
141 array( #5: prepend to non-existing page
142 null, 'prepend', 'foo', "foo"
148 * @dataProvider provideEditAppend
150 function testEditAppend( $text, $op, $append, $expected ) {
154 // assume NS_HELP defaults to wikitext
155 $name = "Help:ApiEditPageTest_testEditAppend_$count";
157 // -- create page (or not) -----------------------------------------
158 if ( $text !== null ) {
159 if ( $text === '' ) {
160 // can't create an empty page, so create it with some content
161 list( $re,, ) = $this->doApiRequestWithToken( array(
164 'text' => '(dummy)', ) );
167 list( $re,, ) = $this->doApiRequestWithToken( array(
170 'text' => $text, ) );
172 $this->assertEquals( 'Success', $re['edit']['result'] ); // sanity
175 // -- try append/prepend --------------------------------------------
176 list( $re,, ) = $this->doApiRequestWithToken( array(
179 $op . 'text' => $append, ) );
181 $this->assertEquals( 'Success', $re['edit']['result'] );
183 // -- validate -----------------------------------------------------
184 $page = new WikiPage( Title
::newFromText( $name ) );
185 $content = $page->getContent();
186 $this->assertNotNull( $content, 'Page should have been created' );
188 $text = $content->getNativeData();
190 $this->assertEquals( $expected, $text );
193 function testEditSection() {
194 $this->markTestIncomplete( "not yet implemented" );
197 function testUndo() {
198 $this->markTestIncomplete( "not yet implemented" );
201 function testEditConflict() {
205 // assume NS_HELP defaults to wikitext
206 $name = "Help:ApiEditPageTest_testEditConflict_$count";
207 $title = Title
::newFromText( $name );
209 $page = WikiPage
::factory( $title );
212 $page->doEditContent( new WikitextContent( "Foo" ),
213 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
214 $this->forceRevisionDate( $page, '20120101000000' );
215 $baseTime = $page->getRevision()->getTimestamp();
218 $page->doEditContent( new WikitextContent( "Foo bar" ),
219 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
220 $this->forceRevisionDate( $page, '20120101020202' );
222 // try to save edit, expect conflict
224 list( $re,, ) = $this->doApiRequestWithToken( array(
227 'text' => 'nix bar!',
228 'basetimestamp' => $baseTime,
229 ), null, self
::$users['sysop']->user
);
231 $this->fail( 'edit conflict expected' );
232 } catch ( UsageException
$ex ) {
233 $this->assertEquals( 'editconflict', $ex->getCodeString() );
237 function testEditConflict_redirect() {
241 // assume NS_HELP defaults to wikitext
242 $name = "Help:ApiEditPageTest_testEditConflict_redirect_$count";
243 $title = Title
::newFromText( $name );
244 $page = WikiPage
::factory( $title );
246 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_r$count";
247 $rtitle = Title
::newFromText( $rname );
248 $rpage = WikiPage
::factory( $rtitle );
250 // base edit for content
251 $page->doEditContent( new WikitextContent( "Foo" ),
252 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
253 $this->forceRevisionDate( $page, '20120101000000' );
254 $baseTime = $page->getRevision()->getTimestamp();
256 // base edit for redirect
257 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
258 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
259 $this->forceRevisionDate( $rpage, '20120101000000' );
261 // conflicting edit to redirect
262 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
263 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
264 $this->forceRevisionDate( $rpage, '20120101020202' );
266 // try to save edit; should work, because we follow the redirect
267 list( $re,, ) = $this->doApiRequestWithToken( array(
270 'text' => 'nix bar!',
271 'basetimestamp' => $baseTime,
273 ), null, self
::$users['sysop']->user
);
275 $this->assertEquals( 'Success', $re['edit']['result'],
276 "no edit conflict expected when following redirect" );
278 // try again, without following the redirect. Should fail.
280 list( $re,, ) = $this->doApiRequestWithToken( array(
283 'text' => 'nix bar!',
284 'basetimestamp' => $baseTime,
285 ), null, self
::$users['sysop']->user
);
287 $this->fail( 'edit conflict expected' );
288 } catch ( UsageException
$ex ) {
289 $this->assertEquals( 'editconflict', $ex->getCodeString() );
293 function testEditConflict_bug41990() {
298 * bug 41990: if the target page has a newer revision than the redirect, then editing the
299 * redirect while specifying 'redirect' and *not* specifying 'basetimestamp' erronously
300 * caused an edit conflict to be detected.
303 // assume NS_HELP defaults to wikitext
304 $name = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_$count";
305 $title = Title
::newFromText( $name );
306 $page = WikiPage
::factory( $title );
308 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_r$count";
309 $rtitle = Title
::newFromText( $rname );
310 $rpage = WikiPage
::factory( $rtitle );
312 // base edit for content
313 $page->doEditContent( new WikitextContent( "Foo" ),
314 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
315 $this->forceRevisionDate( $page, '20120101000000' );
317 // base edit for redirect
318 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
319 "testing 1", EDIT_NEW
, false, self
::$users['sysop']->user
);
320 $this->forceRevisionDate( $rpage, '20120101000000' );
321 $baseTime = $rpage->getRevision()->getTimestamp();
323 // new edit to content
324 $page->doEditContent( new WikitextContent( "Foo bar" ),
325 "testing 2", EDIT_UPDATE
, $page->getLatest(), self
::$users['uploader']->user
);
326 $this->forceRevisionDate( $rpage, '20120101020202' );
328 // try to save edit; should work, following the redirect.
329 list( $re,, ) = $this->doApiRequestWithToken( array(
332 'text' => 'nix bar!',
334 ), null, self
::$users['sysop']->user
);
336 $this->assertEquals( 'Success', $re['edit']['result'],
337 "no edit conflict expected here" );
340 protected function forceRevisionDate( WikiPage
$page, $timestamp ) {
341 $dbw = wfGetDB( DB_MASTER
);
343 $dbw->update( 'revision',
344 array( 'rev_timestamp' => $timestamp ),
345 array( 'rev_id' => $page->getLatest() ) );