From ae6ee23e335888b07cf3b198f361b8bd1c70a37f Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Thu, 29 Sep 2011 18:35:34 +0000 Subject: [PATCH] (bug 31213) Exception thrown when trying to move file cross-namespace. * Don't try to construct a File object when the namespace is wrong * Start on some tests --- RELEASE-NOTES-1.18 | 1 + includes/Title.php | 15 ++++++++--- tests/phpunit/includes/TitleTest.php | 39 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES-1.18 b/RELEASE-NOTES-1.18 index b8e54897e1..c2d165df40 100644 --- a/RELEASE-NOTES-1.18 +++ b/RELEASE-NOTES-1.18 @@ -458,6 +458,7 @@ production. * (bug 30192) Thumbnails of archived files are now deleted * (bug 30843) mediawiki.Title should not convert extensions (anything after the last full stop) to lower case) +* (bug 31213) Exception thrown when trying to move file cross-namespace === API changes in 1.18 === * BREAKING CHANGE: action=watch now requires POST and token. diff --git a/includes/Title.php b/includes/Title.php index ebcc3bc1a9..1c98275416 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -3127,10 +3127,8 @@ class Title { $errors = array(); - if ( $nt->getNamespace() != NS_FILE ) { - $errors[] = array( 'imagenocrossnamespace' ); - } - + // wfFindFile( $nt ) / wfLocalFile( $nt ) is not allowed until below + $file = wfLocalFile( $this ); if ( $file->exists() ) { if ( $nt->getText() != wfStripIllegalFilenameChars( $nt->getText() ) ) { @@ -3140,6 +3138,15 @@ class Title { $errors[] = array( 'imagetypemismatch' ); } } + + if ( $nt->getNamespace() != NS_FILE ) { + $errors[] = array( 'imagenocrossnamespace' ); + // From here we want to do checks on a file object, so if we can't + // create one, we must return. + return $errors; + } + + // wfFindFile( $nt ) / wfLocalFile( $nt ) is allowed below here $destFile = wfLocalFile( $nt ); if ( !$wgUser->isAllowed( 'reupload-shared' ) && !$destFile->exists() && wfFindFile( $nt ) ) { diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index 06ee3b2ab5..51b361602c 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -37,4 +37,43 @@ class TitleTest extends MediaWikiTestCase { array( 'Special:Version/param', 'param' ), ); } + + /** + * Auth-less test of Title::isValidMoveOperation + * + * @param string $source + * @param string $target + * @param array|string|true $requiredErrors + * @dataProvider dataTestIsValidMoveOperation + */ + function testIsValidMoveOperation( $source, $target, $expected ) { + $title = Title::newFromText( $source ); + $nt = Title::newFromText( $target ); + $errors = $title->isValidMoveOperation( $nt, false ); + if ( $expected === true ) { + $this->assertTrue( $errors ); + } else { + $errors = $this->flattenErrorsArray( $errors ); + foreach ( (array)$expected as $error ) { + $this->assertContains( $error, $errors ); + } + } + } + + function flattenErrorsArray( $errors ) { + $result = array(); + foreach ( $errors as $error ) { + $result[] = $error[0]; + } + return $result; + } + + function dataTestIsValidMoveOperation() { + return array( + array( 'Test', 'Test', 'selfmove' ), + array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ) + ); + } + + } -- 2.20.1