From 239be0f2a480bc3d4b9293953435c978958ce4b3 Mon Sep 17 00:00:00 2001 From: webimpress Date: Mon, 20 Jun 2016 18:50:41 +0100 Subject: [PATCH 1/3] added missing `messageTemplates` in `ExcludeMimeType` validator --- src/File/ExcludeMimeType.php | 9 +++++++++ test/File/ExcludeMimeTypeTest.php | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/File/ExcludeMimeType.php b/src/File/ExcludeMimeType.php index f0bd4f8cc..ac8352fac 100644 --- a/src/File/ExcludeMimeType.php +++ b/src/File/ExcludeMimeType.php @@ -21,6 +21,15 @@ class ExcludeMimeType extends MimeType const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected'; const NOT_READABLE = 'fileExcludeMimeTypeNotReadable'; + /** + * @var array Error message templates + */ + protected $messageTemplates = [ + self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'", + self::NOT_DETECTED => "The mimetype could not be detected from the file", + self::NOT_READABLE => "File is not readable or does not exist", + ]; + /** * Returns true if the mimetype of the file does not matche the given ones. Also parts * of mimetypes can be checked. If you give for example "image" all image diff --git a/test/File/ExcludeMimeTypeTest.php b/test/File/ExcludeMimeTypeTest.php index 737f9e126..9e1f23325 100644 --- a/test/File/ExcludeMimeTypeTest.php +++ b/test/File/ExcludeMimeTypeTest.php @@ -51,6 +51,10 @@ public function testBasic($options, $isValidParam, $expected) $validator = new ExcludeMimeType($options); $validator->enableHeaderCheck(); $this->assertEquals($expected, $validator->isValid($isValidParam)); + if (!$expected) { + $this->assertArrayHasKey($validator::FALSE_TYPE, $validator->getMessages()); + $this->assertNotEmpty($validator->getMessages()[$validator::FALSE_TYPE]); + } } /** @@ -137,6 +141,12 @@ public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage() $this->assertFalse($validator->isValid('')); $this->assertArrayHasKey(ExcludeMimeType::NOT_READABLE, $validator->getMessages()); + $this->assertNotEmpty($validator->getMessages()[ExcludeMimeType::NOT_READABLE]); + } + + public function testEmptyArrayFileShouldReturnFalseAdnDisplayNotFoundMessage() + { + $validator = new ExcludeMimeType(); $filesArray = [ 'name' => '', @@ -148,6 +158,7 @@ public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage() $this->assertFalse($validator->isValid($filesArray)); $this->assertArrayHasKey(ExcludeMimeType::NOT_READABLE, $validator->getMessages()); + $this->assertNotEmpty($validator->getMessages()[ExcludeMimeType::NOT_READABLE]); } public function testIsValidRaisesExceptionWithArrayNotInFilesFormat() From 01e455c364a5765231916d609810a77ee6dfb5cf Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 23 Jun 2016 10:30:58 +0100 Subject: [PATCH 2/3] ExcludeMimeType tests - error messages in data provider --- test/File/ExcludeMimeTypeTest.php | 33 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/test/File/ExcludeMimeTypeTest.php b/test/File/ExcludeMimeTypeTest.php index 9e1f23325..10ef300d5 100644 --- a/test/File/ExcludeMimeTypeTest.php +++ b/test/File/ExcludeMimeTypeTest.php @@ -25,17 +25,21 @@ public function basicBehaviorDataProvider() { $testFile = __DIR__ . '/_files/picture.jpg'; $fileUpload = [ - 'tmp_name' => $testFile, 'name' => basename($testFile), - 'size' => 200, 'error' => 0, 'type' => 'image/jpeg' + 'tmp_name' => $testFile, + 'name' => basename($testFile), + 'size' => 200, + 'error' => 0, + 'type' => 'image/jpeg', ]; + return [ - // Options, isValid Param, Expected value - ['image/gif', $fileUpload, true], - ['image', $fileUpload, false], - ['test/notype', $fileUpload, true], - ['image/gif, image/jpeg', $fileUpload, false], + // Options, isValid Param, Expected value, messages + ['image/gif', $fileUpload, true], + ['image', $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], + ['test/notype', $fileUpload, true], + ['image/gif, image/jpeg', $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], [['image/vasa', 'image/gif'], $fileUpload, true], - [['image/gif', 'jpeg'], $fileUpload, false], + [['image/gif', 'jpeg'], $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], [['image/gif', 'gif'], $fileUpload, true], ]; } @@ -44,17 +48,18 @@ public function basicBehaviorDataProvider() * Ensures that the validator follows expected behavior * * @dataProvider basicBehaviorDataProvider - * @return void + * + * @param string|array $options + * @param array $isValidParam + * @param bool $expected + * @param array $messages */ - public function testBasic($options, $isValidParam, $expected) + public function testBasic($options, array $isValidParam, $expected, array $messages = []) { $validator = new ExcludeMimeType($options); $validator->enableHeaderCheck(); $this->assertEquals($expected, $validator->isValid($isValidParam)); - if (!$expected) { - $this->assertArrayHasKey($validator::FALSE_TYPE, $validator->getMessages()); - $this->assertNotEmpty($validator->getMessages()[$validator::FALSE_TYPE]); - } + $this->assertEquals($messages, $validator->getMessages()); } /** From 151955acc63885178b8df186cd590eaa27765748 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 23 Jun 2016 10:45:55 +0100 Subject: [PATCH 3/3] Incorporated feedback from @Maks3w --- test/File/ExcludeMimeTypeTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/File/ExcludeMimeTypeTest.php b/test/File/ExcludeMimeTypeTest.php index 10ef300d5..169f97832 100644 --- a/test/File/ExcludeMimeTypeTest.php +++ b/test/File/ExcludeMimeTypeTest.php @@ -32,15 +32,17 @@ public function basicBehaviorDataProvider() 'type' => 'image/jpeg', ]; + $falseTypeMessage = [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]; + return [ // Options, isValid Param, Expected value, messages - ['image/gif', $fileUpload, true], - ['image', $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], - ['test/notype', $fileUpload, true], - ['image/gif, image/jpeg', $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], - [['image/vasa', 'image/gif'], $fileUpload, true], - [['image/gif', 'jpeg'], $fileUpload, false, [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]], - [['image/gif', 'gif'], $fileUpload, true], + ['image/gif', $fileUpload, true, []], + ['image', $fileUpload, false, $falseTypeMessage], + ['test/notype', $fileUpload, true, []], + ['image/gif, image/jpeg', $fileUpload, false, $falseTypeMessage], + [['image/vasa', 'image/gif'], $fileUpload, true, []], + [['image/gif', 'jpeg'], $fileUpload, false, $falseTypeMessage], + [['image/gif', 'gif'], $fileUpload, true, []], ]; } @@ -54,7 +56,7 @@ public function basicBehaviorDataProvider() * @param bool $expected * @param array $messages */ - public function testBasic($options, array $isValidParam, $expected, array $messages = []) + public function testBasic($options, array $isValidParam, $expected, array $messages) { $validator = new ExcludeMimeType($options); $validator->enableHeaderCheck();