DojoTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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_DojoTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_DojoTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_View_Helper_Dojo */
  28. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  29. /** Zend_Dojo_View_Helper_Dojo_Container */
  30. require_once 'Zend/Dojo/View/Helper/Dojo/Container.php';
  31. /** Zend_Dojo */
  32. require_once 'Zend/Dojo.php';
  33. /** Zend_View */
  34. require_once 'Zend/View.php';
  35. /**
  36. * Test class for Zend_Dojo_View_Helper_Dojo.
  37. *
  38. * @category Zend
  39. * @package Zend_Dojo
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Dojo_View_Helper_DojoTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_DojoTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Sets up the fixture, for example, open a network connection.
  58. * This method is called before a test is executed.
  59. *
  60. * @return void
  61. */
  62. public function setUp()
  63. {
  64. Zend_Registry::_unsetInstance();
  65. $this->view = $this->getView();
  66. $this->helper = new Zend_Dojo_View_Helper_Dojo_Container();
  67. $this->helper->setView($this->view);
  68. Zend_Registry::set('Zend_Dojo_View_Helper_Dojo', $this->helper);
  69. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  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 testViewPropertyShouldBeNullByDefault()
  88. {
  89. $helper = new Zend_Dojo_View_Helper_Dojo();
  90. $this->assertNull($helper->view);
  91. }
  92. public function testShouldBeAbleToSetViewProperty()
  93. {
  94. $this->assertTrue($this->helper->view instanceof Zend_View_Interface);
  95. }
  96. public function testNoModulesShouldBeRegisteredByDefault()
  97. {
  98. $modules = $this->helper->getModules();
  99. $this->assertTrue(empty($modules));
  100. }
  101. public function testShouldBeAbleToRequireModules()
  102. {
  103. $this->helper->requireModule('foo.bar');
  104. $modules = $this->helper->getModules();
  105. $this->assertContains('foo.bar', $modules);
  106. }
  107. /**
  108. * @group ZF-3914
  109. */
  110. public function testShouldAllowRequiringMultipleModulesAtOnce()
  111. {
  112. $modules = array('foo.bar', 'bar.baz', 'baz.bat');
  113. $this->helper->requireModule($modules);
  114. $test = $this->helper->getModules();
  115. foreach ($modules as $module) {
  116. $this->assertTrue(in_array($module, $test));
  117. }
  118. }
  119. public function testInvalidModuleNameShouldThrowExceptionDuringRegistration()
  120. {
  121. try {
  122. $this->helper->requireModule('foo#$!bar');
  123. $this->fail('Invalid module name should throw exception during registration');
  124. } catch (Zend_Dojo_View_Exception $e) {
  125. $this->assertContains('invalid character', $e->getMessage());
  126. }
  127. }
  128. /**
  129. * @group ZF-3916
  130. */
  131. public function testRequireModuleShouldAllowDashAndUnderscoreCharacters()
  132. {
  133. $this->helper->requireModule('dojox.highlight.language._www');
  134. $this->helper->requireModule('dojo.NodeList-fx');
  135. }
  136. public function testShouldNotRegisterDuplicateModules()
  137. {
  138. $this->helper->requireModule('foo.bar');
  139. $this->helper->requireModule('foo.bar');
  140. $modules = $this->helper->getModules();
  141. $this->assertContains('foo.bar', $modules);
  142. $this->assertEquals(1, count($modules));
  143. }
  144. public function testModulePathsShouldBeEmptyByDefault()
  145. {
  146. $paths = $this->helper->getModulePaths();
  147. $this->assertTrue(empty($paths));
  148. }
  149. public function testShouldBeAbleToRegisterModulePaths()
  150. {
  151. $this->helper->registerModulePath('custom', '../custom');
  152. $paths = $this->helper->getModulePaths();
  153. $this->assertTrue(array_key_exists('custom', $paths), var_export($paths, 1));
  154. $this->assertContains('../custom', $paths);
  155. }
  156. public function testShouldNotBeAbleToRegisterDuplicateModulePaths()
  157. {
  158. $this->helper->registerModulePath('custom', '../custom');
  159. $this->helper->registerModulePath('custom', '../custom');
  160. $paths = $this->helper->getModulePaths();
  161. $this->assertEquals(1, count($paths));
  162. $this->assertTrue(array_key_exists('custom', $paths));
  163. $this->assertContains('../custom', $paths);
  164. }
  165. public function testShouldBeDisabledByDefault()
  166. {
  167. $this->assertFalse($this->helper->isEnabled());
  168. }
  169. public function testCallingAUseMethodShouldEnableHelper()
  170. {
  171. $this->testShouldBeDisabledByDefault();
  172. $this->helper->setCdnVersion('1.0');
  173. $this->assertTrue($this->helper->isEnabled());
  174. $this->helper->disable();
  175. $this->assertFalse($this->helper->isEnabled());
  176. $this->helper->setLocalPath('/js/dojo/dojo.js');
  177. $this->assertTrue($this->helper->isEnabled());
  178. }
  179. public function testShouldUtilizeCdnByDefault()
  180. {
  181. $this->helper->enable();
  182. $this->assertTrue($this->helper->useCdn());
  183. }
  184. public function testShouldUseGoogleCdnByDefault()
  185. {
  186. $this->assertEquals(Zend_Dojo::CDN_BASE_GOOGLE, $this->helper->getCdnBase());
  187. }
  188. public function testShouldAllowSpecifyingCdnBasePath()
  189. {
  190. $this->testShouldUseGoogleCdnByDefault();
  191. $this->helper->setCdnBase(Zend_Dojo::CDN_BASE_AOL);
  192. $this->assertEquals(Zend_Dojo::CDN_BASE_AOL, $this->helper->getCdnBase());
  193. }
  194. public function testShouldUseLatestVersionWhenUsingCdnByDefault()
  195. {
  196. $this->helper->enable();
  197. $this->assertEquals('1.2.0', $this->helper->getCdnVersion());
  198. }
  199. public function testShouldAllowSpecifyingDojoVersionWhenUtilizingCdn()
  200. {
  201. $this->helper->setCdnVersion('1.0');
  202. $this->assertEquals('1.0', $this->helper->getCdnVersion());
  203. }
  204. public function testShouldUseAolCdnDojoPathByDefault()
  205. {
  206. $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_AOL, $this->helper->getCdnDojoPath());
  207. }
  208. public function testShouldAllowSpecifyingCdnDojoPath()
  209. {
  210. $this->testShouldUseAolCdnDojoPathByDefault();
  211. $this->helper->setCdnDojoPath(Zend_Dojo::CDN_DOJO_PATH_GOOGLE);
  212. $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_GOOGLE, $this->helper->getCdnDojoPath());
  213. }
  214. public function testShouldAllowSpecifyingLocalDojoInstall()
  215. {
  216. $this->helper->setLocalPath('/js/dojo/dojo.js');
  217. $this->assertTrue($this->helper->useLocalPath());
  218. }
  219. public function testShouldAllowSpecifyingDjConfig()
  220. {
  221. $this->helper->setDjConfig(array('parseOnLoad' => 'true'));
  222. $config = $this->helper->getDjConfig();
  223. $this->assertTrue(is_array($config));
  224. $this->assertTrue(array_key_exists('parseOnLoad', $config));
  225. $this->assertEquals('true', $config['parseOnLoad']);
  226. }
  227. public function testShouldAllowRetrievingIndividualDjConfigKeys()
  228. {
  229. $this->helper->setDjConfigOption('parseOnLoad', 'true');
  230. $this->assertEquals('true', $this->helper->getDjConfigOption('parseOnLoad'));
  231. }
  232. public function testGetDjConfigShouldReturnEmptyArrayByDefault()
  233. {
  234. $this->assertSame(array(), $this->helper->getDjConfig());
  235. }
  236. public function testGetDjConfigOptionShouldReturnNullWhenKeyDoesNotExist()
  237. {
  238. $this->assertNull($this->helper->getDjConfigOption('bogus'));
  239. }
  240. public function testGetDjConfigOptionShouldAllowSpecifyingDefaultValue()
  241. {
  242. $this->assertEquals('bar', $this->helper->getDjConfigOption('foo', 'bar'));
  243. }
  244. public function testDjConfigShouldSerializeToJson()
  245. {
  246. $this->helper->setDjConfigOption('parseOnLoad', true)
  247. ->enable();
  248. $html = $this->helper->__toString();
  249. $this->assertContains('var djConfig = ', $html, var_export($html, 1));
  250. $this->assertContains('"parseOnLoad":', $html, $html);
  251. }
  252. public function testShouldAllowSpecifyingStylesheetByModuleName()
  253. {
  254. $this->helper->addStylesheetModule('dijit.themes.tundra');
  255. $stylesheets = $this->helper->getStylesheetModules();
  256. $this->assertContains('dijit.themes.tundra', $stylesheets);
  257. }
  258. public function testDuplicateStylesheetModulesShouldNotBeAllowed()
  259. {
  260. $this->helper->addStylesheetModule('dijit.themes.tundra');
  261. $stylesheets = $this->helper->getStylesheetModules();
  262. $this->assertContains('dijit.themes.tundra', $stylesheets);
  263. $this->helper->addStylesheetModule('dijit.themes.tundra');
  264. $stylesheets = $this->helper->getStylesheetModules();
  265. $this->assertEquals(1, count($stylesheets));
  266. $this->assertContains('dijit.themes.tundra', $stylesheets);
  267. }
  268. /**
  269. * @group ZF-3916
  270. */
  271. public function testAddingStylesheetModuleShouldAllowDashAndUnderscoreCharacters()
  272. {
  273. $this->helper->addStylesheetModule('dojox._highlight.pygments');
  274. $this->helper->addStylesheetModule('dojo.NodeList-fx.styles');
  275. }
  276. public function testInvalidStylesheetModuleNameShouldThrowException()
  277. {
  278. try {
  279. $this->helper->addStylesheetModule('foo/bar/baz');
  280. $this->fail('invalid module designation should throw exception');
  281. } catch (Zend_Dojo_View_Exception $e) {
  282. $this->assertContains('Invalid', $e->getMessage());
  283. }
  284. }
  285. public function testRenderingModuleStylesheetShouldProperlyCreatePaths()
  286. {
  287. $this->helper->enable()
  288. ->addStylesheetModule('dijit.themes.tundra');
  289. $html = $this->helper->__toString();
  290. $this->assertContains('dijit/themes/tundra/tundra.css', $html);
  291. }
  292. public function testShouldAllowSpecifyingLocalStylesheet()
  293. {
  294. $this->helper->addStylesheet('/css/foo.css');
  295. $css = $this->helper->getStylesheets();
  296. $this->assertTrue(is_array($css));
  297. $this->assertContains('/css/foo.css', $css);
  298. }
  299. public function testShouldNotAllowSpecifyingDuplicateLocalStylesheets()
  300. {
  301. $this->testShouldAllowSpecifyingLocalStylesheet();
  302. $this->helper->addStylesheet('/css/foo.css');
  303. $css = $this->helper->getStylesheets();
  304. $this->assertTrue(is_array($css));
  305. $this->assertEquals(1, count($css));
  306. $this->assertContains('/css/foo.css', $css);
  307. }
  308. public function testShouldAllowSpecifyingOnLoadFunctionPointer()
  309. {
  310. $this->helper->addOnLoad('foo');
  311. $onLoad = $this->helper->getOnLoadActions();
  312. $this->assertTrue(is_array($onLoad));
  313. $this->assertEquals(1, count($onLoad));
  314. $action = array_shift($onLoad);
  315. $this->assertTrue(is_string($action));
  316. $this->assertEquals('foo', $action);
  317. }
  318. public function testShouldAllowCapturingOnLoadActions()
  319. {
  320. $this->helper->onLoadCaptureStart(); ?>
  321. function() {
  322. bar();
  323. baz();
  324. }
  325. <?php $this->helper->onLoadCaptureEnd();
  326. $onLoad = $this->helper->getOnLoadActions();
  327. $this->assertTrue(is_array($onLoad));
  328. $this->assertEquals(1, count($onLoad));
  329. $action = array_shift($onLoad);
  330. $this->assertTrue(is_string($action));
  331. $this->assertContains('function() {', $action);
  332. $this->assertContains('bar();', $action);
  333. $this->assertContains('baz();', $action);
  334. }
  335. public function testShouldNotAllowSpecifyingDuplicateOnLoadActions()
  336. {
  337. $this->helper->addOnLoad('foo');
  338. $this->helper->addOnLoad('foo');
  339. $onLoad = $this->helper->getOnLoadActions();
  340. $this->assertTrue(is_array($onLoad));
  341. $this->assertEquals(1, count($onLoad));
  342. $action = array_shift($onLoad);
  343. $this->assertEquals('foo', $action);
  344. }
  345. public function testDojoMethodShouldReturnContainer()
  346. {
  347. $helper = new Zend_Dojo_View_Helper_Dojo();
  348. $this->assertSame($this->helper, $helper->dojo());
  349. }
  350. public function testHelperStorageShouldPersistBetweenViewObjects()
  351. {
  352. $view1 = $this->getView();
  353. $dojo1 = $view1->getHelper('dojo');
  354. $view2 = $this->getView();
  355. $dojo2 = $view1->getHelper('dojo');
  356. $this->assertSame($dojo1, $dojo2);
  357. }
  358. public function testSerializingToStringShouldReturnEmptyStringByDefault()
  359. {
  360. $this->assertEquals('', $this->helper->__toString());
  361. }
  362. public function testEnablingHelperShouldCauseStringSerializationToWork()
  363. {
  364. $this->setupDojo();
  365. $html = $this->helper->__toString();
  366. $doc = new DOMDocument;
  367. $doc->loadHTML($html);
  368. $xPath = new DOMXPath($doc);
  369. $results = $xPath->query('//script');
  370. $this->assertEquals(3, $results->length);
  371. for ($i = 0; $i < 3; ++$i) {
  372. $script = $doc->saveXML($results->item($i));
  373. switch ($i) {
  374. case 0:
  375. $this->assertContains('var djConfig = ', $script);
  376. $this->assertContains('parseOnLoad', $script);
  377. break;
  378. case 1:
  379. $this->assertRegexp('#src="http://.+/dojo/[0-9.]+/dojo/dojo.xd.js"#', $script);
  380. $this->assertContains('/>', $script);
  381. break;
  382. case 2:
  383. $this->assertContains('dojo.registerModulePath("custom", "../custom")', $script, $script);
  384. $this->assertContains('dojo.require("dijit.layout.ContentPane")', $script, $script);
  385. $this->assertContains('dojo.require("custom.foo")', $script, $script);
  386. $this->assertContains('dojo.addOnLoad(foo)', $script, $script);
  387. break;
  388. }
  389. }
  390. $results = $xPath->query('//style');
  391. $this->assertEquals(1, $results->length, $html);
  392. $style = $doc->saveXML($results->item(0));
  393. $this->assertContains('@import', $style);
  394. $this->assertEquals(2, substr_count($style, '@import'));
  395. $this->assertEquals(1, substr_count($style, 'http://ajax.googleapis.com/ajax/libs/dojo/'), $style);
  396. $this->assertContains('css/custom.css', $style);
  397. $this->assertContains('dijit/themes/tundra/tundra.css', $style);
  398. }
  399. public function testStringSerializationShouldBeDoctypeAware()
  400. {
  401. $view = $this->getView();
  402. $view->doctype('HTML4_LOOSE');
  403. $this->helper->setView($view);
  404. $this->setupDojo();
  405. $html = $this->helper->__toString();
  406. $this->assertRegexp('|<style [^>]*>[\r\n]+\s*<!--|', $html);
  407. $this->assertRegexp('|<script [^>]*>[\r\n]+\s*//<!--|', $html);
  408. $this->helper = new Zend_Dojo_View_Helper_Dojo();
  409. $view->doctype('XHTML1_STRICT');
  410. $this->helper->setView($view);
  411. $this->setupDojo();
  412. $html = $this->helper->__toString();
  413. /**
  414. * @todo should stylesheets be escaped as CDATA when isXhtml()?
  415. */
  416. $this->assertRegexp('|<style [^>]*>[\r\n]+\s*<!--|', $html);
  417. $this->assertRegexp('|<script [^>]*>[\r\n]+\s*//<!\[CDATA\[|', $html);
  418. }
  419. public function testDojoHelperContainerPersistsBetweenViewObjects()
  420. {
  421. $this->setupDojo();
  422. $view = $this->getView();
  423. $this->assertNotSame($this->view, $view);
  424. $helper = $view->dojo();
  425. $this->assertSame($this->helper, $helper);
  426. }
  427. public function testShouldUseProgrammaticDijitCreationByDefault()
  428. {
  429. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  430. }
  431. public function testShouldAllowSpecifyingDeclarativeDijitCreation()
  432. {
  433. $this->testShouldUseProgrammaticDijitCreationByDefault();
  434. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  435. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useDeclarative());
  436. }
  437. public function testShouldAllowSpecifyingProgrammaticDijitCreationWithNoScriptGeneration()
  438. {
  439. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
  440. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  441. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  442. }
  443. public function testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams()
  444. {
  445. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  446. $dijits = $this->helper->getDijits();
  447. $this->assertTrue(is_array($dijits));
  448. $this->assertEquals(1, count($dijits));
  449. $dijit = array_shift($dijits);
  450. $this->assertTrue(is_array($dijit));
  451. $this->assertEquals(2, count($dijit));
  452. $this->assertTrue(array_key_exists('id', $dijit));
  453. $this->assertTrue(array_key_exists('params', $dijit));
  454. $this->assertEquals('foo', $dijit['id']);
  455. $this->assertTrue(is_array($dijit['params']));
  456. $this->assertEquals(1, count($dijit['params']));
  457. $this->assertTrue(array_key_exists('dojoType', $dijit['params']));
  458. $this->assertEquals('dijit.form.Form', $dijit['params']['dojoType']);
  459. }
  460. /**
  461. * @expectedException Zend_Dojo_View_Exception
  462. */
  463. public function testAddingDuplicateProgrammaticDijitsShouldRaiseExceptions()
  464. {
  465. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  466. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.ComboBox'));
  467. }
  468. public function testSettingProgrammaticDijitsShouldOverwriteExistingDijits()
  469. {
  470. $this->testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams();
  471. $this->helper->setDijit('foo', array('dojoType' => 'dijit.form.ComboBox'));
  472. $dijits = $this->helper->getDijits();
  473. $this->assertTrue(is_array($dijits));
  474. $this->assertEquals(1, count($dijits));
  475. $dijit = array_shift($dijits);
  476. $this->assertEquals('dijit.form.ComboBox', $dijit['params']['dojoType']);
  477. }
  478. public function testShouldAllowAddingMultipleDijitsAtOnce()
  479. {
  480. $dijits = array(
  481. 'foo' => array(
  482. 'dojoType' => 'dijit.form.Form'
  483. ),
  484. 'bar' => array(
  485. 'dojoType' => 'dijit.form.TextBox',
  486. ),
  487. );
  488. $this->helper->addDijits($dijits);
  489. $test = $this->helper->getDijits();
  490. $this->assertTrue(is_array($test));
  491. $this->assertEquals(2, count($test));
  492. $keys = array();
  493. foreach ($test as $dijit) {
  494. $keys[] = $dijit['id'];
  495. }
  496. $this->assertSame(array_keys($dijits), $keys);
  497. }
  498. public function testSettingMultipleDijitsAtOnceShouldOverwriteAllDijits()
  499. {
  500. $this->testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams();
  501. $dijits = array(
  502. 'bar' => array(
  503. 'dojoType' => 'dijit.form.Form'
  504. ),
  505. 'baz' => array(
  506. 'dojoType' => 'dijit.form.TextBox',
  507. ),
  508. );
  509. $this->helper->setDijits($dijits);
  510. $test = $this->helper->getDijits();
  511. $this->assertTrue(is_array($test));
  512. $this->assertEquals(2, count($test));
  513. $keys = array();
  514. foreach ($test as $dijit) {
  515. $keys[] = $dijit['id'];
  516. }
  517. $this->assertSame(array_keys($dijits), $keys);
  518. }
  519. public function testRetrievingDijitsByIdShouldReturnJustParams()
  520. {
  521. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  522. $params = $this->helper->getDijit('foo');
  523. $this->assertTrue(is_array($params));
  524. $this->assertEquals(1, count($params), var_export($params, 1));
  525. $this->assertTrue(array_key_exists('dojoType', $params));
  526. $this->assertEquals('dijit.form.Form', $params['dojoType']);
  527. }
  528. public function testShouldAllowRemovingIndividualDijits()
  529. {
  530. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  531. $dijits = $this->helper->getDijits();
  532. $this->assertTrue(is_array($dijits));
  533. $this->assertEquals(1, count($dijits));
  534. $this->helper->removeDijit('foo');
  535. $dijits = $this->helper->getDijits();
  536. $this->assertTrue(is_array($dijits));
  537. $this->assertEquals(0, count($dijits));
  538. }
  539. public function testShouldAllowClearingAllDijits()
  540. {
  541. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  542. $this->helper->clearDijits();
  543. $dijits = $this->helper->getDijits();
  544. $this->assertTrue(is_array($dijits));
  545. $this->assertEquals(0, count($dijits));
  546. }
  547. public function testShouldAllowRetrievingDijitsAsJsonArray()
  548. {
  549. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  550. $json = $this->helper->dijitsToJson();
  551. $array = Zend_Json::decode($json);
  552. $this->assertTrue(is_array($array));
  553. $keys = array();
  554. foreach ($array as $dijit) {
  555. $keys[] = $dijit['id'];
  556. $this->assertTrue(array_key_exists('params', $dijit));
  557. $this->assertTrue(is_array($dijit['params']));
  558. }
  559. $this->assertSame(array('foo', 'bar'), $keys);
  560. }
  561. public function testRenderingShouldCreateZendDijitsObjectAndAddOnloadForDijitsWhenDijitsArePresent()
  562. {
  563. $this->helper->enable();
  564. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  565. $json = $this->helper->dijitsToJson();
  566. $html = $this->helper->__toString();
  567. $this->assertContains($json, $html, $html);
  568. $found = false;
  569. foreach ($this->helper->getOnLoadActions() as $action) {
  570. if (strstr($action, 'dojo.mixin')) {
  571. $found = true;
  572. break;
  573. }
  574. }
  575. $this->assertTrue($found, 'Dijit onload action not created');
  576. $this->assertContains($action, $html);
  577. }
  578. public function testShouldAllowAddingArbitraryJsToPrimaryDojoScriptTag()
  579. {
  580. $this->helper->enable();
  581. $this->helper->addJavascript('var foo = "bar";');
  582. $html = $this->helper->__toString();
  583. $found = false;
  584. if (preg_match_all('|<script[^>]*>(.*?)(</script>)|s', $html, $m)) {
  585. foreach ($m[1] as $script) {
  586. if (strstr($script, 'var foo = "bar";')) {
  587. $found = true;
  588. break;
  589. }
  590. }
  591. }
  592. $this->assertTrue($found, 'Js not found: ' . $html);
  593. }
  594. public function testShouldAllowClearingArbitraryJsStack()
  595. {
  596. $this->testShouldAllowAddingArbitraryJsToPrimaryDojoScriptTag();
  597. $this->helper->clearJavascript();
  598. $js = $this->helper->getJavascript();
  599. $this->assertTrue(is_array($js));
  600. $this->assertEquals(0, count($js));
  601. }
  602. public function testShouldNotAllowAddingDuplicateArbitraryJsToPrimaryDojoScriptTag()
  603. {
  604. $this->helper->addJavascript('var foo = "bar";');
  605. $this->helper->addJavascript('var foo = "bar";');
  606. $js = $this->helper->getJavascript();
  607. $this->assertTrue(is_array($js));
  608. $this->assertEquals(1, count($js), var_export($js, 1));
  609. $this->assertEquals('var foo = "bar";', $js[0]);
  610. }
  611. public function testShouldAllowCapturingArbitraryJsToPrimaryDojoScriptTag()
  612. {
  613. $this->helper->javascriptCaptureStart();
  614. echo 'var foo = "bar";';
  615. $this->helper->javascriptCaptureEnd();
  616. $js = $this->helper->getJavascript();
  617. $this->assertEquals(1, count($js));
  618. $this->assertContains('var foo = "bar";', $js[0]);
  619. }
  620. public function testNoLayersShouldBeRegisteredByDefault()
  621. {
  622. $layers = $this->helper->getLayers();
  623. $this->assertTrue(is_array($layers));
  624. $this->assertTrue(empty($layers));
  625. }
  626. public function testShouldAllowAddingLayers()
  627. {
  628. $this->testNoLayersShouldBeRegisteredByDefault();
  629. $this->helper->addLayer('/js/foo/foo.xd.js');
  630. $layers = $this->helper->getLayers();
  631. $this->assertEquals(1, count($layers));
  632. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  633. $this->helper->addLayer('/js/bar/bar.xd.js');
  634. $layers = $this->helper->getLayers();
  635. $this->assertEquals(2, count($layers));
  636. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  637. $this->assertEquals('/js/bar/bar.xd.js', $layers[1]);
  638. }
  639. public function testShouldNotAllowDuplicateLayers()
  640. {
  641. $this->testShouldAllowAddingLayers();
  642. $this->helper->addLayer('/js/foo/foo.xd.js');
  643. $layers = $this->helper->getLayers();
  644. $this->assertEquals(2, count($layers));
  645. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  646. $this->assertEquals('/js/bar/bar.xd.js', $layers[1]);
  647. }
  648. public function testShouldAllowRemovingLayers()
  649. {
  650. $this->testShouldAllowAddingLayers();
  651. $this->helper->removeLayer('/js/foo/foo.xd.js');
  652. $layers = $this->helper->getLayers();
  653. $this->assertEquals(1, count($layers));
  654. $this->assertEquals('/js/bar/bar.xd.js', $layers[0]);
  655. }
  656. public function testShouldAllowClearingLayers()
  657. {
  658. $this->testShouldAllowAddingLayers();
  659. $this->helper->clearLayers();
  660. $layers = $this->helper->getLayers();
  661. $this->assertTrue(is_array($layers));
  662. $this->assertTrue(empty($layers));
  663. }
  664. public function testShouldRenderScriptTagsWithLayersWhenLayersAreRegistered()
  665. {
  666. $this->setupDojo();
  667. $this->testShouldAllowAddingLayers();
  668. $html = $this->helper->__toString();
  669. $doc = new DOMDocument;
  670. $doc->loadHTML($html);
  671. $xPath = new DOMXPath($doc);
  672. $results = $xPath->query('//script');
  673. $found = array();
  674. for ($i = 0; $i < $results->length; ++$i) {
  675. $script = $doc->saveXML($results->item($i));
  676. foreach (array('foo', 'bar') as $layerType) {
  677. $layer = sprintf('/js/%s/%s.xd.js', $layerType, $layerType);
  678. if (strstr($script, $layer)) {
  679. $found[] = $layerType;
  680. break;
  681. }
  682. }
  683. }
  684. $this->assertSame(array('foo', 'bar'), $found);
  685. }
  686. /**
  687. * @expectedException Zend_Dojo_View_Exception
  688. */
  689. public function testCallingMethodThatDoesNotExistInContainerShouldRaiseException()
  690. {
  691. $dojo = new Zend_Dojo_View_Helper_Dojo();
  692. $dojo->bogus();
  693. }
  694. public function testShouldAllowSpecifyingDeclarativeUsage()
  695. {
  696. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  697. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useDeclarative());
  698. }
  699. public function testShouldAllowSpecifyingProgrammaticUsageWithNoScriptGeneration()
  700. {
  701. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
  702. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  703. }
  704. public function testInvalidFlagPassedToUseProgrammaticShouldUseProgrammaticWithScripts()
  705. {
  706. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic('foo');
  707. $this->assertFalse(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  708. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  709. }
  710. /**
  711. * @see ZF-3962
  712. * @group ZF-3962
  713. */
  714. public function testHelperShouldAllowDisablingParseOnLoadWithDeclarativeStyle()
  715. {
  716. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  717. $this->helper->requireModule('dijit.layout.ContentPane')
  718. ->setDjConfigOption('parseOnLoad', 'false')
  719. ->enable();
  720. $html = $this->helper->__toString();
  721. if (!preg_match('/(var djConfig = .*?(?:};))/s', $html, $matches)) {
  722. $this->fail('Failed to find djConfig settings: ' . $html);
  723. }
  724. $this->assertNotContains('"parseOnLoad":true', $matches[1]);
  725. }
  726. /**
  727. * @group ZF-4522
  728. */
  729. public function testOnLoadCaptureStartShouldReturnVoid()
  730. {
  731. $test = $this->helper->onLoadCaptureStart();
  732. $this->helper->onLoadCaptureEnd();
  733. $this->assertNull($test);
  734. }
  735. /**
  736. * @group ZF-4522
  737. */
  738. public function testJavascriptCaptureStartShouldReturnVoid()
  739. {
  740. $test = $this->helper->javascriptCaptureStart();
  741. $this->helper->javascriptCaptureEnd();
  742. $this->assertNull($test);
  743. }
  744. /**
  745. * @group ZF-4587
  746. * @group ZF-5808
  747. */
  748. public function testZendDijitOnLoadMarkupShouldPrecedeAllOtherOnLoadEvents()
  749. {
  750. $this->helper->addOnLoad('zend.custom');
  751. $this->view->textBox('foo', 'bar');
  752. $test = $this->helper->__toString();
  753. $this->assertRegexp('/zendDijits.*?(zend\.custom)/s', $test, 'Generated markup: ' . $test);
  754. }
  755. public function setupDojo()
  756. {
  757. $this->helper->requireModule('dijit.layout.ContentPane')
  758. ->registerModulePath('custom', '../custom')
  759. ->requireModule('custom.foo')
  760. ->setCdnVersion('1.1')
  761. ->setDjConfig(array('parseOnLoad' => 'true'))
  762. ->addStylesheetModule('dijit.themes.tundra')
  763. ->addStylesheet('/css/custom.css')
  764. ->addOnLoad('foo');
  765. }
  766. }
  767. // Call Zend_Dojo_View_Helper_DojoTest::main() if this source file is executed directly.
  768. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_DojoTest::main") {
  769. Zend_Dojo_View_Helper_DojoTest::main();
  770. }