DojoTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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-2014 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-2014 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. public function testShouldUseAolCdnDojoPathByDefault()
  210. {
  211. $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_AOL, $this->helper->getCdnDojoPath());
  212. }
  213. public function testShouldAllowSpecifyingCdnDojoPath()
  214. {
  215. $this->testShouldUseAolCdnDojoPathByDefault();
  216. $this->helper->setCdnDojoPath(Zend_Dojo::CDN_DOJO_PATH_GOOGLE);
  217. $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_GOOGLE, $this->helper->getCdnDojoPath());
  218. }
  219. public function testShouldAllowSpecifyingLocalDojoInstall()
  220. {
  221. $this->helper->setLocalPath('/js/dojo/dojo.js');
  222. $this->assertTrue($this->helper->useLocalPath());
  223. }
  224. public function testShouldAllowSpecifyingDjConfig()
  225. {
  226. $this->helper->setDjConfig(array('parseOnLoad' => 'true'));
  227. $config = $this->helper->getDjConfig();
  228. $this->assertTrue(is_array($config));
  229. $this->assertTrue(array_key_exists('parseOnLoad', $config));
  230. $this->assertEquals('true', $config['parseOnLoad']);
  231. }
  232. public function testShouldAllowRetrievingIndividualDjConfigKeys()
  233. {
  234. $this->helper->setDjConfigOption('parseOnLoad', 'true');
  235. $this->assertEquals('true', $this->helper->getDjConfigOption('parseOnLoad'));
  236. }
  237. public function testGetDjConfigShouldReturnEmptyArrayByDefault()
  238. {
  239. $this->assertSame(array(), $this->helper->getDjConfig());
  240. }
  241. public function testGetDjConfigOptionShouldReturnNullWhenKeyDoesNotExist()
  242. {
  243. $this->assertNull($this->helper->getDjConfigOption('bogus'));
  244. }
  245. public function testGetDjConfigOptionShouldAllowSpecifyingDefaultValue()
  246. {
  247. $this->assertEquals('bar', $this->helper->getDjConfigOption('foo', 'bar'));
  248. }
  249. public function testDjConfigShouldSerializeToJson()
  250. {
  251. $this->helper->setDjConfigOption('parseOnLoad', true)
  252. ->enable();
  253. $html = $this->helper->__toString();
  254. $this->assertContains('var djConfig = ', $html, var_export($html, 1));
  255. $this->assertContains('"parseOnLoad":', $html, $html);
  256. }
  257. public function testShouldAllowSpecifyingStylesheetByModuleName()
  258. {
  259. $this->helper->addStylesheetModule('dijit.themes.tundra');
  260. $stylesheets = $this->helper->getStylesheetModules();
  261. $this->assertContains('dijit.themes.tundra', $stylesheets);
  262. }
  263. public function testDuplicateStylesheetModulesShouldNotBeAllowed()
  264. {
  265. $this->helper->addStylesheetModule('dijit.themes.tundra');
  266. $stylesheets = $this->helper->getStylesheetModules();
  267. $this->assertContains('dijit.themes.tundra', $stylesheets);
  268. $this->helper->addStylesheetModule('dijit.themes.tundra');
  269. $stylesheets = $this->helper->getStylesheetModules();
  270. $this->assertEquals(1, count($stylesheets));
  271. $this->assertContains('dijit.themes.tundra', $stylesheets);
  272. }
  273. /**
  274. * @group ZF-3916
  275. */
  276. public function testAddingStylesheetModuleShouldAllowDashAndUnderscoreCharacters()
  277. {
  278. $this->helper->addStylesheetModule('dojox._highlight.pygments');
  279. $this->helper->addStylesheetModule('dojo.NodeList-fx.styles');
  280. }
  281. public function testInvalidStylesheetModuleNameShouldThrowException()
  282. {
  283. try {
  284. $this->helper->addStylesheetModule('foo/bar/baz');
  285. $this->fail('invalid module designation should throw exception');
  286. } catch (Zend_Dojo_View_Exception $e) {
  287. $this->assertContains('Invalid', $e->getMessage());
  288. }
  289. }
  290. public function testRenderingModuleStylesheetShouldProperlyCreatePaths()
  291. {
  292. $this->helper->enable()
  293. ->addStylesheetModule('dijit.themes.tundra');
  294. $html = $this->helper->__toString();
  295. $this->assertContains('dijit/themes/tundra/tundra.css', $html);
  296. }
  297. public function testShouldAllowSpecifyingLocalStylesheet()
  298. {
  299. $this->helper->addStylesheet('/css/foo.css');
  300. $css = $this->helper->getStylesheets();
  301. $this->assertTrue(is_array($css));
  302. $this->assertContains('/css/foo.css', $css);
  303. }
  304. public function testShouldNotAllowSpecifyingDuplicateLocalStylesheets()
  305. {
  306. $this->testShouldAllowSpecifyingLocalStylesheet();
  307. $this->helper->addStylesheet('/css/foo.css');
  308. $css = $this->helper->getStylesheets();
  309. $this->assertTrue(is_array($css));
  310. $this->assertEquals(1, count($css));
  311. $this->assertContains('/css/foo.css', $css);
  312. }
  313. public function testShouldAllowSpecifyingOnLoadFunctionPointer()
  314. {
  315. $this->helper->addOnLoad('foo');
  316. $onLoad = $this->helper->getOnLoadActions();
  317. $this->assertTrue(is_array($onLoad));
  318. $this->assertEquals(1, count($onLoad));
  319. $action = array_shift($onLoad);
  320. $this->assertTrue(is_string($action));
  321. $this->assertEquals('foo', $action);
  322. }
  323. public function testShouldAllowCapturingOnLoadActions()
  324. {
  325. $this->helper->onLoadCaptureStart(); ?>
  326. function() {
  327. bar();
  328. baz();
  329. }
  330. <?php $this->helper->onLoadCaptureEnd();
  331. $onLoad = $this->helper->getOnLoadActions();
  332. $this->assertTrue(is_array($onLoad));
  333. $this->assertEquals(1, count($onLoad));
  334. $action = array_shift($onLoad);
  335. $this->assertTrue(is_string($action));
  336. $this->assertContains('function() {', $action);
  337. $this->assertContains('bar();', $action);
  338. $this->assertContains('baz();', $action);
  339. }
  340. public function testShouldNotAllowSpecifyingDuplicateOnLoadActions()
  341. {
  342. $this->helper->addOnLoad('foo');
  343. $this->helper->addOnLoad('foo');
  344. $onLoad = $this->helper->getOnLoadActions();
  345. $this->assertTrue(is_array($onLoad));
  346. $this->assertEquals(1, count($onLoad));
  347. $action = array_shift($onLoad);
  348. $this->assertEquals('foo', $action);
  349. }
  350. public function testDojoMethodShouldReturnContainer()
  351. {
  352. $helper = new Zend_Dojo_View_Helper_Dojo();
  353. $this->assertSame($this->helper, $helper->dojo());
  354. }
  355. public function testHelperStorageShouldPersistBetweenViewObjects()
  356. {
  357. $view1 = $this->getView();
  358. $dojo1 = $view1->getHelper('dojo');
  359. $view2 = $this->getView();
  360. $dojo2 = $view1->getHelper('dojo');
  361. $this->assertSame($dojo1, $dojo2);
  362. }
  363. public function testSerializingToStringShouldReturnEmptyStringByDefault()
  364. {
  365. $this->assertEquals('', $this->helper->__toString());
  366. }
  367. public function testEnablingHelperShouldCauseStringSerializationToWork()
  368. {
  369. $this->setupDojo();
  370. $html = $this->helper->__toString();
  371. $doc = new DOMDocument;
  372. $doc->loadHTML($html);
  373. $xPath = new DOMXPath($doc);
  374. $results = $xPath->query('//script');
  375. $this->assertEquals(3, $results->length);
  376. for ($i = 0; $i < 3; ++$i) {
  377. $script = $doc->saveXML($results->item($i));
  378. switch ($i) {
  379. case 0:
  380. $this->assertContains('var djConfig = ', $script);
  381. $this->assertContains('parseOnLoad', $script);
  382. break;
  383. case 1:
  384. $this->assertRegexp('#src="http://.+/dojo/[0-9.]+/dojo/dojo.xd.js"#', $script);
  385. $this->assertContains('/>', $script);
  386. break;
  387. case 2:
  388. $this->assertContains('dojo.registerModulePath("custom", "../custom")', $script, $script);
  389. $this->assertContains('dojo.require("dijit.layout.ContentPane")', $script, $script);
  390. $this->assertContains('dojo.require("custom.foo")', $script, $script);
  391. $this->assertContains('dojo.addOnLoad(foo)', $script, $script);
  392. break;
  393. }
  394. }
  395. $results = $xPath->query('//style');
  396. $this->assertEquals(1, $results->length, $html);
  397. $style = $doc->saveXML($results->item(0));
  398. $this->assertContains('@import', $style);
  399. $this->assertEquals(2, substr_count($style, '@import'));
  400. $this->assertEquals(1, substr_count($style, 'http://ajax.googleapis.com/ajax/libs/dojo/'), $style);
  401. $this->assertContains('css/custom.css', $style);
  402. $this->assertContains('dijit/themes/tundra/tundra.css', $style);
  403. }
  404. public function testStringSerializationShouldBeDoctypeAware()
  405. {
  406. $view = $this->getView();
  407. $view->doctype('HTML4_LOOSE');
  408. $this->helper->setView($view);
  409. $this->setupDojo();
  410. $html = $this->helper->__toString();
  411. $this->assertRegexp('|<style [^>]*>[\r\n]+\s*<!--|', $html);
  412. $this->assertRegexp('|<script [^>]*>[\r\n]+\s*//<!--|', $html);
  413. $this->helper = new Zend_Dojo_View_Helper_Dojo();
  414. $view->doctype('XHTML1_STRICT');
  415. $this->helper->setView($view);
  416. $this->setupDojo();
  417. $html = $this->helper->__toString();
  418. /**
  419. * @todo should stylesheets be escaped as CDATA when isXhtml()?
  420. */
  421. $this->assertRegexp('|<style [^>]*>[\r\n]+\s*<!--|', $html);
  422. $this->assertRegexp('|<script [^>]*>[\r\n]+\s*//<!\[CDATA\[|', $html);
  423. }
  424. public function testDojoHelperContainerPersistsBetweenViewObjects()
  425. {
  426. $this->setupDojo();
  427. $view = $this->getView();
  428. $this->assertNotSame($this->view, $view);
  429. $helper = $view->dojo();
  430. $this->assertSame($this->helper, $helper);
  431. }
  432. public function testShouldUseProgrammaticDijitCreationByDefault()
  433. {
  434. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  435. }
  436. public function testShouldAllowSpecifyingDeclarativeDijitCreation()
  437. {
  438. $this->testShouldUseProgrammaticDijitCreationByDefault();
  439. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  440. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useDeclarative());
  441. }
  442. public function testShouldAllowSpecifyingProgrammaticDijitCreationWithNoScriptGeneration()
  443. {
  444. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
  445. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  446. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  447. }
  448. public function testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams()
  449. {
  450. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  451. $dijits = $this->helper->getDijits();
  452. $this->assertTrue(is_array($dijits));
  453. $this->assertEquals(1, count($dijits));
  454. $dijit = array_shift($dijits);
  455. $this->assertTrue(is_array($dijit));
  456. $this->assertEquals(2, count($dijit));
  457. $this->assertTrue(array_key_exists('id', $dijit));
  458. $this->assertTrue(array_key_exists('params', $dijit));
  459. $this->assertEquals('foo', $dijit['id']);
  460. $this->assertTrue(is_array($dijit['params']));
  461. $this->assertEquals(1, count($dijit['params']));
  462. $this->assertTrue(array_key_exists('dojoType', $dijit['params']));
  463. $this->assertEquals('dijit.form.Form', $dijit['params']['dojoType']);
  464. }
  465. /**
  466. * @expectedException Zend_Dojo_View_Exception
  467. */
  468. public function testAddingDuplicateProgrammaticDijitsShouldRaiseExceptions()
  469. {
  470. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  471. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.ComboBox'));
  472. }
  473. public function testSettingProgrammaticDijitsShouldOverwriteExistingDijits()
  474. {
  475. $this->testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams();
  476. $this->helper->setDijit('foo', array('dojoType' => 'dijit.form.ComboBox'));
  477. $dijits = $this->helper->getDijits();
  478. $this->assertTrue(is_array($dijits));
  479. $this->assertEquals(1, count($dijits));
  480. $dijit = array_shift($dijits);
  481. $this->assertEquals('dijit.form.ComboBox', $dijit['params']['dojoType']);
  482. }
  483. public function testShouldAllowAddingMultipleDijitsAtOnce()
  484. {
  485. $dijits = array(
  486. 'foo' => array(
  487. 'dojoType' => 'dijit.form.Form'
  488. ),
  489. 'bar' => array(
  490. 'dojoType' => 'dijit.form.TextBox',
  491. ),
  492. );
  493. $this->helper->addDijits($dijits);
  494. $test = $this->helper->getDijits();
  495. $this->assertTrue(is_array($test));
  496. $this->assertEquals(2, count($test));
  497. $keys = array();
  498. foreach ($test as $dijit) {
  499. $keys[] = $dijit['id'];
  500. }
  501. $this->assertSame(array_keys($dijits), $keys);
  502. }
  503. public function testSettingMultipleDijitsAtOnceShouldOverwriteAllDijits()
  504. {
  505. $this->testAddingProgrammaticDijitsShouldAcceptIdAndArrayOfDijitParams();
  506. $dijits = array(
  507. 'bar' => array(
  508. 'dojoType' => 'dijit.form.Form'
  509. ),
  510. 'baz' => array(
  511. 'dojoType' => 'dijit.form.TextBox',
  512. ),
  513. );
  514. $this->helper->setDijits($dijits);
  515. $test = $this->helper->getDijits();
  516. $this->assertTrue(is_array($test));
  517. $this->assertEquals(2, count($test));
  518. $keys = array();
  519. foreach ($test as $dijit) {
  520. $keys[] = $dijit['id'];
  521. }
  522. $this->assertSame(array_keys($dijits), $keys);
  523. }
  524. public function testRetrievingDijitsByIdShouldReturnJustParams()
  525. {
  526. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  527. $params = $this->helper->getDijit('foo');
  528. $this->assertTrue(is_array($params));
  529. $this->assertEquals(1, count($params), var_export($params, 1));
  530. $this->assertTrue(array_key_exists('dojoType', $params));
  531. $this->assertEquals('dijit.form.Form', $params['dojoType']);
  532. }
  533. public function testShouldAllowRemovingIndividualDijits()
  534. {
  535. $this->helper->addDijit('foo', array('dojoType' => 'dijit.form.Form'));
  536. $dijits = $this->helper->getDijits();
  537. $this->assertTrue(is_array($dijits));
  538. $this->assertEquals(1, count($dijits));
  539. $this->helper->removeDijit('foo');
  540. $dijits = $this->helper->getDijits();
  541. $this->assertTrue(is_array($dijits));
  542. $this->assertEquals(0, count($dijits));
  543. }
  544. public function testShouldAllowClearingAllDijits()
  545. {
  546. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  547. $this->helper->clearDijits();
  548. $dijits = $this->helper->getDijits();
  549. $this->assertTrue(is_array($dijits));
  550. $this->assertEquals(0, count($dijits));
  551. }
  552. public function testShouldAllowRetrievingDijitsAsJsonArray()
  553. {
  554. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  555. $json = $this->helper->dijitsToJson();
  556. $array = Zend_Json::decode($json);
  557. $this->assertTrue(is_array($array));
  558. $keys = array();
  559. foreach ($array as $dijit) {
  560. $keys[] = $dijit['id'];
  561. $this->assertTrue(array_key_exists('params', $dijit));
  562. $this->assertTrue(is_array($dijit['params']));
  563. }
  564. $this->assertSame(array('foo', 'bar'), $keys);
  565. }
  566. public function testRenderingShouldCreateZendDijitsObjectAndAddOnloadForDijitsWhenDijitsArePresent()
  567. {
  568. $this->helper->enable();
  569. $this->testShouldAllowAddingMultipleDijitsAtOnce();
  570. $json = $this->helper->dijitsToJson();
  571. $html = $this->helper->__toString();
  572. $this->assertContains($json, $html, $html);
  573. $found = false;
  574. foreach ($this->helper->_getZendLoadActions() as $action) {
  575. if (strstr($action, 'dojo.mixin')) {
  576. $found = true;
  577. break;
  578. }
  579. }
  580. $this->assertTrue($found, 'Dijit onload action not created');
  581. $this->assertContains($action, $html);
  582. }
  583. public function testShouldAllowAddingArbitraryJsToPrimaryDojoScriptTag()
  584. {
  585. $this->helper->enable();
  586. $this->helper->addJavascript('var foo = "bar";');
  587. $html = $this->helper->__toString();
  588. $found = false;
  589. if (preg_match_all('|<script[^>]*>(.*?)(</script>)|s', $html, $m)) {
  590. foreach ($m[1] as $script) {
  591. if (strstr($script, 'var foo = "bar";')) {
  592. $found = true;
  593. break;
  594. }
  595. }
  596. }
  597. $this->assertTrue($found, 'Js not found: ' . $html);
  598. }
  599. public function testShouldAllowClearingArbitraryJsStack()
  600. {
  601. $this->testShouldAllowAddingArbitraryJsToPrimaryDojoScriptTag();
  602. $this->helper->clearJavascript();
  603. $js = $this->helper->getJavascript();
  604. $this->assertTrue(is_array($js));
  605. $this->assertEquals(0, count($js));
  606. }
  607. public function testShouldNotAllowAddingDuplicateArbitraryJsToPrimaryDojoScriptTag()
  608. {
  609. $this->helper->addJavascript('var foo = "bar";');
  610. $this->helper->addJavascript('var foo = "bar";');
  611. $js = $this->helper->getJavascript();
  612. $this->assertTrue(is_array($js));
  613. $this->assertEquals(1, count($js), var_export($js, 1));
  614. $this->assertEquals('var foo = "bar";', $js[0]);
  615. }
  616. public function testShouldAllowCapturingArbitraryJsToPrimaryDojoScriptTag()
  617. {
  618. $this->helper->javascriptCaptureStart();
  619. echo 'var foo = "bar";';
  620. $this->helper->javascriptCaptureEnd();
  621. $js = $this->helper->getJavascript();
  622. $this->assertEquals(1, count($js));
  623. $this->assertContains('var foo = "bar";', $js[0]);
  624. }
  625. public function testNoLayersShouldBeRegisteredByDefault()
  626. {
  627. $layers = $this->helper->getLayers();
  628. $this->assertTrue(is_array($layers));
  629. $this->assertTrue(empty($layers));
  630. }
  631. public function testShouldAllowAddingLayers()
  632. {
  633. $this->testNoLayersShouldBeRegisteredByDefault();
  634. $this->helper->addLayer('/js/foo/foo.xd.js');
  635. $layers = $this->helper->getLayers();
  636. $this->assertEquals(1, count($layers));
  637. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  638. $this->helper->addLayer('/js/bar/bar.xd.js');
  639. $layers = $this->helper->getLayers();
  640. $this->assertEquals(2, count($layers));
  641. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  642. $this->assertEquals('/js/bar/bar.xd.js', $layers[1]);
  643. }
  644. public function testShouldNotAllowDuplicateLayers()
  645. {
  646. $this->testShouldAllowAddingLayers();
  647. $this->helper->addLayer('/js/foo/foo.xd.js');
  648. $layers = $this->helper->getLayers();
  649. $this->assertEquals(2, count($layers));
  650. $this->assertEquals('/js/foo/foo.xd.js', $layers[0]);
  651. $this->assertEquals('/js/bar/bar.xd.js', $layers[1]);
  652. }
  653. public function testShouldAllowRemovingLayers()
  654. {
  655. $this->testShouldAllowAddingLayers();
  656. $this->helper->removeLayer('/js/foo/foo.xd.js');
  657. $layers = $this->helper->getLayers();
  658. $this->assertEquals(1, count($layers));
  659. $this->assertEquals('/js/bar/bar.xd.js', $layers[0]);
  660. }
  661. public function testShouldAllowClearingLayers()
  662. {
  663. $this->testShouldAllowAddingLayers();
  664. $this->helper->clearLayers();
  665. $layers = $this->helper->getLayers();
  666. $this->assertTrue(is_array($layers));
  667. $this->assertTrue(empty($layers));
  668. }
  669. public function testShouldRenderScriptTagsWithLayersWhenLayersAreRegistered()
  670. {
  671. $this->setupDojo();
  672. $this->testShouldAllowAddingLayers();
  673. $html = $this->helper->__toString();
  674. $doc = new DOMDocument;
  675. $doc->loadHTML($html);
  676. $xPath = new DOMXPath($doc);
  677. $results = $xPath->query('//script');
  678. $found = array();
  679. for ($i = 0; $i < $results->length; ++$i) {
  680. $script = $doc->saveXML($results->item($i));
  681. foreach (array('foo', 'bar') as $layerType) {
  682. $layer = sprintf('/js/%s/%s.xd.js', $layerType, $layerType);
  683. if (strstr($script, $layer)) {
  684. $found[] = $layerType;
  685. break;
  686. }
  687. }
  688. }
  689. $this->assertSame(array('foo', 'bar'), $found);
  690. }
  691. /**
  692. * @expectedException Zend_Dojo_View_Exception
  693. */
  694. public function testCallingMethodThatDoesNotExistInContainerShouldRaiseException()
  695. {
  696. $dojo = new Zend_Dojo_View_Helper_Dojo();
  697. $dojo->bogus();
  698. }
  699. public function testShouldAllowSpecifyingDeclarativeUsage()
  700. {
  701. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  702. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useDeclarative());
  703. }
  704. public function testShouldAllowSpecifyingProgrammaticUsageWithNoScriptGeneration()
  705. {
  706. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
  707. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  708. }
  709. public function testInvalidFlagPassedToUseProgrammaticShouldUseProgrammaticWithScripts()
  710. {
  711. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic('foo');
  712. $this->assertFalse(Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript());
  713. $this->assertTrue(Zend_Dojo_View_Helper_Dojo::useProgrammatic());
  714. }
  715. /**
  716. * @group ZF-3962
  717. */
  718. public function testHelperShouldAllowDisablingParseOnLoadWithDeclarativeStyle()
  719. {
  720. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  721. $this->helper->requireModule('dijit.layout.ContentPane')
  722. ->setDjConfigOption('parseOnLoad', 'false')
  723. ->enable();
  724. $html = $this->helper->__toString();
  725. if (!preg_match('/(var djConfig = .*?(?:};))/s', $html, $matches)) {
  726. $this->fail('Failed to find djConfig settings: ' . $html);
  727. }
  728. $this->assertNotContains('"parseOnLoad":true', $matches[1]);
  729. }
  730. /**
  731. * @group ZF-4522
  732. */
  733. public function testOnLoadCaptureStartShouldReturnVoid()
  734. {
  735. $test = $this->helper->onLoadCaptureStart();
  736. $this->helper->onLoadCaptureEnd();
  737. $this->assertNull($test);
  738. }
  739. /**
  740. * @group ZF-4522
  741. */
  742. public function testJavascriptCaptureStartShouldReturnVoid()
  743. {
  744. $test = $this->helper->javascriptCaptureStart();
  745. $this->helper->javascriptCaptureEnd();
  746. $this->assertNull($test);
  747. }
  748. /**
  749. * @group ZF-4587
  750. * @group ZF-5808
  751. */
  752. public function testZendDijitOnLoadMarkupShouldPrecedeAllOtherOnLoadEvents()
  753. {
  754. $this->helper->addOnLoad('zend.custom');
  755. $this->view->textBox('foo', 'bar');
  756. $test = $this->helper->__toString();
  757. $this->assertRegexp('/zendDijits.*?(zend\.custom)/s', $test, 'Generated markup: ' . $test);
  758. }
  759. public function testDojoViewHelperContainerAddOptionsPassesOnAllStringOptions() {
  760. $helper = $this->helper;
  761. $options = array(
  762. 'requireModules' => 'ZfTestRequiredModule',
  763. 'laYers' => '_added_layer_',
  764. 'cdnBase' => 'ZF-RLZ',
  765. 'cdnVersion' => '1.9.5',
  766. 'cdnDojoPath' => '_cdn_dojo_path_',
  767. 'localPath' => '/srv/ZF/dojo/',
  768. 'stylesheetmodules' => 'test.stylesheet.module',
  769. 'stylesheets' => 'someStyleSheet',
  770. 'registerdojostylesheet' => true
  771. );
  772. $helper->setOptions($options);
  773. $this->assertEquals(array('ZfTestRequiredModule'), $helper->getModules());
  774. $this->assertEquals(array('_added_layer_'), $helper->getLayers());
  775. $this->assertEquals('ZF-RLZ', $helper->getCdnBase());
  776. $this->assertEquals('1.9.5', $helper->getCdnVersion());
  777. $this->assertEquals('_cdn_dojo_path_', $helper->getCdnDojoPath());
  778. $this->assertEquals('/srv/ZF/dojo/', $helper->getLocalPath());
  779. $this->assertEquals(array('test.stylesheet.module'), $helper->getStyleSheetModules());
  780. $this->assertEquals(array('someStyleSheet'), $helper->getStylesheets());
  781. $this->assertTrue($helper->registerDojoStylesheet());
  782. }
  783. public function testDojoViewHelperContainerAddOptionsPassesOnAllArrayOptions() {
  784. $helper = $this->helper;
  785. $modulePaths = array('module1' => 'path1', 'module2' => 'path2');
  786. $layers = array('layer_two','layer_three');
  787. $djConfig = array('foo1' => 'bar1', 'foo2' => 'bar2');
  788. $stylesheetMods = array('test.one.style', 'test.two.style');
  789. $stylesheets = array('style1', 'style2');
  790. $options = array(
  791. 'modulePaths' => $modulePaths,
  792. 'layers' => $layers,
  793. 'djConfig' => $djConfig,
  794. 'styleShEEtModules' => $stylesheetMods,
  795. 'stylesheets' => $stylesheets,
  796. 'registerdojostylesheet' => false
  797. );
  798. $helper->setOptions($options);
  799. $this->assertEquals($modulePaths, $helper->getModulePaths());
  800. $this->assertEquals($layers, $helper->getLayers());
  801. $this->assertEquals($djConfig, $helper->getDjConfig());
  802. $this->assertEquals($stylesheetMods, $helper->getStyleSheetModules());
  803. $this->assertEquals($stylesheets, $helper->getStylesheets());
  804. $this->assertFalse($helper->registerDojoStylesheet());
  805. }
  806. public function testJsonExpressionRenders()
  807. {
  808. $this->helper->addDijit('foo',
  809. array('dojoType' => 'dijit.form.TextBox',
  810. 'onChange' => new Zend_Json_Expr('function(){alert(\'foo\');}'),
  811. ));
  812. $output = $this->helper->dijitsToJson();
  813. $this->assertRegexp('#(function\\(\\){alert\\(\'foo\'\\);})#', $output);
  814. }
  815. /**
  816. * @group GH-340
  817. */
  818. public function testRenderStylesheetsOrder()
  819. {
  820. $helper = $this->helper;
  821. $options = array(
  822. 'localPath' => '',
  823. 'stylesheetmodules' => 'test.stylesheet.module',
  824. 'registerdojostylesheet' => true,
  825. 'enable' => true,
  826. );
  827. $helper->setOptions($options);
  828. $expected = '<style type="text/css">' . "\n"
  829. . '<!--' . "\n"
  830. . ' @import "/dojo/resources/dojo.css";' . "\n"
  831. . ' @import "/test/stylesheet/module/module.css";' . "\n"
  832. . '-->' . "\n"
  833. . '</style>';
  834. $actual = (string) $helper;
  835. $end = '</style>';
  836. $actual = substr($actual, 0, strpos($actual, $end) + strlen($end));
  837. $this->assertEquals($expected, $actual);
  838. }
  839. public function setupDojo()
  840. {
  841. $this->helper->requireModule('dijit.layout.ContentPane')
  842. ->registerModulePath('custom', '../custom')
  843. ->requireModule('custom.foo')
  844. ->setCdnVersion('1.1')
  845. ->setDjConfig(array('parseOnLoad' => 'true'))
  846. ->addStylesheetModule('dijit.themes.tundra')
  847. ->addStylesheet('/css/custom.css')
  848. ->addOnLoad('foo');
  849. }
  850. }
  851. // Call Zend_Dojo_View_Helper_DojoTest::main() if this source file is executed directly.
  852. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_DojoTest::main") {
  853. Zend_Dojo_View_Helper_DojoTest::main();
  854. }