PasswordTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_PasswordTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_PasswordTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Password.php';
  27. require_once 'Zend/View.php';
  28. /**
  29. * Test class for Zend_Form_Element_Password
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 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_PasswordTest 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. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_PasswordTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, for example, open a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->errors = array();
  59. $this->element = new Zend_Form_Element_Password('foo');
  60. }
  61. /**
  62. * Tears down the fixture, for example, close a network connection.
  63. * This method is called after a test is executed.
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. }
  70. public function testPasswordElementSubclassesXhtmlElement()
  71. {
  72. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  73. }
  74. public function testPasswordElementInstanceOfBaseElement()
  75. {
  76. $this->assertTrue($this->element instanceof Zend_Form_Element);
  77. }
  78. public function testHelperAttributeSetToFormPasswordByDefault()
  79. {
  80. $this->assertEquals('formPassword', $this->element->getAttrib('helper'));
  81. }
  82. public function testPasswordElementUsesPasswordHelperInViewHelperDecoratorByDefault()
  83. {
  84. $this->_checkZf2794();
  85. $decorator = $this->element->getDecorator('viewHelper');
  86. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  87. $decorator->setElement($this->element);
  88. $helper = $decorator->getHelper();
  89. $this->assertEquals('formPassword', $helper);
  90. }
  91. public function testPasswordValueMaskedByGetMessages()
  92. {
  93. $this->element->addValidators(array(
  94. 'Alpha',
  95. 'Alnum'
  96. ));
  97. $value = 'abc-123';
  98. $expect = '*******';
  99. $this->assertFalse($this->element->isValid($value));
  100. foreach ($this->element->getMessages() as $message) {
  101. $this->assertNotContains($value, $message);
  102. $this->assertContains($expect, $message, $message);
  103. }
  104. }
  105. public function handleErrors($errno, $errmsg, $errfile, $errline, $errcontext)
  106. {
  107. if (!isset($this->errors)) {
  108. $this->errors = array();
  109. }
  110. $this->errors[] = $errmsg;
  111. }
  112. /**
  113. * ZF-2656
  114. */
  115. public function testGetMessagesReturnsEmptyArrayWhenNoMessagesRegistered()
  116. {
  117. set_error_handler(array($this, 'handleErrors'));
  118. $messages = $this->element->getMessages();
  119. restore_error_handler();
  120. $this->assertSame(array(), $messages);
  121. $this->assertTrue(empty($this->errors));
  122. }
  123. /**
  124. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  125. *
  126. * @link http://framework.zend.com/issues/browse/ZF-2794
  127. * @return void
  128. */
  129. protected function _checkZf2794()
  130. {
  131. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  132. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  133. }
  134. }
  135. public function testRenderPasswordAttributeShouldDefaultToFalse()
  136. {
  137. $this->assertFalse($this->element->renderPassword());
  138. }
  139. public function testShouldAllowSettingRenderPasswordFlag()
  140. {
  141. $this->testRenderPasswordAttributeShouldDefaultToFalse();
  142. $this->element->setRenderPassword(true);
  143. $this->assertTrue($this->element->renderPassword());
  144. $this->element->setRenderPassword(false);
  145. $this->assertFalse($this->element->renderPassword());
  146. }
  147. public function testShouldPassRenderPasswordAttributeToViewHelper()
  148. {
  149. $this->element->setValue('foobar')
  150. ->setView(new Zend_View());
  151. $test = $this->element->render();
  152. $this->assertContains('value=""', $test);
  153. $this->element->setRenderPassword(true);
  154. $test = $this->element->render();
  155. $this->assertContains('value="foobar"', $test);
  156. }
  157. }
  158. // Call Zend_Form_Element_PasswordTest::main() if this source file is executed directly.
  159. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_PasswordTest::main") {
  160. Zend_Form_Element_PasswordTest::main();
  161. }