2
0

Sha1Test.php 7.4 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-2009 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_Sha1Test::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_Sha1Test::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_Sha1
  32. */
  33. require_once 'Zend/Validate/File/Sha1.php';
  34. /**
  35. * Sha1 testbed
  36. *
  37. * @category Zend
  38. * @package Zend_Validate_File
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Validate
  43. */
  44. class Zend_Validate_File_Sha1Test extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_Sha1Test");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Ensures that the validator follows expected behavior
  58. *
  59. * @return void
  60. */
  61. public function testBasic()
  62. {
  63. $valuesExpected = array(
  64. array('b2a5334847b4328e7d19d9b41fd874dffa911c98', true),
  65. array('52a5334847b4328e7d19d9b41fd874dffa911c98', false),
  66. array(array('42a5334847b4328e7d19d9b41fd874dffa911c98', 'b2a5334847b4328e7d19d9b41fd874dffa911c98'), true),
  67. array(array('42a5334847b4328e7d19d9b41fd874dffa911c98', '72a5334847b4328e7d19d9b41fd874dffa911c98'), false),
  68. );
  69. foreach ($valuesExpected as $element) {
  70. $validator = new Zend_Validate_File_Sha1($element[0]);
  71. $this->assertEquals(
  72. $element[1],
  73. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  74. "Tested with " . var_export($element, 1)
  75. );
  76. }
  77. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  78. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo'));
  79. $this->assertTrue(array_key_exists('fileSha1NotFound', $validator->getMessages()));
  80. $files = array(
  81. 'name' => 'test1',
  82. 'type' => 'text',
  83. 'size' => 200,
  84. 'tmp_name' => 'tmp_test1',
  85. 'error' => 0
  86. );
  87. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  88. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo', $files));
  89. $this->assertTrue(array_key_exists('fileSha1NotFound', $validator->getMessages()));
  90. $files = array(
  91. 'name' => 'testsize.mo',
  92. 'type' => 'text',
  93. 'size' => 200,
  94. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  95. 'error' => 0
  96. );
  97. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  98. $this->assertTrue($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  99. $files = array(
  100. 'name' => 'testsize.mo',
  101. 'type' => 'text',
  102. 'size' => 200,
  103. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  104. 'error' => 0
  105. );
  106. $validator = new Zend_Validate_File_Sha1('42a5334847b4328e7d19d9b41fd874dffa911c98');
  107. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  108. $this->assertTrue(array_key_exists('fileSha1DoesNotMatch', $validator->getMessages()));
  109. }
  110. /**
  111. * Ensures that getSha1() returns expected value
  112. *
  113. * @return void
  114. */
  115. public function testgetSha1()
  116. {
  117. $validator = new Zend_Validate_File_Sha1('12345');
  118. $this->assertEquals(array('12345' => 'sha1'), $validator->getSha1());
  119. $validator = new Zend_Validate_File_Sha1(array('12345', '12333', '12344'));
  120. $this->assertEquals(array('12345' => 'sha1', '12333' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  121. }
  122. /**
  123. * Ensures that getHash() returns expected value
  124. *
  125. * @return void
  126. */
  127. public function testgetHash()
  128. {
  129. $validator = new Zend_Validate_File_Sha1('12345');
  130. $this->assertEquals(array('12345' => 'sha1'), $validator->getHash());
  131. $validator = new Zend_Validate_File_Sha1(array('12345', '12333', '12344'));
  132. $this->assertEquals(array('12345' => 'sha1', '12333' => 'sha1', '12344' => 'sha1'), $validator->getHash());
  133. }
  134. /**
  135. * Ensures that setSha1() returns expected value
  136. *
  137. * @return void
  138. */
  139. public function testSetSha1()
  140. {
  141. $validator = new Zend_Validate_File_Sha1('12345');
  142. $validator->setSha1('12333');
  143. $this->assertEquals(array('12333' => 'sha1'), $validator->getSha1());
  144. $validator->setSha1(array('12321', '12121'));
  145. $this->assertEquals(array('12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  146. }
  147. /**
  148. * Ensures that setHash() returns expected value
  149. *
  150. * @return void
  151. */
  152. public function testSetHash()
  153. {
  154. $validator = new Zend_Validate_File_Sha1('12345');
  155. $validator->setHash('12333');
  156. $this->assertEquals(array('12333' => 'sha1'), $validator->getSha1());
  157. $validator->setHash(array('12321', '12121'));
  158. $this->assertEquals(array('12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  159. }
  160. /**
  161. * Ensures that addSha1() returns expected value
  162. *
  163. * @return void
  164. */
  165. public function testAddSha1()
  166. {
  167. $validator = new Zend_Validate_File_Sha1('12345');
  168. $validator->addSha1('12344');
  169. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  170. $validator->addSha1(array('12321', '12121'));
  171. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1', '12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  172. }
  173. /**
  174. * Ensures that addHash() returns expected value
  175. *
  176. * @return void
  177. */
  178. public function testAddHash()
  179. {
  180. $validator = new Zend_Validate_File_Sha1('12345');
  181. $validator->addHash('12344');
  182. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  183. $validator->addHash(array('12321', '12121'));
  184. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1', '12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  185. }
  186. }
  187. // Call Zend_Validate_File_Sha1Test::main() if this source file is executed directly.
  188. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_Sha1Test::main") {
  189. Zend_Validate_File_Sha1Test::main();
  190. }