PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Validator/File/HashTest.php

https://github.com/Thinkscape/zf2
PHP | 181 lines | 113 code | 19 blank | 49 comment | 3 complexity | 77eb7792547ad0bb575487f9dde68d8c MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Validator\File;
  10. use Zend\Validator\File;
  11. /**
  12. * Hash testbed
  13. *
  14. * @group Zend_Validator
  15. */
  16. class HashTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @return array
  20. */
  21. public function basicBehaviorDataProvider()
  22. {
  23. $testFile = __DIR__ . '/_files/picture.jpg';
  24. $pictureTests = array(
  25. // Options, isValid Param, Expected value, Expected message
  26. array('3f8d07e2', $testFile, true, ''),
  27. array('9f8d07e2', $testFile, false, 'fileHashDoesNotMatch'),
  28. array(array('9f8d07e2', '3f8d07e2'), $testFile, true, ''),
  29. array(array('9f8d07e2', '7f8d07e2'), $testFile, false, 'fileHashDoesNotMatch'),
  30. array(
  31. array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'),
  32. $testFile, true, ''
  33. ),
  34. array(
  35. array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'),
  36. $testFile, false, 'fileHashDoesNotMatch'
  37. ),
  38. array(
  39. array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'),
  40. $testFile, true, ''
  41. ),
  42. array(
  43. array('4d74c22109fe9f110579f77b053b8bc3', '7d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'),
  44. $testFile, false, 'fileHashDoesNotMatch'
  45. ),
  46. );
  47. $testFile = __DIR__ . '/_files/nofile.mo';
  48. $noFileTests = array(
  49. // Options, isValid Param, Expected value, message
  50. array('3f8d07e2', $testFile, false, 'fileHashNotFound'),
  51. );
  52. $testFile = __DIR__ . '/_files/testsize.mo';
  53. $sizeFileTests = array(
  54. // Options, isValid Param, Expected value, message
  55. array('ffeb8d5d', $testFile, true, ''),
  56. array('9f8d07e2', $testFile, false, 'fileHashDoesNotMatch'),
  57. );
  58. // Dupe data in File Upload format
  59. $testData = array_merge($pictureTests, $noFileTests, $sizeFileTests);
  60. foreach ($testData as $data) {
  61. $fileUpload = array(
  62. 'tmp_name' => $data[1], 'name' => basename($data[1]),
  63. 'size' => 200, 'error' => 0, 'type' => 'text'
  64. );
  65. $testData[] = array($data[0], $fileUpload, $data[2], $data[3]);
  66. }
  67. return $testData;
  68. }
  69. /**
  70. * Ensures that the validator follows expected behavior
  71. *
  72. * @dataProvider basicBehaviorDataProvider
  73. * @return void
  74. */
  75. public function testBasic($options, $isValidParam, $expected, $messageKey)
  76. {
  77. $validator = new File\Hash($options);
  78. $this->assertEquals($expected, $validator->isValid($isValidParam));
  79. if (!$expected) {
  80. $this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
  81. }
  82. }
  83. /**
  84. * Ensures that the validator follows expected behavior for legacy Zend\Transfer API
  85. *
  86. * @dataProvider basicBehaviorDataProvider
  87. * @return void
  88. */
  89. public function testLegacy($options, $isValidParam, $expected, $messageKey)
  90. {
  91. if (is_array($isValidParam)) {
  92. $validator = new File\Hash($options);
  93. $this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
  94. if (!$expected) {
  95. $this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
  96. }
  97. }
  98. }
  99. /**
  100. * Ensures that getHash() returns expected value
  101. *
  102. * @return void
  103. */
  104. public function testgetHash()
  105. {
  106. $validator = new File\Hash('12345');
  107. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  108. $validator = new File\Hash(array('12345', '12333', '12344'));
  109. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  110. }
  111. /**
  112. * Ensures that setHash() returns expected value
  113. *
  114. * @return void
  115. */
  116. public function testSetHash()
  117. {
  118. $validator = new File\Hash('12345');
  119. $validator->setHash('12333');
  120. $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
  121. $validator->setHash(array('12321', '12121'));
  122. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  123. }
  124. /**
  125. * Ensures that addHash() returns expected value
  126. *
  127. * @return void
  128. */
  129. public function testAddHash()
  130. {
  131. $validator = new File\Hash('12345');
  132. $validator->addHash('12344');
  133. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  134. $validator->addHash(array('12321', '12121'));
  135. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  136. }
  137. /**
  138. * @group ZF-11258
  139. */
  140. public function testZF11258()
  141. {
  142. $validator = new File\Hash('3f8d07e2');
  143. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  144. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  145. $this->assertContains("does not exist", current($validator->getMessages()));
  146. }
  147. public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage()
  148. {
  149. $validator = new File\Hash();
  150. $this->assertFalse($validator->isValid(''));
  151. $this->assertArrayHasKey(File\Hash::NOT_FOUND, $validator->getMessages());
  152. $filesArray = array(
  153. 'name' => '',
  154. 'size' => 0,
  155. 'tmp_name' => '',
  156. 'error' => UPLOAD_ERR_NO_FILE,
  157. 'type' => '',
  158. );
  159. $this->assertFalse($validator->isValid($filesArray));
  160. $this->assertArrayHasKey(File\Hash::NOT_FOUND, $validator->getMessages());
  161. }
  162. }