10 class ApiMoveTest
extends ApiTestCase
{
12 * @param string $from Prefixed name of source
13 * @param string $to Prefixed name of destination
14 * @param string $id Page id of the page to move
15 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
17 protected function assertMoved( $from, $to, $id, $opts = null ) {
20 $fromTitle = Title
::newFromText( $from );
21 $toTitle = Title
::newFromText( $to );
23 $this->assertTrue( $toTitle->exists(),
24 "Destination {$toTitle->getPrefixedText()} does not exist" );
26 if ( in_array( 'noredirect', $opts ) ) {
27 $this->assertFalse( $fromTitle->exists(),
28 "Source {$fromTitle->getPrefixedText()} exists" );
30 $this->assertTrue( $fromTitle->exists(),
31 "Source {$fromTitle->getPrefixedText()} does not exist" );
32 $this->assertTrue( $fromTitle->isRedirect(),
33 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
35 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
36 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
39 $this->assertSame( $id, $toTitle->getArticleId() );
43 * Shortcut function to create a page and return its id.
45 * @param string $name Page to create
46 * @return int ID of created page
48 protected function createPage( $name ) {
49 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
52 public function testFromWithFromid() {
53 $this->setExpectedException( ApiUsageException
::class,
54 'The parameters "from" and "fromid" can not be used together.' );
56 $this->doApiRequestWithToken( [
58 'from' => 'Some page',
60 'to' => 'Some other page',
64 public function testMove() {
65 $name = ucfirst( __FUNCTION__
);
67 $id = $this->createPage( $name );
69 $res = $this->doApiRequestWithToken( [
75 $this->assertMoved( $name, "$name 2", $id );
76 $this->assertArrayNotHasKey( 'warnings', $res[0] );
79 public function testMoveById() {
80 $name = ucfirst( __FUNCTION__
);
82 $id = $this->createPage( $name );
84 $res = $this->doApiRequestWithToken( [
90 $this->assertMoved( $name, "$name 2", $id );
91 $this->assertArrayNotHasKey( 'warnings', $res[0] );
94 public function testMoveNonexistent() {
95 $this->setExpectedException( ApiUsageException
::class,
96 "The page you specified doesn't exist." );
98 $this->doApiRequestWithToken( [
100 'from' => 'Nonexistent page',
101 'to' => 'Different page'
105 public function testMoveNonexistentId() {
106 $this->setExpectedException( ApiUsageException
::class,
107 'There is no page with ID 2147483647.' );
109 $this->doApiRequestWithToken( [
111 'fromid' => pow( 2, 31 ) - 1,
112 'to' => 'Different page',
116 public function testMoveToInvalidPageName() {
117 $this->setExpectedException( ApiUsageException
::class, 'Bad title "[".' );
119 $name = ucfirst( __FUNCTION__
);
120 $id = $this->createPage( $name );
123 $this->doApiRequestWithToken( [
129 $this->assertSame( $id, Title
::newFromText( $name )->getArticleId() );
135 public function testPingLimiter() {
136 global $wgRateLimits;
138 $this->setExpectedException( ApiUsageException
::class,
139 "You've exceeded your rate limit. Please wait some time and try again." );
141 $name = ucfirst( __FUNCTION__
);
143 $this->setMwGlobals( 'wgMainCacheType', 'hash' );
145 $this->stashMwGlobals( 'wgRateLimits' );
146 $wgRateLimits['move'] = [ '&can-bypass' => false, 'user' => [ 1, 60 ] ];
148 $id = $this->createPage( $name );
150 $res = $this->doApiRequestWithToken( [
156 $this->assertMoved( $name, "$name 2", $id );
157 $this->assertArrayNotHasKey( 'warnings', $res[0] );
160 $this->doApiRequestWithToken( [
166 $this->assertSame( $id, Title
::newFromText( "$name 2" )->getArticleId() );
167 $this->assertFalse( Title
::newFromText( "$name 3" )->exists(),
168 "\"$name 3\" should not exist" );
172 public function testTagsNoPermission() {
173 $this->setExpectedException( ApiUsageException
::class,
174 'You do not have permission to apply change tags along with your changes.' );
176 $name = ucfirst( __FUNCTION__
);
178 ChangeTags
::defineTag( 'custom tag' );
180 $this->setGroupPermissions( 'user', 'applychangetags', false );
182 $id = $this->createPage( $name );
185 $this->doApiRequestWithToken( [
189 'tags' => 'custom tag',
192 $this->assertSame( $id, Title
::newFromText( $name )->getArticleId() );
193 $this->assertFalse( Title
::newFromText( "$name 2" )->exists(),
194 "\"$name 2\" should not exist" );
198 public function testSelfMove() {
199 $this->setExpectedException( ApiUsageException
::class,
200 'The title is the same; cannot move a page over itself.' );
202 $name = ucfirst( __FUNCTION__
);
203 $this->createPage( $name );
205 $this->doApiRequestWithToken( [
212 public function testMoveTalk() {
213 $name = ucfirst( __FUNCTION__
);
215 $id = $this->createPage( $name );
216 $talkId = $this->createPage( "Talk:$name" );
218 $res = $this->doApiRequestWithToken( [
225 $this->assertMoved( $name, "$name 2", $id );
226 $this->assertMoved( "Talk:$name", "Talk:$name 2", $talkId );
228 $this->assertArrayNotHasKey( 'warnings', $res[0] );
231 public function testMoveTalkFailed() {
232 $name = ucfirst( __FUNCTION__
);
234 $id = $this->createPage( $name );
235 $talkId = $this->createPage( "Talk:$name" );
236 $talkDestinationId = $this->createPage( "Talk:$name 2" );
238 $res = $this->doApiRequestWithToken( [
245 $this->assertMoved( $name, "$name 2", $id );
246 $this->assertSame( $talkId, Title
::newFromText( "Talk:$name" )->getArticleId() );
247 $this->assertSame( $talkDestinationId,
248 Title
::newFromText( "Talk:$name 2" )->getArticleId() );
249 $this->assertSame( [ [
250 'message' => 'articleexists',
252 'code' => 'articleexists',
254 ] ], $res[0]['move']['talkmove-errors'] );
256 $this->assertArrayNotHasKey( 'warnings', $res[0] );
259 public function testMoveSubpages() {
260 global $wgNamespacesWithSubpages;
262 $name = ucfirst( __FUNCTION__
);
264 $this->stashMwGlobals( 'wgNamespacesWithSubpages' );
265 $wgNamespacesWithSubpages[NS_MAIN
] = true;
267 $pages = [ $name, "$name/1", "$name/2", "Talk:$name", "Talk:$name/1", "Talk:$name/3" ];
269 foreach ( array_merge( $pages, [ "$name/error", "$name 2/error" ] ) as $page ) {
270 $ids[$page] = $this->createPage( $page );
273 $res = $this->doApiRequestWithToken( [
278 'movesubpages' => '',
281 foreach ( $pages as $page ) {
282 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
285 $this->assertSame( $ids["$name/error"],
286 Title
::newFromText( "$name/error" )->getArticleId() );
287 $this->assertSame( $ids["$name 2/error"],
288 Title
::newFromText( "$name 2/error" )->getArticleId() );
290 $results = array_merge( $res[0]['move']['subpages'], $res[0]['move']['subpages-talk'] );
291 foreach ( $results as $arr ) {
292 if ( $arr['from'] === "$name/error" ) {
293 $this->assertSame( [ [
294 'message' => 'articleexists',
296 'code' => 'articleexists',
298 ] ], $arr['errors'] );
300 $this->assertSame( str_replace( $name, "$name 2", $arr['from'] ), $arr['to'] );
302 $this->assertCount( 2, $arr );
305 $this->assertArrayNotHasKey( 'warnings', $res[0] );
308 public function testMoveNoPermission() {
309 $this->setExpectedException( ApiUsageException
::class,
310 'You must be a registered user and [[Special:UserLogin|logged in]] to move a page.' );
312 $name = ucfirst( __FUNCTION__
);
314 $id = $this->createPage( $name );
319 $this->doApiRequestWithToken( [
325 $this->assertSame( $id, Title
::newFromText( "$name" )->getArticleId() );
326 $this->assertFalse( Title
::newFromText( "$name 2" )->exists(),
327 "\"$name 2\" should not exist" );
331 public function testSuppressRedirect() {
332 $name = ucfirst( __FUNCTION__
);
334 $id = $this->createPage( $name );
336 $res = $this->doApiRequestWithToken( [
343 $this->assertMoved( $name, "$name 2", $id, 'noredirect' );
344 $this->assertArrayNotHasKey( 'warnings', $res[0] );
347 public function testSuppressRedirectNoPermission() {
348 $name = ucfirst( __FUNCTION__
);
350 $this->setGroupPermissions( 'sysop', 'suppressredirect', false );
352 $id = $this->createPage( $name );
354 $res = $this->doApiRequestWithToken( [
361 $this->assertMoved( $name, "$name 2", $id );
362 $this->assertArrayNotHasKey( 'warnings', $res[0] );
365 public function testMoveSubpagesError() {
366 $name = ucfirst( __FUNCTION__
);
368 // Subpages are allowed in talk but not main
369 $idBase = $this->createPage( "Talk:$name" );
370 $idSub = $this->createPage( "Talk:$name/1" );
372 $res = $this->doApiRequestWithToken( [
374 'from' => "Talk:$name",
376 'movesubpages' => '',
379 $this->assertMoved( "Talk:$name", $name, $idBase );
380 $this->assertSame( $idSub, Title
::newFromText( "Talk:$name/1" )->getArticleId() );
381 $this->assertFalse( Title
::newFromText( "$name/1" )->exists(),
382 "\"$name/1\" should not exist" );
384 $this->assertSame( [ 'errors' => [ [
385 'message' => 'namespace-nosubpages',
387 'code' => 'namespace-nosubpages',
389 ] ] ], $res[0]['move']['subpages'] );
391 $this->assertArrayNotHasKey( 'warnings', $res[0] );