HashTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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_Form_Element_HashTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_HashTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Hash.php';
  27. /**
  28. * Test class for Zend_Form_Element_Hash
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class Zend_Form_Element_HashTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_HashTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. if (isset($this->hash)) {
  58. unset($this->hash);
  59. }
  60. $session = new Zend_Form_Element_HashTest_SessionContainer();
  61. $session->hash = null;
  62. $this->element = new Zend_Form_Element_Hash('foo', array(
  63. 'session' => $session,
  64. ));
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. }
  75. public function testHashElementSubclassesXhtmlElement()
  76. {
  77. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  78. }
  79. public function testHashElementInstanceOfBaseElement()
  80. {
  81. $this->assertTrue($this->element instanceof Zend_Form_Element);
  82. }
  83. public function testSaltPopulatedByDefault()
  84. {
  85. $salt = $this->element->getSalt();
  86. $this->assertFalse(empty($salt));
  87. }
  88. public function testCanSetSalt()
  89. {
  90. $salt = $this->element->getSalt();
  91. $this->element->setSalt('foobar');
  92. $this->assertNotEquals($salt, $this->element->getSalt());
  93. $this->assertEquals('foobar', $this->element->getSalt());
  94. }
  95. public function testTimeoutPopulatedByDefault()
  96. {
  97. $ttl = $this->element->getTimeout();
  98. $this->assertFalse(empty($ttl));
  99. $this->assertTrue(is_int($ttl));
  100. }
  101. public function testCanSetTimeout()
  102. {
  103. $ttl = $this->element->getTimeout();
  104. $this->element->setTimeout(3600);
  105. $this->assertNotEquals($ttl, $this->element->getTimeout());
  106. $this->assertEquals(3600, $this->element->getTimeout());
  107. }
  108. public function testGetHashReturnsHashValue()
  109. {
  110. $hash = $this->element->getHash();
  111. $this->assertFalse(empty($hash));
  112. $this->assertTrue(is_string($hash));
  113. $this->hash = $hash;
  114. }
  115. public function testGetHashSetsElementValueToHash()
  116. {
  117. $this->testGetHashReturnsHashValue();
  118. $this->assertEquals($this->hash, $this->element->getValue());
  119. }
  120. public function testHashIsMd5()
  121. {
  122. $this->testGetHashReturnsHashValue();
  123. $this->assertEquals(32, strlen($this->hash));
  124. $this->assertRegexp('/^[a-f0-9]{32}$/', $this->hash);
  125. }
  126. public function testLabelIsNull()
  127. {
  128. $this->assertNull($this->element->getLabel());
  129. }
  130. public function testSessionNameContainsSaltAndName()
  131. {
  132. $sessionName = $this->element->getSessionName();
  133. $this->assertContains($this->element->getSalt(), $sessionName);
  134. $this->assertContains($this->element->getName(), $sessionName);
  135. }
  136. public function getView()
  137. {
  138. require_once 'Zend/View.php';
  139. $view = new Zend_View();
  140. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  141. return $view;
  142. }
  143. public function testValidatorTokenReceivesSessionHashWhenPresent()
  144. {
  145. $this->_checkZf2794();
  146. $session = $this->element->getSession();
  147. $session->hash = $this->element->getHash();
  148. $element = new Zend_Form_Element_Hash('foo', array('session' => $session));
  149. $validator = $element->getValidator('Identical');
  150. $this->assertEquals($session->hash, $validator->getToken());
  151. }
  152. public function testRenderInitializesSessionHashToken()
  153. {
  154. $session = $this->element->getSession();
  155. $this->assertNull($session->hash);
  156. $html = $this->element->render($this->getView());
  157. $this->assertEquals($this->element->getHash(), $session->hash);
  158. $this->assertEquals(1, $session->setExpirationHops);
  159. $this->assertEquals($this->element->getTimeout(), $session->setExpirationSeconds);
  160. }
  161. public function testHashTokenIsRendered()
  162. {
  163. $html = $this->element->render($this->getView());
  164. $this->assertContains($this->element->getHash(), $html);
  165. }
  166. public function testHiddenInputRenderedByDefault()
  167. {
  168. $html = $this->element->render($this->getView());
  169. $this->assertRegexp('/<input[^>]*?type="hidden"/', $html, $html);
  170. }
  171. /**
  172. * @group ZF-7404
  173. */
  174. public function testShouldRenderHashTokenIfRenderedThroughMagicCall()
  175. {
  176. $this->element->setView($this->getView());
  177. $html = $this->element->renderViewHelper();
  178. $this->assertContains($this->element->getHash(), $html, 'Html is: ' . $html);
  179. }
  180. /**
  181. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  182. *
  183. * @link http://framework.zend.com/issues/browse/ZF-2794
  184. * @return void
  185. */
  186. protected function _checkZf2794()
  187. {
  188. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  189. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  190. }
  191. }
  192. }
  193. class Zend_Form_Element_HashTest_SessionContainer
  194. {
  195. protected static $_hash;
  196. public function __get($name)
  197. {
  198. if ('hash' == $name) {
  199. return self::$_hash;
  200. }
  201. return null;
  202. }
  203. public function __set($name, $value)
  204. {
  205. if ('hash' == $name) {
  206. self::$_hash = $value;
  207. } else {
  208. $this->$name = $value;
  209. }
  210. }
  211. public function __isset($name)
  212. {
  213. if (('hash' == $name) && (null !== self::$_hash)) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. public function __call($method, $args)
  219. {
  220. switch ($method) {
  221. case 'setExpirationHops':
  222. case 'setExpirationSeconds':
  223. $this->$method = array_shift($args);
  224. break;
  225. default:
  226. }
  227. }
  228. }
  229. // Call Zend_Form_Element_HashTest::main() if this source file is executed directly.
  230. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_HashTest::main") {
  231. Zend_Form_Element_HashTest::main();
  232. }