MediaWiki  1.32.5
ApiMoveTest.php
Go to the documentation of this file.
1 <?php
2 
10 class ApiMoveTest extends ApiTestCase {
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 
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 
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 }
ApiMoveTest\testMoveSubpagesError
testMoveSubpagesError()
Definition: ApiMoveTest.php:360
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:280
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:901
ApiMoveTest\testMoveSubpages
testMoveSubpages()
Definition: ApiMoveTest.php:257
ApiMoveTest\testMoveNoPermission
testMoveNoPermission()
Definition: ApiMoveTest.php:303
ApiMoveTest\testSuppressRedirectNoPermission
testSuppressRedirectNoPermission()
Definition: ApiMoveTest.php:342
$res
$res
Definition: database.txt:21
User
User
Definition: All_system_messages.txt:425
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:133
ApiMoveTest\testMoveById
testMoveById()
Definition: ApiMoveTest.php:79
ApiMoveTest\testMoveTalkFailed
testMoveTalkFailed()
Definition: ApiMoveTest.php:229
ApiMoveTest\testSuppressRedirect
testSuppressRedirect()
Definition: ApiMoveTest.php:326
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
ApiMoveTest\testMoveToInvalidPageName
testMoveToInvalidPageName()
Definition: ApiMoveTest.php:116
ApiTestCase\doApiRequestWithToken
doApiRequestWithToken(array $params, array $session=null, User $user=null, $tokenType='auto')
Convenience function to access the token parameter of doApiRequest() more succinctly.
Definition: ApiTestCase.php:133
ApiMoveTest\testTagsNoPermission
testTagsNoPermission()
Definition: ApiMoveTest.php:170
ChangeTags\defineTag
static defineTag( $tag)
Defines a tag in the valid_tag table and/or update ctd_user_defined field in change_tag_def,...
Definition: ChangeTags.php:915
ApiMoveTest\assertMoved
assertMoved( $from, $to, $id, $opts=null)
Definition: ApiMoveTest.php:17
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
ApiMoveTest\testPingLimiter
testPingLimiter()
Definition: ApiMoveTest.php:135
ApiMoveTest\createPage
createPage( $name)
Shortcut function to create a page and return its id.
Definition: ApiMoveTest.php:48
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ApiMoveTest\testSelfMove
testSelfMove()
Definition: ApiMoveTest.php:196
MediaWikiTestCase\editPage
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
Definition: MediaWikiTestCase.php:2333
ApiTestCase
Definition: ApiTestCase.php:5
MediaWikiTestCase\setGroupPermissions
setGroupPermissions( $newPerms, $newKey=null, $newValue=null)
Alters $wgGroupPermissions for the duration of the test.
Definition: MediaWikiTestCase.php:1092
ApiMoveTest\testFromWithFromid
testFromWithFromid()
Definition: ApiMoveTest.php:52
ApiMoveTest\testMoveTalk
testMoveTalk()
Definition: ApiMoveTest.php:210
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiMoveTest\testMove
testMove()
Definition: ApiMoveTest.php:64
ApiMoveTest
API Database medium.
Definition: ApiMoveTest.php:10
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
ApiMoveTest\testMoveNonexistentId
testMoveNonexistentId()
Definition: ApiMoveTest.php:105
ApiMoveTest\testMoveNonexistent
testMoveNonexistent()
Definition: ApiMoveTest.php:94