DojoTest.php 33 KB

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