SubmitButtonTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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_Dojo_Form_Element_SubmitButtonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_Form_Element_SubmitButtonTest::main");
  25. }
  26. /** Zend_Dojo_Form_Element_SubmitButton */
  27. require_once 'Zend/Dojo/Form/Element/SubmitButton.php';
  28. /** Zend_View */
  29. require_once 'Zend/View.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /** Zend_Translate */
  33. require_once 'Zend/Translate.php';
  34. /** Zend_Dojo_View_Helper_Dojo */
  35. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  36. /**
  37. * Test class for Zend_Dojo_Form_Element_SubmitButton.
  38. *
  39. * @category Zend
  40. * @package Zend_Dojo
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Dojo
  45. * @group Zend_Dojo_Form
  46. */
  47. class Zend_Dojo_Form_Element_SubmitButtonTest extends PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_Form_Element_SubmitButtonTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Sets up the fixture, for example, open a network connection.
  61. * This method is called before a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. Zend_Registry::_unsetInstance();
  68. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  69. $this->view = $this->getView();
  70. $this->element = $this->getElement();
  71. $this->element->setView($this->view);
  72. }
  73. /**
  74. * Tears down the fixture, for example, close a network connection.
  75. * This method is called after a test is executed.
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. }
  82. public function getView()
  83. {
  84. require_once 'Zend/View.php';
  85. $view = new Zend_View();
  86. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  87. return $view;
  88. }
  89. public function getElement()
  90. {
  91. $element = new Zend_Dojo_Form_Element_SubmitButton('foo');
  92. return $element;
  93. }
  94. public function testGetLabelReturnsNameIfNoValuePresent()
  95. {
  96. $this->assertEquals($this->element->getName(), $this->element->getLabel());
  97. }
  98. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  99. {
  100. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  101. $translate = new Zend_Translate('array', $translations, 'en');
  102. $this->element->setTranslator($translate)
  103. ->setLabel('submit');
  104. $test = $this->element->getLabel();
  105. $this->assertEquals($translations['submit'], $test);
  106. }
  107. public function testTranslatedLabelIsRendered()
  108. {
  109. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  110. $this->element->setView($this->getView());
  111. $decorator = $this->element->getDecorator('DijitElement');
  112. $decorator->setElement($this->element);
  113. $html = $decorator->render('');
  114. $this->assertRegexp('/<(input|button)[^>]*?(value="Submit Button")/', $html, 'Label: ' . $this->element->getLabel() . "\nHTML: " . $html);
  115. }
  116. public function testConstructorSetsLabelToNameIfNoLabelProvided()
  117. {
  118. $button = new Zend_Dojo_Form_Element_SubmitButton('foo');
  119. $this->assertEquals('foo', $button->getName());
  120. $this->assertEquals('foo', $button->getLabel());
  121. }
  122. public function testCanPassLabelAsParameterToConstructor()
  123. {
  124. $button = new Zend_Dojo_Form_Element_SubmitButton('foo', 'Label');
  125. $this->assertEquals('Label', $button->getLabel());
  126. }
  127. public function testLabelIsTranslatedWhenTranslationAvailable()
  128. {
  129. require_once 'Zend/Translate.php';
  130. $translations = array('Label' => 'This is the Submit Label');
  131. $translate = new Zend_Translate('array', $translations);
  132. $button = new Zend_Dojo_Form_Element_SubmitButton('foo', 'Label');
  133. $button->setTranslator($translate);
  134. $this->assertEquals($translations['Label'], $button->getLabel());
  135. }
  136. public function testIsCheckedReturnsFalseWhenNoValuePresent()
  137. {
  138. $this->assertFalse($this->element->isChecked());
  139. }
  140. public function testIsCheckedReturnsFalseWhenValuePresentButDoesNotMatchLabel()
  141. {
  142. $this->assertFalse($this->element->isChecked());
  143. $this->element->setValue('bar');
  144. $this->assertFalse($this->element->isChecked());
  145. }
  146. public function testIsCheckedReturnsTrueWhenValuePresentAndMatchesLabel()
  147. {
  148. $this->testIsCheckedReturnsFalseWhenNoValuePresent();
  149. $this->element->setValue('foo');
  150. $this->assertTrue($this->element->isChecked());
  151. }
  152. public function testShouldRenderButtonDijit()
  153. {
  154. $html = $this->element->render();
  155. $this->assertContains('dojoType="dijit.form.Button"', $html);
  156. }
  157. public function testShouldRenderSubmitInput()
  158. {
  159. $html = $this->element->render();
  160. $this->assertContains('type="submit"', $html);
  161. }
  162. /**
  163. * @group ZF-4977
  164. */
  165. public function testElementShouldRenderLabelAsInputValue()
  166. {
  167. $this->element->setLabel('Label!');
  168. $html = $this->element->render();
  169. $this->assertRegexp('/<input[^>]*(value="Label!")/', $html, $html);
  170. }
  171. }
  172. // Call Zend_Dojo_Form_Element_SubmitButtonTest::main() if this source file is executed directly.
  173. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_Form_Element_SubmitButtonTest::main") {
  174. Zend_Dojo_Form_Element_SubmitButtonTest::main();
  175. }