3 use MediaWiki\Config\ServiceOptions
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Page\MovePageFactory
;
6 use MediaWiki\Permissions\PermissionManager
;
7 use Wikimedia\Rdbms\IDatabase
;
8 use Wikimedia\Rdbms\LoadBalancer
;
13 class MovePageTest
extends MediaWikiTestCase
{
15 * @param string $class
16 * @return object A mock that throws on any method call
18 private function getNoOpMock( $class ) {
19 $mock = $this->createMock( $class );
20 $mock->expects( $this->never() )->method( $this->anythingBut( '__destruct' ) );
25 * The only files that exist are 'File:Existent.jpg', 'File:Existent2.jpg', and
26 * 'File:Existent-file-no-page.jpg'. Calling unexpected methods causes a test failure.
30 private function getMockRepoGroup() : RepoGroup
{
31 $mockExistentFile = $this->createMock( LocalFile
::class );
32 $mockExistentFile->method( 'exists' )->willReturn( true );
33 $mockExistentFile->method( 'getMimeType' )->willReturn( 'image/jpeg' );
34 $mockExistentFile->expects( $this->never() )
35 ->method( $this->anythingBut( 'exists', 'load', 'getMimeType', '__destruct' ) );
37 $mockNonexistentFile = $this->createMock( LocalFile
::class );
38 $mockNonexistentFile->method( 'exists' )->willReturn( false );
39 $mockNonexistentFile->expects( $this->never() )
40 ->method( $this->anythingBut( 'exists', 'load', '__destruct' ) );
42 $mockLocalRepo = $this->createMock( LocalRepo
::class );
43 $mockLocalRepo->method( 'newFile' )->will( $this->returnCallback(
44 function ( Title
$title ) use ( $mockExistentFile, $mockNonexistentFile ) {
45 if ( in_array( $title->getPrefixedText(),
46 [ 'File:Existent.jpg', 'File:Existent2.jpg', 'File:Existent-file-no-page.jpg' ]
48 return $mockExistentFile;
50 return $mockNonexistentFile;
53 $mockLocalRepo->expects( $this->never() )
54 ->method( $this->anythingBut( 'newFile', '__destruct' ) );
56 $mockRepoGroup = $this->createMock( RepoGroup
::class );
57 $mockRepoGroup->method( 'getLocalRepo' )->willReturn( $mockLocalRepo );
58 $mockRepoGroup->expects( $this->never() )
59 ->method( $this->anythingBut( 'getLocalRepo', '__destruct' ) );
61 return $mockRepoGroup;
65 * @param LinkTarget $old
66 * @param LinkTarget $new
67 * @param array $params Valid keys are: db, options, nsInfo, wiStore, repoGroup.
68 * options is an indexed array that will overwrite our defaults, not a ServiceOptions, so it
69 * need not contain all keys.
72 private function newMovePage( $old, $new, array $params = [] ) : MovePage
{
73 $mockLB = $this->createMock( LoadBalancer
::class );
74 $mockLB->method( 'getConnection' )
75 ->willReturn( $params['db'] ??
$this->getNoOpMock( IDatabase
::class ) );
76 $mockLB->expects( $this->never() )
77 ->method( $this->anythingBut( 'getConnection', '__destruct' ) );
79 $mockNsInfo = $this->createMock( NamespaceInfo
::class );
80 $mockNsInfo->method( 'isMovable' )->will( $this->returnCallback(
85 $mockNsInfo->expects( $this->never() )
86 ->method( $this->anythingBut( 'isMovable', '__destruct' ) );
92 MovePageFactory
::$constructorOptions,
93 $params['options'] ??
[],
95 'CategoryCollation' => 'uppercase',
96 'ContentHandlerUseDB' => true,
100 $params['nsInfo'] ??
$mockNsInfo,
101 $params['wiStore'] ??
$this->getNoOpMock( WatchedItemStore
::class ),
102 $params['permMgr'] ??
$this->getNoOpMock( PermissionManager
::class ),
103 $params['repoGroup'] ??
$this->getMockRepoGroup()
107 public function setUp() {
110 // Ensure we have some pages that are guaranteed to exist or not
111 $this->getExistingTestPage( 'Existent' );
112 $this->getExistingTestPage( 'Existent2' );
113 $this->getExistingTestPage( 'File:Existent.jpg' );
114 $this->getExistingTestPage( 'File:Existent2.jpg' );
115 $this->getExistingTestPage( 'MediaWiki:Existent.js' );
116 $this->getExistingTestPage( 'Hooked in place' );
117 $this->getNonExistingTestPage( 'Nonexistent' );
118 $this->getNonExistingTestPage( 'Nonexistent2' );
119 $this->getNonExistingTestPage( 'File:Nonexistent.jpg' );
120 $this->getNonExistingTestPage( 'File:Nonexistent.png' );
121 $this->getNonExistingTestPage( 'File:Existent-file-no-page.jpg' );
122 $this->getNonExistingTestPage( 'MediaWiki:Nonexistent' );
123 $this->getNonExistingTestPage( 'No content allowed' );
125 // Set a couple of hooks for specific pages
126 $this->setTemporaryHook( 'ContentModelCanBeUsedOn',
127 function ( $modelId, Title
$title, &$ok ) {
128 if ( $title->getPrefixedText() === 'No content allowed' ) {
134 $this->setTemporaryHook( 'TitleIsMovable',
135 function ( Title
$title, &$result ) {
136 if ( strtolower( $title->getPrefixedText() ) === 'hooked in place' ) {
142 $this->tablesUsed
[] = 'page';
143 $this->tablesUsed
[] = 'revision';
144 $this->tablesUsed
[] = 'comment';
148 * @covers MovePage::__construct
150 public function testConstructorDefaults() {
151 $services = MediaWikiServices
::getInstance();
153 $obj1 = new MovePage( Title
::newFromText( 'A' ), Title
::newFromText( 'B' ) );
154 $obj2 = new MovePage(
155 Title
::newFromText( 'A' ),
156 Title
::newFromText( 'B' ),
157 new ServiceOptions( MovePageFactory
::$constructorOptions, $services->getMainConfig() ),
158 $services->getDBLoadBalancer(),
159 $services->getNamespaceInfo(),
160 $services->getWatchedItemStore(),
161 $services->getPermissionManager(),
162 $services->getRepoGroup(),
163 $services->getTitleFormatter()
166 $this->assertEquals( $obj2, $obj1 );
170 * @dataProvider provideIsValidMove
171 * @covers MovePage::isValidMove
172 * @covers MovePage::isValidMoveTarget
173 * @covers MovePage::isValidFileMove
174 * @covers MovePage::__construct
175 * @covers Title::isValidMoveOperation
177 * @param string|Title $old
178 * @param string|Title $new
179 * @param array $expectedErrors
180 * @param array $extraOptions
182 public function testIsValidMove(
183 $old, $new, array $expectedErrors, array $extraOptions = []
185 if ( is_string( $old ) ) {
186 $old = Title
::newFromText( $old );
188 if ( is_string( $new ) ) {
189 $new = Title
::newFromText( $new );
191 // Can't test MovePage with a null target, only isValidMoveOperation
193 $mp = $this->newMovePage( $old, $new, [ 'options' => $extraOptions ] );
194 $this->assertSame( $expectedErrors, $mp->isValidMove()->getErrorsArray() );
197 foreach ( $extraOptions as $key => $val ) {
198 $this->setMwGlobals( "wg$key", $val );
200 $this->overrideMwServices();
201 $this->setService( 'RepoGroup', $this->getMockRepoGroup() );
202 // This returns true instead of an array if there are no errors
203 $this->hideDeprecated( 'Title::isValidMoveOperation' );
204 $this->assertSame( $expectedErrors ?
: true, $old->isValidMoveOperation( $new, false ) );
207 public static function provideIsValidMove() {
208 global $wgMultiContentRevisionSchemaMigrationStage;
218 [ [ 'badtitletext' ] ],
220 'Move from empty name' => [
221 Title
::makeTitle( NS_MAIN
, '' ),
223 // @todo More specific error message, or make the move valid if the page actually
224 // exists somehow in the database
225 [ [ 'badarticleerror' ] ],
227 'Move to empty name' => [
229 Title
::makeTitle( NS_MAIN
, '' ),
230 [ [ 'movepage-invalid-target-title' ] ],
232 'Move to invalid name' => [
234 Title
::makeTitle( NS_MAIN
, '<' ),
235 [ [ 'movepage-invalid-target-title' ] ],
237 'Move between invalid names' => [
238 Title
::makeTitle( NS_MAIN
, '<' ),
239 Title
::makeTitle( NS_MAIN
, '>' ),
240 // @todo First error message should be more specific, or maybe we should make moving
241 // such pages valid if they actually exist somehow in the database
242 [ [ 'movepage-source-doesnt-exist' ], [ 'movepage-invalid-target-title' ] ],
244 'Move nonexistent' => [
247 [ [ 'movepage-source-doesnt-exist' ] ],
249 'Move over existing' => [
252 [ [ 'articleexists' ] ],
254 'Move from another wiki' => [
255 Title
::makeTitle( NS_MAIN
, 'Test', '', 'otherwiki' ),
257 [ [ 'immobile-source-namespace-iw' ] ],
259 'Move special page' => [
262 [ [ 'immobile-source-namespace', 'Special' ] ],
264 'Move to another wiki' => [
266 Title
::makeTitle( NS_MAIN
, 'Test', '', 'otherwiki' ),
267 [ [ 'immobile-target-namespace-iw' ] ],
269 'Move to special page' =>
270 [ 'Existent', 'Special:FooBar', [ [ 'immobile-target-namespace', 'Special' ] ] ],
271 'Move to allowed content model' => [
272 'MediaWiki:Existent.js',
273 'MediaWiki:Nonexistent',
276 'Move to prohibited content model' => [
278 'No content allowed',
279 [ [ 'content-not-allowed-here', 'wikitext', 'No content allowed', 'main' ] ],
281 'Aborted by hook' => [
284 // @todo Error is wrong
285 [ [ 'immobile-source-namespace', '' ] ],
287 'Doubly aborted by hook' => [
290 // @todo Both errors are wrong
291 [ [ 'immobile-source-namespace', '' ], [ 'immobile-target-namespace', '' ] ],
293 'Non-file to file' =>
294 [ 'Existent', 'File:Nonexistent.jpg', [ [ 'nonfile-cannot-move-to-file' ] ] ],
295 'File to non-file' => [
298 [ [ 'imagenocrossnamespace' ] ],
300 'Existing file to non-existing file' => [
302 'File:Nonexistent.jpg',
305 'Existing file to existing file' => [
307 'File:Existent2.jpg',
308 [ [ 'articleexists' ] ],
310 'Existing file to existing file with no page' => [
312 'File:Existent-file-no-page.jpg',
313 // @todo Is this correct? Moving over an existing file with no page should succeed?
316 'Existing file to name with slash' => [
318 'File:Existent/slashed.jpg',
319 [ [ 'imageinvalidfilename' ] ],
321 'Mismatched file extension' => [
323 'File:Nonexistent.png',
324 [ [ 'imagetypemismatch' ] ],
327 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
328 // ContentHandlerUseDB = false only works with the old schema
329 $ret['Move to different content model (ContentHandlerUseDB false)'] = [
330 'MediaWiki:Existent.js',
331 'MediaWiki:Nonexistent',
332 [ [ 'bad-target-model', 'JavaScript', 'wikitext' ] ],
333 [ 'ContentHandlerUseDB' => false ],
340 * @dataProvider provideMove
341 * @covers MovePage::move
343 * @param string $old Old name
344 * @param string $new New name
345 * @param array $expectedErrors
346 * @param array $extraOptions
348 public function testMove( $old, $new, array $expectedErrors, array $extraOptions = [] ) {
349 if ( is_string( $old ) ) {
350 $old = Title
::newFromText( $old );
352 if ( is_string( $new ) ) {
353 $new = Title
::newFromText( $new );
356 $params = [ 'options' => $extraOptions ];
357 if ( $expectedErrors === [] ) {
358 $this->markTestIncomplete( 'Checking actual moves has not yet been implemented' );
361 $obj = $this->newMovePage( $old, $new, $params );
362 $status = $obj->move( $this->getTestUser()->getUser() );
363 $this->assertSame( $expectedErrors, $status->getErrorsArray() );
366 public static function provideMove() {
368 foreach ( self
::provideIsValidMove() as $name => $arr ) {
369 list( $old, $new, $expectedErrors, $extraOptions ) = array_pad( $arr, 4, [] );
371 // Not supported by testMove
380 * Integration test to catch regressions like T74870. Taken and modified
381 * from SemanticMediaWiki
383 * @covers Title::moveTo
384 * @covers MovePage::move
386 public function testTitleMoveCompleteIntegrationTest() {
387 $this->hideDeprecated( 'Title::moveTo' );
389 $oldTitle = Title
::newFromText( 'Help:Some title' );
390 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
391 $newTitle = Title
::newFromText( 'Help:Some other title' );
393 WikiPage
::factory( $newTitle )->getRevision()
396 $this->assertTrue( $oldTitle->moveTo( $newTitle, false, 'test1', true ) );
397 $this->assertNotNull(
398 WikiPage
::factory( $oldTitle )->getRevision()
400 $this->assertNotNull(
401 WikiPage
::factory( $newTitle )->getRevision()
406 * Test for the move operation being aborted via the TitleMove hook
407 * @covers MovePage::move
409 public function testMoveAbortedByTitleMoveHook() {
410 $error = 'Preventing move operation with TitleMove hook.';
411 $this->setTemporaryHook( 'TitleMove',
412 function ( $old, $new, $user, $reason, $status ) use ( $error ) {
413 $status->fatal( $error );
417 $oldTitle = Title
::newFromText( 'Some old title' );
418 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
419 $newTitle = Title
::newFromText( 'A brand new title' );
420 $mp = $this->newMovePage( $oldTitle, $newTitle );
421 $user = User
::newFromName( 'TitleMove tester' );
422 $status = $mp->move( $user, 'Reason', true );
423 $this->assertTrue( $status->hasMessage( $error ) );
427 * Test moving subpages from one page to another
428 * @covers MovePage::moveSubpages
430 public function testMoveSubpages() {
431 $name = ucfirst( __FUNCTION__
);
433 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
441 foreach ( array_merge( $pages, $subPages ) as $page ) {
442 $ids[$page] = $this->createPage( $page );
445 $oldTitle = Title
::newFromText( "Talk:$name" );
446 $newTitle = Title
::newFromText( "Talk:$name 2" );
447 $mp = new MovePage( $oldTitle, $newTitle );
448 $status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
450 $this->assertTrue( $status->isGood(),
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] );
458 * Test moving subpages from one page to another
459 * @covers MovePage::moveSubpagesIfAllowed
461 public function testMoveSubpagesIfAllowed() {
462 $name = ucfirst( __FUNCTION__
);
464 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
472 foreach ( array_merge( $pages, $subPages ) as $page ) {
473 $ids[$page] = $this->createPage( $page );
476 $oldTitle = Title
::newFromText( "Talk:$name" );
477 $newTitle = Title
::newFromText( "Talk:$name 2" );
478 $mp = new MovePage( $oldTitle, $newTitle );
479 $status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
481 $this->assertTrue( $status->isGood(),
482 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
483 foreach ( $subPages as $page ) {
484 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
489 * Shortcut function to create a page and return its id.
491 * @param string $name Page to create
492 * @return int ID of created page
494 protected function createPage( $name ) {
495 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
499 * @param string $from Prefixed name of source
500 * @param string $to Prefixed name of destination
501 * @param string $id Page id of the page to move
502 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
504 protected function assertMoved( $from, $to, $id, $opts = null ) {
505 $opts = (array)$opts;
507 Title
::clearCaches();
508 $fromTitle = Title
::newFromText( $from );
509 $toTitle = Title
::newFromText( $to );
511 $this->assertTrue( $toTitle->exists(),
512 "Destination {$toTitle->getPrefixedText()} does not exist" );
514 if ( in_array( 'noredirect', $opts ) ) {
515 $this->assertFalse( $fromTitle->exists(),
516 "Source {$fromTitle->getPrefixedText()} exists" );
518 $this->assertTrue( $fromTitle->exists(),
519 "Source {$fromTitle->getPrefixedText()} does not exist" );
520 $this->assertTrue( $fromTitle->isRedirect(),
521 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
523 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
524 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
527 $this->assertSame( $id, $toTitle->getArticleID() );