PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pcelta/zf2
PHP | 155 lines | 99 code | 17 blank | 39 comment | 0 complexity | 5ec74fa4288c0a0c9230881dea82b51b 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Validator
  9. */
  10. namespace ZendTest\Validator\File;
  11. use Zend\Validator\File;
  12. /**
  13. * Hash testbed
  14. *
  15. * @category Zend
  16. * @package Zend_Validator_File
  17. * @subpackage UnitTests
  18. * @group Zend_Validator
  19. */
  20. class HashTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * Ensures that the validator follows expected behavior
  24. *
  25. * @return void
  26. */
  27. public function testBasic()
  28. {
  29. $valuesExpected = array(
  30. array('3f8d07e2', true),
  31. array('9f8d07e2', false),
  32. array(array('9f8d07e2', '3f8d07e2'), true),
  33. array(array('9f8d07e2', '7f8d07e2'), false),
  34. );
  35. foreach ($valuesExpected as $element) {
  36. $validator = new File\Hash($element[0]);
  37. $this->assertEquals(
  38. $element[1],
  39. $validator->isValid(__DIR__ . '/_files/picture.jpg'),
  40. "Tested with " . var_export($element, 1)
  41. );
  42. }
  43. $valuesExpected = array(
  44. array(array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  45. array(array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  46. array(array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  47. array(array('1d74c22109fe9f110579f77b053b8bc3', '4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  48. );
  49. foreach ($valuesExpected as $element) {
  50. $validator = new File\Hash($element[0]);
  51. $this->assertEquals(
  52. $element[1],
  53. $validator->isValid(__DIR__ . '/_files/picture.jpg'),
  54. "Tested with " . var_export($element, 1)
  55. );
  56. }
  57. $validator = new File\Hash('3f8d07e2');
  58. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  59. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  60. $files = array(
  61. 'name' => 'test1',
  62. 'type' => 'text',
  63. 'size' => 200,
  64. 'tmp_name' => 'tmp_test1',
  65. 'error' => 0
  66. );
  67. $validator = new File\Hash('3f8d07e2');
  68. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo', $files));
  69. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  70. $files = array(
  71. 'name' => 'testsize.mo',
  72. 'type' => 'text',
  73. 'size' => 200,
  74. 'tmp_name' => __DIR__ . '/_files/testsize.mo',
  75. 'error' => 0
  76. );
  77. $validator = new File\Hash('3f8d07e2');
  78. $this->assertTrue($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
  79. $files = array(
  80. 'name' => 'testsize.mo',
  81. 'type' => 'text',
  82. 'size' => 200,
  83. 'tmp_name' => __DIR__ . '/_files/testsize.mo',
  84. 'error' => 0
  85. );
  86. $validator = new File\Hash('9f8d07e2');
  87. $this->assertFalse($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
  88. $this->assertTrue(array_key_exists('fileHashDoesNotMatch', $validator->getMessages()));
  89. }
  90. /**
  91. * Ensures that getHash() returns expected value
  92. *
  93. * @return void
  94. */
  95. public function testgetHash()
  96. {
  97. $validator = new File\Hash('12345');
  98. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  99. $validator = new File\Hash(array('12345', '12333', '12344'));
  100. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  101. }
  102. /**
  103. * Ensures that setHash() returns expected value
  104. *
  105. * @return void
  106. */
  107. public function testSetHash()
  108. {
  109. $validator = new File\Hash('12345');
  110. $validator->setHash('12333');
  111. $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
  112. $validator->setHash(array('12321', '12121'));
  113. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  114. }
  115. /**
  116. * Ensures that addHash() returns expected value
  117. *
  118. * @return void
  119. */
  120. public function testAddHash()
  121. {
  122. $validator = new File\Hash('12345');
  123. $validator->addHash('12344');
  124. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  125. $validator->addHash(array('12321', '12121'));
  126. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  127. }
  128. /**
  129. * @group ZF-11258
  130. */
  131. public function testZF11258()
  132. {
  133. $validator = new File\Hash('3f8d07e2');
  134. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  135. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  136. $this->assertContains("'nofile.mo'", current($validator->getMessages()));
  137. }
  138. }