Debian Codegraph @

tests/phpunit/includes/page/MovePageTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Content\WikitextContent;
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Page\MovePage;
9 use MediaWiki\Revision\RevisionRecord;
10 use MediaWiki\Revision\SlotRecord;
11 use MediaWiki\Tests\Rest\Handler\MediaTestTrait;
12 use MediaWiki\Tests\Unit\DummyServicesTrait;
13 use MediaWiki\Title\Title;
14 use MediaWiki\User\User;
15 use MediaWiki\Watchlist\WatchedItemStore;
16 use Wikimedia\Rdbms\IConnectionProvider;
17 use Wikimedia\Rdbms\IDatabase;
18 use Wikimedia\Rdbms\IDBAccessObject;
19
20 /**
21 * @covers \MediaWiki\Page\MovePage
22 * @group Database
23 */
24 class MovePageTest extends MediaWikiIntegrationTestCase {
25 use DummyServicesTrait;
26 use MediaTestTrait;
27
28 /**
29 * @param Title $old
30 * @param Title $new
31 * @param array $params Valid keys are: db, options,
32 * options is an indexed array that will overwrite our defaults, not a ServiceOptions, so it
33 * need not contain all keys.
34 * @return MovePage
35 */
36 private function newMovePageWithMocks( $old, $new, array $params = [] ): MovePage {
37 $mockProvider = $this->createNoOpMock( IConnectionProvider::class, [ 'getPrimaryDatabase' ] );
38 $mockProvider->method( 'getPrimaryDatabase' )
39 ->willReturn( $params['db'] ?? $this->createNoOpMock( IDatabase::class ) );
40
41 return new MovePage(
42 $old,
43 $new,
44 new ServiceOptions(
45 MovePage::CONSTRUCTOR_OPTIONS,
46 $params['options'] ?? [],
47 [
48 MainConfigNames::CategoryCollation => 'uppercase',
49 MainConfigNames::MaximumMovedPages => 100,
50 ]
51 ),
52 $mockProvider,
53 $this->getDummyNamespaceInfo(),
54 $this->createMock( WatchedItemStore::class ),
55 $this->makeMockRepoGroup(
56 [ 'Existent.jpg', 'Existent2.jpg', 'Existent-file-no-page.jpg' ]
57 ),
58 $this->getServiceContainer()->getContentHandlerFactory(),
59 $this->getServiceContainer()->getRevisionStore(),
60 $this->getServiceContainer()->getSpamChecker(),
61 $this->getServiceContainer()->getHookContainer(),
62 $this->getServiceContainer()->getWikiPageFactory(),
63 $this->getServiceContainer()->getUserFactory(),
64 $this->getServiceContainer()->getUserEditTracker(),
65 $this->getServiceContainer()->getMovePageFactory(),
66 $this->getServiceContainer()->getCollationFactory(),
67 $this->getServiceContainer()->getPageUpdaterFactory(),
68 $this->getServiceContainer()->getRestrictionStore(),
69 $this->getServiceContainer()->getDeletePageFactory(),
70 $this->getServiceContainer()->getLogFormatterFactory()
71 );
72 }
73
74 protected function setUp(): void {
75 parent::setUp();
76
77 // To avoid problems with namespace localization
78 $this->overrideConfigValue( MainConfigNames::LanguageCode, 'en' );
79
80 // Ensure we have some pages that are guaranteed to exist or not
81 $this->getExistingTestPage( 'Existent' );
82 $this->getExistingTestPage( 'Existent2' );
83 $this->getExistingTestPage( 'File:Existent.jpg' );
84 $this->getExistingTestPage( 'File:Existent2.jpg' );
85 $this->getExistingTestPage( 'File:Non-file.jpg' );
86 // Special treatment as we can't just add wikitext to a JS page
87 $this->insertPage( 'MediaWiki:Existent.js', '// Hello this is JavaScript!' );
88 $this->getExistingTestPage( 'Hooked in place' );
89 $this->getNonexistingTestPage( 'Nonexistent' );
90 $this->getNonexistingTestPage( 'Nonexistent2' );
91 $this->getNonexistingTestPage( 'File:Nonexistent.jpg' );
92 $this->getNonexistingTestPage( 'File:Nonexistent.png' );
93 $this->getNonexistingTestPage( 'File:Existent-file-no-page.jpg' );
94 $this->getNonexistingTestPage( 'MediaWiki:Nonexistent' );
95 $this->getNonexistingTestPage( 'No content allowed' );
96
97 // Set a couple of hooks for specific pages
98 $this->setTemporaryHook( 'ContentModelCanBeUsedOn',
99 static function ( $modelId, Title $title, &$ok ) {
100 if ( $title->getPrefixedText() === 'No content allowed' ) {
101 $ok = false;
102 }
103 }
104 );
105
106 $this->setTemporaryHook( 'TitleIsMovable',
107 static function ( Title $title, &$result ) {
108 if ( strtolower( $title->getPrefixedText() ) === 'hooked in place' ) {
109 $result = false;
110 }
111 }
112 );
113 }
114
115 /**
116 * @dataProvider provideIsValidMove
117 * @covers \MediaWiki\Page\MovePage::isValidMove
118 * @covers \MediaWiki\Page\MovePage::isValidMoveTarget
119 * @covers \MediaWiki\Page\MovePage::isValidFileMove
120 * @covers \MediaWiki\Page\MovePage::__construct
121 *
122 * @param string|Title $old
123 * @param string|Title $new
124 * @param StatusValue $expectedStatus
125 * @param array $extraOptions
126 */
127 public function testIsValidMove(
128 $old, $new, StatusValue $expectedStatus, array $extraOptions = []
129 ) {
130 $iwLookup = $this->createMock( InterwikiLookup::class );
131 $iwLookup->method( 'isValidInterwiki' )
132 ->willReturn( true );
133
134 $this->setService( 'InterwikiLookup', $iwLookup );
135
136 $old = $old instanceof Title ? $old : Title::newFromText( $old );
137 $new = $new instanceof Title ? $new : Title::newFromText( $new );
138 $mp = $this->newMovePageWithMocks( $old, $new, [ 'options' => $extraOptions ] );
139 $this->assertStatusMessagesExactly(
140 $expectedStatus,
141 $mp->isValidMove()
142 );
143 }
144
145 public static function provideIsValidMove() {
146 $ret = [
147 'Valid move with redirect' => [
148 'Existent',
149 'Nonexistent',
150 StatusValue::newGood(),
151 [ 'createRedirect' => true ]
152 ],
153 'Valid move without redirect' => [
154 'Existent',
155 'Nonexistent',
156 StatusValue::newGood(),
157 [ 'createRedirect' => false ]
158 ],
159 'Self move' => [
160 'Existent',
161 'Existent',
162 StatusValue::newFatal( 'selfmove' ),
163 ],
164 'Move from empty name' => [
165 Title::makeTitle( NS_MAIN, '' ),
166 'Nonexistent',
167 // @todo More specific error message, or make the move valid if the page actually
168 // exists somehow in the database
169 StatusValue::newFatal( 'badarticleerror' ),
170 ],
171 'Move to empty name' => [
172 'Existent',
173 Title::makeTitle( NS_MAIN, '' ),
174 StatusValue::newFatal( 'movepage-invalid-target-title' ),
175 ],
176 'Move to invalid name' => [
177 'Existent',
178 Title::makeTitle( NS_MAIN, '<' ),
179 StatusValue::newFatal( 'movepage-invalid-target-title' ),
180 ],
181 'Move between invalid names' => [
182 Title::makeTitle( NS_MAIN, '<' ),
183 Title::makeTitle( NS_MAIN, '>' ),
184 // @todo First error message should be more specific, or maybe we should make moving
185 // such pages valid if they actually exist somehow in the database
186 StatusValue::newFatal( 'movepage-source-doesnt-exist', '<' )
187 ->fatal( 'movepage-invalid-target-title' ),
188 ],
189 'Move nonexistent' => [
190 'Nonexistent',
191 'Nonexistent2',
192 StatusValue::newFatal( 'movepage-source-doesnt-exist', 'Nonexistent' ),
193 ],
194 'Move over existing' => [
195 'Existent',
196 'Existent2',
197 StatusValue::newFatal( 'articleexists', 'Existent2' ),
198 ],
199 'Move from another wiki' => [
200 Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ),
201 'Nonexistent',
202 StatusValue::newFatal( 'immobile-source-namespace-iw' ),
203 ],
204 'Move special page' => [
205 'Special:FooBar',
206 'Nonexistent',
207 StatusValue::newFatal( 'immobile-source-namespace', 'Special' ),
208 ],
209 'Move to another wiki' => [
210 'Existent',
211 Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ),
212 StatusValue::newFatal( 'immobile-target-namespace-iw' ),
213 ],
214 'Move to special page' => [
215 'Existent',
216 'Special:FooBar',
217 StatusValue::newFatal( 'immobile-target-namespace', 'Special' ),
218 ],
219 'Move to allowed content model' => [
220 'MediaWiki:Existent.js',
221 'MediaWiki:Nonexistent',
222 StatusValue::newGood(),
223 ],
224 'Move to prohibited content model' => [
225 'Existent',
226 'No content allowed',
227 StatusValue::newFatal( 'content-not-allowed-here', 'wikitext', 'No content allowed', 'main' ),
228 ],
229 'Aborted by hook' => [
230 'Hooked in place',
231 'Nonexistent',
232 StatusValue::newFatal( 'immobile-source-namespace', '(Main)' ),
233 ],
234 'Doubly aborted by hook' => [
235 'Hooked in place',
236 'Hooked In Place',
237 StatusValue::newFatal( 'immobile-source-namespace', '(Main)' )
238 ->fatal( 'immobile-target-namespace', '(Main)' ),
239 ],
240 'Non-file to file' => [
241 'Existent',
242 'File:Nonexistent.jpg',
243 StatusValue::newFatal( 'nonfile-cannot-move-to-file' ),
244 ],
245 'File to non-file' => [
246 'File:Existent.jpg',
247 'Nonexistent',
248 StatusValue::newFatal( 'imagenocrossnamespace' ),
249 ],
250 'Existing file to non-existing file' => [
251 'File:Existent.jpg',
252 'File:Nonexistent.jpg',
253 StatusValue::newGood(),
254 ],
255 'Existing file to existing file' => [
256 'File:Existent.jpg',
257 'File:Existent2.jpg',
258 StatusValue::newFatal( 'articleexists', 'File:Existent2.jpg' ),
259 ],
260 'Existing file to existing file with no page' => [
261 'File:Existent.jpg',
262 'File:Existent-file-no-page.jpg',
263 // @todo Is this correct? Moving over an existing file with no page should succeed?
264 StatusValue::newGood(),
265 ],
266 'Existing file to name with slash' => [
267 'File:Existent.jpg',
268 'File:Existent/slashed.jpg',
269 StatusValue::newFatal( 'imageinvalidfilename' ),
270 ],
271 'Mismatched file extension' => [
272 'File:Existent.jpg',
273 'File:Nonexistent.png',
274 StatusValue::newFatal( 'imagetypemismatch' ),
275 ],
276 'Non-file page in the File namespace' => [
277 'File:Non-file.jpg',
278 'File:Non-file-new.png',
279 StatusValue::newGood(),
280 ],
281 'File too long' => [
282 'File:Existent.jpg',
283 'File:0123456789012345678901234567890123456789012345678901234567890123456789' .
284 '0123456789012345678901234567890123456789012345678901234567890123456789' .
285 '0123456789012345678901234567890123456789012345678901234567890123456789' .
286 '012345678901234567890123456789-long.jpg',
287 StatusValue::newFatal( 'filename-toolong' ),
288 ],
289 // The FileRepo mock does not return true for ->backendSupportsUnicodePaths()
290 'Non-ascii' => [
291 'File:Existent.jpg',
292 'File:🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈.jpg',
293 StatusValue::newFatal( 'filename-toolong' )
294 ->fatal( 'windows-nonascii-filename' ),
295 ],
296 'Non-file move long with unicode' => [
297 'File:Non-file.jpg',
298 'File:🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈🏳️‍🌈🏳️‍🌈🏳️‍🌈 🏳️‍🌈.jpg',
299 StatusValue::newGood()
300 ],
301 'File just extension' => [
302 'File:Existent.jpg',
303 'File:.jpg',
304 StatusValue::newFatal( 'filename-tooshort' )
305 ->fatal( 'imagetypemismatch' ),
306 ],
307 ];
308 return $ret;
309 }
310
311 /**
312 * @dataProvider provideIsValidMove
313 *
314 * @param string|Title $old Old name
315 * @param string|Title $new New name
316 * @param StatusValue $expectedStatus
317 * @param array $extraOptions
318 */
319 public function testMove( $old, $new, StatusValue $expectedStatus, array $extraOptions = [] ) {
320 $iwLookup = $this->createMock( InterwikiLookup::class );
321 $iwLookup->method( 'isValidInterwiki' )
322 ->willReturn( true );
323
324 $this->setService( 'InterwikiLookup', $iwLookup );
325
326 $old = $old instanceof Title ? $old : Title::newFromText( $old );
327 $new = $new instanceof Title ? $new : Title::newFromText( $new );
328
329 $createRedirect = $extraOptions['createRedirect'] ?? true;
330 unset( $extraOptions['createRedirect'] );
331 $params = [ 'options' => $extraOptions ];
332
333 if ( !$expectedStatus->isGood() ) {
334 $obj = $this->newMovePageWithMocks( $old, $new, $params );
335 $status = $obj->move( $this->getTestUser()->getUser() );
336 $this->assertStatusMessagesExactly(
337 $expectedStatus,
338 $status
339 );
340 } else {
341 $oldPageId = $old->getArticleID();
342 $status = $this->getServiceContainer()
343 ->getMovePageFactory()
344 ->newMovePage( $old, $new )
345 ->move( $this->getTestUser()->getUser(), 'move reason', $createRedirect );
346 $this->assertStatusOK( $status );
347 $this->assertMoved( $old, $new, $oldPageId, $createRedirect );
348
349 [
350 'nullRevision' => $nullRevision,
351 'redirectRevision' => $redirectRevision
352 ] = $status->getValue();
353 $this->assertInstanceOf( RevisionRecord::class, $nullRevision );
354 $this->assertSame( $oldPageId, $nullRevision->getPageId() );
355 if ( $createRedirect ) {
356 $this->assertInstanceOf( RevisionRecord::class, $redirectRevision );
357 $this->assertSame( $old->getArticleID( IDBAccessObject::READ_LATEST ), $redirectRevision->getPageId() );
358 } else {
359 $this->assertNull( $redirectRevision );
360 }
361 }
362 }
363
364 /**
365 * Test for the move operation being aborted via the TitleMove hook
366 * @covers \MediaWiki\Page\MovePage::move
367 */
368 public function testMoveAbortedByTitleMoveHook() {
369 $error = 'Preventing move operation with TitleMove hook.';
370 $this->setTemporaryHook( 'TitleMove',
371 static function ( $old, $new, $user, $reason, $status ) use ( $error ) {
372 $status->fatal( $error );
373 }
374 );
375
376 $oldTitle = Title::makeTitle( NS_MAIN, 'Some old title' );
377 $this->editPage(
378 $oldTitle,
379 new WikitextContent( 'foo' ),
380 'bar',
381 NS_MAIN,
382 $this->getTestSysop()->getAuthority()
383 );
384 $newTitle = Title::makeTitle( NS_MAIN, 'A brand new title' );
385 $mp = $this->newMovePageWithMocks( $oldTitle, $newTitle );
386 $user = User::newFromName( 'TitleMove tester' );
387 $status = $mp->move( $user, 'Reason', true );
388 $this->assertStatusError( $error, $status );
389 }
390
391 /**
392 * Test moving subpages from one page to another
393 * @covers \MediaWiki\Page\MovePage::moveSubpages
394 */
395 public function testMoveSubpages() {
396 $name = ucfirst( __FUNCTION__ );
397
398 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
399 $ids = [];
400 $pages = [
401 $name,
402 "Talk:$name",
403 "$name 2",
404 "Talk:$name 2",
405 ];
406 foreach ( array_merge( $pages, $subPages ) as $page ) {
407 $ids[$page] = $this->createPage( $page );
408 }
409
410 $oldTitle = Title::newFromText( "Talk:$name" );
411 $newTitle = Title::newFromText( "Talk:$name 2" );
412 $status = $this->getServiceContainer()
413 ->getMovePageFactory()
414 ->newMovePage( $oldTitle, $newTitle )
415 ->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
416
417 $this->assertStatusGood( $status,
418 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
419 foreach ( $subPages as $page ) {
420 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
421 }
422 }
423
424 /**
425 * Test moving subpages from one page to another
426 * @covers \MediaWiki\Page\MovePage::moveSubpagesIfAllowed
427 */
428 public function testMoveSubpagesIfAllowed() {
429 $name = ucfirst( __FUNCTION__ );
430
431 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
432 $ids = [];
433 $pages = [
434 $name,
435 "Talk:$name",
436 "$name 2",
437 "Talk:$name 2",
438 ];
439 foreach ( array_merge( $pages, $subPages ) as $page ) {
440 $ids[$page] = $this->createPage( $page );
441 }
442
443 $oldTitle = Title::newFromText( "Talk:$name" );
444 $newTitle = Title::newFromText( "Talk:$name 2" );
445 $status = $this->getServiceContainer()
446 ->getMovePageFactory()
447 ->newMovePage( $oldTitle, $newTitle )
448 ->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
449
450 $this->assertStatusGood( $status,
451 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
452 foreach ( $subPages as $page ) {
453 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
454 }
455 }
456
457 /**
458 * Shortcut function to create a page and return its id.
459 *
460 * @param string $name Page to create
461 * @return int ID of created page
462 */
463 protected function createPage( $name ) {
464 return $this->editPage( $name, 'Content' )->getNewRevision()->getPageId();
465 }
466
467 /**
468 * @param string $from Prefixed name of source
469 * @param string|Title $to Prefixed name of destination
470 * @param string|Title $id Page id of the page to move
471 * @param bool $createRedirect
472 */
473 protected function assertMoved( $from, $to, $id, bool $createRedirect = true ) {
474 Title::clearCaches();
475 $fromTitle = $from instanceof Title ? $from : Title::newFromText( $from );
476 $toTitle = $to instanceof Title ? $to : Title::newFromText( $to );
477
478 $this->assertTrue( $toTitle->exists(),
479 "Destination {$toTitle->getPrefixedText()} does not exist" );
480
481 if ( !$createRedirect ) {
482 $this->assertFalse( $fromTitle->exists(),
483 "Source {$fromTitle->getPrefixedText()} exists" );
484 } else {
485 $this->assertTrue( $fromTitle->exists(),
486 "Source {$fromTitle->getPrefixedText()} does not exist" );
487 $this->assertTrue( $fromTitle->isRedirect(),
488 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
489
490 $target = $this->getServiceContainer()
491 ->getRevisionLookup()
492 ->getRevisionByTitle( $fromTitle )
493 ->getContent( SlotRecord::MAIN )
494 ->getRedirectTarget();
495 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
496 }
497
498 $this->assertSame( $id, $toTitle->getArticleID() );
499 }
500
501 /**
502 * Test redirect handling
503 *
504 * @covers \MediaWiki\Page\MovePage::isValidMove
505 */
506 public function testRedirects() {
507 $this->editPage( 'ExistentRedirect', '#REDIRECT [[Existent]]' );
508 $mp = $this->newMovePageWithMocks(
509 Title::makeTitle( NS_MAIN, 'Existent' ),
510 Title::makeTitle( NS_MAIN, 'ExistentRedirect' )
511 );
512 $this->assertStatusGood(
513 $mp->isValidMove(),
514 'Can move over normal redirect'
515 );
516
517 $this->editPage( 'ExistentRedirect3', '#REDIRECT [[Existent]]' );
518 $mp = $this->newMovePageWithMocks(
519 Title::makeTitle( NS_MAIN, 'Existent2' ),
520 Title::makeTitle( NS_MAIN, 'ExistentRedirect3' )
521 );
522 $this->assertStatusError(
523 'redirectexists',
524 $mp->isValidMove(),
525 'Cannot move over redirect with a different target'
526 );
527
528 $this->editPage( 'ExistentRedirect3', '#REDIRECT [[Existent2]]' );
529 $mp = $this->newMovePageWithMocks(
530 Title::makeTitle( NS_MAIN, 'Existent' ),
531 Title::makeTitle( NS_MAIN, 'ExistentRedirect3' )
532 );
533 $this->assertStatusError(
534 'articleexists',
535 $mp->isValidMove(),
536 'Multi-revision redirects count as articles'
537 );
538 }
539
540 /**
541 * Assert that links tables are updated after cross namespace page move (T299275).
542 */
543 public function testCrossNamespaceLinksUpdate() {
544 $title = Title::makeTitle( NS_TEMPLATE, 'Test' );
545 $this->getExistingTestPage( $title );
546
547 $wikitext = "[[Test]], [[Image:Existent.jpg]], {{Test}}";
548
549 $old = Title::makeTitle( NS_USER, __METHOD__ );
550 $this->editPage( $old, $wikitext );
551 $pageId = $old->getId();
552
553 // do a cross-namespace move
554 $new = Title::makeTitle( NS_PROJECT, __METHOD__ );
555 $obj = $this->newMovePageWithMocks( $old, $new, [ 'db' => $this->getDb() ] );
556 $status = $obj->move( $this->getTestUser()->getUser() );
557
558 // sanity checks
559 $this->assertStatusOK( $status );
560 $this->assertSame( $pageId, $new->getId() );
561 $this->assertNotSame( $pageId, $old->getId() );
562
563 // ensure links tables where updated
564 $this->newSelectQueryBuilder()
565 ->select( [ 'lt_namespace', 'lt_title', 'pl_from_namespace' ] )
566 ->from( 'pagelinks' )
567 ->join( 'linktarget', null, 'pl_target_id=lt_id' )
568 ->where( [ 'pl_from' => $pageId ] )
569 ->assertResultSet( [
570 [ NS_MAIN, 'Test', NS_PROJECT ]
571 ] );
572 $targetId = MediaWikiServices::getInstance()->getLinkTargetLookup()->getLinkTargetId( $title );
573 $this->newSelectQueryBuilder()
574 ->select( [ 'tl_target_id', 'tl_from_namespace' ] )
575 ->from( 'templatelinks' )
576 ->where( [ 'tl_from' => $pageId ] )
577 ->assertResultSet( [
578 [ $targetId, NS_PROJECT ]
579 ] );
580 $this->newSelectQueryBuilder()
581 ->select( [ 'il_to', 'il_from_namespace' ] )
582 ->from( 'imagelinks' )
583 ->where( [ 'il_from' => $pageId ] )
584 ->assertResultSet( [
585 [ 'Existent.jpg', NS_PROJECT ]
586 ] );
587 }
588
589 }