DojoTest.php 33 KB

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