ImageTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2015 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_ImageTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_ImageTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Image.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/Translate/Adapter/Array.php';
  29. /**
  30. * Test class for Zend_Form_Element_Image
  31. *
  32. * @category Zend
  33. * @package Zend_Form
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Form
  38. */
  39. class Zend_Form_Element_ImageTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_ImageTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->element = new Zend_Form_Element_Image('foo');
  60. }
  61. /**
  62. * Tears down the fixture, for example, close a network connection.
  63. * This method is called after a test is executed.
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. }
  70. public function testImageElementSubclassesXhtmlElement()
  71. {
  72. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  73. }
  74. public function testImageElementInstanceOfBaseElement()
  75. {
  76. $this->assertTrue($this->element instanceof Zend_Form_Element);
  77. }
  78. public function testImageElementUsesImageDecoratorByDefault()
  79. {
  80. $this->_checkZf2794();
  81. $decorator = $this->element->getDecorator('Image');
  82. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Image);
  83. }
  84. /**
  85. * ZF-2717
  86. */
  87. public function testImageShouldSetHelperPropertyToFormImageByDefault()
  88. {
  89. $this->assertEquals('formImage', $this->element->helper);
  90. }
  91. public function testImageSourceValueNullByDefault()
  92. {
  93. $this->assertNull($this->element->getImage());
  94. $this->assertNull($this->element->src);
  95. }
  96. public function testCanSetImageSourceViaAccessors()
  97. {
  98. $this->element->setImage('foo.gif');
  99. $this->assertEquals('foo.gif', $this->element->getImage());
  100. $this->assertEquals('foo.gif', $this->element->src);
  101. }
  102. public function testImageSourceUsedWhenRenderingImage()
  103. {
  104. $this->testCanSetImageSourceViaAccessors();
  105. $html = $this->element->render(new Zend_View());
  106. $this->assertContains('src="foo.gif"', $html);
  107. }
  108. public function testHelperAttributeNotRenderedWhenRenderingImage()
  109. {
  110. $this->testCanSetImageSourceViaAccessors();
  111. $html = $this->element->render(new Zend_View());
  112. $this->assertNotContains('helper="', $html);
  113. }
  114. public function testValueEmptyWhenRenderingImageByDefault()
  115. {
  116. $this->testCanSetImageSourceViaAccessors();
  117. $html = $this->element->render(new Zend_View());
  118. if (!strstr($html, 'value="')) {
  119. return;
  120. }
  121. $this->assertContains('value=""', $html);
  122. }
  123. public function testLabelUsedAsAltAttribute()
  124. {
  125. $this->element->setLabel('Foo Bar');
  126. $html = $this->element->render(new Zend_View());
  127. $this->assertRegexp('#<input[^>]*alt="Foo Bar"#', $html);
  128. }
  129. public function testImageValueRenderedAsElementValue()
  130. {
  131. $this->element->setImageValue('foo')
  132. ->setImage('foo.gif');
  133. $html = $this->element->render(new Zend_View());
  134. $this->assertRegexp('#<input[^>]*value="foo"#', $html, $html);
  135. }
  136. public function testIsCheckedReturnsSetValueMatchesImageValue()
  137. {
  138. $this->assertFalse($this->element->isChecked());
  139. $this->element->setImageValue('foo');
  140. $this->assertFalse($this->element->isChecked());
  141. $this->element->setValue('foo');
  142. $this->assertTrue($this->element->isChecked());
  143. $this->element->setValue('bar');
  144. $this->assertFalse($this->element->isChecked());
  145. }
  146. /*
  147. * Tests if title attribute (tooltip) is translated if the default decorators are loaded.
  148. * These decorators should load the Tooltip decorator as the first decorator.
  149. * @group ZF-6151
  150. */
  151. public function testTitleAttributeGetsTranslated()
  152. {
  153. $this->element->setAttrib('title', 'bar');
  154. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  155. $this->element->setTranslator($translator);
  156. $html = $this->element->render(new Zend_View());
  157. $this->assertContains('title', $html);
  158. $this->assertContains('baz', $html);
  159. $this->assertNotContains('bar', $html);
  160. }
  161. public function testTitleAttributeDoesNotGetTranslatedIfTranslatorIsDisabled()
  162. {
  163. $this->element->setAttrib('title', 'bar');
  164. $translator = new Zend_Translate_Adapter_Array(array("bar" => "baz"), 'de');
  165. $this->element->setTranslator($translator);
  166. // now disable translator and see if that works
  167. $this->element->setDisableTranslator(true);
  168. $html = $this->element->render(new Zend_View());
  169. $this->assertContains('title', $html);
  170. $this->assertContains('bar', $html);
  171. $this->assertNotContains('baz', $html);
  172. }
  173. /**
  174. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  175. *
  176. * @link http://framework.zend.com/issues/browse/ZF-2794
  177. * @return void
  178. */
  179. protected function _checkZf2794()
  180. {
  181. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  182. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  183. }
  184. }
  185. /**
  186. * Prove the fluent interface on Zend_Form_Element_Image::loadDefaultDecorators
  187. *
  188. * @link http://framework.zend.com/issues/browse/ZF-9913
  189. * @return void
  190. */
  191. public function testFluentInterfaceOnLoadDefaultDecorators()
  192. {
  193. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  194. }
  195. }
  196. // Call Zend_Form_Element_ImageTest::main() if this source file is executed directly.
  197. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_ImageTest::main") {
  198. Zend_Form_Element_ImageTest::main();
  199. }