2
0

SubmitTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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-2010 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_SubmitTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_SubmitTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/Form/Element/Submit.php';
  30. require_once 'Zend/Translate.php';
  31. require_once 'Zend/Translate/Adapter/Array.php';
  32. /**
  33. * Test class for Zend_Form_Element_Submit
  34. *
  35. * @category Zend
  36. * @package Zend_Form
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Form
  41. */
  42. class Zend_Form_Element_SubmitTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. require_once "PHPUnit/TextUI/TestRunner.php";
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_SubmitTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Sets up the fixture, for example, open a network connection.
  57. * This method is called before a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. $this->element = new Zend_Form_Element_Submit('foo');
  64. }
  65. /**
  66. * Tears down the fixture, for example, close a network connection.
  67. * This method is called after a test is executed.
  68. *
  69. * @return void
  70. */
  71. public function tearDown()
  72. {
  73. }
  74. public function getView()
  75. {
  76. require_once 'Zend/View.php';
  77. $view = new Zend_View();
  78. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper/');
  79. return $view;
  80. }
  81. public function testSubmitElementSubclassesXhtmlElement()
  82. {
  83. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  84. }
  85. public function testSubmitElementInstanceOfBaseElement()
  86. {
  87. $this->assertTrue($this->element instanceof Zend_Form_Element);
  88. }
  89. public function testSubmitElementUsesViewHelperDecoratorByDefault()
  90. {
  91. $this->_checkZf2794();
  92. $decorator = $this->element->getDecorator('viewHelper');
  93. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  94. }
  95. public function testSubmitElementSpecifiesFormSubmitAsDefaultHelper()
  96. {
  97. $this->assertEquals('formSubmit', $this->element->helper);
  98. }
  99. public function testGetLabelReturnsNameIfNoValuePresent()
  100. {
  101. $this->assertEquals($this->element->getName(), $this->element->getLabel());
  102. }
  103. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  104. {
  105. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  106. $translate = new Zend_Translate('array', $translations, 'en');
  107. $this->element->setTranslator($translate)
  108. ->setLabel('submit');
  109. $test = $this->element->getLabel();
  110. $this->assertEquals($translations['submit'], $test);
  111. }
  112. public function testTranslatedLabelIsRendered()
  113. {
  114. $this->_checkZf2794();
  115. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  116. $this->element->setView($this->getView());
  117. $decorator = $this->element->getDecorator('ViewHelper');
  118. $decorator->setElement($this->element);
  119. $html = $decorator->render('');
  120. $this->assertRegexp('/<(input|button)[^>]*?value="Submit Button"/', $html);
  121. }
  122. public function testConstructorSetsLabelToNameIfNoLabelProvided()
  123. {
  124. $submit = new Zend_Form_Element_Submit('foo');
  125. $this->assertEquals('foo', $submit->getName());
  126. $this->assertEquals('foo', $submit->getLabel());
  127. }
  128. public function testCanPassLabelAsParameterToConstructor()
  129. {
  130. $submit = new Zend_Form_Element_Submit('foo', 'Label');
  131. $this->assertEquals('Label', $submit->getLabel());
  132. }
  133. public function testLabelIsTranslatedWhenTranslationAvailable()
  134. {
  135. require_once 'Zend/Translate.php';
  136. $translations = array('Label' => 'This is the Submit Label');
  137. $translate = new Zend_Translate('array', $translations);
  138. $submit = new Zend_Form_Element_Submit('foo', 'Label');
  139. $submit->setTranslator($translate);
  140. $this->assertEquals($translations['Label'], $submit->getLabel());
  141. }
  142. public function testIsCheckedReturnsFalseWhenNoValuePresent()
  143. {
  144. $this->assertFalse($this->element->isChecked());
  145. }
  146. public function testIsCheckedReturnsFalseWhenValuePresentButDoesNotMatchLabel()
  147. {
  148. $this->assertFalse($this->element->isChecked());
  149. $this->element->setValue('bar');
  150. $this->assertFalse($this->element->isChecked());
  151. }
  152. public function testIsCheckedReturnsTrueWhenValuePresentAndMatchesLabel()
  153. {
  154. $this->testIsCheckedReturnsFalseWhenNoValuePresent();
  155. $this->element->setValue('foo');
  156. $this->assertTrue($this->element->isChecked());
  157. }
  158. /**
  159. * Tests that the isChecked method works as expected when using a translator.
  160. * @group ZF-4073
  161. */
  162. public function testIsCheckedReturnsExpectedValueWhenUsingTranslator()
  163. {
  164. $translations = array('label' => 'translation');
  165. $translate = new Zend_Translate('array', $translations);
  166. $submit = new Zend_Form_Element_Submit('foo', 'label');
  167. $submit->setTranslator($translate);
  168. $submit->setValue($translations['label']);
  169. $this->assertTrue($submit->isChecked());
  170. $submit->setValue('label');
  171. $this->assertFalse($submit->isChecked());
  172. }
  173. /*
  174. * Tests if title attribute (tooltip) is translated if the default decorators are loaded.
  175. * These decorators should load the Tooltip decorator as the first decorator.
  176. * @group ZF-6151
  177. */
  178. public function testTitleAttributeGetsTranslated()
  179. {
  180. $this->element->setAttrib('title', 'bar');
  181. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  182. $this->element->setTranslator($translator);
  183. $html = $this->element->render(new Zend_View());
  184. $this->assertContains('title', $html);
  185. $this->assertContains('baz', $html);
  186. $this->assertNotContains('bar', $html);
  187. }
  188. public function testTitleAttributeDoesNotGetTranslatedIfTranslatorIsDisabled()
  189. {
  190. $this->element->setAttrib('title', 'bar');
  191. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  192. $this->element->setTranslator($translator);
  193. // now disable translator and see if that works
  194. $this->element->setDisableTranslator(true);
  195. $html = $this->element->render(new Zend_View());
  196. $this->assertContains('title', $html);
  197. $this->assertContains('bar', $html);
  198. $this->assertNotContains('baz', $html);
  199. }
  200. public function testSetDefaultIgnoredToTrueWhenNotDefined()
  201. {
  202. $this->assertTrue($this->element->getIgnore());
  203. }
  204. /**
  205. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  206. *
  207. * @link http://framework.zend.com/issues/browse/ZF-2794
  208. * @return void
  209. */
  210. protected function _checkZf2794()
  211. {
  212. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  213. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  214. }
  215. }
  216. }
  217. // Call Zend_Form_Element_SubmitTest::main() if this source file is executed directly.
  218. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_SubmitTest::main") {
  219. Zend_Form_Element_SubmitTest::main();
  220. }