Browse Source

Merge pull request #12405 from ndm2/issue/feature/psr7-uploaded-file-validation

Fix/improve PSR-7 Uploaded file validation support
Mark Story 7 years ago
parent
commit
99b75f5b1e
2 changed files with 37 additions and 8 deletions
  1. 7 8
      src/Validation/Validation.php
  2. 30 0
      tests/TestCase/Validation/ValidationTest.php

+ 7 - 8
src/Validation/Validation.php

@@ -775,12 +775,15 @@ class Validation
     /**
      * Checks that value has a valid file extension.
      *
-     * @param string|array $check Value to check
+     * @param string|array|\Psr\Http\Message\UploadedFileInterface $check Value to check
      * @param array $extensions file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'
      * @return bool Success
      */
     public static function extension($check, $extensions = ['gif', 'jpeg', 'png', 'jpg'])
     {
+        if ($check instanceof UploadedFileInterface) {
+            return static::extension($check->getClientFilename(), $extensions);
+        }
         if (is_array($check)) {
             $check = isset($check['name']) ? $check['name'] : array_shift($check);
 
@@ -1258,7 +1261,7 @@ class Validation
      * - `optional` - Whether or not this file is optional. Defaults to false.
      *   If true a missing file will pass the validator regardless of other constraints.
      *
-     * @param array $file The uploaded file data from PHP.
+     * @param array|\Psr\Http\Message\UploadedFileInterface $file The uploaded file data from PHP.
      * @param array $options An array of options for the validation.
      * @return bool
      */
@@ -1310,7 +1313,7 @@ class Validation
     /**
      * Validates the size of an uploaded image.
      *
-     * @param array $file The uploaded file data from PHP.
+     * @param array|\Psr\Http\Message\UploadedFileInterface  $file The uploaded file data from PHP.
      * @param array $options Options to validate width and height.
      * @return bool
      */
@@ -1320,11 +1323,7 @@ class Validation
             throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`.');
         }
 
-        if ($file instanceof UploadedFileInterface) {
-            $file = $file->getStream()->getContents();
-        } elseif (is_array($file) && isset($file['tmp_name'])) {
-            $file = $file['tmp_name'];
-        }
+        $file = static::getFilename($file);
 
         list($width, $height) = getimagesize($file);
 

+ 30 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -2337,6 +2337,20 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * Test extension with a PSR7 object
+     */
+    public function testExtensionPsr7()
+    {
+        $file = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
+
+        $upload = new UploadedFile($file, 5308, UPLOAD_ERR_OK, 'extension.jpeg', 'image/jpeg');
+        $this->assertTrue(Validation::extension($upload));
+
+        $upload = new UploadedFile($file, 163, UPLOAD_ERR_OK, 'no_php_extension', 'text/plain');
+        $this->assertFalse(Validation::extension($upload));
+    }
+
+    /**
      * testMoney method
      *
      * @return void
@@ -3118,6 +3132,22 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * Test imageSize with a PSR7 object
+     *
+     * @return void
+     */
+    public function testImageSizePsr7()
+    {
+        $image = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
+        $upload = new UploadedFile($image, 5308, UPLOAD_ERR_OK, 'test.jpg', 'image/jpeg');
+
+        $this->assertTrue(Validation::imageSize($upload, [
+            'width' => [Validation::COMPARE_GREATER, 100],
+            'height' => [Validation::COMPARE_GREATER, 100],
+        ]));
+    }
+
+    /**
      * Test imageHeight
      *
      * @return void