SubmitTest.php 8.6 KB

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