Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Oct 23, 2016
1 parent 9f10c26 commit 6e51ede
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
16 changes: 9 additions & 7 deletions phalcon/validation/validator/file.zep
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ class File extends Validator
*/
public function validate(<Validation> 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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down
6 changes: 6 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
241 changes: 241 additions & 0 deletions tests/unit/Validation/Validator/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php

namespace Phalcon\Test\Unit\Validation\Validator;

use AspectMock\Test;
use Phalcon\Http\Request;
use Phalcon\Test\Module\UnitTest;
use Phalcon\Validation;
use Phalcon\Validation\Validator\File;

/**
* \Phalcon\Test\Unit\Validation\Validator\FileTest
* Tests the \Phalcon\Validation\Validator\File component
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Andres Gutierrez <[email protected]>
* @author Nikolaos Dimopoulos <[email protected]>
* @author Wojciech Ślawski <[email protected]>
* @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 [email protected]
* so that we can send you a copy immediately.
*/
class FileTest extends UnitTest
{
/**
* @var array
*/
protected $files;

/**
* executed before each test
*/
public function _before()
{
$_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());
$this->files = $request->getUploadedFiles();
Test::func(
'Phalcon\Test\Unit\Validation\Validator',
'getimagesize',
function ($tmpName) {
$tmpSizes = [
't0' => null,
't1' => null,
't2' => [500, 500],
't3' => [1000, 1000],
't4' => null,
];

return $tmpSizes[$tmpName];
}
);
Test::func(
'Phalcon\Test\Unit\Validation\Validator',
'finfo_open',
function ($mimeType) {
return null;
}
);
Test::func(
'Phalcon\Test\Unit\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\Test\Unit\Validation\Validator',
'finfo_close',
function ($tmp, $tmpName) {
}
);
}

/**
* executed after each test
*/
public function _after()
{
Test::clean();
}

/**
* Tests file validator with single field
*
* @author Wojciech Ślawski <[email protected]>
* @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 <[email protected]>
* @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);
}
);
}
}

0 comments on commit 6e51ede

Please sign in to comment.