Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4431'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed May 8, 2013
3 parents 02fa5b3 + 927b7df + 0a19046 commit 10b9b3c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 36 deletions.
6 changes: 0 additions & 6 deletions src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ public function isValid($value, $file = null)
}
}

if (empty($this->type) &&
(function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))
) {
$this->type = mime_content_type($file);
}

if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
}
Expand Down
1 change: 1 addition & 0 deletions src/File/IsCompressed.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function __construct($options = array())
'application/x-stuffit',
'application/x-tar',
'application/zip',
'application/x-zip',
'application/zoo',
'multipart/x-gzip',
);
Expand Down
6 changes: 0 additions & 6 deletions src/File/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,6 @@ public function isValid($value, $file = null)
}
}

if (empty($this->type)
&& (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))
) {
$this->type = mime_content_type($file);
}

if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
}
Expand Down
80 changes: 56 additions & 24 deletions test/File/IsCompressedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,66 @@ protected function getMagicMime()
public function basicBehaviorDataProvider()
{
$testFile = __DIR__ . '/_files/test.zip';
$fileUpload = array(
'tmp_name' => $testFile, 'name' => basename($testFile),
'size' => 200, 'error' => 0, 'type' => 'application/zip'

// Sometimes finfo gives application/zip and sometimes
// application/x-zip ...
$expectedMimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $testFile);

$allowed = array('application/zip', 'application/x-zip');
$fileUpload = array(
'tmp_name' => $testFile,
'name' => basename($testFile),
'size' => 200,
'error' => 0,
'type' => in_array($expectedMimeType, $allowed) ? $expectedMimeType : 'application/zip',
);

return array(
// Options, isValid Param, Expected value
array(null, $fileUpload, true),
array('zip', $fileUpload, true),
array('test/notype', $fileUpload, false),
array('application/zip, application/x-tar', $fileUpload, true),
array(array('application/zip', 'application/x-tar'), $fileUpload, true),
array(array('zip', 'tar'), $fileUpload, true),
array(array('tar', 'arj'), $fileUpload, false),
array(null, $fileUpload, true),
array('zip', $fileUpload, true),
array('test/notype', $fileUpload, false),
array('application/x-zip, application/zip, application/x-tar', $fileUpload, true),
array(array('application/x-zip', 'application/zip', 'application/x-tar'), $fileUpload, true),
array(array('zip', 'tar'), $fileUpload, true),
array(array('tar', 'arj'), $fileUpload, false),
);
}

/**
* @return void
* Skip a test if the file info extension is missing
*/
protected function skipIfNoFileInfoExtension()
{
if (!extension_loaded('fileinfo') &&
function_exists('mime_content_type') && ini_get('mime_magic.magicfile') &&
(mime_content_type(__DIR__ . '/_files/test.zip') == 'text/plain')
) {
if (!extension_loaded('fileinfo')) {
$this->markTestSkipped(
'This PHP Version has no finfo, has mime_content_type, ' .
' but mime_content_type exhibits buggy behavior on this system.'
'This PHP Version has no finfo extension'
);
}
}

/**
* Skip a test if finfo returns buggy information
*/
protected function skipIfBuggyMimeContentType($options)
{
if (!is_array($options)) {
$options = (array) $options;
}

if (!in_array('application/zip', $options)) {
// finfo does not play a role; no need to skip
return;
}

// Sometimes finfo gives application/zip and sometimes
// application/x-zip ...
$expectedMimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), __DIR__ . '/_files/test.zip');
if (!in_array($expectedMimeType, array('application/zip', 'application/x-zip'))) {
$this->markTestSkipped('finfo exhibits buggy behavior on this system!');
}
}

/**
* Ensures that the validator follows expected behavior
*
Expand All @@ -83,6 +111,7 @@ function_exists('mime_content_type') && ini_get('mime_magic.magicfile') &&
public function testBasic($options, $isValidParam, $expected)
{
$this->skipIfNoFileInfoExtension();
$this->skipIfBuggyMimeContentType($options);

$validator = new File\IsCompressed($options);
$validator->enableHeaderCheck();
Expand All @@ -93,17 +122,20 @@ public function testBasic($options, $isValidParam, $expected)
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected)
{
if (is_array($isValidParam)) {
$this->skipIfNoFileInfoExtension();

$validator = new File\IsCompressed($options);
$validator->enableHeaderCheck();
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (!is_array($isValidParam)) {
// nothing to test
return;
}

$this->skipIfNoFileInfoExtension();
$this->skipIfBuggyMimeContentType($options);

$validator = new File\IsCompressed($options);
$validator->enableHeaderCheck();
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}

/**
Expand Down

0 comments on commit 10b9b3c

Please sign in to comment.