/tests/Zend/Validator/File/IsCompressedTest.php

https://github.com/adaykin/zf2 · PHP · 189 lines · 110 code · 21 blank · 58 comment · 4 complexity · de7019d4d374343ce7b918566141934d 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. /**
  27. * @see Zend_Validator_File_IsCompressed
  28. */
  29. /**
  30. * IsCompressed testbed
  31. *
  32. * @category Zend
  33. * @package Zend_Validator_File
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Validator
  38. */
  39. class IsCompressedTest extends \PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Ensures that the validator follows expected behavior
  43. *
  44. * @return void
  45. */
  46. public function testBasic()
  47. {
  48. if (!extension_loaded('fileinfo') &&
  49. function_exists('mime_content_type') && ini_get('mime_magic.magicfile') &&
  50. (mime_content_type(__DIR__ . '/_files/test.zip') == 'text/plain')
  51. ) {
  52. $this->markTestSkipped('This PHP Version has no finfo, has mime_content_type, '
  53. . ' but mime_content_type exhibits buggy behavior on this system.'
  54. );
  55. }
  56. $valuesExpected = array(
  57. array(null, true),
  58. array('zip', true),
  59. array('test/notype', false),
  60. array('application/zip, application/x-tar', true),
  61. array(array('application/zip', 'application/x-tar'), true),
  62. array(array('zip', 'tar'), true),
  63. array(array('tar', 'arj'), false),
  64. );
  65. $files = array(
  66. 'name' => 'test.zip',
  67. 'type' => 'application/zip',
  68. 'size' => 200,
  69. 'tmp_name' => __DIR__ . '/_files/test.zip',
  70. 'error' => 0
  71. );
  72. foreach ($valuesExpected as $element) {
  73. $validator = new File\IsCompressed($element[0]);
  74. $validator->enableHeaderCheck();
  75. $this->assertEquals(
  76. $element[1],
  77. $validator->isValid(__DIR__ . '/_files/test.zip', $files),
  78. "Tested with " . var_export($element, 1)
  79. );
  80. }
  81. }
  82. /**
  83. * Ensures that getMimeType() returns expected value
  84. *
  85. * @return void
  86. */
  87. public function testGetMimeType()
  88. {
  89. $validator = new File\IsCompressed('image/gif');
  90. $this->assertEquals('image/gif', $validator->getMimeType());
  91. $validator = new File\IsCompressed(array('image/gif', 'video', 'text/test'));
  92. $this->assertEquals('image/gif,video,text/test', $validator->getMimeType());
  93. $validator = new File\IsCompressed(array('image/gif', 'video', 'text/test'));
  94. $this->assertEquals(array('image/gif', 'video', 'text/test'), $validator->getMimeType(true));
  95. }
  96. /**
  97. * Ensures that setMimeType() returns expected value
  98. *
  99. * @return void
  100. */
  101. public function testSetMimeType()
  102. {
  103. $validator = new File\IsCompressed('image/gif');
  104. $validator->setMimeType('image/jpeg');
  105. $this->assertEquals('image/jpeg', $validator->getMimeType());
  106. $this->assertEquals(array('image/jpeg'), $validator->getMimeType(true));
  107. $validator->setMimeType('image/gif, text/test');
  108. $this->assertEquals('image/gif,text/test', $validator->getMimeType());
  109. $this->assertEquals(array('image/gif', 'text/test'), $validator->getMimeType(true));
  110. $validator->setMimeType(array('video/mpeg', 'gif'));
  111. $this->assertEquals('video/mpeg,gif', $validator->getMimeType());
  112. $this->assertEquals(array('video/mpeg', 'gif'), $validator->getMimeType(true));
  113. }
  114. /**
  115. * Ensures that addMimeType() returns expected value
  116. *
  117. * @return void
  118. */
  119. public function testAddMimeType()
  120. {
  121. $validator = new File\IsCompressed('image/gif');
  122. $validator->addMimeType('text');
  123. $this->assertEquals('image/gif,text', $validator->getMimeType());
  124. $this->assertEquals(array('image/gif', 'text'), $validator->getMimeType(true));
  125. $validator->addMimeType('jpg, to');
  126. $this->assertEquals('image/gif,text,jpg,to', $validator->getMimeType());
  127. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to'), $validator->getMimeType(true));
  128. $validator->addMimeType(array('zip', 'ti'));
  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. $validator->addMimeType('');
  132. $this->assertEquals('image/gif,text,jpg,to,zip,ti', $validator->getMimeType());
  133. $this->assertEquals(array('image/gif', 'text', 'jpg', 'to', 'zip', 'ti'), $validator->getMimeType(true));
  134. }
  135. /**
  136. * @ZF-8111
  137. */
  138. public function testErrorMessages()
  139. {
  140. $files = array(
  141. 'name' => 'picture.jpg',
  142. 'type' => 'image/jpeg',
  143. 'size' => 200,
  144. 'tmp_name' => __DIR__ . '/_files/picture.jpg',
  145. 'error' => 0
  146. );
  147. $validator = new File\IsCompressed('test/notype');
  148. $validator->enableHeaderCheck();
  149. $this->assertFalse($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
  150. $error = $validator->getMessages();
  151. $this->assertTrue(array_key_exists('fileIsCompressedFalseType', $error));
  152. }
  153. public function testOptionsAtConstructor()
  154. {
  155. if (!extension_loaded('fileinfo')) {
  156. $this->markTestSkipped('This PHP Version has no finfo installed');
  157. }
  158. $magicFile = dirname(__FILE__) . '/_files/magic.mime';
  159. $validator = new File\IsCompressed(array(
  160. 'image/gif',
  161. 'image/jpg',
  162. 'magicfile' => $magicFile,
  163. 'headerCheck' => true));
  164. $this->assertEquals($magicFile, $validator->getMagicFile());
  165. $this->assertTrue($validator->getHeaderCheck());
  166. $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
  167. }
  168. }