ImageTest.php 5.9 KB

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