jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/383324 )

Change subject: Rework Upload*Exception classes to implement ILocalizedException
......................................................................


Rework Upload*Exception classes to implement ILocalizedException

Bug: T154781
Change-Id: Ia64295d7ea502014586b8b8e3e3f34272b72443c
---
M includes/upload/UploadStash.php
M tests/phpunit/includes/upload/UploadStashTest.php
2 files changed, 49 insertions(+), 12 deletions(-)

Approvals:
  Matthias Mullie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php
index da896f9..c3d2e4d 100644
--- a/includes/upload/UploadStash.php
+++ b/includes/upload/UploadStash.php
@@ -265,8 +265,7 @@
                        // At this point, $error should contain the single 
"most important"
                        // error, plus any parameters.
                        $errorMsg = array_shift( $error );
-                       throw new UploadStashFileException( "Error storing file 
in '$path': "
-                               . wfMessage( $errorMsg, $error )->text() );
+                       throw new UploadStashFileException( wfMessage( 
$errorMsg, $error ) );
                }
                $stashPath = $storeStatus->value;
 
@@ -739,7 +738,27 @@
        }
 }
 
-class UploadStashException extends MWException {
+class UploadStashException extends MWException implements ILocalizedException {
+       /** @var string|array|MessageSpecifier */
+       protected $messageSpec;
+
+       /**
+        * @param string|array|MessageSpecifier $messageSpec See 
Message::newFromSpecifier
+        * @param int $code Exception code
+        * @param Exception|Throwable $previous The previous exception used for 
the exception chaining.
+        */
+       public function __construct( $messageSpec, $code = 0, $previous = null 
) {
+               $this->messageSpec = $messageSpec;
+
+               $msg = $this->getMessageObject()->text();
+               $msg = preg_replace( '!</?(var|kbd|samp|code)>!', '"', $msg );
+               $msg = Sanitizer::stripAllTags( $msg );
+               parent::__construct( $msg, $code, $previous );
+       }
+
+       public function getMessageObject() {
+               return Message::newFromSpecifier( $this->messageSpec );
+       }
 }
 
 class UploadStashFileNotFoundException extends UploadStashException {
diff --git a/tests/phpunit/includes/upload/UploadStashTest.php 
b/tests/phpunit/includes/upload/UploadStashTest.php
index 7c40a2d..d754ba5 100644
--- a/tests/phpunit/includes/upload/UploadStashTest.php
+++ b/tests/phpunit/includes/upload/UploadStashTest.php
@@ -14,14 +14,13 @@
        /**
         * @var string
         */
-       private $bug29408File;
+       private $tmpFile;
 
        protected function setUp() {
                parent::setUp();
 
-               // Setup a file for T31408
-               $this->bug29408File = wfTempDir() . '/bug29408';
-               file_put_contents( $this->bug29408File, "\x00" );
+               $this->tmpFile = wfTempDir() . '/' . uniqid();
+               file_put_contents( $this->tmpFile, "\x00" );
 
                self::$users = [
                        'sysop' => new TestUser(
@@ -40,12 +39,12 @@
        }
 
        protected function tearDown() {
-               if ( file_exists( $this->bug29408File . "." ) ) {
-                       unlink( $this->bug29408File . "." );
+               if ( file_exists( $this->tmpFile . "." ) ) {
+                       unlink( $this->tmpFile . "." );
                }
 
-               if ( file_exists( $this->bug29408File ) ) {
-                       unlink( $this->bug29408File );
+               if ( file_exists( $this->tmpFile ) ) {
+                       unlink( $this->tmpFile );
                }
 
                parent::tearDown();
@@ -61,7 +60,7 @@
                $stash = new UploadStash( $repo );
 
                // Throws exception caught by PHPUnit on failure
-               $file = $stash->stashFile( $this->bug29408File );
+               $file = $stash->stashFile( $this->tmpFile );
                // We'll never reach this point if we hit T31408
                $this->assertTrue( true, 'Unrecognized file without extension' 
);
 
@@ -104,4 +103,23 @@
                $this->assertTrue( UploadFromStash::isValidRequest( $request ) 
);
        }
 
+       public function testExceptionWhenStoreTempFails() {
+               $mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
+               $mockRepo = $this->getMockBuilder( FileRepo::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $mockRepo->expects( $this->once() )
+                       ->method( 'storeTemp' )
+                       ->willReturn( $mockRepoStoreStatusResult );
+
+               $stash = new UploadStash( $mockRepo );
+               try {
+                       $stash->stashFile( $this->tmpFile );
+                       $this->fail( 'Expected UploadStashFileException not 
thrown' );
+               } catch ( UploadStashFileException $e ) {
+                       $this->assertInstanceOf( 'ILocalizedException', $e );
+               } catch ( Exception $e ) {
+                       $this->fail( 'Unexpected exception class ' . get_class( 
$e ) );
+               }
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/383324
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia64295d7ea502014586b8b8e3e3f34272b72443c
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Cparle <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Cparle <[email protected]>
Gerrit-Reviewer: Matthias Mullie <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to