/tests/Zend/Validator/File/MimeTypeTest.php

https://github.com/bruisedlee/zf2 · PHP · 270 lines · 179 code · 30 blank · 61 comment · 4 complexity · 89f38395e06051beae63fa56344cee54 MD5 · raw file

  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_Validator_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Validator\File;
  25. use Zend\Validator\File;
  26. use Zend\Validator;
  27. /**
  28. * MimeType testbed
  29. *
  30. * @category Zend
  31. * @package Zend_Validator_File
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Validator
  36. */
  37. class MimeTypeTest extends \PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Ensures that the validator follows expected behavior
  41. *
  42. * @return void
  43. */
  44. public function testBasic()
  45. {
  46. $valuesExpected = array(
  47. array(array('image/jpg', 'image/jpeg'), true),
  48. array('image', true),
  49. array('test/notype', false),
  50. array('image/gif, image/jpg, image/jpeg', true),
  51. array(array('image/vasa', 'image/jpg', 'image/jpeg'), true),
  52. array(array('image/jpg', 'image/jpeg', 'gif'), true),
  53. array(array('image/gif', 'gif'), false),
  54. array('image/jp', false),
  55. array('image/jpg2000', false),
  56. array('image/jpeg2000', false),
  57. );
  58. $filetest = __DIR__ . '/_files/picture.jpg';
  59. $files = array(
  60. 'name' => 'picture.jpg',
  61. 'type' => 'image/jpg',
  62. 'size' => 200,
  63. 'tmp_name' => $filetest,
  64. 'error' => 0
  65. );
  66. foreach ($valuesExpected as $element) {
  67. $options = array_shift($element);
  68. $expected = array_shift($element);
  69. $validator = new File\MimeType($options);
  70. $validator->enableHeaderCheck();
  71. $this->assertEquals(
  72. $expected,
  73. $validator->isValid($filetest, $files),
  74. "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1)
  75. . "\nMessages: " . var_export($validator->getMessages(), 1)
  76. );
  77. }
  78. }
  79. /**
  80. * Ensures that getMimeType() returns expected value
  81. *
  82. * @return void
  83. */
  84. public function testGetMimeType()
  85. {
  86. $validator = new File\MimeType('image/gif');
  87. $this->assertEquals('image/gif', $validator->getMimeType());
  88. $validator = new File\MimeType(array('image/gif', 'video', 'text/test'));
  89. $this->assertEquals('image/gif,video,text/test', $validator->getMimeType());
  90. $validator = new File\MimeType(array('image/gif', 'video', 'text/test'));
  91. $this->assertEquals(array('image/gif', 'video', 'text/test'), $validator->getMimeType(true));
  92. }
  93. /**
  94. * Ensures that setMimeType() returns expected value
  95. *
  96. * @return void
  97. */
  98. public function testSetMimeType()
  99. {
  100. $validator = new File\MimeType('image/gif');
  101. $validator->setMimeType('image/jpeg');
  102. $this->assertEquals('image/jpeg', $validator->getMimeType());
  103. $this->assertEquals(array('image/jpeg'), $validator->getMimeType(true));
  104. $validator->setMimeType('image/gif, text/test');
  105. $this->assertEquals('image/gif,text/test', $validator->getMimeType());
  106. $this->assertEquals(array('image/gif', 'text/test'), $validator->getMimeType(true));
  107. $validator->setMimeType(array('video/mpeg', 'gif'));
  108. $this->assertEquals('video/mpeg,gif', $validator->getMimeType());
  109. $this->assertEquals(array('video/mpeg', 'gif'), $validator->getMimeType(true));
  110. }
  111. /**
  112. * Ensures that addMimeType() returns expected value
  113. *
  114. * @return void
  115. */
  116. public function testAddMimeType()
  117. {
  118. $validator = new File\MimeType('image/gif');
  119. $validator->addMimeType('text');
  120. $this->assertEquals('image/gif,text', $validator->getMimeType());
  121. $this->assertEquals(array('image/gif', 'text'), $validator->getMimeType(true));
  122. $validator->addMimeType('jpg, to');
  123. $this->assertEquals('image/gif,text,jpg,to', $validator->getMimeType());
  124. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to'), $validator->getMimeType(true));
  125. $validator->addMimeType(array('zip', 'ti'));
  126. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  127. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  128. $validator->addMimeType('');
  129. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  130. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  131. }
  132. public function testSetAndGetMagicFile()
  133. {
  134. $validator = new File\MimeType('image/gif');
  135. $magic = getenv('magic');
  136. if (!empty($magic)) {
  137. $mimetype = $validator->getMagicFile();
  138. $this->assertEquals($magic, $mimetype);
  139. }
  140. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'can not be');
  141. $validator->setMagicFile('/unknown/magic/file');
  142. }
  143. public function testSetMagicFileWithinConstructor()
  144. {
  145. if (!extension_loaded('fileinfo')) {
  146. $this->markTestSkipped('This PHP Version has no finfo installed');
  147. }
  148. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'The given magicfile is not accepted by finfo');
  149. $validator = new File\MimeType(array('image/gif', 'magicFile' => __FILE__));
  150. }
  151. public function testOptionsAtConstructor()
  152. {
  153. $validator = new File\MimeType(array(
  154. 'image/gif',
  155. 'image/jpg',
  156. 'enableHeaderCheck' => true));
  157. $this->assertTrue($validator->getHeaderCheck());
  158. $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
  159. }
  160. /**
  161. * @group ZF-9686
  162. */
  163. public function testDualValidation()
  164. {
  165. $valuesExpected = array(
  166. array('image', true),
  167. );
  168. $filetest = __DIR__ . '/_files/picture.jpg';
  169. $files = array(
  170. 'name' => 'picture.jpg',
  171. 'type' => 'image/jpg',
  172. 'size' => 200,
  173. 'tmp_name' => $filetest,
  174. 'error' => 0
  175. );
  176. foreach ($valuesExpected as $element) {
  177. $options = array_shift($element);
  178. $expected = array_shift($element);
  179. $validator = new File\MimeType($options);
  180. $validator->enableHeaderCheck();
  181. $this->assertEquals(
  182. $expected,
  183. $validator->isValid($filetest, $files),
  184. "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1)
  185. . "\nMessages: " . var_export($validator->getMessages(), 1)
  186. );
  187. $validator = new File\MimeType($options);
  188. $validator->enableHeaderCheck();
  189. $this->assertEquals(
  190. $expected,
  191. $validator->isValid($filetest, $files),
  192. "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1)
  193. . "\nMessages: " . var_export($validator->getMessages(), 1)
  194. );
  195. }
  196. }
  197. /**
  198. * @group ZF-11258
  199. */
  200. public function testZF11258()
  201. {
  202. $validator = new File\MimeType(array(
  203. 'image/gif',
  204. 'image/jpg',
  205. 'headerCheck' => true));
  206. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  207. $this->assertTrue(array_key_exists('fileMimeTypeNotReadable', $validator->getMessages()));
  208. $this->assertContains("'nofile.mo'", current($validator->getMessages()));
  209. }
  210. public function testDisableMagicFile()
  211. {
  212. $validator = new File\MimeType('image/gif');
  213. $magic = getenv('magic');
  214. if (!empty($magic)) {
  215. $mimetype = $validator->getMagicFile();
  216. $this->assertEquals($magic, $mimetype);
  217. }
  218. $validator->disableMagicFile(true);
  219. $this->assertTrue($validator->isMagicFileDisabled());
  220. if (!empty($magic)) {
  221. $mimetype = $validator->getMagicFile();
  222. $this->assertEquals($magic, $mimetype);
  223. }
  224. }
  225. /**
  226. * @group ZF-10461
  227. */
  228. public function testDisablingMagicFileByConstructor()
  229. {
  230. $files = array(
  231. 'name' => 'picture.jpg',
  232. 'size' => 200,
  233. 'tmp_name' => dirname(__FILE__) . '/_files/picture.jpg',
  234. 'error' => 0,
  235. 'magicFile' => false,
  236. );
  237. $validator = new File\MimeType($files);
  238. $this->assertFalse($validator->getMagicFile());
  239. }
  240. }