EditorTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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-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_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. /** Zend_Dojo_View_Helper_Editor */
  27. require_once 'Zend/Dojo/View/Helper/Editor.php';
  28. /** Zend_View */
  29. require_once 'Zend/View.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /** Zend_Dojo_View_Helper_Dojo */
  33. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  34. /**
  35. * Test class for Zend_Dojo_View_Helper_Editor.
  36. *
  37. * @category Zend
  38. * @package Zend_Dojo
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Dojo
  43. * @group Zend_Dojo_View
  44. */
  45. class Zend_Dojo_View_Helper_EditorTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_EditorTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. /**
  58. * Sets up the fixture, for example, open a network connection.
  59. * This method is called before a test is executed.
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. Zend_Registry::_unsetInstance();
  66. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  67. $this->view = $this->getView();
  68. $this->helper = new Zend_Dojo_View_Helper_Editor();
  69. $this->helper->setView($this->view);
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. }
  80. public function getView()
  81. {
  82. require_once 'Zend/View.php';
  83. $view = new Zend_View();
  84. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  85. return $view;
  86. }
  87. public function testHelperShouldRenderAlteredId()
  88. {
  89. $html = $this->helper->editor('foo');
  90. $this->assertContains('id="foo-Editor"', $html, $html);
  91. }
  92. public function testHelperShouldRenderHiddenElementWithGivenIdentifier()
  93. {
  94. $html = $this->helper->editor('foo');
  95. if (!preg_match('#(<input[^>]*(?:type="hidden")[^>]*>)#', $html, $matches)) {
  96. $this->fail('No hidden element generated');
  97. }
  98. $this->assertContains('id="foo"', $matches[1]);
  99. }
  100. public function testHelperShouldRenderDojoTypeWhenUsedDeclaratively()
  101. {
  102. $html = $this->helper->editor('foo');
  103. $this->assertContains('dojoType="dijit.Editor"', $html);
  104. }
  105. public function testHelperShouldRegisterDijitModule()
  106. {
  107. $html = $this->helper->editor('foo');
  108. $modules = $this->view->dojo()->getModules();
  109. $this->assertContains('dijit.Editor', $modules);
  110. }
  111. public function testHelperShouldNormalizeArrayId()
  112. {
  113. $html = $this->helper->editor('foo[]');
  114. $this->assertContains('id="foo-Editor"', $html, $html);
  115. $html = $this->helper->editor('foo[bar]');
  116. $this->assertContains('id="foo-bar-Editor"', $html, $html);
  117. }
  118. public function testHelperShouldJsonifyPlugins()
  119. {
  120. $plugins = array('copy', 'cut', 'paste');
  121. $html = $this->helper->editor('foo', '', array('plugins' => $plugins));
  122. $pluginsString = Zend_Json::encode($plugins);
  123. $pluginsString = str_replace('"', "'", $pluginsString);
  124. $this->assertContains('plugins="' . $pluginsString . '"', $html);
  125. }
  126. public function testHelperShouldCreateJavascriptToConnectEditorToHiddenValue()
  127. {
  128. $this->helper->editor('foo');
  129. $onLoadActions = $this->view->dojo()->getOnLoadActions();
  130. $found = false;
  131. foreach ($onLoadActions as $action) {
  132. if (strstr($action, "value = dijit.byId('foo-Editor').getValue(false);")) {
  133. $found = true;
  134. break;
  135. }
  136. }
  137. $this->assertTrue($found, var_export($onLoadActions, 1));
  138. }
  139. public function testHelperShouldCreateJavascriptToFindParentForm()
  140. {
  141. $this->helper->editor('foo');
  142. $javascript = $this->view->dojo()->getJavascript();
  143. $found = false;
  144. foreach ($javascript as $action) {
  145. if (strstr($action, "zend.findParentForm = function")) {
  146. $found = true;
  147. break;
  148. }
  149. }
  150. $this->assertTrue($found, var_export($javascript, 1));
  151. }
  152. public function testHelperShouldNotRegisterDojoStylesheet()
  153. {
  154. $this->helper->editor('foo');
  155. $this->assertFalse($this->view->dojo()->registerDojoStylesheet());
  156. }
  157. /**
  158. * @group ZF-4461
  159. */
  160. public function testHelperShouldRegisterPluginModulesWithDojo()
  161. {
  162. $plugins = array(
  163. 'createLink' => 'LinkDialog',
  164. 'fontName' => 'FontChoice',
  165. );
  166. $html = $this->helper->editor('foo', '', array('plugins' => array_keys($plugins)));
  167. $dojo = $this->view->dojo()->__toString();
  168. foreach (array_values($plugins) as $plugin) {
  169. $this->assertContains('dojo.require("dijit._editor.plugins.' . $plugin . '")', $dojo, $dojo);
  170. }
  171. }
  172. /**
  173. * @group ZF-6753
  174. * @group ZF-8127
  175. */
  176. public function testHelperShouldUseDivByDefault()
  177. {
  178. $html = $this->helper->editor('foo');
  179. $this->assertRegexp('#</?div[^>]*>#', $html, $html);
  180. }
  181. /**
  182. * @group ZF-6753
  183. * @group ZF-8127
  184. */
  185. public function testHelperShouldOnlyUseTextareaInNoscriptTag()
  186. {
  187. $html = $this->helper->editor('foo');
  188. $this->assertRegexp('#<noscript><textarea[^>]*>#', $html, $html);
  189. }
  190. /**
  191. * @group ZF-11315
  192. */
  193. public function testHiddenInputShouldBeRenderedLast()
  194. {
  195. $html = $this->helper->editor('foo');
  196. $this->assertRegexp('#</noscript><input#', $html, $html);
  197. }
  198. /** @group ZF-5711 */
  199. public function testHelperShouldJsonifyExtraPlugins()
  200. {
  201. $extraPlugins = array('copy', 'cut', 'paste');
  202. $html = $this->helper->editor('foo', '', array('extraPlugins' => $extraPlugins));
  203. $pluginsString = Zend_Json::encode($extraPlugins);
  204. $pluginsString = str_replace('"', "'", $pluginsString);
  205. $this->assertContains('extraPlugins="' . $pluginsString . '"', $html);
  206. }
  207. }
  208. // Call Zend_Dojo_View_Helper_EditorTest::main() if this source file is executed directly.
  209. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_EditorTest::main") {
  210. Zend_Dojo_View_Helper_EditorTest::main();
  211. }