DojoTest.php 34 KB

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