-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Rakit\Validation\Rules; | ||
|
||
use Rakit\Validation\Rule; | ||
use Rakit\Validation\MimeTypeGuesser; | ||
|
||
class Mimes extends Rule | ||
{ | ||
use FileTrait; | ||
|
||
/** @var string */ | ||
protected $message = "The :attribute file type is not allowed"; | ||
|
||
/** @var string|int */ | ||
protected $maxSize = null; | ||
|
||
/** @var string|int */ | ||
protected $minSize = null; | ||
|
||
/** @var array */ | ||
protected $allowedTypes = []; | ||
|
||
/** | ||
* Given $params and assign $this->params | ||
* | ||
* @param array $params | ||
* @return self | ||
*/ | ||
public function fillParameters(array $params): Rule | ||
{ | ||
$this->allowTypes($params); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Given $types and assign $this->params | ||
* | ||
* @param mixed $types | ||
* @return self | ||
*/ | ||
public function allowTypes($types): Rule | ||
{ | ||
if (is_string($types)) { | ||
$types = explode('|', $types); | ||
} | ||
|
||
$this->params['allowed_types'] = $types; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Check the $value is valid | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function check($value): bool | ||
{ | ||
$allowedTypes = $this->parameter('allowed_types'); | ||
|
||
// below is Required rule job | ||
if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) { | ||
return true; | ||
} | ||
|
||
if (!$this->isUploadedFile($value)) { | ||
return false; | ||
} | ||
|
||
// just make sure there is no error | ||
if ($value['error']) { | ||
return false; | ||
} | ||
|
||
if (!empty($allowedTypes)) { | ||
$guesser = new MimeTypeGuesser; | ||
$ext = $guesser->getExtension($value['type']); | ||
unset($guesser); | ||
|
||
if (!in_array($ext, $allowedTypes)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Rakit\Validation\Tests; | ||
|
||
use Rakit\Validation\Rules\Mimes; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MimesTest extends TestCase | ||
{ | ||
|
||
public function setUp() | ||
{ | ||
$this->rule = new Mimes(); | ||
} | ||
|
||
public function testValidMimes() | ||
{ | ||
$file = [ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'text/plain', | ||
'size' => filesize(__FILE__), | ||
'tmp_name' => __FILE__, | ||
'error' => UPLOAD_ERR_OK | ||
]; | ||
|
||
$uploadedFileRule = $this->getMockBuilder(Mimes::class) | ||
->setMethods(['isUploadedFile']) | ||
->getMock(); | ||
|
||
$uploadedFileRule->expects($this->once()) | ||
->method('isUploadedFile') | ||
->willReturn(true); | ||
|
||
$this->assertTrue($uploadedFileRule->check($file)); | ||
} | ||
|
||
/** | ||
* Make sure we can't just passing array like valid $_FILES['key'] | ||
*/ | ||
public function testValidateWithoutMockShouldBeInvalid() | ||
{ | ||
$this->assertFalse($this->rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'text/plain', | ||
'size' => filesize(__FILE__), | ||
'tmp_name' => __FILE__, | ||
'error' => UPLOAD_ERR_OK | ||
])); | ||
} | ||
|
||
/** | ||
* Missing UPLOAD_ERR_NO_FILE should be valid because it is job for required rule | ||
*/ | ||
public function testEmptyMimesShouldBeValid() | ||
{ | ||
$this->assertTrue($this->rule->check([ | ||
'name' => '', | ||
'type' => '', | ||
'size' => '', | ||
'tmp_name' => '', | ||
'error' => UPLOAD_ERR_NO_FILE | ||
])); | ||
} | ||
|
||
public function testUploadError() | ||
{ | ||
$this->assertFalse($this->rule->check([ | ||
'name' => '', | ||
'type' => '', | ||
'size' => '', | ||
'tmp_name' => '', | ||
'error' => 5 | ||
])); | ||
} | ||
|
||
public function testFileTypes() | ||
{ | ||
|
||
$rule = $this->getMockBuilder(Mimes::class) | ||
->setMethods(['isUploadedFile']) | ||
->getMock(); | ||
|
||
$rule->expects($this->exactly(3)) | ||
->method('isUploadedFile') | ||
->willReturn(true); | ||
|
||
$rule->allowTypes('png|jpeg'); | ||
|
||
$this->assertFalse($rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'text/plain', | ||
'size' => 1024, // 1K | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
|
||
$this->assertTrue($rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'image/png', | ||
'size' => 10 * 1024, | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
|
||
$this->assertTrue($rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'image/jpeg', | ||
'size' => 10 * 1024, | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
} | ||
|
||
/** | ||
* Missing array key(s) should be valid because it is job for required rule | ||
*/ | ||
public function testMissingAKeyShouldBeValid() | ||
{ | ||
// missing name | ||
$this->assertTrue($this->rule->check([ | ||
'type' => 'text/plain', | ||
'size' => filesize(__FILE__), | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
|
||
// missing type | ||
$this->assertTrue($this->rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'size' => filesize(__FILE__), | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
|
||
// missing size | ||
$this->assertTrue($this->rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'text/plain', | ||
'tmp_name' => __FILE__, | ||
'error' => 0 | ||
])); | ||
|
||
// missing tmp_name | ||
$this->assertTrue($this->rule->check([ | ||
'name' => pathinfo(__FILE__, PATHINFO_BASENAME), | ||
'type' => 'text/plain', | ||
'size' => filesize(__FILE__), | ||
'error' => 0 | ||
])); | ||
} | ||
} |