(bug 31213) Exception thrown when trying to move file cross-namespace.
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 29 Sep 2011 18:35:34 +0000 (18:35 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 29 Sep 2011 18:35:34 +0000 (18:35 +0000)
* Don't try to construct a File object when the namespace is wrong
* Start on some tests

RELEASE-NOTES-1.18
includes/Title.php
tests/phpunit/includes/TitleTest.php

index b8e5489..c2d165d 100644 (file)
@@ -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.
index ebcc3bc..1c98275 100644 (file)
@@ -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 ) ) {
index 06ee3b2..51b3616 100644 (file)
@@ -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' )
+               );
+       }
+       
+       
 }