IsCompressedTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_IsCompressedTest::main");
  25. }
  26. /**
  27. * @see Zend_Validate_File_IsCompressed
  28. */
  29. require_once 'Zend/Validate/File/IsCompressed.php';
  30. /**
  31. * IsCompressed testbed
  32. *
  33. * @category Zend
  34. * @package Zend_Validate_File
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Validate
  39. */
  40. class Zend_Validate_File_IsCompressedTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_IsCompressedTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Ensures that the validator follows expected behavior
  54. *
  55. * @return void
  56. */
  57. public function testBasic()
  58. {
  59. if (!extension_loaded('fileinfo') &&
  60. function_exists('mime_content_type') && ini_get('mime_magic.magicfile') &&
  61. (mime_content_type(dirname(__FILE__) . '/_files/test.zip') == 'text/plain')
  62. ) {
  63. $this->markTestSkipped('This PHP Version has no finfo, has mime_content_type, '
  64. . ' but mime_content_type exhibits buggy behavior on this system.'
  65. );
  66. }
  67. // Prevent error in the next check
  68. if (!function_exists('mime_content_type')) {
  69. $this->markTestSkipped('mime_content_type function is not available.');
  70. }
  71. // Sometimes mime_content_type() gives application/zip and sometimes
  72. // application/x-zip ...
  73. $expectedMimeType = mime_content_type(dirname(__FILE__) . '/_files/test.zip');
  74. if (!in_array($expectedMimeType, array('application/zip', 'application/x-zip'))) {
  75. $this->markTestSkipped('mime_content_type exhibits buggy behavior on this system!');
  76. }
  77. $valuesExpected = array(
  78. array(null, true),
  79. array('zip', true),
  80. array('test/notype', false),
  81. array('application/x-zip, application/zip, application/x-tar', true),
  82. array(array('application/x-zip', 'application/zip', 'application/x-tar'), true),
  83. array(array('zip', 'tar'), true),
  84. array(array('tar', 'arj'), false),
  85. );
  86. $files = array(
  87. 'name' => 'test.zip',
  88. 'type' => $expectedMimeType,
  89. 'size' => 200,
  90. 'tmp_name' => dirname(__FILE__) . '/_files/test.zip',
  91. 'error' => 0
  92. );
  93. foreach ($valuesExpected as $element) {
  94. $validator = new Zend_Validate_File_IsCompressed($element[0]);
  95. $validator->enableHeaderCheck();
  96. $this->assertEquals(
  97. $element[1],
  98. $validator->isValid(dirname(__FILE__) . '/_files/test.zip', $files),
  99. "Tested with " . var_export($element, 1)
  100. );
  101. }
  102. }
  103. /**
  104. * Ensures that getMimeType() returns expected value
  105. *
  106. * @return void
  107. */
  108. public function testGetMimeType()
  109. {
  110. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  111. $this->assertEquals('image/gif', $validator->getMimeType());
  112. $validator = new Zend_Validate_File_IsCompressed(array('image/gif', 'video', 'text/test'));
  113. $this->assertEquals('image/gif,video,text/test', $validator->getMimeType());
  114. $validator = new Zend_Validate_File_IsCompressed(array('image/gif', 'video', 'text/test'));
  115. $this->assertEquals(array('image/gif', 'video', 'text/test'), $validator->getMimeType(true));
  116. }
  117. /**
  118. * Ensures that setMimeType() returns expected value
  119. *
  120. * @return void
  121. */
  122. public function testSetMimeType()
  123. {
  124. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  125. $validator->setMimeType('image/jpeg');
  126. $this->assertEquals('image/jpeg', $validator->getMimeType());
  127. $this->assertEquals(array('image/jpeg'), $validator->getMimeType(true));
  128. $validator->setMimeType('image/gif, text/test');
  129. $this->assertEquals('image/gif,text/test', $validator->getMimeType());
  130. $this->assertEquals(array('image/gif', 'text/test'), $validator->getMimeType(true));
  131. $validator->setMimeType(array('video/mpeg', 'gif'));
  132. $this->assertEquals('video/mpeg,gif', $validator->getMimeType());
  133. $this->assertEquals(array('video/mpeg', 'gif'), $validator->getMimeType(true));
  134. }
  135. /**
  136. * Ensures that addMimeType() returns expected value
  137. *
  138. * @return void
  139. */
  140. public function testAddMimeType()
  141. {
  142. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  143. $validator->addMimeType('text');
  144. $this->assertEquals('image/gif,text', $validator->getMimeType());
  145. $this->assertEquals(array('image/gif', 'text'), $validator->getMimeType(true));
  146. $validator->addMimeType('jpg, to');
  147. $this->assertEquals('image/gif,text,jpg,to', $validator->getMimeType());
  148. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to'), $validator->getMimeType(true));
  149. $validator->addMimeType(array('zip', 'ti'));
  150. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  151. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  152. $validator->addMimeType('');
  153. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  154. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  155. }
  156. /**
  157. * @ZF-8111
  158. */
  159. public function testErrorMessages()
  160. {
  161. $files = array(
  162. 'name' => 'picture.jpg',
  163. 'type' => 'image/jpeg',
  164. 'size' => 200,
  165. 'tmp_name' => dirname(__FILE__) . '/_files/picture.jpg',
  166. 'error' => 0
  167. );
  168. $validator = new Zend_Validate_File_IsCompressed('test/notype');
  169. $validator->enableHeaderCheck();
  170. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  171. $error = $validator->getMessages();
  172. $this->assertTrue(array_key_exists('fileIsCompressedFalseType', $error));
  173. }
  174. public function testOptionsAtConstructor()
  175. {
  176. if (!extension_loaded('fileinfo')) {
  177. $this->markTestSkipped('This PHP Version has no finfo installed');
  178. }
  179. if (version_compare(PHP_VERSION, '5.3', '>=')) {
  180. $magicFile = dirname(__FILE__) . '/_files/magic-php53.mime';
  181. } else {
  182. $magicFile = dirname(__FILE__) . '/_files/magic.mime';
  183. }
  184. $validator = new Zend_Validate_File_IsCompressed(array(
  185. 'image/gif',
  186. 'image/jpg',
  187. 'magicfile' => $magicFile,
  188. 'headerCheck' => true));
  189. $this->assertEquals($magicFile, $validator->getMagicFile());
  190. $this->assertTrue($validator->getHeaderCheck());
  191. $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
  192. }
  193. }
  194. // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.
  195. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_IsCompressedTest::main") {
  196. Zend_Validate_File_IsCompressedTest::main();
  197. }