PageRenderTime 772ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/Form/Element/HashTest.php

http://firephp.googlecode.com/
PHP | 233 lines | 170 code | 35 blank | 28 comment | 16 complexity | 478a07c13d8d641a7e59ae42fcf71f90 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  1. <?php
  2. // Call Zend_Form_Element_HashTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_HashTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Form/Element/Hash.php';
  8. /**
  9. * Test class for Zend_Form_Element_Hash
  10. */
  11. class Zend_Form_Element_HashTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * Runs the test methods of this class.
  15. *
  16. * @return void
  17. */
  18. public static function main()
  19. {
  20. require_once "PHPUnit/TextUI/TestRunner.php";
  21. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_HashTest");
  22. $result = PHPUnit_TextUI_TestRunner::run($suite);
  23. }
  24. /**
  25. * Sets up the fixture, for example, open a network connection.
  26. * This method is called before a test is executed.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. if (isset($this->hash)) {
  33. unset($this->hash);
  34. }
  35. $session = new Zend_Form_Element_HashTest_SessionContainer();
  36. $session->hash = null;
  37. $this->element = new Zend_Form_Element_Hash('foo', array(
  38. 'session' => $session,
  39. ));
  40. }
  41. /**
  42. * Tears down the fixture, for example, close a network connection.
  43. * This method is called after a test is executed.
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. }
  50. public function testHashElementSubclassesXhtmlElement()
  51. {
  52. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  53. }
  54. public function testHashElementInstanceOfBaseElement()
  55. {
  56. $this->assertTrue($this->element instanceof Zend_Form_Element);
  57. }
  58. public function testSaltPopulatedByDefault()
  59. {
  60. $salt = $this->element->getSalt();
  61. $this->assertFalse(empty($salt));
  62. }
  63. public function testCanSetSalt()
  64. {
  65. $salt = $this->element->getSalt();
  66. $this->element->setSalt('foobar');
  67. $this->assertNotEquals($salt, $this->element->getSalt());
  68. $this->assertEquals('foobar', $this->element->getSalt());
  69. }
  70. public function testTimeoutPopulatedByDefault()
  71. {
  72. $ttl = $this->element->getTimeout();
  73. $this->assertFalse(empty($ttl));
  74. $this->assertTrue(is_int($ttl));
  75. }
  76. public function testCanSetTimeout()
  77. {
  78. $ttl = $this->element->getTimeout();
  79. $this->element->setTimeout(3600);
  80. $this->assertNotEquals($ttl, $this->element->getTimeout());
  81. $this->assertEquals(3600, $this->element->getTimeout());
  82. }
  83. public function testGetHashReturnsHashValue()
  84. {
  85. $hash = $this->element->getHash();
  86. $this->assertFalse(empty($hash));
  87. $this->assertTrue(is_string($hash));
  88. $this->hash = $hash;
  89. }
  90. public function testGetHashSetsElementValueToHash()
  91. {
  92. $this->testGetHashReturnsHashValue();
  93. $this->assertEquals($this->hash, $this->element->getValue());
  94. }
  95. public function testHashIsMd5()
  96. {
  97. $this->testGetHashReturnsHashValue();
  98. $this->assertEquals(32, strlen($this->hash));
  99. $this->assertRegexp('/^[a-f0-9]{32}$/', $this->hash);
  100. }
  101. public function testLabelIsNull()
  102. {
  103. $this->assertNull($this->element->getLabel());
  104. }
  105. public function testSessionNameContainsSaltAndName()
  106. {
  107. $sessionName = $this->element->getSessionName();
  108. $this->assertContains($this->element->getSalt(), $sessionName);
  109. $this->assertContains($this->element->getName(), $sessionName);
  110. }
  111. public function getView()
  112. {
  113. require_once 'Zend/View.php';
  114. $view = new Zend_View();
  115. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  116. return $view;
  117. }
  118. public function testValidatorTokenReceivesSessionHashWhenPresent()
  119. {
  120. $this->_checkZf2794();
  121. $session = $this->element->getSession();
  122. $session->hash = $this->element->getHash();
  123. $element = new Zend_Form_Element_Hash('foo', array('session' => $session));
  124. $validator = $element->getValidator('Identical');
  125. $this->assertEquals($session->hash, $validator->getToken());
  126. }
  127. public function testRenderInitializesSessionHashToken()
  128. {
  129. $session = $this->element->getSession();
  130. $this->assertNull($session->hash);
  131. $html = $this->element->render($this->getView());
  132. $this->assertEquals($this->element->getHash(), $session->hash);
  133. $this->assertEquals(1, $session->setExpirationHops);
  134. $this->assertEquals($this->element->getTimeout(), $session->setExpirationSeconds);
  135. }
  136. public function testHashTokenIsRendered()
  137. {
  138. $html = $this->element->render($this->getView());
  139. $this->assertContains($this->element->getHash(), $html);
  140. }
  141. public function testHiddenInputRenderedByDefault()
  142. {
  143. $html = $this->element->render($this->getView());
  144. $this->assertRegexp('/<input[^>]*?type="hidden"/', $html, $html);
  145. }
  146. /**
  147. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  148. *
  149. * @link http://framework.zend.com/issues/browse/ZF-2794
  150. * @return void
  151. */
  152. protected function _checkZf2794()
  153. {
  154. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  155. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  156. }
  157. }
  158. }
  159. class Zend_Form_Element_HashTest_SessionContainer
  160. {
  161. protected static $_hash;
  162. public function __get($name)
  163. {
  164. if ('hash' == $name) {
  165. return self::$_hash;
  166. }
  167. return null;
  168. }
  169. public function __set($name, $value)
  170. {
  171. if ('hash' == $name) {
  172. self::$_hash = $value;
  173. } else {
  174. $this->$name = $value;
  175. }
  176. }
  177. public function __isset($name)
  178. {
  179. if (('hash' == $name) && (null !== self::$_hash)) {
  180. return true;
  181. }
  182. return false;
  183. }
  184. public function __call($method, $args)
  185. {
  186. switch ($method) {
  187. case 'setExpirationHops':
  188. case 'setExpirationSeconds':
  189. $this->$method = array_shift($args);
  190. break;
  191. default:
  192. }
  193. }
  194. }
  195. // Call Zend_Form_Element_HashTest::main() if this source file is executed directly.
  196. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_HashTest::main") {
  197. Zend_Form_Element_HashTest::main();
  198. }