2
0

ButtonTest.php 6.2 KB

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