ImageTest.php 6.8 KB

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