EditorTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 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_Dojo_View_Helper_EditorTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_EditorTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_View_Helper_Editor */
  28. require_once 'Zend/Dojo/View/Helper/Editor.php';
  29. /** Zend_View */
  30. require_once 'Zend/View.php';
  31. /** Zend_Registry */
  32. require_once 'Zend/Registry.php';
  33. /** Zend_Dojo_View_Helper_Dojo */
  34. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  35. /**
  36. * Test class for Zend_Dojo_View_Helper_Editor.
  37. *
  38. * @package Zend_Dojo
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Dojo_View_Helper_EditorTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @return void
  49. */
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_EditorTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Sets up the fixture, for example, open a network connection.
  57. * This method is called before a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. Zend_Registry::_unsetInstance();
  64. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  65. $this->view = $this->getView();
  66. $this->helper = new Zend_Dojo_View_Helper_Editor();
  67. $this->helper->setView($this->view);
  68. }
  69. /**
  70. * Tears down the fixture, for example, close a network connection.
  71. * This method is called after a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. }
  78. public function getView()
  79. {
  80. require_once 'Zend/View.php';
  81. $view = new Zend_View();
  82. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  83. return $view;
  84. }
  85. public function testHelperShouldRenderTextareaWithAlteredId()
  86. {
  87. $html = $this->helper->editor('foo');
  88. $this->assertRegexp('#<textarea[^>]*(id="foo-Editor")#', $html, $html);
  89. $this->assertContains('</textarea>', $html);
  90. }
  91. public function testHelperShouldRenderHiddenElementWithGivenIdentifier()
  92. {
  93. $html = $this->helper->editor('foo');
  94. if (!preg_match('#(<input[^>]*(?:type="hidden")[^>]*>)#', $html, $matches)) {
  95. $this->fail('No hidden element generated');
  96. }
  97. $this->assertContains('id="foo"', $matches[1]);
  98. }
  99. public function testHelperShouldRenderDojoTypeWhenUsedDeclaratively()
  100. {
  101. $html = $this->helper->editor('foo');
  102. $this->assertRegexp('#<textarea[^>]*(dojoType="dijit.Editor")#', $html);
  103. }
  104. public function testHelperShouldRegisterDijitModule()
  105. {
  106. $html = $this->helper->editor('foo');
  107. $modules = $this->view->dojo()->getModules();
  108. $this->assertContains('dijit.Editor', $modules);
  109. }
  110. public function testHelperShouldNormalizeArrayName()
  111. {
  112. $html = $this->helper->editor('foo[]');
  113. $this->assertRegexp('#<textarea[^>]*(name="foo\[Editor\]\[\]")#', $html, $html);
  114. $this->assertRegexp('#<textarea[^>]*(id="foo-Editor")#', $html, $html);
  115. $html = $this->helper->editor('foo[bar]');
  116. $this->assertRegexp('#<textarea[^>]*(name="foo\[bar\]\[Editor\]")#', $html, $html);
  117. $this->assertRegexp('#<textarea[^>]*(id="foo-bar-Editor")#', $html, $html);
  118. }
  119. public function testHelperShouldJsonifyPlugins()
  120. {
  121. $plugins = array('copy', 'cut', 'paste');
  122. $html = $this->helper->editor('foo', '', array('plugins' => $plugins));
  123. $pluginsString = Zend_Json::encode($plugins);
  124. $pluginsString = str_replace('"', "'", $pluginsString);
  125. $this->assertRegexp('#<textarea[^>]*(plugins="' . preg_quote($pluginsString) . '")#', $html);
  126. }
  127. public function testHelperShouldCreateJavascriptToConnectTextareaToHiddenValue()
  128. {
  129. $this->helper->editor('foo');
  130. $onLoadActions = $this->view->dojo()->getOnLoadActions();
  131. $found = false;
  132. foreach ($onLoadActions as $action) {
  133. if (strstr($action, "dojo.byId('foo').value = dijit.byId('foo-Editor').getValue(false);")) {
  134. $found = true;
  135. break;
  136. }
  137. }
  138. $this->assertTrue($found, var_export($onLoadActions, 1));
  139. }
  140. public function testHelperShouldCreateJavascriptToFindParentForm()
  141. {
  142. $this->helper->editor('foo');
  143. $javascript = $this->view->dojo()->getJavascript();
  144. $found = false;
  145. foreach ($javascript as $action) {
  146. if (strstr($action, "zend.findParentForm = function")) {
  147. $found = true;
  148. break;
  149. }
  150. }
  151. $this->assertTrue($found, var_export($javascript, 1));
  152. }
  153. public function testHelperShouldNotRegisterDojoStylesheet()
  154. {
  155. $this->helper->editor('foo');
  156. $this->assertFalse($this->view->dojo()->registerDojoStylesheet());
  157. }
  158. }
  159. // Call Zend_Dojo_View_Helper_EditorTest::main() if this source file is executed directly.
  160. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_EditorTest::main") {
  161. Zend_Dojo_View_Helper_EditorTest::main();
  162. }