Debian Codegraph @

tests/phpunit/includes/api/ApiMoveTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use MediaWiki\Api\ApiUsageException;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Revision\SlotRecord;
9 use MediaWiki\Title\Title;
10 use MediaWiki\User\User;
11 use Wikimedia\Rdbms\IDBAccessObject;
12
13 /**
14 * @group API
15 * @group Database
16 * @group medium
17 *
18 * @covers MediaWiki\Api\ApiMove
19 */
20 class ApiMoveTest extends ApiTestCase {
21
22 protected function setUp(): void {
23 parent::setUp();
24
25 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
26 }
27
28 /**
29 * @param Title $fromTitle
30 * @param Title $toTitle
31 * @param string $id Page id of the page to move
32 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
33 */
34 protected function assertMoved( $fromTitle, $toTitle, $id, $opts = null ) {
35 $opts = (array)$opts;
36
37 $this->assertTrue( $toTitle->exists( IDBAccessObject::READ_LATEST ),
38 "Destination {$toTitle->getPrefixedText()} does not exist" );
39
40 if ( in_array( 'noredirect', $opts ) ) {
41 $this->assertFalse( $fromTitle->exists( IDBAccessObject::READ_LATEST ),
42 "Source {$fromTitle->getPrefixedText()} exists" );
43 } else {
44 $this->assertTrue( $fromTitle->exists( IDBAccessObject::READ_LATEST ),
45 "Source {$fromTitle->getPrefixedText()} does not exist" );
46 $this->assertTrue( $fromTitle->isRedirect( IDBAccessObject::READ_LATEST ),
47 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
48
49 $target = $this->getServiceContainer()
50 ->getRevisionLookup()
51 ->getRevisionByTitle( $fromTitle )
52 ->getContent( SlotRecord::MAIN )
53 ->getRedirectTarget();
54 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
55 }
56
57 $this->assertSame( $id, $toTitle->getArticleID( IDBAccessObject::READ_LATEST ) );
58 }
59
60 /**
61 * Shortcut function to create a page and return its id.
62 *
63 * @param Title $name Page to create
64 * @return int ID of created page
65 */
66 protected function createPage( $name ) {
67 return $this->editPage( $name, 'Content' )->getNewRevision()->getPageId();
68 }
69
70 public function testFromWithFromid() {
71 $this->expectApiErrorCode( 'invalidparammix' );
72
73 $this->doApiRequestWithToken( [
74 'action' => 'move',
75 'from' => 'Some page',
76 'fromid' => 123,
77 'to' => 'Some other page',
78 ] );
79 }
80
81 public function testMove() {
82 $title = Title::makeTitle( NS_MAIN, 'TestMove' );
83 $title2 = Title::makeTitle( NS_MAIN, 'TestMove 2' );
84
85 $id = $this->createPage( $title );
86
87 $res = $this->doApiRequestWithToken( [
88 'action' => 'move',
89 'from' => $title->getPrefixedText(),
90 'to' => $title2->getPrefixedText(),
91 ] );
92
93 $this->assertMoved( $title, $title2, $id );
94 $this->assertArrayNotHasKey( 'warnings', $res[0] );
95 }
96
97 public function testMoveById() {
98 $title = Title::makeTitle( NS_MAIN, 'TestMoveById' );
99 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveById 2' );
100
101 $id = $this->createPage( $title );
102
103 $res = $this->doApiRequestWithToken( [
104 'action' => 'move',
105 'fromid' => $id,
106 'to' => $title2->getPrefixedText(),
107 ] );
108
109 $this->assertMoved( $title, $title2, $id );
110 $this->assertArrayNotHasKey( 'warnings', $res[0] );
111 }
112
113 public function testMoveAndWatch(): void {
114 $title = Title::makeTitle( NS_MAIN, 'TestMoveAndWatch' );
115 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveAndWatch 2' );
116 $this->createPage( $title );
117
118 $this->doApiRequestWithToken( [
119 'action' => 'move',
120 'from' => $title->getPrefixedText(),
121 'to' => $title2->getPrefixedText(),
122 'watchlist' => 'watch',
123 'watchlistexpiry' => '99990123000000',
124 ] );
125
126 $user = $this->getTestSysop()->getUser();
127 $watchlistManager = $this->getServiceContainer()->getWatchlistManager();
128 $this->assertTrue( $watchlistManager->isTempWatched( $user, $title ) );
129 $this->assertTrue( $watchlistManager->isTempWatched( $user, $title2 ) );
130 }
131
132 public function testMoveWithWatchUnchanged(): void {
133 $title = Title::makeTitle( NS_MAIN, 'TestMoveWithWatchUnchanged' );
134 $this->createPage( $title );
135 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveWithWatchUnchanged 2' );
136 $user = $this->getTestSysop()->getUser();
137
138 // Temporarily watch the page.
139 $this->doApiRequestWithToken( [
140 'action' => 'watch',
141 'titles' => $title->getDBkey(),
142 'expiry' => '99990123000000',
143 ] );
144
145 // Fetched stored expiry (maximum duration may override '99990123000000').
146 $store = $this->getServiceContainer()->getWatchedItemStore();
147 $expiry = $store->getWatchedItem( $user, $title )->getExpiry();
148
149 // Move to new location, without changing the watched state.
150 $this->doApiRequestWithToken( [
151 'action' => 'move',
152 'from' => $title->getDBkey(),
153 'to' => $title2->getDBkey(),
154 ] );
155
156 // New page should have the same expiry.
157 $expiry2 = $store->getWatchedItem( $user, $title2 )->getExpiry();
158 $this->assertSame( wfTimestamp( TS_MW, $expiry ), $expiry2 );
159 }
160
161 public function testMoveNonexistent() {
162 $this->expectApiErrorCode( 'missingtitle' );
163
164 $this->doApiRequestWithToken( [
165 'action' => 'move',
166 'from' => 'Nonexistent page',
167 'to' => 'Different page'
168 ] );
169 }
170
171 public function testMoveNonexistentId() {
172 $this->expectApiErrorCode( 'nosuchpageid' );
173
174 $this->doApiRequestWithToken( [
175 'action' => 'move',
176 'fromid' => pow( 2, 31 ) - 1,
177 'to' => 'Different page',
178 ] );
179 }
180
181 public function testMoveToInvalidPageName() {
182 $this->expectApiErrorCode( 'invalidtitle' );
183
184 $title = Title::makeTitle( NS_MAIN, 'TestMoveToInvalidPageName' );
185 $id = $this->createPage( $title );
186
187 try {
188 $this->doApiRequestWithToken( [
189 'action' => 'move',
190 'from' => $title->getPrefixedText(),
191 'to' => '[',
192 ] );
193 } finally {
194 $this->assertSame( $id, $title->getArticleID( IDBAccessObject::READ_LATEST ) );
195 }
196 }
197
198 public function testMoveWhileBlocked() {
199 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
200 $this->assertNull( $blockStore->newFromTarget( '127.0.0.1' ) );
201
202 $user = $this->getTestSysop()->getUser();
203 $block = new DatabaseBlock( [
204 'address' => $user->getName(),
205 'by' => $user,
206 'reason' => 'Capriciousness',
207 'timestamp' => '19370101000000',
208 'expiry' => 'infinity',
209 'enableAutoblock' => true,
210 ] );
211 $blockStore->insertBlock( $block );
212
213 $title = Title::makeTitle( NS_MAIN, 'TestMoveWhileBlocked' );
214 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveWhileBlocked 2' );
215 $id = $this->createPage( $title );
216
217 try {
218 $this->doApiRequestWithToken( [
219 'action' => 'move',
220 'from' => $title->getPrefixedText(),
221 'to' => $title2->getPrefixedText(),
222 ] );
223 $this->fail( 'Expected exception not thrown' );
224 } catch ( ApiUsageException $ex ) {
225 $this->assertApiErrorCode( 'blocked', $ex );
226 $this->assertNotNull( $blockStore->newFromTarget( '127.0.0.1' ), 'Autoblock spread' );
227 $this->assertSame( $id, $title->getArticleID( IDBAccessObject::READ_LATEST ) );
228 }
229 }
230
231 // @todo File moving
232
233 public function testRateLimit() {
234 $name1 = 'TestPingLimiter 1';
235 $name2 = 'TestPingLimiter 2';
236 $name3 = 'TestPingLimiter 3';
237
238 $title1 = Title::makeTitle( NS_MAIN, $name1 );
239 $title2 = Title::makeTitle( NS_MAIN, $name2 );
240
241 $this->overrideConfigValue( MainConfigNames::RateLimits,
242 [ 'move' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ]
243 );
244
245 $id = $this->createPage( $title1 );
246 $res = $this->doApiRequestWithToken( [
247 'action' => 'move',
248 'from' => $name1,
249 'to' => $name2,
250 ] );
251 $this->assertMoved( $title1, $title2, $id );
252 $this->assertArrayNotHasKey( 'warnings', $res[0] );
253
254 try {
255 $this->doApiRequestWithToken( [
256 'action' => 'move',
257 'from' => $name2,
258 'to' => $name3,
259 ] );
260
261 $this->fail( 'Rate limit was expected to trigger an exception' );
262 } catch ( ApiUsageException $ex ) {
263 $this->assertStatusError( 'apierror-ratelimited', $ex->getStatusValue() );
264 } finally {
265 $title3 = Title::makeTitle( NS_MAIN, $name3 );
266 $this->assertSame( $id, $title2->getArticleID( IDBAccessObject::READ_LATEST ) );
267 $this->assertFalse( $title3->exists( IDBAccessObject::READ_LATEST ),
268 "\"{$title3->getPrefixedText()}\" should not exist" );
269 }
270 }
271
272 public function testTagsNoPermission() {
273 $this->expectApiErrorCode( 'tags-apply-no-permission' );
274
275 $title = Title::makeTitle( NS_MAIN, 'TestTagsNoPermission' );
276 $title2 = Title::makeTitle( NS_MAIN, 'TestTagsNoPermission 2' );
277
278 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
279
280 $this->setGroupPermissions( 'user', 'applychangetags', false );
281
282 $id = $this->createPage( $title );
283
284 try {
285 $this->doApiRequestWithToken( [
286 'action' => 'move',
287 'from' => $title->getPrefixedText(),
288 'to' => $title2->getPrefixedText(),
289 'tags' => 'custom tag',
290 ] );
291 } finally {
292 $this->assertSame( $id, $title->getArticleID( IDBAccessObject::READ_LATEST ) );
293 $this->assertFalse( $title2->exists( IDBAccessObject::READ_LATEST ),
294 "\"{$title2->getPrefixedText()}\" should not exist" );
295 }
296 }
297
298 public function testSelfMove() {
299 $this->expectApiErrorCode( 'selfmove' );
300
301 $title = Title::makeTitle( NS_MAIN, 'TestSelfMove' );
302 $this->createPage( $title );
303
304 $this->doApiRequestWithToken( [
305 'action' => 'move',
306 'from' => $title->getPrefixedText(),
307 'to' => $title->getPrefixedText(),
308 ] );
309 }
310
311 public function testMoveTalk() {
312 $title = Title::makeTitle( NS_MAIN, 'TestMoveTalk' );
313 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveTalk 2' );
314 $talkTitle = $title->getTalkPageIfDefined();
315 $talkTitle2 = $title2->getTalkPageIfDefined();
316
317 $id = $this->createPage( $title );
318 $talkId = $this->createPage( $talkTitle );
319
320 $res = $this->doApiRequestWithToken( [
321 'action' => 'move',
322 'from' => $title->getPrefixedText(),
323 'to' => $title2->getPrefixedText(),
324 'movetalk' => '',
325 ] );
326
327 $this->assertMoved( $title, $title2, $id );
328 $this->assertMoved( $talkTitle, $talkTitle2, $talkId );
329
330 $this->assertArrayNotHasKey( 'warnings', $res[0] );
331 }
332
333 public function testMoveTalkFailed() {
334 $title = Title::makeTitle( NS_MAIN, 'TestMoveTalkFailed' );
335 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveTalkFailed 2' );
336 $talkTitle = $title->getTalkPageIfDefined();
337 $talkDestinationTitle = $title2->getTalkPageIfDefined();
338
339 $id = $this->createPage( $title );
340 $talkId = $this->createPage( $talkTitle );
341 $talkDestinationId = $this->createPage( $talkDestinationTitle );
342
343 $res = $this->doApiRequestWithToken( [
344 'action' => 'move',
345 'from' => $title->getPrefixedText(),
346 'to' => $title2->getPrefixedText(),
347 'movetalk' => '',
348 ] );
349
350 $this->assertMoved( $title, $title2, $id );
351 $this->assertSame( $talkId, $talkTitle->getArticleID( IDBAccessObject::READ_LATEST ) );
352 $this->assertSame( $talkDestinationId,
353 $talkDestinationTitle->getArticleID( IDBAccessObject::READ_LATEST ) );
354 $this->assertSame( [ [
355 'message' => 'articleexists',
356 'params' => [ $talkDestinationTitle->getPrefixedText() ],
357 'code' => 'articleexists',
358 'type' => 'error',
359 ] ], $res[0]['move']['talkmove-errors'] );
360
361 $this->assertArrayNotHasKey( 'warnings', $res[0] );
362 }
363
364 public function testMoveSubpages() {
365 $title = Title::makeTitle( NS_MAIN, 'TestMoveSubpages' );
366 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveSubpages 2' );
367
368 $this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => true ] );
369
370 $titleError = Title::makeTitle( NS_MAIN, 'TestMoveSubpages/error' );
371 $idError = $this->createPage( $titleError );
372 $title2Error = Title::makeTitle( NS_MAIN, 'TestMoveSubpages 2/error' );
373 $id2Error = $this->createPage( $title2Error );
374
375 $titles = [
376 [ $title, $title2 ],
377 [ $title->getTalkPageIfDefined(), $title2->getTalkPageIfDefined() ],
378 [ Title::makeTitle( NS_MAIN, 'TestMoveSubpages/1' ), Title::makeTitle( NS_MAIN, 'TestMoveSubpages 2/1' ) ],
379 [ Title::makeTitle( NS_MAIN, 'TestMoveSubpages/2' ), Title::makeTitle( NS_MAIN, 'TestMoveSubpages 2/2' ) ],
380 [ Title::makeTitle( NS_TALK, 'TestMoveSubpages/1' ), Title::makeTitle( NS_TALK, 'TestMoveSubpages 2/1' ) ],
381 [ Title::makeTitle( NS_TALK, 'TestMoveSubpages/3' ), Title::makeTitle( NS_TALK, 'TestMoveSubpages 2/3' ) ],
382 ];
383 $ids = [];
384 $mapTitles = [];
385 foreach ( $titles as [ $from, $to ] ) {
386 $ids[$from->getPrefixedText()] = $this->createPage( $from );
387 $mapTitles[$from->getPrefixedText()] = $to->getPrefixedText();
388 }
389
390 $res = $this->doApiRequestWithToken( [
391 'action' => 'move',
392 'from' => $title->getPrefixedText(),
393 'to' => $title2->getPrefixedText(),
394 'movetalk' => '',
395 'movesubpages' => '',
396 ] );
397
398 foreach ( $titles as [ $from, $to ] ) {
399 $this->assertMoved( $from, $to, $ids[$from->getPrefixedText()] );
400 }
401
402 $this->assertSame( $idError, $titleError->getArticleID( IDBAccessObject::READ_LATEST ) );
403 $this->assertSame( $id2Error, $title2Error->getArticleID( IDBAccessObject::READ_LATEST ) );
404
405 $results = array_merge( $res[0]['move']['subpages'], $res[0]['move']['subpages-talk'] );
406 foreach ( $results as $arr ) {
407 if ( $arr['from'] === $titleError->getPrefixedText() ) {
408 $this->assertSame( [ [
409 'message' => 'articleexists',
410 'params' => [ $title2Error->getPrefixedText() ],
411 'code' => 'articleexists',
412 'type' => 'error'
413 ] ], $arr['errors'] );
414 } else {
415 $this->assertSame( $mapTitles[$arr['from']], $arr['to'] );
416 }
417 $this->assertCount( 2, $arr );
418 }
419
420 $this->assertArrayNotHasKey( 'warnings', $res[0] );
421 }
422
423 public function testMoveNoPermission() {
424 $title = Title::makeTitle( NS_MAIN, 'TestMoveNoPermission' );
425 $title2 = Title::makeTitle( NS_MAIN, 'TestMoveNoPermission 2' );
426
427 $id = $this->createPage( $title );
428
429 $user = new User();
430
431 try {
432 $this->doApiRequestWithToken( [
433 'action' => 'move',
434 'from' => $title->getPrefixedText(),
435 'to' => $title2->getPrefixedText(),
436 ], null, $user );
437 } catch ( ApiUsageException $ex ) {
438 // This one has two errors! So weird
439 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'cantmove-anon' ) );
440 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'cantmove' ) );
441 } finally {
442 $this->assertSame( $id, $title->getArticleID( IDBAccessObject::READ_LATEST ) );
443 $this->assertFalse( $title2->exists( IDBAccessObject::READ_LATEST ),
444 "\"{$title2->getPrefixedText()}\" should not exist" );
445 }
446 }
447
448 public function testSuppressRedirect() {
449 $title = Title::makeTitle( NS_MAIN, 'TestSuppressRedirect' );
450 $title2 = Title::makeTitle( NS_MAIN, 'TestSuppressRedirect 2' );
451
452 $id = $this->createPage( $title );
453
454 $res = $this->doApiRequestWithToken( [
455 'action' => 'move',
456 'from' => $title->getPrefixedText(),
457 'to' => $title2->getPrefixedText(),
458 'noredirect' => '',
459 ] );
460
461 $this->assertMoved( $title, $title2, $id, 'noredirect' );
462 $this->assertArrayNotHasKey( 'warnings', $res[0] );
463 }
464
465 public function testSuppressRedirectNoPermission() {
466 $title = Title::makeTitle( NS_MAIN, 'TestSuppressRedirectNoPermission' );
467 $title2 = Title::makeTitle( NS_MAIN, 'TestSuppressRedirectNoPermission 2' );
468
469 $this->setGroupPermissions( 'sysop', 'suppressredirect', false );
470 $id = $this->createPage( $title );
471
472 $res = $this->doApiRequestWithToken( [
473 'action' => 'move',
474 'from' => $title->getPrefixedText(),
475 'to' => $title2->getPrefixedText(),
476 'noredirect' => '',
477 ] );
478
479 $this->assertMoved( $title, $title2, $id );
480 $this->assertArrayNotHasKey( 'warnings', $res[0] );
481 }
482
483 public function testMoveSubpagesError() {
484 $title = Title::makeTitle( NS_MAIN, 'TestMoveSubpagesError' );
485 $titleBase = $title->getTalkPageIfDefined();
486 $titleSub = Title::makeTitle( NS_MAIN, 'TestMoveSubpagesError/1' );
487 $talkTitleSub = $titleSub->getTalkPageIfDefined();
488
489 // Subpages are allowed in talk but not main
490 $idBase = $this->createPage( $titleBase );
491 $idSub = $this->createPage( $talkTitleSub );
492
493 $res = $this->doApiRequestWithToken( [
494 'action' => 'move',
495 'from' => $titleBase->getPrefixedText(),
496 'to' => $title->getPrefixedText(),
497 'movesubpages' => '',
498 ] );
499
500 $this->assertMoved( $titleBase, $title, $idBase );
501 $this->assertSame( $idSub, $talkTitleSub->getArticleID( IDBAccessObject::READ_LATEST ) );
502 $this->assertFalse( $titleSub->exists( IDBAccessObject::READ_LATEST ),
503 "\"{$titleSub->getPrefixedText()}\" should not exist" );
504
505 $this->assertSame( [ 'errors' => [ [
506 'message' => 'namespace-nosubpages',
507 'params' => [ '' ],
508 'code' => 'namespace-nosubpages',
509 'type' => 'error',
510 ] ] ], $res[0]['move']['subpages'] );
511
512 $this->assertArrayNotHasKey( 'warnings', $res[0] );
513 }
514 }