PasswordTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // Call Zend_Form_Element_PasswordTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_PasswordTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Form/Element/Password.php';
  10. require_once 'Zend/View.php';
  11. /**
  12. * Test class for Zend_Form_Element_Password
  13. */
  14. class Zend_Form_Element_PasswordTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Runs the test methods of this class.
  18. *
  19. * @return void
  20. */
  21. public static function main()
  22. {
  23. require_once "PHPUnit/TextUI/TestRunner.php";
  24. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_PasswordTest");
  25. $result = PHPUnit_TextUI_TestRunner::run($suite);
  26. }
  27. /**
  28. * Sets up the fixture, for example, open a network connection.
  29. * This method is called before a test is executed.
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. $this->errors = array();
  36. $this->element = new Zend_Form_Element_Password('foo');
  37. }
  38. /**
  39. * Tears down the fixture, for example, close a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. }
  47. public function testPasswordElementSubclassesXhtmlElement()
  48. {
  49. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  50. }
  51. public function testPasswordElementInstanceOfBaseElement()
  52. {
  53. $this->assertTrue($this->element instanceof Zend_Form_Element);
  54. }
  55. public function testHelperAttributeSetToFormPasswordByDefault()
  56. {
  57. $this->assertEquals('formPassword', $this->element->getAttrib('helper'));
  58. }
  59. public function testPasswordElementUsesPasswordHelperInViewHelperDecoratorByDefault()
  60. {
  61. $this->_checkZf2794();
  62. $decorator = $this->element->getDecorator('viewHelper');
  63. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  64. $decorator->setElement($this->element);
  65. $helper = $decorator->getHelper();
  66. $this->assertEquals('formPassword', $helper);
  67. }
  68. public function testPasswordValueMaskedByGetMessages()
  69. {
  70. $this->element->addValidators(array(
  71. 'Alpha',
  72. 'Alnum'
  73. ));
  74. $value = 'abc-123';
  75. $expect = '*******';
  76. $this->assertFalse($this->element->isValid($value));
  77. foreach ($this->element->getMessages() as $message) {
  78. $this->assertNotContains($value, $message);
  79. $this->assertContains($expect, $message, $message);
  80. }
  81. }
  82. public function handleErrors($errno, $errmsg, $errfile, $errline, $errcontext)
  83. {
  84. if (!isset($this->errors)) {
  85. $this->errors = array();
  86. }
  87. $this->errors[] = $errmsg;
  88. }
  89. /**
  90. * ZF-2656
  91. */
  92. public function testGetMessagesReturnsEmptyArrayWhenNoMessagesRegistered()
  93. {
  94. set_error_handler(array($this, 'handleErrors'));
  95. $messages = $this->element->getMessages();
  96. restore_error_handler();
  97. $this->assertSame(array(), $messages);
  98. $this->assertTrue(empty($this->errors));
  99. }
  100. /**
  101. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  102. *
  103. * @link http://framework.zend.com/issues/browse/ZF-2794
  104. * @return void
  105. */
  106. protected function _checkZf2794()
  107. {
  108. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  109. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  110. }
  111. }
  112. public function testRenderPasswordAttributeShouldDefaultToFalse()
  113. {
  114. $this->assertFalse($this->element->renderPassword());
  115. }
  116. public function testShouldAllowSettingRenderPasswordFlag()
  117. {
  118. $this->testRenderPasswordAttributeShouldDefaultToFalse();
  119. $this->element->setRenderPassword(true);
  120. $this->assertTrue($this->element->renderPassword());
  121. $this->element->setRenderPassword(false);
  122. $this->assertFalse($this->element->renderPassword());
  123. }
  124. public function testShouldPassRenderPasswordAttributeToViewHelper()
  125. {
  126. $this->element->setValue('foobar')
  127. ->setView(new Zend_View());
  128. $test = $this->element->render();
  129. $this->assertContains('value=""', $test);
  130. $this->element->setRenderPassword(true);
  131. $test = $this->element->render();
  132. $this->assertContains('value="foobar"', $test);
  133. }
  134. }
  135. // Call Zend_Form_Element_PasswordTest::main() if this source file is executed directly.
  136. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_PasswordTest::main") {
  137. Zend_Form_Element_PasswordTest::main();
  138. }