SubmitButtonTest.php 6.4 KB

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