Browse Source

Throw expection for invalid attachement.

ADmad 6 years ago
parent
commit
80d86fda0d
2 changed files with 20 additions and 2 deletions
  1. 8 2
      src/Mailer/Message.php
  2. 12 0
      tests/TestCase/Mailer/MessageTest.php

+ 8 - 2
src/Mailer/Message.php

@@ -1164,7 +1164,7 @@ class Message implements JsonSerializable, Serializable
                     /** @var string $name */
                     $name = $fileInfo['file']->getClientFilename();
                 }
-            } else {
+            } elseif (is_string($fileInfo['file'])) {
                 $fileName = $fileInfo['file'];
                 $fileInfo['file'] = realpath($fileInfo['file']);
                 if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
@@ -1173,8 +1173,14 @@ class Message implements JsonSerializable, Serializable
                 if (is_int($name)) {
                     $name = basename($fileInfo['file']);
                 }
+            } else {
+                throw new InvalidArgumentException(sprintf(
+                    'File must be a filepath or UploadedFileInterface instance. Found `%s` instead.',
+                    gettype($fileInfo['file'])
+                ));
             }
-            if (!isset($fileInfo['mimetype'])
+            if (
+                !isset($fileInfo['mimetype'])
                 && isset($fileInfo['file'])
                 && is_string($fileInfo['file'])
                 && function_exists('mime_content_type')

+ 12 - 0
tests/TestCase/Mailer/MessageTest.php

@@ -976,6 +976,18 @@ HTML;
         $this->assertSame($expected, $this->message->getAttachments());
     }
 
+    public function testSetAttachmentInvalidFile()
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionMessage(
+            'File must be a filepath or UploadedFileInterface instance. Found `boolean` instead.'
+        );
+
+        $this->message->setAttachments(['cake.icon.gif' => [
+            'file' => true,
+        ]]);
+    }
+
     /**
      * testReset method
      *