Merge "Chinese Conversion Table Update 2018-3"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiMoveTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiMove
9 */
10 class ApiMoveTest extends ApiTestCase {
11 /**
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
16 */
17 protected function assertMoved( $from, $to, $id, $opts = null ) {
18 $opts = (array)$opts;
19
20 $fromTitle = Title::newFromText( $from );
21 $toTitle = Title::newFromText( $to );
22
23 $this->assertTrue( $toTitle->exists(),
24 "Destination {$toTitle->getPrefixedText()} does not exist" );
25
26 if ( in_array( 'noredirect', $opts ) ) {
27 $this->assertFalse( $fromTitle->exists(),
28 "Source {$fromTitle->getPrefixedText()} exists" );
29 } else {
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" );
34
35 $target = Revision::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
36 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
37 }
38
39 $this->assertSame( $id, $toTitle->getArticleId() );
40 }
41
42 /**
43 * Shortcut function to create a page and return its id.
44 *
45 * @param string $name Page to create
46 * @return int ID of created page
47 */
48 protected function createPage( $name ) {
49 return $this->editPage( $name, 'Content' )->value['revision']->getPage();
50 }
51
52 public function testFromWithFromid() {
53 $this->setExpectedException( ApiUsageException::class,
54 'The parameters "from" and "fromid" can not be used together.' );
55
56 $this->doApiRequestWithToken( [
57 'action' => 'move',
58 'from' => 'Some page',
59 'fromid' => 123,
60 'to' => 'Some other page',
61 ] );
62 }
63
64 public function testMove() {
65 $name = ucfirst( __FUNCTION__ );
66
67 $id = $this->createPage( $name );
68
69 $res = $this->doApiRequestWithToken( [
70 'action' => 'move',
71 'from' => $name,
72 'to' => "$name 2",
73 ] );
74
75 $this->assertMoved( $name, "$name 2", $id );
76 $this->assertArrayNotHasKey( 'warnings', $res[0] );
77 }
78
79 public function testMoveById() {
80 $name = ucfirst( __FUNCTION__ );
81
82 $id = $this->createPage( $name );
83
84 $res = $this->doApiRequestWithToken( [
85 'action' => 'move',
86 'fromid' => $id,
87 'to' => "$name 2",
88 ] );
89
90 $this->assertMoved( $name, "$name 2", $id );
91 $this->assertArrayNotHasKey( 'warnings', $res[0] );
92 }
93
94 public function testMoveNonexistent() {
95 $this->setExpectedException( ApiUsageException::class,
96 "The page you specified doesn't exist." );
97
98 $this->doApiRequestWithToken( [
99 'action' => 'move',
100 'from' => 'Nonexistent page',
101 'to' => 'Different page'
102 ] );
103 }
104
105 public function testMoveNonexistentId() {
106 $this->setExpectedException( ApiUsageException::class,
107 'There is no page with ID 2147483647.' );
108
109 $this->doApiRequestWithToken( [
110 'action' => 'move',
111 'fromid' => pow( 2, 31 ) - 1,
112 'to' => 'Different page',
113 ] );
114 }
115
116 public function testMoveToInvalidPageName() {
117 $this->setExpectedException( ApiUsageException::class, 'Bad title "[".' );
118
119 $name = ucfirst( __FUNCTION__ );
120 $id = $this->createPage( $name );
121
122 try {
123 $this->doApiRequestWithToken( [
124 'action' => 'move',
125 'from' => $name,
126 'to' => '[',
127 ] );
128 } finally {
129 $this->assertSame( $id, Title::newFromText( $name )->getArticleId() );
130 }
131 }
132
133 // @todo File moving
134
135 public function testPingLimiter() {
136 $this->setExpectedException( ApiUsageException::class,
137 "You've exceeded your rate limit. Please wait some time and try again." );
138
139 $name = ucfirst( __FUNCTION__ );
140
141 $this->setMwGlobals( 'wgMainCacheType', 'hash' );
142
143 $this->mergeMwGlobalArrayValue( 'wgRateLimits',
144 [ 'move' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
145
146 $id = $this->createPage( $name );
147
148 $res = $this->doApiRequestWithToken( [
149 'action' => 'move',
150 'from' => $name,
151 'to' => "$name 2",
152 ] );
153
154 $this->assertMoved( $name, "$name 2", $id );
155 $this->assertArrayNotHasKey( 'warnings', $res[0] );
156
157 try {
158 $this->doApiRequestWithToken( [
159 'action' => 'move',
160 'from' => "$name 2",
161 'to' => "$name 3",
162 ] );
163 } finally {
164 $this->assertSame( $id, Title::newFromText( "$name 2" )->getArticleId() );
165 $this->assertFalse( Title::newFromText( "$name 3" )->exists(),
166 "\"$name 3\" should not exist" );
167 }
168 }
169
170 public function testTagsNoPermission() {
171 $this->setExpectedException( ApiUsageException::class,
172 'You do not have permission to apply change tags along with your changes.' );
173
174 $name = ucfirst( __FUNCTION__ );
175
176 ChangeTags::defineTag( 'custom tag' );
177
178 $this->setGroupPermissions( 'user', 'applychangetags', false );
179
180 $id = $this->createPage( $name );
181
182 try {
183 $this->doApiRequestWithToken( [
184 'action' => 'move',
185 'from' => $name,
186 'to' => "$name 2",
187 'tags' => 'custom tag',
188 ] );
189 } finally {
190 $this->assertSame( $id, Title::newFromText( $name )->getArticleId() );
191 $this->assertFalse( Title::newFromText( "$name 2" )->exists(),
192 "\"$name 2\" should not exist" );
193 }
194 }
195
196 public function testSelfMove() {
197 $this->setExpectedException( ApiUsageException::class,
198 'The title is the same; cannot move a page over itself.' );
199
200 $name = ucfirst( __FUNCTION__ );
201 $this->createPage( $name );
202
203 $this->doApiRequestWithToken( [
204 'action' => 'move',
205 'from' => $name,
206 'to' => $name,
207 ] );
208 }
209
210 public function testMoveTalk() {
211 $name = ucfirst( __FUNCTION__ );
212
213 $id = $this->createPage( $name );
214 $talkId = $this->createPage( "Talk:$name" );
215
216 $res = $this->doApiRequestWithToken( [
217 'action' => 'move',
218 'from' => $name,
219 'to' => "$name 2",
220 'movetalk' => '',
221 ] );
222
223 $this->assertMoved( $name, "$name 2", $id );
224 $this->assertMoved( "Talk:$name", "Talk:$name 2", $talkId );
225
226 $this->assertArrayNotHasKey( 'warnings', $res[0] );
227 }
228
229 public function testMoveTalkFailed() {
230 $name = ucfirst( __FUNCTION__ );
231
232 $id = $this->createPage( $name );
233 $talkId = $this->createPage( "Talk:$name" );
234 $talkDestinationId = $this->createPage( "Talk:$name 2" );
235
236 $res = $this->doApiRequestWithToken( [
237 'action' => 'move',
238 'from' => $name,
239 'to' => "$name 2",
240 'movetalk' => '',
241 ] );
242
243 $this->assertMoved( $name, "$name 2", $id );
244 $this->assertSame( $talkId, Title::newFromText( "Talk:$name" )->getArticleId() );
245 $this->assertSame( $talkDestinationId,
246 Title::newFromText( "Talk:$name 2" )->getArticleId() );
247 $this->assertSame( [ [
248 'message' => 'articleexists',
249 'params' => [],
250 'code' => 'articleexists',
251 'type' => 'error',
252 ] ], $res[0]['move']['talkmove-errors'] );
253
254 $this->assertArrayNotHasKey( 'warnings', $res[0] );
255 }
256
257 public function testMoveSubpages() {
258 $name = ucfirst( __FUNCTION__ );
259
260 $this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => true ] );
261
262 $pages = [ $name, "$name/1", "$name/2", "Talk:$name", "Talk:$name/1", "Talk:$name/3" ];
263 $ids = [];
264 foreach ( array_merge( $pages, [ "$name/error", "$name 2/error" ] ) as $page ) {
265 $ids[$page] = $this->createPage( $page );
266 }
267
268 $res = $this->doApiRequestWithToken( [
269 'action' => 'move',
270 'from' => $name,
271 'to' => "$name 2",
272 'movetalk' => '',
273 'movesubpages' => '',
274 ] );
275
276 foreach ( $pages as $page ) {
277 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
278 }
279
280 $this->assertSame( $ids["$name/error"],
281 Title::newFromText( "$name/error" )->getArticleId() );
282 $this->assertSame( $ids["$name 2/error"],
283 Title::newFromText( "$name 2/error" )->getArticleId() );
284
285 $results = array_merge( $res[0]['move']['subpages'], $res[0]['move']['subpages-talk'] );
286 foreach ( $results as $arr ) {
287 if ( $arr['from'] === "$name/error" ) {
288 $this->assertSame( [ [
289 'message' => 'articleexists',
290 'params' => [],
291 'code' => 'articleexists',
292 'type' => 'error'
293 ] ], $arr['errors'] );
294 } else {
295 $this->assertSame( str_replace( $name, "$name 2", $arr['from'] ), $arr['to'] );
296 }
297 $this->assertCount( 2, $arr );
298 }
299
300 $this->assertArrayNotHasKey( 'warnings', $res[0] );
301 }
302
303 public function testMoveNoPermission() {
304 $this->setExpectedException( ApiUsageException::class,
305 'You must be a registered user and [[Special:UserLogin|logged in]] to move a page.' );
306
307 $name = ucfirst( __FUNCTION__ );
308
309 $id = $this->createPage( $name );
310
311 $user = new User();
312
313 try {
314 $this->doApiRequestWithToken( [
315 'action' => 'move',
316 'from' => $name,
317 'to' => "$name 2",
318 ], null, $user );
319 } finally {
320 $this->assertSame( $id, Title::newFromText( "$name" )->getArticleId() );
321 $this->assertFalse( Title::newFromText( "$name 2" )->exists(),
322 "\"$name 2\" should not exist" );
323 }
324 }
325
326 public function testSuppressRedirect() {
327 $name = ucfirst( __FUNCTION__ );
328
329 $id = $this->createPage( $name );
330
331 $res = $this->doApiRequestWithToken( [
332 'action' => 'move',
333 'from' => $name,
334 'to' => "$name 2",
335 'noredirect' => '',
336 ] );
337
338 $this->assertMoved( $name, "$name 2", $id, 'noredirect' );
339 $this->assertArrayNotHasKey( 'warnings', $res[0] );
340 }
341
342 public function testSuppressRedirectNoPermission() {
343 $name = ucfirst( __FUNCTION__ );
344
345 $this->setGroupPermissions( 'sysop', 'suppressredirect', false );
346
347 $id = $this->createPage( $name );
348
349 $res = $this->doApiRequestWithToken( [
350 'action' => 'move',
351 'from' => $name,
352 'to' => "$name 2",
353 'noredirect' => '',
354 ] );
355
356 $this->assertMoved( $name, "$name 2", $id );
357 $this->assertArrayNotHasKey( 'warnings', $res[0] );
358 }
359
360 public function testMoveSubpagesError() {
361 $name = ucfirst( __FUNCTION__ );
362
363 // Subpages are allowed in talk but not main
364 $idBase = $this->createPage( "Talk:$name" );
365 $idSub = $this->createPage( "Talk:$name/1" );
366
367 $res = $this->doApiRequestWithToken( [
368 'action' => 'move',
369 'from' => "Talk:$name",
370 'to' => $name,
371 'movesubpages' => '',
372 ] );
373
374 $this->assertMoved( "Talk:$name", $name, $idBase );
375 $this->assertSame( $idSub, Title::newFromText( "Talk:$name/1" )->getArticleId() );
376 $this->assertFalse( Title::newFromText( "$name/1" )->exists(),
377 "\"$name/1\" should not exist" );
378
379 $this->assertSame( [ 'errors' => [ [
380 'message' => 'namespace-nosubpages',
381 'params' => [ '' ],
382 'code' => 'namespace-nosubpages',
383 'type' => 'error',
384 ] ] ], $res[0]['move']['subpages'] );
385
386 $this->assertArrayNotHasKey( 'warnings', $res[0] );
387 }
388 }