PasswordTest.php 5.8 KB

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