Skip to content

Commit

Permalink
feature/Validators for image sizes were added, unit-tests refactoring,
Browse files Browse the repository at this point in the history
overall refactoring, documentation was updated
  • Loading branch information
alexdodonov committed May 14, 2020
1 parent e1ef6df commit 0e97a6e
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 22 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -47,4 +47,16 @@ $sizeValidator = new Validators\File\MymeType(['image/png', 'image/jpeg', 'image

And then call isUploadedFileValid like in the example above.

### Image validators

You can use validators for the image size:

```php
new Mezon\Security\Validators\File\ImageMaximumWidthHeight(<maximum width>, <maximum height>);

// and

new Mezon\Security\Validators\File\ImageMinimumWidthHeight(<minimum width>, <minimum height>);
```

# I'll be very glad if you'll press "STAR" button )
117 changes: 104 additions & 13 deletions Tests/SecurityRulesUnitTest.php
Expand Up @@ -11,6 +11,20 @@ class SecurityRulesUnitTest extends \PHPUnit\Framework\TestCase
*/
public const PATH_TO_FILE_STORAGE = '/data/files/';

/**
* Field name of the testing file
*
* @var string
*/
public const TEST_FILE_FIELD_NAME = 'test-file';

/**
* Testing image
*
* @var string
*/
public const TEST_PNG_IMAGE_PATH = __DIR__.'/res/test.png';

/**
* Method returns path to storage
*
Expand Down Expand Up @@ -77,6 +91,24 @@ protected function constructUploadedFile(int $size, string $file, string $name,
return $return;
}

/**
* Method constructs $_FILES array with one element
*
* @param int $size
* size of the file
* @param string $file
* file path
* @param string $name
* file name
* @return array element of the $_FILES array
*/
protected function constructTestFiles(int $size, string $file, string $name, string $tmpName = ''): array
{
return [
SecurityRulesUnitTest::TEST_FILE_FIELD_NAME => $this->constructUploadedFile($size, $file, $name, $tmpName)
];
}

/**
* Data provider for the testGetFileValue test
*
Expand All @@ -87,21 +119,15 @@ public function getFileValueProvider(): array
return [
[
true,
[
'test-file' => $this->constructUploadedFile(2000, '1', '1')
]
$this->constructTestFiles(2000, '1', '1')
],
[
false,
[
'test-file' => $this->constructUploadedFile(1, '1', '1')
]
$this->constructTestFiles(1, '1', '1')
],
[
true,
[
'test-file' => $this->constructUploadedFile(1, '', '1', '1')
]
$this->constructTestFiles(1, '', '1', '1')
]
];
}
Expand Down Expand Up @@ -141,7 +167,7 @@ public function testGetFileValue(bool $storeFile, array $files): void
->getMock();

if ($storeFile) {
if ($this->tmpNameSet($files['test-file'])) {
if ($this->tmpNameSet($files[SecurityRulesUnitTest::TEST_FILE_FIELD_NAME])) {
$securityRules->expects($this->once())
->method('moveUploadedFile');
} else {
Expand All @@ -151,7 +177,7 @@ public function testGetFileValue(bool $storeFile, array $files): void
}

// test body
$result = $securityRules->getFileValue('test-file', $storeFile);
$result = $securityRules->getFileValue(SecurityRulesUnitTest::TEST_FILE_FIELD_NAME, $storeFile);

// assertions
if ($storeFile) {
Expand All @@ -160,7 +186,7 @@ public function testGetFileValue(bool $storeFile, array $files): void
$this->assertEquals(1, $result['size']);

$this->assertEquals('1', $result['name']);
if ($this->tmpNameSet($files['test-file'])) {
if ($this->tmpNameSet($files[SecurityRulesUnitTest::TEST_FILE_FIELD_NAME])) {
$this->assertEquals('1', $result['tmp_name']);
} else {
$this->assertEquals('1', $result['file']);
Expand Down Expand Up @@ -311,6 +337,71 @@ public function isUploadFileValidProvider(): array
])
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\MimeType([
'image/png'
])
],
true
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 500)
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 600)
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 500)
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(600, 600)
],
true
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(500, 500)
],
true
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(600, 500)
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(500, 600)
],
false
],
[
$this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
[
new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(600, 600)
],
false
]
];
}
Expand Down Expand Up @@ -352,7 +443,7 @@ public function validatingUnexistingFileProvider(): array
],
[
new \Mezon\Security\Validators\File\MimeType([
'image/png'
'image/jpeg'
])
]
];
Expand Down
Binary file added Tests/res/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Validators/AbstractValidator.php
@@ -0,0 +1,35 @@
<?php
namespace Mezon\Security\Validators;

/**
* Class AbstractValidator
*
* @package Mezon
* @subpackage Security
* @author Dodonov A.A.
* @version v.1.0 (2020/05/13)
* @copyright Copyright (c) 2020, aeon.org
*/

/**
* Abstract class for validators
*
* @author gdever
*/
abstract class AbstractValidator implements ValidatorInterface
{

/**
* Method validates that $_FILES[$field] exists.
* If it does not then exception will be thrown
*
* @param string $field
* field name
*/
public function validateFilesFieldExists(string $field): void
{
if (isset($_FILES[$field]) === false) {
throw (new \Exception('The index "' . $field . '" was not found in the $_FILES array', - 1));
}
}
}
76 changes: 76 additions & 0 deletions Validators/File/ImageMaximumWidthHeight.php
@@ -0,0 +1,76 @@
<?php
namespace Mezon\Security\Validators\File;

/**
* Class ImageMaximumWidthHeight
*
* @package Mezon
* @subpackage Security
* @author Dodonov A.A.
* @version v.1.0 (2020/05/13)
* @copyright Copyright (c) 2020, aeon.org
*/
class ImageMaximumWidthHeight extends \Mezon\Security\Validators\AbstractValidator
{

/**
* Index in the $_FILES array
*
* @var string
*/
private $file = '';

/**
* Maximum width of the image
*
* @var integer
*/
private $maximumWidth = 0;

/**
* Maximum height of the image
*
* @var integer
*/
private $maximumHeight = 0;

/**
* Constructor
*
* @param int $width
* width constraint for the file
* @param int $height
* height constraint for the file
* @codeCoverageIgnore
*/
public function __construct(int $width, int $height)
{
$this->maximumWidth = $width;

$this->maximumHeight = $height;
}

/**
*
* {@inheritdoc}
* @see \Mezon\Security\Validators\ValidatorInterface::valid()
*/
public function valid(): bool
{
$this->validateFilesFieldExists($this->file);

list($width, $height) = getimagesize($_FILES[$this->file]['tmp_name']);

return $width <= $this->maximumWidth && $height <= $this->maximumHeight;
}

/**
*
* {@inheritdoc}
* @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
*/
public function setValidatingData($data): void
{
$this->file = $data;
}
}
76 changes: 76 additions & 0 deletions Validators/File/ImageMinimumWidthHeight.php
@@ -0,0 +1,76 @@
<?php
namespace Mezon\Security\Validators\File;

/**
* Class ImageMinimumWidthHeight
*
* @package Mezon
* @subpackage Security
* @author Dodonov A.A.
* @version v.1.0 (2020/05/13)
* @copyright Copyright (c) 2020, aeon.org
*/
class ImageMinimumWidthHeight extends \Mezon\Security\Validators\AbstractValidator
{

/**
* Index in the $_FILES array
*
* @var string
*/
private $file = '';

/**
* Minumum width of the image
*
* @var integer
*/
private $minimumWidth = 0;

/**
* Minumum height of the image
*
* @var integer
*/
private $minimumHeight = 0;

/**
* Constructor
*
* @param int $width
* width constraint for the file
* @param int $height
* height constraint for the file
* @codeCoverageIgnore
*/
public function __construct(int $width, int $height)
{
$this->minimumWidth = $width;

$this->minimumHeight = $height;
}

/**
*
* {@inheritdoc}
* @see \Mezon\Security\Validators\ValidatorInterface::valid()
*/
public function valid(): bool
{
$this->validateFilesFieldExists($this->file);

list($width, $height) = getimagesize($_FILES[$this->file]['tmp_name']);

return $width >= $this->minimumWidth && $height >= $this->minimumHeight;
}

/**
*
* {@inheritdoc}
* @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
*/
public function setValidatingData($data): void
{
$this->file = $data;
}
}

0 comments on commit 0e97a6e

Please sign in to comment.