IsCompressedTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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-2012 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-2012 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. // Sometimes mime_content_type() gives application/zip and sometimes
  68. // application/x-zip ...
  69. $expectedMimeType = mime_content_type(dirname(__FILE__) . '/_files/test.zip');
  70. if (!in_array($expectedMimeType, array('application/zip', 'application/x-zip'))) {
  71. $this->markTestSkipped('mime_content_type exhibits buggy behavior on this system!');
  72. }
  73. $valuesExpected = array(
  74. array(null, true),
  75. array('zip', true),
  76. array('test/notype', false),
  77. array('application/x-zip, application/zip, application/x-tar', true),
  78. array(array('application/x-zip', 'application/zip', 'application/x-tar'), true),
  79. array(array('zip', 'tar'), true),
  80. array(array('tar', 'arj'), false),
  81. );
  82. $files = array(
  83. 'name' => 'test.zip',
  84. 'type' => $expectedMimeType,
  85. 'size' => 200,
  86. 'tmp_name' => dirname(__FILE__) . '/_files/test.zip',
  87. 'error' => 0
  88. );
  89. foreach ($valuesExpected as $element) {
  90. $validator = new Zend_Validate_File_IsCompressed($element[0]);
  91. $validator->enableHeaderCheck();
  92. $this->assertEquals(
  93. $element[1],
  94. $validator->isValid(dirname(__FILE__) . '/_files/test.zip', $files),
  95. "Tested with " . var_export($element, 1)
  96. );
  97. }
  98. }
  99. /**
  100. * Ensures that getMimeType() returns expected value
  101. *
  102. * @return void
  103. */
  104. public function testGetMimeType()
  105. {
  106. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  107. $this->assertEquals('image/gif', $validator->getMimeType());
  108. $validator = new Zend_Validate_File_IsCompressed(array('image/gif', 'video', 'text/test'));
  109. $this->assertEquals('image/gif,video,text/test', $validator->getMimeType());
  110. $validator = new Zend_Validate_File_IsCompressed(array('image/gif', 'video', 'text/test'));
  111. $this->assertEquals(array('image/gif', 'video', 'text/test'), $validator->getMimeType(true));
  112. }
  113. /**
  114. * Ensures that setMimeType() returns expected value
  115. *
  116. * @return void
  117. */
  118. public function testSetMimeType()
  119. {
  120. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  121. $validator->setMimeType('image/jpeg');
  122. $this->assertEquals('image/jpeg', $validator->getMimeType());
  123. $this->assertEquals(array('image/jpeg'), $validator->getMimeType(true));
  124. $validator->setMimeType('image/gif, text/test');
  125. $this->assertEquals('image/gif,text/test', $validator->getMimeType());
  126. $this->assertEquals(array('image/gif', 'text/test'), $validator->getMimeType(true));
  127. $validator->setMimeType(array('video/mpeg', 'gif'));
  128. $this->assertEquals('video/mpeg,gif', $validator->getMimeType());
  129. $this->assertEquals(array('video/mpeg', 'gif'), $validator->getMimeType(true));
  130. }
  131. /**
  132. * Ensures that addMimeType() returns expected value
  133. *
  134. * @return void
  135. */
  136. public function testAddMimeType()
  137. {
  138. $validator = new Zend_Validate_File_IsCompressed('image/gif');
  139. $validator->addMimeType('text');
  140. $this->assertEquals('image/gif,text', $validator->getMimeType());
  141. $this->assertEquals(array('image/gif', 'text'), $validator->getMimeType(true));
  142. $validator->addMimeType('jpg, to');
  143. $this->assertEquals('image/gif,text,jpg,to', $validator->getMimeType());
  144. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to'), $validator->getMimeType(true));
  145. $validator->addMimeType(array('zip', 'ti'));
  146. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  147. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  148. $validator->addMimeType('');
  149. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  150. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  151. }
  152. /**
  153. * @ZF-8111
  154. */
  155. public function testErrorMessages()
  156. {
  157. $files = array(
  158. 'name' => 'picture.jpg',
  159. 'type' => 'image/jpeg',
  160. 'size' => 200,
  161. 'tmp_name' => dirname(__FILE__) . '/_files/picture.jpg',
  162. 'error' => 0
  163. );
  164. $validator = new Zend_Validate_File_IsCompressed('test/notype');
  165. $validator->enableHeaderCheck();
  166. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  167. $error = $validator->getMessages();
  168. $this->assertTrue(array_key_exists('fileIsCompressedFalseType', $error));
  169. }
  170. public function testOptionsAtConstructor()
  171. {
  172. if (!extension_loaded('fileinfo')) {
  173. $this->markTestSkipped('This PHP Version has no finfo installed');
  174. }
  175. $magicFile = dirname(__FILE__) . '/_files/magic.mime';
  176. $validator = new Zend_Validate_File_IsCompressed(array(
  177. 'image/gif',
  178. 'image/jpg',
  179. 'magicfile' => $magicFile,
  180. 'headerCheck' => true));
  181. $this->assertEquals($magicFile, $validator->getMagicFile());
  182. $this->assertTrue($validator->getHeaderCheck());
  183. $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
  184. }
  185. }
  186. // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.
  187. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_IsCompressedTest::main") {
  188. Zend_Validate_File_IsCompressedTest::main();
  189. }