From d942583f20752e08f74a78105d9883644ed62e96 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 6 May 2013 13:59:13 -0500 Subject: [PATCH 1/4] [zendframework/zf2#4431] Refactored tests slightly - Was observing test failures due to calling markTestSkipped() inside a data provider. - Created new skipIfBuggyMimeContentType() method to call to skip a test if mime_content_type() returns buggy information for zip files --- test/File/IsCompressedTest.php | 61 ++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/test/File/IsCompressedTest.php b/test/File/IsCompressedTest.php index e86db1d56..73adb5a61 100644 --- a/test/File/IsCompressedTest.php +++ b/test/File/IsCompressedTest.php @@ -45,29 +45,29 @@ public function basicBehaviorDataProvider() // Sometimes mime_content_type() gives application/zip and sometimes // application/x-zip ... - $expectedMimeType = mime_content_type(__DIR__ . '/_files/test.zip'); - if (!in_array($expectedMimeType, array('application/zip', 'application/x-zip'))) { - $this->markTestSkipped('mime_content_type exhibits buggy behavior on this system!'); - } - - $fileUpload = array( - 'tmp_name' => $testFile, 'name' => basename($testFile), - 'size' => 200, 'error' => 0, 'type' => $expectedMimeType + $expectedMimeType = mime_content_type($testFile); + $fileUpload = array( + 'tmp_name' => $testFile, + 'name' => basename($testFile), + 'size' => 200, + 'error' => 0, + 'type' => $expectedMimeType, ); + return array( // Options, isValid Param, Expected value - 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(null, $fileUpload, true), + array('zip', $fileUpload, true), + array('test/notype', $fileUpload, false), + array(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), + 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() { @@ -82,6 +82,19 @@ function_exists('mime_content_type') && ini_get('mime_magic.magicfile') && } } + /** + * Skip a test if mime_content_type returns buggy information + */ + protected function skipIfBuggyMimeContentType() + { + // Sometimes mime_content_type() gives application/zip and sometimes + // application/x-zip ... + $expectedMimeType = mime_content_type(__DIR__ . '/_files/test.zip'); + if (!in_array($expectedMimeType, array('application/zip', 'application/x-zip'))) { + $this->markTestSkipped('mime_content_type exhibits buggy behavior on this system!'); + } + } + /** * Ensures that the validator follows expected behavior * @@ -91,6 +104,7 @@ function_exists('mime_content_type') && ini_get('mime_magic.magicfile') && public function testBasic($options, $isValidParam, $expected) { $this->skipIfNoFileInfoExtension(); + $this->skipIfBuggyMimeContentType(); $validator = new File\IsCompressed($options); $validator->enableHeaderCheck(); @@ -101,17 +115,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(); + + $validator = new File\IsCompressed($options); + $validator->enableHeaderCheck(); + $this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam)); } /** From 42f951635820207bb32a349ee270905e1b8b8fbc Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 6 May 2013 14:10:54 -0500 Subject: [PATCH 2/4] [zendframework/zf2#4431] Better mime_content_type behavior - Only skip if we're dealing with a zip type in the first place. --- test/File/IsCompressedTest.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/File/IsCompressedTest.php b/test/File/IsCompressedTest.php index 73adb5a61..9bdd3cd63 100644 --- a/test/File/IsCompressedTest.php +++ b/test/File/IsCompressedTest.php @@ -45,13 +45,14 @@ public function basicBehaviorDataProvider() // Sometimes mime_content_type() gives application/zip and sometimes // application/x-zip ... + $allowed = array('application/zip', 'application/x-zip'); $expectedMimeType = mime_content_type($testFile); $fileUpload = array( 'tmp_name' => $testFile, 'name' => basename($testFile), 'size' => 200, 'error' => 0, - 'type' => $expectedMimeType, + 'type' => in_array($expectedMimeType, $allowed) ? $expectedMimeType : 'application/zip', ); return array( @@ -59,7 +60,7 @@ public function basicBehaviorDataProvider() array(null, $fileUpload, true), array('zip', $fileUpload, true), array('test/notype', $fileUpload, false), - array(array('application/x-zip, application/zip, application/x-tar'), $fileUpload, true), + 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), @@ -85,8 +86,17 @@ function_exists('mime_content_type') && ini_get('mime_magic.magicfile') && /** * Skip a test if mime_content_type returns buggy information */ - protected function skipIfBuggyMimeContentType() + protected function skipIfBuggyMimeContentType($options) { + if (!is_array($options)) { + $options = (array) $options; + } + + if (!in_array('application/zip', $options)) { + // mime_content_type does not play a role; no need to skip + return; + } + // Sometimes mime_content_type() gives application/zip and sometimes // application/x-zip ... $expectedMimeType = mime_content_type(__DIR__ . '/_files/test.zip'); @@ -104,7 +114,7 @@ protected function skipIfBuggyMimeContentType() public function testBasic($options, $isValidParam, $expected) { $this->skipIfNoFileInfoExtension(); - $this->skipIfBuggyMimeContentType(); + $this->skipIfBuggyMimeContentType($options); $validator = new File\IsCompressed($options); $validator->enableHeaderCheck(); @@ -124,7 +134,7 @@ public function testLegacy($options, $isValidParam, $expected) } $this->skipIfNoFileInfoExtension(); - $this->skipIfBuggyMimeContentType(); + $this->skipIfBuggyMimeContentType($options); $validator = new File\IsCompressed($options); $validator->enableHeaderCheck(); From 927b7df588714fb7a4234e7daa635a57507ab1a2 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 8 May 2013 13:28:39 -0500 Subject: [PATCH 3/4] [zendframework/zf2#4447] revert changes to changelog, emailaddress test --- test/EmailAddressTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/EmailAddressTest.php b/test/EmailAddressTest.php index e7ac20eda..9f8306a01 100644 --- a/test/EmailAddressTest.php +++ b/test/EmailAddressTest.php @@ -420,7 +420,7 @@ public function testHostnameValidatorMessagesShouldBeTranslated() public function testEmailsExceedingLength() { $emailAddresses = array( - 'thislocalpathoftheemailaddressislongerthantheallowedsizeof64characters@domain.com', + 'thislocalpathoftheemailadressislongerthantheallowedsizeof64characters@domain.com', 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarexpialidociousspoonfulofsugar.com', ); foreach ($emailAddresses as $input) { From 0a19046d77339ed1d217b66abd9ae7de78e69d11 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 8 May 2013 15:04:16 -0500 Subject: [PATCH 4/4] [zendframework/zf2#4431] Remove usage of mime_content_type - Deprecated since 5.3.0, and fileinfo is enabled by default in vanilla PHP builds. It makes no sense to fallback to it, when finfo does a much better job. --- src/File/ExcludeMimeType.php | 6 ------ src/File/MimeType.php | 6 ------ test/File/IsCompressedTest.php | 23 ++++++++++------------- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/File/ExcludeMimeType.php b/src/File/ExcludeMimeType.php index 2d2477455..3d79fee79 100644 --- a/src/File/ExcludeMimeType.php +++ b/src/File/ExcludeMimeType.php @@ -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; } diff --git a/src/File/MimeType.php b/src/File/MimeType.php index 251ec925a..240d426d5 100644 --- a/src/File/MimeType.php +++ b/src/File/MimeType.php @@ -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; } diff --git a/test/File/IsCompressedTest.php b/test/File/IsCompressedTest.php index 9bdd3cd63..690fd4eea 100644 --- a/test/File/IsCompressedTest.php +++ b/test/File/IsCompressedTest.php @@ -43,10 +43,11 @@ public function basicBehaviorDataProvider() { $testFile = __DIR__ . '/_files/test.zip'; - // Sometimes mime_content_type() gives application/zip and sometimes + // 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'); - $expectedMimeType = mime_content_type($testFile); $fileUpload = array( 'tmp_name' => $testFile, 'name' => basename($testFile), @@ -72,19 +73,15 @@ public function basicBehaviorDataProvider() */ 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 mime_content_type returns buggy information + * Skip a test if finfo returns buggy information */ protected function skipIfBuggyMimeContentType($options) { @@ -93,15 +90,15 @@ protected function skipIfBuggyMimeContentType($options) } if (!in_array('application/zip', $options)) { - // mime_content_type does not play a role; no need to skip + // finfo does not play a role; no need to skip return; } - // Sometimes mime_content_type() gives application/zip and sometimes + // Sometimes finfo gives application/zip and sometimes // application/x-zip ... - $expectedMimeType = mime_content_type(__DIR__ . '/_files/test.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('mime_content_type exhibits buggy behavior on this system!'); + $this->markTestSkipped('finfo exhibits buggy behavior on this system!'); } }