HashTest.php 7.7 KB

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