EditorTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-2010 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. * @category Zend
  39. * @package Zend_Dojo
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Dojo
  44. * @group Zend_Dojo_View
  45. */
  46. class Zend_Dojo_View_Helper_EditorTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_EditorTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. Zend_Registry::_unsetInstance();
  67. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  68. $this->view = $this->getView();
  69. $this->helper = new Zend_Dojo_View_Helper_Editor();
  70. $this->helper->setView($this->view);
  71. }
  72. /**
  73. * Tears down the fixture, for example, close a network connection.
  74. * This method is called after a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. }
  81. public function getView()
  82. {
  83. require_once 'Zend/View.php';
  84. $view = new Zend_View();
  85. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  86. return $view;
  87. }
  88. public function testHelperShouldRenderAlteredId()
  89. {
  90. $html = $this->helper->editor('foo');
  91. $this->assertContains('id="foo-Editor"', $html, $html);
  92. }
  93. public function testHelperShouldRenderHiddenElementWithGivenIdentifier()
  94. {
  95. $html = $this->helper->editor('foo');
  96. if (!preg_match('#(<input[^>]*(?:type="hidden")[^>]*>)#', $html, $matches)) {
  97. $this->fail('No hidden element generated');
  98. }
  99. $this->assertContains('id="foo"', $matches[1]);
  100. }
  101. public function testHelperShouldRenderDojoTypeWhenUsedDeclaratively()
  102. {
  103. $html = $this->helper->editor('foo');
  104. $this->assertContains('dojoType="dijit.Editor"', $html);
  105. }
  106. public function testHelperShouldRegisterDijitModule()
  107. {
  108. $html = $this->helper->editor('foo');
  109. $modules = $this->view->dojo()->getModules();
  110. $this->assertContains('dijit.Editor', $modules);
  111. }
  112. public function testHelperShouldNormalizeArrayId()
  113. {
  114. $html = $this->helper->editor('foo[]');
  115. $this->assertContains('id="foo-Editor"', $html, $html);
  116. $html = $this->helper->editor('foo[bar]');
  117. $this->assertContains('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->assertContains('plugins="' . $pluginsString . '"', $html);
  126. }
  127. public function testHelperShouldCreateJavascriptToConnectEditorToHiddenValue()
  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. * @group ZF-4461
  160. */
  161. public function testHelperShouldRegisterPluginModulesWithDojo()
  162. {
  163. $plugins = array(
  164. 'createLink' => 'LinkDialog',
  165. 'fontName' => 'FontChoice',
  166. );
  167. $html = $this->helper->editor('foo', '', array('plugins' => array_keys($plugins)));
  168. $dojo = $this->view->dojo()->__toString();
  169. foreach (array_values($plugins) as $plugin) {
  170. $this->assertContains('dojo.require("dijit._editor.plugins.' . $plugin . '")', $dojo, $dojo);
  171. }
  172. }
  173. /**
  174. * @group ZF-6753
  175. * @group ZF-8127
  176. */
  177. public function testHelperShouldUseDivByDefault()
  178. {
  179. $html = $this->helper->editor('foo');
  180. $this->assertRegexp('#</?div[^>]*>#', $html, $html);
  181. }
  182. /**
  183. * @group ZF-6753
  184. * @group ZF-8127
  185. */
  186. public function testHelperShouldOnlyUseTextareaInNoscriptTag()
  187. {
  188. $html = $this->helper->editor('foo');
  189. $this->assertRegexp('#<noscript><textarea[^>]*>#', $html, $html);
  190. }
  191. }
  192. // Call Zend_Dojo_View_Helper_EditorTest::main() if this source file is executed directly.
  193. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_EditorTest::main") {
  194. Zend_Dojo_View_Helper_EditorTest::main();
  195. }