3 use MediaWiki\Block\DatabaseBlock
;
12 class ApiMoveTest
extends ApiTestCase
{
14 * @param string $from Prefixed name of source
15 * @param string $to Prefixed name of destination
16 * @param string $id Page id of the page to move
17 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
19 protected function assertMoved( $from, $to, $id, $opts = null ) {
23 $fromTitle = Title
::newFromText( $from );
24 $toTitle = Title
::newFromText( $to );
26 $this->assertTrue( $toTitle->exists(),
27 "Destination {$toTitle->getPrefixedText()} does not exist" );
29 if ( in_array( 'noredirect', $opts ) ) {
30 $this->assertFalse( $fromTitle->exists(),
31 "Source {$fromTitle->getPrefixedText()} exists" );
33 $this->assertTrue( $fromTitle->exists(),
34 "Source {$fromTitle->getPrefixedText()} does not exist" );
35 $this->assertTrue( $fromTitle->isRedirect(),
36 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
38 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
39 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
42 $this->assertSame( $id, $toTitle->getArticleID() );
46 * Shortcut function to create a page and return its id.
48 * @param string $name Page to create
49 * @return int ID of created page
51 protected function createPage( $name ) {
52 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
55 public function testFromWithFromid() {
56 $this->setExpectedException( ApiUsageException
::class,
57 'The parameters "from" and "fromid" can not be used together.' );
59 $this->doApiRequestWithToken( [
61 'from' => 'Some page',
63 'to' => 'Some other page',
67 public function testMove() {
68 $name = ucfirst( __FUNCTION__
);
70 $id = $this->createPage( $name );
72 $res = $this->doApiRequestWithToken( [
78 $this->assertMoved( $name, "$name 2", $id );
79 $this->assertArrayNotHasKey( 'warnings', $res[0] );
82 public function testMoveById() {
83 $name = ucfirst( __FUNCTION__
);
85 $id = $this->createPage( $name );
87 $res = $this->doApiRequestWithToken( [
93 $this->assertMoved( $name, "$name 2", $id );
94 $this->assertArrayNotHasKey( 'warnings', $res[0] );
97 public function testMoveNonexistent() {
98 $this->setExpectedException( ApiUsageException
::class,
99 "The page you specified doesn't exist." );
101 $this->doApiRequestWithToken( [
103 'from' => 'Nonexistent page',
104 'to' => 'Different page'
108 public function testMoveNonexistentId() {
109 $this->setExpectedException( ApiUsageException
::class,
110 'There is no page with ID 2147483647.' );
112 $this->doApiRequestWithToken( [
114 'fromid' => pow( 2, 31 ) - 1,
115 'to' => 'Different page',
119 public function testMoveToInvalidPageName() {
120 $this->setExpectedException( ApiUsageException
::class, 'Bad title "[".' );
122 $name = ucfirst( __FUNCTION__
);
123 $id = $this->createPage( $name );
126 $this->doApiRequestWithToken( [
132 $this->assertSame( $id, Title
::newFromText( $name )->getArticleID() );
136 public function testMoveWhileBlocked() {
137 $this->assertNull( DatabaseBlock
::newFromTarget( '127.0.0.1' ), 'Sanity check' );
139 $block = new DatabaseBlock( [
140 'address' => self
::$users['sysop']->getUser()->getName(),
141 'by' => self
::$users['sysop']->getUser()->getId(),
142 'reason' => 'Capriciousness',
143 'timestamp' => '19370101000000',
144 'expiry' => 'infinity',
145 'enableAutoblock' => true,
149 $name = ucfirst( __FUNCTION__
);
150 $id = $this->createPage( $name );
153 $this->doApiRequestWithToken( [
158 $this->fail( 'Expected exception not thrown' );
159 } catch ( ApiUsageException
$ex ) {
160 $this->assertSame( 'You have been blocked from editing.', $ex->getMessage() );
161 $this->assertNotNull( DatabaseBlock
::newFromTarget( '127.0.0.1' ), 'Autoblock spread' );
164 self
::$users['sysop']->getUser()->clearInstanceCache();
165 $this->assertSame( $id, Title
::newFromText( $name )->getArticleID() );
171 public function testPingLimiter() {
172 $this->setExpectedException( ApiUsageException
::class,
173 "You've exceeded your rate limit. Please wait some time and try again." );
175 $name = ucfirst( __FUNCTION__
);
177 $this->setMwGlobals( 'wgMainCacheType', 'hash' );
179 $this->mergeMwGlobalArrayValue( 'wgRateLimits',
180 [ 'move' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
182 $id = $this->createPage( $name );
184 $res = $this->doApiRequestWithToken( [
190 $this->assertMoved( $name, "$name 2", $id );
191 $this->assertArrayNotHasKey( 'warnings', $res[0] );
194 $this->doApiRequestWithToken( [
200 $this->assertSame( $id, Title
::newFromText( "$name 2" )->getArticleID() );
201 $this->assertFalse( Title
::newFromText( "$name 3" )->exists(),
202 "\"$name 3\" should not exist" );
206 public function testTagsNoPermission() {
207 $this->setExpectedException( ApiUsageException
::class,
208 'You do not have permission to apply change tags along with your changes.' );
210 $name = ucfirst( __FUNCTION__
);
212 ChangeTags
::defineTag( 'custom tag' );
214 $this->setGroupPermissions( 'user', 'applychangetags', false );
215 $this->overrideMwServices();
217 $id = $this->createPage( $name );
220 $this->doApiRequestWithToken( [
224 'tags' => 'custom tag',
227 $this->assertSame( $id, Title
::newFromText( $name )->getArticleID() );
228 $this->assertFalse( Title
::newFromText( "$name 2" )->exists(),
229 "\"$name 2\" should not exist" );
233 public function testSelfMove() {
234 $this->setExpectedException( ApiUsageException
::class,
235 'The title is the same; cannot move a page over itself.' );
237 $name = ucfirst( __FUNCTION__
);
238 $this->createPage( $name );
240 $this->doApiRequestWithToken( [
247 public function testMoveTalk() {
248 $name = ucfirst( __FUNCTION__
);
250 $id = $this->createPage( $name );
251 $talkId = $this->createPage( "Talk:$name" );
253 $res = $this->doApiRequestWithToken( [
260 $this->assertMoved( $name, "$name 2", $id );
261 $this->assertMoved( "Talk:$name", "Talk:$name 2", $talkId );
263 $this->assertArrayNotHasKey( 'warnings', $res[0] );
266 public function testMoveTalkFailed() {
267 $name = ucfirst( __FUNCTION__
);
269 $id = $this->createPage( $name );
270 $talkId = $this->createPage( "Talk:$name" );
271 $talkDestinationId = $this->createPage( "Talk:$name 2" );
273 $res = $this->doApiRequestWithToken( [
280 $this->assertMoved( $name, "$name 2", $id );
281 $this->assertSame( $talkId, Title
::newFromText( "Talk:$name" )->getArticleID() );
282 $this->assertSame( $talkDestinationId,
283 Title
::newFromText( "Talk:$name 2" )->getArticleID() );
284 $this->assertSame( [ [
285 'message' => 'articleexists',
287 'code' => 'articleexists',
289 ] ], $res[0]['move']['talkmove-errors'] );
291 $this->assertArrayNotHasKey( 'warnings', $res[0] );
294 public function testMoveSubpages() {
295 $name = ucfirst( __FUNCTION__
);
297 $this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN
=> true ] );
298 $this->overrideMwServices();
300 $pages = [ $name, "$name/1", "$name/2", "Talk:$name", "Talk:$name/1", "Talk:$name/3" ];
302 foreach ( array_merge( $pages, [ "$name/error", "$name 2/error" ] ) as $page ) {
303 $ids[$page] = $this->createPage( $page );
306 $res = $this->doApiRequestWithToken( [
311 'movesubpages' => '',
314 foreach ( $pages as $page ) {
315 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
318 $this->assertSame( $ids["$name/error"],
319 Title
::newFromText( "$name/error" )->getArticleID() );
320 $this->assertSame( $ids["$name 2/error"],
321 Title
::newFromText( "$name 2/error" )->getArticleID() );
323 $results = array_merge( $res[0]['move']['subpages'], $res[0]['move']['subpages-talk'] );
324 foreach ( $results as $arr ) {
325 if ( $arr['from'] === "$name/error" ) {
326 $this->assertSame( [ [
327 'message' => 'articleexists',
329 'code' => 'articleexists',
331 ] ], $arr['errors'] );
333 $this->assertSame( str_replace( $name, "$name 2", $arr['from'] ), $arr['to'] );
335 $this->assertCount( 2, $arr );
338 $this->assertArrayNotHasKey( 'warnings', $res[0] );
341 public function testMoveNoPermission() {
342 $this->setExpectedException( ApiUsageException
::class,
343 'You must be a registered user and [[Special:UserLogin|logged in]] to move a page.' );
345 $name = ucfirst( __FUNCTION__
);
347 $id = $this->createPage( $name );
352 $this->doApiRequestWithToken( [
358 $this->assertSame( $id, Title
::newFromText( "$name" )->getArticleID() );
359 $this->assertFalse( Title
::newFromText( "$name 2" )->exists(),
360 "\"$name 2\" should not exist" );
364 public function testSuppressRedirect() {
365 $name = ucfirst( __FUNCTION__
);
367 $id = $this->createPage( $name );
369 $res = $this->doApiRequestWithToken( [
376 $this->assertMoved( $name, "$name 2", $id, 'noredirect' );
377 $this->assertArrayNotHasKey( 'warnings', $res[0] );
380 public function testSuppressRedirectNoPermission() {
381 $name = ucfirst( __FUNCTION__
);
383 $this->setGroupPermissions( 'sysop', 'suppressredirect', false );
384 $this->overrideMwServices();
386 $id = $this->createPage( $name );
388 $res = $this->doApiRequestWithToken( [
395 $this->assertMoved( $name, "$name 2", $id );
396 $this->assertArrayNotHasKey( 'warnings', $res[0] );
399 public function testMoveSubpagesError() {
400 $name = ucfirst( __FUNCTION__
);
402 // Subpages are allowed in talk but not main
403 $idBase = $this->createPage( "Talk:$name" );
404 $idSub = $this->createPage( "Talk:$name/1" );
406 $res = $this->doApiRequestWithToken( [
408 'from' => "Talk:$name",
410 'movesubpages' => '',
413 $this->assertMoved( "Talk:$name", $name, $idBase );
414 $this->assertSame( $idSub, Title
::newFromText( "Talk:$name/1" )->getArticleID() );
415 $this->assertFalse( Title
::newFromText( "$name/1" )->exists(),
416 "\"$name/1\" should not exist" );
418 $this->assertSame( [ 'errors' => [ [
419 'message' => 'namespace-nosubpages',
421 'code' => 'namespace-nosubpages',
423 ] ] ], $res[0]['move']['subpages'] );
425 $this->assertArrayNotHasKey( 'warnings', $res[0] );