PageRenderTime 20ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Zend/Form/Element/SubmitTest.php

https://github.com/christophervalles/Zend-Framework-Skeleton
PHP | 286 lines | 178 code | 34 blank | 74 comment | 6 complexity | 69899e1eb65be24e2d25cff8e3f630e6 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: SubmitTest.php 24280 2011-07-28 18:39:33Z matthew $
  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 'Zend/Form/Element/Submit.php';
  27. require_once 'Zend/Form.php';
  28. require_once 'Zend/Registry.php';
  29. require_once 'Zend/Translate.php';
  30. require_once 'Zend/Translate/Adapter/Array.php';
  31. /**
  32. * Test class for Zend_Form_Element_Submit
  33. *
  34. * @category Zend
  35. * @package Zend_Form
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Form
  40. */
  41. class Zend_Form_Element_SubmitTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_SubmitTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Sets up the fixture, for example, open a network connection.
  55. * This method is called before a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. Zend_Registry::_unsetInstance();
  62. Zend_Form::setDefaultTranslator(null);
  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 testLabelWhichIsSetToNameIsTranslatedWhenTranslationAvailable()
  143. {
  144. require_once 'Zend/Translate.php';
  145. $translations = array('foo' => 'This is the Submit Label');
  146. $translate = new Zend_Translate('array', $translations);
  147. $submit = new Zend_Form_Element_Submit('foo');
  148. $submit->setTranslator($translate);
  149. $this->assertEquals($translations['foo'], $submit->getLabel());
  150. }
  151. /**
  152. * @group ZF-8764
  153. */
  154. public function testLabelIsNotTranslatedTwice()
  155. {
  156. require_once 'Zend/Translate.php';
  157. $translations = array('firstLabel' => 'secondLabel',
  158. 'secondLabel' => 'thirdLabel');
  159. $translate = new Zend_Translate('array', $translations);
  160. $submit = new Zend_Form_Element_Submit('foo', 'firstLabel');
  161. $submit->setTranslator($translate);
  162. $this->assertEquals($translations['firstLabel'], $submit->getLabel());
  163. }
  164. public function testIsCheckedReturnsFalseWhenNoValuePresent()
  165. {
  166. $this->assertFalse($this->element->isChecked());
  167. }
  168. public function testIsCheckedReturnsFalseWhenValuePresentButDoesNotMatchLabel()
  169. {
  170. $this->assertFalse($this->element->isChecked());
  171. $this->element->setValue('bar');
  172. $this->assertFalse($this->element->isChecked());
  173. }
  174. public function testIsCheckedReturnsTrueWhenValuePresentAndMatchesLabel()
  175. {
  176. $this->testIsCheckedReturnsFalseWhenNoValuePresent();
  177. $this->element->setValue('foo');
  178. $this->assertTrue($this->element->isChecked());
  179. }
  180. /**
  181. * Tests that the isChecked method works as expected when using a translator.
  182. * @group ZF-4073
  183. */
  184. public function testIsCheckedReturnsExpectedValueWhenUsingTranslator()
  185. {
  186. $translations = array('label' => 'translation');
  187. $translate = new Zend_Translate('array', $translations);
  188. $submit = new Zend_Form_Element_Submit('foo', 'label');
  189. $submit->setTranslator($translate);
  190. $submit->setValue($translations['label']);
  191. $this->assertTrue($submit->isChecked());
  192. $submit->setValue('label');
  193. $this->assertFalse($submit->isChecked());
  194. }
  195. /*
  196. * Tests if title attribute (tooltip) is translated if the default decorators are loaded.
  197. * These decorators should load the Tooltip decorator as the first decorator.
  198. * @group ZF-6151
  199. */
  200. public function testTitleAttributeGetsTranslated()
  201. {
  202. $this->element->setAttrib('title', 'bar');
  203. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  204. $this->element->setTranslator($translator);
  205. $html = $this->element->render(new Zend_View());
  206. $this->assertContains('title', $html);
  207. $this->assertContains('baz', $html);
  208. $this->assertNotContains('bar', $html);
  209. }
  210. public function testTitleAttributeDoesNotGetTranslatedIfTranslatorIsDisabled()
  211. {
  212. $this->element->setAttrib('title', 'bar');
  213. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  214. $this->element->setTranslator($translator);
  215. // now disable translator and see if that works
  216. $this->element->setDisableTranslator(true);
  217. $html = $this->element->render(new Zend_View());
  218. $this->assertContains('title', $html);
  219. $this->assertContains('bar', $html);
  220. $this->assertNotContains('baz', $html);
  221. }
  222. public function testSetDefaultIgnoredToTrueWhenNotDefined()
  223. {
  224. $this->assertTrue($this->element->getIgnore());
  225. }
  226. /**
  227. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  228. *
  229. * @link http://framework.zend.com/issues/browse/ZF-2794
  230. * @return void
  231. */
  232. protected function _checkZf2794()
  233. {
  234. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  235. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  236. }
  237. }
  238. /**
  239. * Prove the fluent interface on Zend_Form_Element_Submit::loadDefaultDecorators
  240. *
  241. * @link http://framework.zend.com/issues/browse/ZF-9913
  242. * @return void
  243. */
  244. public function testFluentInterfaceOnLoadDefaultDecorators()
  245. {
  246. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  247. }
  248. }
  249. // Call Zend_Form_Element_SubmitTest::main() if this source file is executed directly.
  250. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_SubmitTest::main") {
  251. Zend_Form_Element_SubmitTest::main();
  252. }