2
0

SubmitTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // Call Zend_Form_Element_SubmitTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_SubmitTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Form/Element/Submit.php';
  10. require_once 'Zend/Translate.php';
  11. require_once 'Zend/Translate/Adapter/Array.php';
  12. /**
  13. * Test class for Zend_Form_Element_Submit
  14. */
  15. class Zend_Form_Element_SubmitTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Runs the test methods of this class.
  19. *
  20. * @return void
  21. */
  22. public static function main()
  23. {
  24. require_once "PHPUnit/TextUI/TestRunner.php";
  25. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_SubmitTest");
  26. $result = PHPUnit_TextUI_TestRunner::run($suite);
  27. }
  28. /**
  29. * Sets up the fixture, for example, open a network connection.
  30. * This method is called before a test is executed.
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. $this->element = new Zend_Form_Element_Submit('foo');
  37. }
  38. /**
  39. * Tears down the fixture, for example, close a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. }
  47. public function getView()
  48. {
  49. require_once 'Zend/View.php';
  50. $view = new Zend_View();
  51. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper/');
  52. return $view;
  53. }
  54. public function testSubmitElementSubclassesXhtmlElement()
  55. {
  56. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  57. }
  58. public function testSubmitElementInstanceOfBaseElement()
  59. {
  60. $this->assertTrue($this->element instanceof Zend_Form_Element);
  61. }
  62. public function testSubmitElementUsesViewHelperDecoratorByDefault()
  63. {
  64. $this->_checkZf2794();
  65. $decorator = $this->element->getDecorator('viewHelper');
  66. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  67. }
  68. public function testSubmitElementSpecifiesFormSubmitAsDefaultHelper()
  69. {
  70. $this->assertEquals('formSubmit', $this->element->helper);
  71. }
  72. public function testGetLabelReturnsNameIfNoValuePresent()
  73. {
  74. $this->assertEquals($this->element->getName(), $this->element->getLabel());
  75. }
  76. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  77. {
  78. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  79. $translate = new Zend_Translate('array', $translations, 'en');
  80. $this->element->setTranslator($translate)
  81. ->setLabel('submit');
  82. $test = $this->element->getLabel();
  83. $this->assertEquals($translations['submit'], $test);
  84. }
  85. public function testTranslatedLabelIsRendered()
  86. {
  87. $this->_checkZf2794();
  88. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  89. $this->element->setView($this->getView());
  90. $decorator = $this->element->getDecorator('ViewHelper');
  91. $decorator->setElement($this->element);
  92. $html = $decorator->render('');
  93. $this->assertRegexp('/<(input|button)[^>]*?value="Submit Button"/', $html);
  94. }
  95. public function testConstructorSetsLabelToNameIfNoLabelProvided()
  96. {
  97. $submit = new Zend_Form_Element_Submit('foo');
  98. $this->assertEquals('foo', $submit->getName());
  99. $this->assertEquals('foo', $submit->getLabel());
  100. }
  101. public function testCanPassLabelAsParameterToConstructor()
  102. {
  103. $submit = new Zend_Form_Element_Submit('foo', 'Label');
  104. $this->assertEquals('Label', $submit->getLabel());
  105. }
  106. public function testLabelIsTranslatedWhenTranslationAvailable()
  107. {
  108. require_once 'Zend/Translate.php';
  109. $translations = array('Label' => 'This is the Submit Label');
  110. $translate = new Zend_Translate('array', $translations);
  111. $submit = new Zend_Form_Element_Submit('foo', 'Label');
  112. $submit->setTranslator($translate);
  113. $this->assertEquals($translations['Label'], $submit->getLabel());
  114. }
  115. public function testIsCheckedReturnsFalseWhenNoValuePresent()
  116. {
  117. $this->assertFalse($this->element->isChecked());
  118. }
  119. public function testIsCheckedReturnsFalseWhenValuePresentButDoesNotMatchLabel()
  120. {
  121. $this->assertFalse($this->element->isChecked());
  122. $this->element->setValue('bar');
  123. $this->assertFalse($this->element->isChecked());
  124. }
  125. public function testIsCheckedReturnsTrueWhenValuePresentAndMatchesLabel()
  126. {
  127. $this->testIsCheckedReturnsFalseWhenNoValuePresent();
  128. $this->element->setValue('foo');
  129. $this->assertTrue($this->element->isChecked());
  130. }
  131. /*
  132. * Tests if title attribute (tooltip) is translated if the default decorators are loaded.
  133. * These decorators should load the Tooltip decorator as the first decorator.
  134. * @group ZF-6151
  135. */
  136. public function testTitleAttributeGetsTranslated()
  137. {
  138. $this->element->setAttrib('title', 'bar');
  139. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  140. $this->element->setTranslator($translator);
  141. $html = $this->element->render(new Zend_View());
  142. $this->assertContains('title', $html);
  143. $this->assertContains('baz', $html);
  144. $this->assertNotContains('bar', $html);
  145. }
  146. public function testTitleAttributeDoesNotGetTranslatedIfTranslatorIsDisabled()
  147. {
  148. $this->element->setAttrib('title', 'bar');
  149. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  150. $this->element->setTranslator($translator);
  151. // now disable translator and see if that works
  152. $this->element->setDisableTranslator(true);
  153. $html = $this->element->render(new Zend_View());
  154. $this->assertContains('title', $html);
  155. $this->assertContains('bar', $html);
  156. $this->assertNotContains('baz', $html);
  157. }
  158. /**
  159. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  160. *
  161. * @link http://framework.zend.com/issues/browse/ZF-2794
  162. * @return void
  163. */
  164. protected function _checkZf2794()
  165. {
  166. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  167. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  168. }
  169. }
  170. }
  171. // Call Zend_Form_Element_SubmitTest::main() if this source file is executed directly.
  172. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_SubmitTest::main") {
  173. Zend_Form_Element_SubmitTest::main();
  174. }