diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ba49de99e7..994631731f6 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Fixed `Phalcon\Db\Dialect\Mysql` and `Phalcon\Db\Dialect\Postresql` to correctly check schema in missing methods - Fixed `Phalcon\Cache\Backend\Apc::flush` to remove only it's own keys by prefix [#12153](https://github.com/phalcon/cphalcon/issues/12153) - Fixed `Phalcon\Acl\Adapter\Memory::isAllowed` to call closures when using wildcard [#12333](https://github.com/phalcon/cphalcon/issues/12333) +- Fixed `Phalcon\Validation\Validator\File` array to string conversion in `minResolution` and `maxResolution` [#12349](https://github.com/phalcon/cphalcon/issues/12349) # [3.0.1](https://github.com/phalcon/cphalcon/releases/tag/v3.0.1) (2016-08-24) - Fixed `Phalcon\Cache\Backend\Redis::flush` in order to flush cache correctly diff --git a/composer.json b/composer.json index cd6a084d5f8..a3320c3581c 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "codeception/codeception": "~2.2", "codeception/verify": "~0.3", "codeception/specify": "~0.4", - "phalcon/zephir": "dev-master" + "phalcon/zephir": "dev-master", + "codeception/aspect-mock": "*" }, "license": "BSD-3-Clause", "authors": [ diff --git a/phalcon/validation/validator/file.zep b/phalcon/validation/validator/file.zep index 746929a1683..660b6c9a647 100644 --- a/phalcon/validation/validator/file.zep +++ b/phalcon/validation/validator/file.zep @@ -97,7 +97,9 @@ class File extends Validator */ public function validate( validation, string! field) -> boolean { - var value, message, label, replacePairs, types, byteUnits, unit, maxSize, matches, bytes, mime, tmp, width, height, minResolution, maxResolution, minWidth, maxWidth, minHeight, maxHeight, fieldTypes, code; + var value, message, label, replacePairs, types, byteUnits, unit, maxSize, matches, bytes, mime, tmp, width, + height, minResolution, maxResolution, minWidth, maxWidth, minHeight, maxHeight, fieldTypes, code, + minResolutionArray, maxResolutionArray; let value = validation->getValue(field), label = this->getOption("label"); @@ -251,9 +253,9 @@ class File extends Validator if typeof minResolution == "array" { let minResolution = minResolution[field]; } - let minResolution = explode("x", minResolution), - minWidth = minResolution[0], - minHeight = minResolution[1]; + let minResolutionArray = explode("x", minResolution), + minWidth = minResolutionArray[0], + minHeight = minResolutionArray[1]; } else { let minWidth = 1, minHeight = 1; @@ -281,9 +283,9 @@ class File extends Validator if typeof maxResolution == "array" { let maxResolution = maxResolution[field]; } - let maxResolution = explode("x", maxResolution), - maxWidth = maxResolution[0], - maxHeight = maxResolution[1]; + let maxResolutionArray = explode("x", maxResolution), + maxWidth = maxResolutionArray[0], + maxHeight = maxResolutionArray[1]; if width > maxWidth || height > maxHeight { let message = this->getOption("messageMaxResolution"), diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 4a74b9f9ef0..baddf46f271 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -36,6 +36,12 @@ include_once PROJECT_PATH . 'vendor/autoload.php'; include_once TESTS_PATH . 'shim.php'; +$kernel = \AspectMock\Kernel::getInstance(); +$kernel->init([ + 'debug' => true, + 'includePaths' => [__DIR__.'/../src'] +]); + if (extension_loaded('xdebug')) { ini_set('xdebug.cli_color', 1); ini_set('xdebug.collect_params', 0); diff --git a/tests/unit/Validation/Validator/FileTest.php b/tests/unit/Validation/Validator/FileTest.php new file mode 100644 index 00000000000..8e0a809d6a9 --- /dev/null +++ b/tests/unit/Validation/Validator/FileTest.php @@ -0,0 +1,263 @@ + + * @author Nikolaos Dimopoulos + * @author Wojciech Ślawski + * @package Phalcon\Test\Unit\Validation\Validator + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class FileTest extends UnitTest +{ + /** + * @var array + */ + protected $files; + + /** + * executed before each test + */ + public function _before() + { + $this->markTestSkipped('Mocking up php functions doesnt work for zephir extensions.'); + $_FILES = [ + 'photo' => [ + 'name' => ['f0', 'f1', ['f2', 'f3'], [[[['f4']]]]], + 'type' => [ + 'text/plain', + 'text/csv', + ['image/png', 'image/jpeg'], + [[[['application/octet-stream']]]], + ], + 'tmp_name' => ['t0', 't1', ['t2', 't3'], [[[['t4']]]]], + 'error' => [0, 0, [0, 0], [[[[8]]]]], + 'size' => [10, 20, [30, 40], [[[[50]]]]], + ], + ]; + $request = new Request(); + $request->setDI($this->tester->getApplication()->getDI()); + $uploadedFiles = $request->getUploadedFiles(); + $files = []; + /** @var Request\File $file */ + foreach ($uploadedFiles as $file) { + $files[] = [ + 'name' => $file->getName(), + 'tmp_name' => $file->getTempName(), + 'type' => $file->getType(), + 'size' => $file->getSize(), + 'error' => $file->getError(), + ]; + } + $this->files = $files; + Test::func( + 'Phalcon\Validation\Validator', + 'getimagesize', + function ($tmpName) { + $tmpSizes = [ + 't0' => null, + 't1' => null, + 't2' => [500, 500], + 't3' => [1000, 1000], + 't4' => null, + ]; + + return $tmpSizes[$tmpName]; + } + ); + Test::func( + 'Phalcon\Validation\Validator', + 'finfo_open', + function ($mimeType) { + return null; + } + ); + Test::func( + 'Phalcon\Validation\Validator', + 'finfo_file', + function ($tmp, $tmpName) { + $tmpTypes = [ + 't0' => 'text/plain', + 't1' => 'text/csv', + 't2' => 'image/png', + 't3' => 'image/jpeg', + 't4' => 'application/octet-stream', + ]; + + return $tmpTypes[$tmpName]; + } + ); + Test::func( + 'Phalcon\Validation\Validator', + 'is_uploaded_file', + function ($tmpName) { + return true; + } + ); + Test::func( + 'Phalcon\Validation\Validator', + 'finfo_close', + function ($tmp, $tmpName) { + } + ); + $_SERVER["REQUEST_METHOD"] = "POST"; + } + + /** + * executed after each test + */ + public function _after() + { + Test::clean(); + unset($_SERVER["REQUEST_METHOD"]); + } + + /** + * Tests file validator with single field + * + * @author Wojciech Ślawski + * @since 2016-10-23 + */ + public function testSingleField() + { + $this->specify( + 'The file validator does not validates correctly with single field', + function () { + $validation = new Validation(); + $validation->add( + 'file', + new File( + [ + 'maxSize' => '500K', + 'allowedTypes' => ['image/jpeg', 'image/png'], + 'maxResolution' => '800x800', + 'minResolution' => '1x1', + 'message' => 'Image should have max 800x800 resolution', + ] + ) + ); + $messages = $validation->validate(['file' => $this->files[2]]); + expect($messages->count())->equals(0); + $messages = $validation->validate(['file' => $this->files[3]]); + expect($messages->count())->equals(1); + $expectedMessages = Validation\Message\Group::__set_state( + [ + '_messages' => [ + 0 => Validation\Message::__set_state( + [ + '_type' => 'File', + '_message' => 'Image should have max 800x800 resolution', + '_field' => 'file', + '_code' => '0', + ] + ), + ], + ] + ); + + expect($expectedMessages)->equals($messages); + } + ); + } + + /** + * Tests file validator with multiple field + * + * @author Wojciech Ślawski + * @since 2016-10-23 + */ + public function testMultipleField() + { + $this->specify( + 'The file validator does not validates correctly with multiple field', + function () { + $validation = new Validation(); + $validation->add( + ['file', 'anotherFile'], + new File( + [ + 'maxSize' => [ + 'file' => '500K', + 'anotherFile' => '600K', + ], + 'allowedTypes' => ['image/jpeg', 'image/png'], + 'maxResolution' => [ + 'file' => '800x800', + 'anotherFile' => '900x900', + ], + 'minResolution' => '1x1', + 'message' => [ + 'file' => 'Image should have max 800x800 resolution', + 'anotherFile' => 'Image should have max 900x900 resolution', + ], + ] + ) + ); + $messages = $validation->validate(['file' => $this->files[2], 'anotherFile' => $this->files[2]]); + expect($messages->count())->equals(0); + $messages = $validation->validate(['file' => $this->files[2], 'anotherFile' => $this->files[3]]); + expect($messages->count())->equals(1); + $expectedMessages = Validation\Message\Group::__set_state( + [ + '_messages' => [ + 0 => Validation\Message::__set_state( + [ + '_type' => 'File', + '_message' => 'Image should have max 900x900 resolution', + '_field' => 'anotherFile', + '_code' => '0', + ] + ), + ], + ] + ); + + expect($expectedMessages)->equals($messages); + $messages = $validation->validate(['file' => $this->files[3], 'anotherFile' => $this->files[3]]); + expect($messages->count())->equals(2); + $expectedMessages = Validation\Message\Group::__set_state( + [ + '_messages' => [ + 0 => Validation\Message::__set_state( + [ + '_type' => 'File', + '_message' => 'Image should have max 800x800 resolution', + '_field' => 'file', + '_code' => '0', + ] + ), + 1 => Validation\Message::__set_state( + [ + '_type' => 'File', + '_message' => 'Image should have max 900x900 resolution', + '_field' => 'anotherFile', + '_code' => '0', + ] + ), + ], + ] + ); + + expect($expectedMessages)->equals($messages); + } + ); + } +}