Merge "Add 'mw-anonuserlink' class for anonymous users"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiEditPageTest.php
1 <?php
2
3 /**
4 * Tests for MediaWiki api.php?action=edit.
5 *
6 * @author Daniel Kinzler
7 *
8 * @group API
9 * @group Database
10 * @group medium
11 *
12 * @covers ApiEditPage
13 */
14 class ApiEditPageTest extends ApiTestCase {
15
16 protected function setUp() {
17 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
18
19 parent::setUp();
20
21 $this->setMwGlobals( array(
22 'wgExtraNamespaces' => $wgExtraNamespaces,
23 'wgNamespaceContentModels' => $wgNamespaceContentModels,
24 'wgContentHandlers' => $wgContentHandlers,
25 'wgContLang' => $wgContLang,
26 ) );
27
28 $wgExtraNamespaces[12312] = 'Dummy';
29 $wgExtraNamespaces[12313] = 'Dummy_talk';
30
31 $wgNamespaceContentModels[12312] = "testing";
32 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
33
34 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
35 $wgContLang->resetNamespaces(); # reset namespace cache
36
37 $this->doLogin();
38 }
39
40 protected function tearDown() {
41 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
42 parent::tearDown();
43 }
44
45 public function testEdit() {
46 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
47
48 // -- test new page --------------------------------------------
49 $apiResult = $this->doApiRequestWithToken( array(
50 'action' => 'edit',
51 'title' => $name,
52 'text' => 'some text',
53 ) );
54 $apiResult = $apiResult[0];
55
56 // Validate API result data
57 $this->assertArrayHasKey( 'edit', $apiResult );
58 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
59 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
60
61 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
62 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
63
64 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
65
66 // -- test existing page, no change ----------------------------
67 $data = $this->doApiRequestWithToken( array(
68 'action' => 'edit',
69 'title' => $name,
70 'text' => 'some text',
71 ) );
72
73 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
74
75 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
76 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
77
78 // -- test existing page, with change --------------------------
79 $data = $this->doApiRequestWithToken( array(
80 'action' => 'edit',
81 'title' => $name,
82 'text' => 'different text'
83 ) );
84
85 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
86
87 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
88 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
89
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"
96 );
97 }
98
99 public function testNonTextEdit() {
100 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
101 $data = serialize( 'some bla bla text' );
102
103 // -- test new page --------------------------------------------
104 $apiResult = $this->doApiRequestWithToken( array(
105 'action' => 'edit',
106 'title' => $name,
107 'text' => $data, ) );
108 $apiResult = $apiResult[0];
109
110 // Validate API result data
111 $this->assertArrayHasKey( 'edit', $apiResult );
112 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
113 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
114
115 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
116 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
117
118 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
119
120 // validate resulting revision
121 $page = WikiPage::factory( Title::newFromText( $name ) );
122 $this->assertEquals( "testing", $page->getContentModel() );
123 $this->assertEquals( $data, $page->getContent()->serialize() );
124 }
125
126 /**
127 * @return array
128 */
129 public static function provideEditAppend() {
130 return array(
131 array( #0: append
132 'foo', 'append', 'bar', "foobar"
133 ),
134 array( #1: prepend
135 'foo', 'prepend', 'bar', "barfoo"
136 ),
137 array( #2: append to empty page
138 '', 'append', 'foo', "foo"
139 ),
140 array( #3: prepend to empty page
141 '', 'prepend', 'foo', "foo"
142 ),
143 array( #4: append to non-existing page
144 null, 'append', 'foo', "foo"
145 ),
146 array( #5: prepend to non-existing page
147 null, 'prepend', 'foo', "foo"
148 ),
149 );
150 }
151
152 /**
153 * @dataProvider provideEditAppend
154 */
155 public function testEditAppend( $text, $op, $append, $expected ) {
156 static $count = 0;
157 $count++;
158
159 // assume NS_HELP defaults to wikitext
160 $name = "Help:ApiEditPageTest_testEditAppend_$count";
161
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(
167 'action' => 'edit',
168 'title' => $name,
169 'text' => '(dummy)', ) );
170 }
171
172 list( $re ) = $this->doApiRequestWithToken( array(
173 'action' => 'edit',
174 'title' => $name,
175 'text' => $text, ) );
176
177 $this->assertEquals( 'Success', $re['edit']['result'] ); // sanity
178 }
179
180 // -- try append/prepend --------------------------------------------
181 list( $re ) = $this->doApiRequestWithToken( array(
182 'action' => 'edit',
183 'title' => $name,
184 $op . 'text' => $append, ) );
185
186 $this->assertEquals( 'Success', $re['edit']['result'] );
187
188 // -- validate -----------------------------------------------------
189 $page = new WikiPage( Title::newFromText( $name ) );
190 $content = $page->getContent();
191 $this->assertNotNull( $content, 'Page should have been created' );
192
193 $text = $content->getNativeData();
194
195 $this->assertEquals( $expected, $text );
196 }
197
198 /**
199 * Test editing of sections
200 */
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' );
207
208 list( $re ) = $this->doApiRequestWithToken( array(
209 'action' => 'edit',
210 'title' => $name,
211 'section' => '1',
212 'text' => "==section 1==\nnew content 1",
213 ) );
214 $this->assertEquals( 'Success', $re['edit']['result'] );
215 $newtext = WikiPage::factory( Title::newFromText( $name ) )
216 ->getContent( Revision::RAW )
217 ->getNativeData();
218 $this->assertEquals( "==section 1==\nnew content 1\n\n==section 2==\ncontent2", $newtext );
219
220 // Test that we raise a 'nosuchsection' error
221 try {
222 $this->doApiRequestWithToken( array(
223 'action' => 'edit',
224 'title' => $name,
225 'section' => '9999',
226 'text' => 'text',
227 ) );
228 $this->fail( "Should have raised a UsageException" );
229 } catch ( UsageException $e ) {
230 $this->assertEquals( 'nosuchsection', $e->getCodeString() );
231 }
232 }
233
234 /**
235 * Test action=edit&section=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
238 * does exist
239 */
240 public function testEditNewSection() {
241 $name = 'Help:ApiEditPageTest_testEditNewSection';
242
243 // Test on a page that does not already exist
244 $this->assertFalse( Title::newFromText( $name )->exists() );
245 list( $re ) = $this->doApiRequestWithToken( array(
246 'action' => 'edit',
247 'title' => $name,
248 'section' => 'new',
249 'text' => 'test',
250 'summary' => 'header',
251 ));
252
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 )
257 ->getNativeData();
258 $this->assertEquals( "== header ==\n\ntest", $text );
259
260 // Now on one that does
261 $this->assertTrue( Title::newFromText( $name )->exists() );
262 list( $re2 ) = $this->doApiRequestWithToken( array(
263 'action' => 'edit',
264 'title' => $name,
265 'section' => 'new',
266 'text' => 'test',
267 'summary' => 'header',
268 ));
269
270 $this->assertEquals( 'Success', $re2['edit']['result'] );
271 $text = WikiPage::factory( Title::newFromText( $name ) )
272 ->getContent( Revision::RAW )
273 ->getNativeData();
274 $this->assertEquals( "== header ==\n\ntest\n\n== header ==\n\ntest", $text );
275 }
276
277 public function testEditConflict() {
278 static $count = 0;
279 $count++;
280
281 // assume NS_HELP defaults to wikitext
282 $name = "Help:ApiEditPageTest_testEditConflict_$count";
283 $title = Title::newFromText( $name );
284
285 $page = WikiPage::factory( $title );
286
287 // base edit
288 $page->doEditContent( new WikitextContent( "Foo" ),
289 "testing 1", EDIT_NEW, false, self::$users['sysop']->user );
290 $this->forceRevisionDate( $page, '20120101000000' );
291 $baseTime = $page->getRevision()->getTimestamp();
292
293 // conflicting edit
294 $page->doEditContent( new WikitextContent( "Foo bar" ),
295 "testing 2", EDIT_UPDATE, $page->getLatest(), self::$users['uploader']->user );
296 $this->forceRevisionDate( $page, '20120101020202' );
297
298 // try to save edit, expect conflict
299 try {
300 $this->doApiRequestWithToken( array(
301 'action' => 'edit',
302 'title' => $name,
303 'text' => 'nix bar!',
304 'basetimestamp' => $baseTime,
305 ), null, self::$users['sysop']->user );
306
307 $this->fail( 'edit conflict expected' );
308 } catch ( UsageException $ex ) {
309 $this->assertEquals( 'editconflict', $ex->getCodeString() );
310 }
311 }
312
313 public function testEditConflict_redirect() {
314 static $count = 0;
315 $count++;
316
317 // assume NS_HELP defaults to wikitext
318 $name = "Help:ApiEditPageTest_testEditConflict_redirect_$count";
319 $title = Title::newFromText( $name );
320 $page = WikiPage::factory( $title );
321
322 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_r$count";
323 $rtitle = Title::newFromText( $rname );
324 $rpage = WikiPage::factory( $rtitle );
325
326 // base edit for content
327 $page->doEditContent( new WikitextContent( "Foo" ),
328 "testing 1", EDIT_NEW, false, self::$users['sysop']->user );
329 $this->forceRevisionDate( $page, '20120101000000' );
330 $baseTime = $page->getRevision()->getTimestamp();
331
332 // base edit for redirect
333 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
334 "testing 1", EDIT_NEW, false, self::$users['sysop']->user );
335 $this->forceRevisionDate( $rpage, '20120101000000' );
336
337 // conflicting edit to redirect
338 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]\n\n[[Category:Test]]" ),
339 "testing 2", EDIT_UPDATE, $page->getLatest(), self::$users['uploader']->user );
340 $this->forceRevisionDate( $rpage, '20120101020202' );
341
342 // try to save edit; should work, because we follow the redirect
343 list( $re, , ) = $this->doApiRequestWithToken( array(
344 'action' => 'edit',
345 'title' => $rname,
346 'text' => 'nix bar!',
347 'basetimestamp' => $baseTime,
348 'redirect' => true,
349 ), null, self::$users['sysop']->user );
350
351 $this->assertEquals( 'Success', $re['edit']['result'],
352 "no edit conflict expected when following redirect" );
353
354 // try again, without following the redirect. Should fail.
355 try {
356 $this->doApiRequestWithToken( array(
357 'action' => 'edit',
358 'title' => $rname,
359 'text' => 'nix bar!',
360 'basetimestamp' => $baseTime,
361 ), null, self::$users['sysop']->user );
362
363 $this->fail( 'edit conflict expected' );
364 } catch ( UsageException $ex ) {
365 $this->assertEquals( 'editconflict', $ex->getCodeString() );
366 }
367 }
368
369 public function testEditConflict_bug41990() {
370 static $count = 0;
371 $count++;
372
373 /*
374 * bug 41990: if the target page has a newer revision than the redirect, then editing the
375 * redirect while specifying 'redirect' and *not* specifying 'basetimestamp' erroneously
376 * caused an edit conflict to be detected.
377 */
378
379 // assume NS_HELP defaults to wikitext
380 $name = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_$count";
381 $title = Title::newFromText( $name );
382 $page = WikiPage::factory( $title );
383
384 $rname = "Help:ApiEditPageTest_testEditConflict_redirect_bug41990_r$count";
385 $rtitle = Title::newFromText( $rname );
386 $rpage = WikiPage::factory( $rtitle );
387
388 // base edit for content
389 $page->doEditContent( new WikitextContent( "Foo" ),
390 "testing 1", EDIT_NEW, false, self::$users['sysop']->user );
391 $this->forceRevisionDate( $page, '20120101000000' );
392
393 // base edit for redirect
394 $rpage->doEditContent( new WikitextContent( "#REDIRECT [[$name]]" ),
395 "testing 1", EDIT_NEW, false, self::$users['sysop']->user );
396 $this->forceRevisionDate( $rpage, '20120101000000' );
397
398 // new edit to content
399 $page->doEditContent( new WikitextContent( "Foo bar" ),
400 "testing 2", EDIT_UPDATE, $page->getLatest(), self::$users['uploader']->user );
401 $this->forceRevisionDate( $rpage, '20120101020202' );
402
403 // try to save edit; should work, following the redirect.
404 list( $re, , ) = $this->doApiRequestWithToken( array(
405 'action' => 'edit',
406 'title' => $rname,
407 'text' => 'nix bar!',
408 'redirect' => true,
409 ), null, self::$users['sysop']->user );
410
411 $this->assertEquals( 'Success', $re['edit']['result'],
412 "no edit conflict expected here" );
413 }
414
415 /**
416 * @param WikiPage $page
417 * @param string|int $timestamp
418 */
419 protected function forceRevisionDate( WikiPage $page, $timestamp ) {
420 $dbw = wfGetDB( DB_MASTER );
421
422 $dbw->update( 'revision',
423 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ),
424 array( 'rev_id' => $page->getLatest() ) );
425
426 $page->clear();
427 }
428 }