ButtonTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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-2014 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_ButtonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_ButtonTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Button.php';
  27. require_once 'Zend/Translate.php';
  28. /**
  29. * Test class for Zend_Form_Element_Button
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2014 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_ButtonTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Form_Element_Button
  42. */
  43. protected $element;
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_ButtonTest");
  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->element = new Zend_Form_Element_Button('foo');
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. }
  73. public function getView()
  74. {
  75. require_once 'Zend/View.php';
  76. $view = new Zend_View();
  77. return $view;
  78. }
  79. public function testButtonElementSubclassesSubmitElement()
  80. {
  81. $this->assertTrue($this->element instanceof Zend_Form_Element_Submit);
  82. }
  83. public function testButtonElementSubclassesXhtmlElement()
  84. {
  85. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  86. }
  87. public function testButtonElementInstanceOfBaseElement()
  88. {
  89. $this->assertTrue($this->element instanceof Zend_Form_Element);
  90. }
  91. public function testHelperAttributeSetToFormButtonByDefault()
  92. {
  93. $this->assertEquals('formButton', $this->element->getAttrib('helper'));
  94. }
  95. public function testButtonElementUsesButtonHelperInViewHelperDecoratorByDefault()
  96. {
  97. $this->_checkZf2794();
  98. $decorator = $this->element->getDecorator('viewHelper');
  99. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  100. $decorator->setElement($this->element);
  101. $helper = $decorator->getHelper();
  102. $this->assertEquals('formButton', $helper);
  103. }
  104. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  105. {
  106. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  107. $translate = new Zend_Translate('array', $translations, 'en');
  108. $this->element->setTranslator($translate)
  109. ->setLabel('submit');
  110. $test = $this->element->getLabel();
  111. $this->assertEquals($translations['submit'], $test);
  112. }
  113. public function testTranslatedLabelIsRendered()
  114. {
  115. $this->_checkZf2794();
  116. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  117. $this->element->setView($this->getView());
  118. $decorator = $this->element->getDecorator('ViewHelper');
  119. $decorator->setElement($this->element);
  120. $html = $decorator->render('');
  121. $this->assertRegexp('/<(input|button)[^>]*?>Submit Button/', $html, $html);
  122. }
  123. /**
  124. * @group ZF-3961
  125. */
  126. public function testValuePropertyShouldNotBeRendered()
  127. {
  128. $this->element->setLabel('Button Label')
  129. ->setView($this->getView());
  130. $html = $this->element->render();
  131. $this->assertContains('Button Label', $html, $html);
  132. $this->assertNotContains('value="', $html);
  133. }
  134. public function testSetDefaultIgnoredToTrueWhenNotDefined()
  135. {
  136. $this->assertTrue($this->element->getIgnore());
  137. }
  138. /**
  139. * @group ZF-5056
  140. */
  141. public function testValidateAlwaysReturnsTrue()
  142. {
  143. $this->element->setValue('foo');
  144. $this->assertTrue($this->element->isValid('bar'));
  145. }
  146. /**
  147. * @group ZF-5056
  148. */
  149. public function testRenderingWithValueAfterValidation()
  150. {
  151. // Set element options
  152. $this->element->setOptions(
  153. array(
  154. 'label' => 'Foo',
  155. 'value' => 'bar',
  156. 'decorators' => array(
  157. 'ViewHelper',
  158. ),
  159. )
  160. );
  161. // Validate
  162. $this->element->isValid(null);
  163. $this->assertEquals(
  164. PHP_EOL . '<button name="foo" id="foo" type="button" value="bar">Foo</button>',
  165. $this->element->render($this->getView())
  166. );
  167. }
  168. /**
  169. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  170. *
  171. * @link http://framework.zend.com/issues/browse/ZF-2794
  172. * @return void
  173. */
  174. protected function _checkZf2794()
  175. {
  176. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  177. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  178. }
  179. }
  180. }
  181. // Call Zend_Form_Element_ButtonTest::main() if this source file is executed directly.
  182. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_ButtonTest::main") {
  183. Zend_Form_Element_ButtonTest::main();
  184. }