jQueryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2013 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. require_once 'jQueryTestCase.php';
  23. class ZendX_JQuery_View_jQueryTest extends ZendX_JQuery_View_jQueryTestCase
  24. {
  25. public function testHelperSuccessfulCallForward()
  26. {
  27. $jquery = new ZendX_JQuery_View_Helper_JQuery;
  28. $jquery->addJavascript('alert();');
  29. }
  30. /**
  31. * @expectedException Zend_View_Exception
  32. */
  33. public function testHelperFailingCallForward()
  34. {
  35. $jquery = new ZendX_JQuery_View_Helper_JQuery();
  36. $jquery->addAsdf();
  37. }
  38. public function testShouldCallHelperWithoutExceptions()
  39. {
  40. $jquery = $this->view->jQuery();
  41. $this->assertTrue($jquery instanceof ZendX_JQuery_View_Helper_JQuery_Container);
  42. }
  43. public function testShouldAllowSpecifyingJQueryVersion()
  44. {
  45. $this->jquery->setVersion('1.2.3');
  46. $this->assertEquals('1.2.3', $this->jquery->getVersion());
  47. }
  48. public function testShouldUseDefaultSupportedVersionWhenNotSpecifiedOtherwise()
  49. {
  50. $this->assertEquals(ZendX_JQuery::DEFAULT_JQUERY_VERSION, $this->jquery->getVersion());
  51. $this->assertEquals(ZendX_JQuery::DEFAULT_JQUERY_VERSION, $this->jquery->getCdnVersion());
  52. }
  53. /**
  54. * Behaviour changed in 1.8
  55. *
  56. * @group ZF-5667
  57. */
  58. public function testUsingCdnShouldNotEnableHelperAnymore()
  59. {
  60. $this->jquery->setCdnVersion();
  61. $this->assertFalse($this->jquery->isEnabled());
  62. }
  63. public function testShouldBeNotEnabledByDefault()
  64. {
  65. $this->assertFalse($this->jquery->isEnabled());
  66. }
  67. public function testUsingLocalPath()
  68. {
  69. $this->jquery->setLocalPath('/js/jquery.min.js');
  70. $this->assertFalse($this->jquery->useCDN());
  71. $this->assertFalse($this->jquery->isEnabled());
  72. $this->assertTrue($this->jquery->useLocalPath());
  73. $this->assertContains('/js/jquery.min.js', $this->jquery->getLocalPath());
  74. $render = $this->jquery->__toString();
  75. $this->assertNotContains('/js/jquery.min.js', $render);
  76. }
  77. public function testUiDisabledDefault()
  78. {
  79. $this->assertFalse($this->jquery->uiIsEnabled());
  80. }
  81. public function testUiUsingCdnByDefault()
  82. {
  83. $this->assertFalse($this->jquery->useUiLocal());
  84. $this->assertTrue($this->jquery->useUiCdn());
  85. $this->assertNull($this->jquery->getUiPath());
  86. }
  87. public function testGetUiVersionReturnsDefaultSupportedVersionIfNotSpecifiedOtherwise()
  88. {
  89. $this->assertEquals(ZendX_JQuery::DEFAULT_UI_VERSION, $this->jquery->getUiVersion());
  90. $this->assertEquals(ZendX_JQuery::DEFAULT_UI_VERSION, $this->jquery->getUiVersion());
  91. }
  92. public function testShouldAllowEnableUi()
  93. {
  94. $this->jquery->uiEnable();
  95. $render = $this->jquery->__toString();
  96. $this->assertContains('jquery-ui', $render);
  97. $this->assertContains($this->jquery->getUiVersion(), $render);
  98. }
  99. public function testShouldAllowSetUiVersion()
  100. {
  101. $this->jquery->setUiVersion('1.5.1');
  102. $this->assertContains('1.5.1', $this->jquery->getUiVersion());
  103. }
  104. public function testShouldAllowSetLocalUiPath()
  105. {
  106. $this->jquery->setUiLocalPath('/js/jquery-ui.min.js');
  107. $this->assertTrue($this->jquery->useUiLocal());
  108. $this->assertFalse($this->jquery->useUiCdn());
  109. $this->assertContains('/js/jquery-ui.min.js', $this->jquery->getUiPath());
  110. }
  111. public function testNoConflictShouldBeDisabledDefault()
  112. {
  113. $this->assertFalse(ZendX_JQuery_View_Helper_JQuery::getNoConflictMode());
  114. }
  115. public function testUsingNoConflictMode()
  116. {
  117. ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
  118. $this->jquery->setCDNVersion('1.2.6');
  119. $this->jquery->enable();
  120. $render = $this->jquery->__toString();
  121. $this->assertContains('var $j = jQuery.noConflict();', $render);
  122. }
  123. public function testDefaultRenderModeShouldIncludeAllBlocks()
  124. {
  125. $this->assertEquals(ZendX_JQuery::RENDER_ALL, $this->jquery->getRenderMode());
  126. }
  127. public function testShouldAllowSettingRenderMode()
  128. {
  129. $this->jquery->setRenderMode(1);
  130. $this->assertEquals(1, $this->jquery->getRenderMode());
  131. $this->jquery->setRenderMode(2);
  132. $this->assertEquals(2, $this->jquery->getRenderMode());
  133. $this->jquery->setRenderMode(4);
  134. $this->assertEquals(4, $this->jquery->getRenderMode());
  135. }
  136. public function testShouldAllowUsingAddOnLoadStack()
  137. {
  138. $this->jquery->addOnLoad('$(document).alert();');
  139. $this->assertEquals(array('$(document).alert();'), $this->jquery->getOnLoadActions());
  140. }
  141. public function testShouldAllowStackingMultipleOnLoad()
  142. {
  143. $this->jquery->addOnLoad('1');
  144. $this->jquery->addOnLoad('2');
  145. $this->assertEquals(2, count($this->jquery->getOnLoadActions()));
  146. }
  147. public function testAddOnLoadEnablesJQuery()
  148. {
  149. $this->assertFalse($this->jquery->isEnabled());
  150. $this->jquery->addOnLoad('1');
  151. $this->assertTrue($this->jquery->isEnabled());
  152. }
  153. public function testShouldAllowCaptureOnLoad()
  154. {
  155. $this->jquery->onLoadCaptureStart();
  156. echo '$(document).alert();';
  157. $this->jquery->onLoadCaptureEnd();
  158. $this->assertEquals(array('$(document).alert();'), $this->jquery->getOnLoadActions());
  159. }
  160. public function testShouldAllowCaptureJavascript()
  161. {
  162. $this->jquery->javascriptCaptureStart();
  163. echo '$(document).alert();';
  164. $this->jquery->javascriptCaptureEnd();
  165. $this->assertEquals(array('$(document).alert();'), $this->jquery->getJavascript());
  166. $this->jquery->clearJavascript();
  167. $this->assertEquals(array(), $this->jquery->getJavascript());
  168. }
  169. /**
  170. * @expectedException Zend_Exception
  171. */
  172. public function testShouldDisallowNestingCapturesWithException()
  173. {
  174. $this->jquery->javascriptCaptureStart();
  175. $this->jquery->javascriptCaptureStart();
  176. }
  177. /**
  178. * @expectedException Zend_Exception
  179. */
  180. public function testShouldDisallowNestingCapturesWithException2()
  181. {
  182. $this->jquery->onLoadCaptureStart();
  183. $this->jquery->onLoadCaptureStart();
  184. $this->setExpectedException('Zend_Exception');
  185. }
  186. public function testAddJavascriptFiles()
  187. {
  188. $this->jquery->addJavascriptFile('/js/test.js');
  189. $this->jquery->addJavascriptFile('/js/test2.js');
  190. $this->jquery->addJavascriptFile('http://example.com/test3.js');
  191. $this->assertEquals(array('/js/test.js', '/js/test2.js', 'http://example.com/test3.js'), $this->jquery->getJavascriptFiles());
  192. }
  193. public function testAddedJavascriptFilesCanBeCleared()
  194. {
  195. $this->jquery->addJavascriptFile('/js/test.js');
  196. $this->jquery->addJavascriptFile('/js/test2.js');
  197. $this->jquery->addJavascriptFile('http://example.com/test3.js');
  198. $this->jquery->clearJavascriptFiles();
  199. $this->assertEquals(array(), $this->jquery->getJavascriptFiles());
  200. }
  201. public function testAddedJavascriptFilesRender()
  202. {
  203. $this->jquery->addJavascriptFile('/js/test.js');
  204. $this->jquery->addJavascriptFile('/js/test2.js');
  205. $this->jquery->addJavascriptFile('http://example.com/test3.js');
  206. $this->jquery->enable();
  207. $render = $this->jquery->__toString();
  208. $this->assertContains('src="/js/test.js"', $render);
  209. $this->assertContains('src="/js/test2.js"', $render);
  210. $this->assertContains('src="http://example.com/test3.js', $render);
  211. }
  212. public function testAddStylesheet()
  213. {
  214. $this->jquery->addStylesheet('test.css');
  215. $this->jquery->addStylesheet('test2.css');
  216. $this->assertEquals(array('test.css', 'test2.css'), $this->jquery->getStylesheets());
  217. }
  218. public function testShouldRenderNothingOnDisable()
  219. {
  220. $this->jquery->setCDNVersion('1.2.6');
  221. $this->jquery->addJavascriptFile('test.js');
  222. $this->jquery->disable();
  223. $this->assertEquals(strlen(''), strlen($this->jquery->__toString()));
  224. }
  225. public function testShouldAllowBasicSetupWithCDN()
  226. {
  227. $this->jquery->enable();
  228. $this->jquery->setCDNVersion('1.2.3');
  229. $this->jquery->addJavascriptFile('test.js');
  230. $render = $this->jquery->__toString();
  231. $this->assertTrue($this->jquery->useCDN());
  232. $this->assertContains('jquery.min.js', $render);
  233. $this->assertContains('1.2.3', $render);
  234. $this->assertContains('test.js', $render);
  235. $this->assertContains('<script type="text/javascript"', $render);
  236. }
  237. public function testShouldAllowUseRenderMode()
  238. {
  239. $this->jquery->enable();
  240. $this->jquery->setCDNVersion('1.2.3');
  241. $this->jquery->addJavascriptFile('test.js');
  242. $this->jquery->addJavascript('helloWorld();');
  243. $this->jquery->addStylesheet('test.css');
  244. $this->jquery->addOnLoad('alert();');
  245. // CHeck CDN Usage
  246. $this->assertTrue($this->jquery->useCDN());
  247. // Test with Render No Parts
  248. $this->jquery->setRenderMode(0);
  249. $this->assertEquals(strlen(''), strlen(trim($this->jquery->__toString())));
  250. // Test Render Only Library
  251. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_LIBRARY);
  252. $render = $this->jquery->__toString();
  253. $this->assertContains('1.2.3/jquery.min.js', $render);
  254. $this->assertNotContains('test.css', $render);
  255. $this->assertNotContains('test.js', $render);
  256. $this->assertNotContains('alert();', $render);
  257. $this->assertNotContains('helloWorld();', $render);
  258. // Test Render Only AddOnLoad
  259. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_JQUERY_ON_LOAD);
  260. $render = $this->jquery->__toString();
  261. $this->assertNotContains('1.2.3/jquery.min.js', $render);
  262. $this->assertNotContains('test.css', $render);
  263. $this->assertNotContains('test.js', $render);
  264. $this->assertContains('alert();', $render);
  265. $this->assertNotContains('helloWorld();', $render);
  266. // Test Render Only Javascript
  267. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_SOURCES);
  268. $render = $this->jquery->__toString();
  269. $this->assertNotContains('1.2.3/jquery.min.js', $render);
  270. $this->assertNotContains('test.css', $render);
  271. $this->assertContains('test.js', $render);
  272. $this->assertNotContains('alert();', $render);
  273. $this->assertNotContains('helloWorld();', $render);
  274. // Test Render Only Javascript
  275. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_STYLESHEETS);
  276. $render = $this->jquery->__toString();
  277. $this->assertNotContains('1.2.3/jquery.min.js', $render);
  278. $this->assertContains('test.css', $render);
  279. $this->assertNotContains('test.js', $render);
  280. $this->assertNotContains('alert();', $render);
  281. $this->assertNotContains('helloWorld();', $render);
  282. // Test Render Library and AddOnLoad
  283. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_LIBRARY | ZendX_JQuery::RENDER_JQUERY_ON_LOAD);
  284. $render = $this->jquery->__toString();
  285. $this->assertContains('1.2.3/jquery.min.js', $render);
  286. $this->assertNotContains('test.css', $render);
  287. $this->assertNotContains('test.js', $render);
  288. $this->assertContains('alert();', $render);
  289. $this->assertNotContains('helloWorld();', $render);
  290. // Test Render All
  291. $this->jquery->setRenderMode(ZendX_JQuery::RENDER_ALL);
  292. $render = $this->jquery->__toString();
  293. $this->assertContains('1.2.3/jquery.min.js', $render);
  294. $this->assertContains('test.css', $render);
  295. $this->assertContains('test.js', $render);
  296. $this->assertContains('alert();', $render);
  297. $this->assertContains('helloWorld();', $render);
  298. }
  299. /**
  300. * @group ZF-5185
  301. */
  302. public function testClearAddOnLoadStack()
  303. {
  304. $this->jquery->addOnLoad('foo');
  305. $this->jquery->addOnLoad('bar');
  306. $this->jquery->addOnLoad('baz');
  307. $this->assertEquals(array('foo', 'bar', 'baz'), $this->jquery->getOnLoadActions());
  308. $this->jquery->clearOnLoadActions();
  309. $this->assertEquals(array(), $this->jquery->getOnLoadActions());
  310. }
  311. /**
  312. * @group ZF-5344
  313. */
  314. public function testNoConflictModeIsRecognizedInRenderingOnLoadStackEvent()
  315. {
  316. ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
  317. $this->jquery->addOnLoad('foo');
  318. $this->jquery->addOnLoad('bar');
  319. $this->jquery->enable();
  320. $jQueryStack = $this->jquery->__toString();
  321. $this->assertContains('$j(document).ready(function()', $jQueryStack);
  322. ZendX_JQuery_View_Helper_JQuery::disableNoConflictMode();
  323. $jQueryStack = $this->jquery->__toString();
  324. $this->assertNotContains('$j(document).ready(function()', $jQueryStack);
  325. }
  326. /**
  327. * @group ZF-5839
  328. */
  329. public function testStylesheetShouldRenderCorrectClosingBracketBasedOnHtmlDoctypeDefinition()
  330. {
  331. $this->jquery->addStylesheet('test.css');
  332. $this->view->doctype('HTML4_STRICT');
  333. $assert = '<link rel="stylesheet" href="test.css" type="text/css" media="screen">';
  334. $this->jquery->enable();
  335. $this->assertContains($assert, $this->jquery->__toString());
  336. }
  337. /**
  338. * @group ZF-5839
  339. */
  340. public function testStylesheetShouldRenderCorrectClosingBracketBasedOnXHtmlDoctypeDefinition()
  341. {
  342. $this->jquery->addStylesheet('test.css');
  343. $this->view->doctype('XHTML1_STRICT');
  344. $assert = '<link rel="stylesheet" href="test.css" type="text/css" media="screen" />';
  345. $this->jquery->enable();
  346. $this->assertContains($assert, $this->jquery->__toString());
  347. }
  348. /**
  349. * @group ZF-11592
  350. */
  351. public function testAddedStylesheetsCanBeCleared()
  352. {
  353. $this->jquery->addStylesheet('foo.css');
  354. $this->jquery->addStylesheet('bar.css');
  355. $this->jquery->clearStylesheets();
  356. $this->assertSame(array(), $this->jquery->getStylesheets());
  357. }
  358. /**
  359. * @group ZF-6078
  360. */
  361. public function testIncludeJQueryLibraryFromSslPath()
  362. {
  363. $this->jquery->setCdnSsl(true);
  364. $this->jquery->enable();
  365. $this->assertContains(ZendX_JQuery::CDN_BASE_GOOGLE_SSL, $this->jquery->__toString());
  366. }
  367. /**
  368. * @group ZF-6594
  369. */
  370. public function testJQueryGoogleCdnPathIsBuiltCorrectly()
  371. {
  372. $jQueryCdnPath = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js';
  373. $this->jquery->setVersion('1.3.1');
  374. $this->jquery->enable();
  375. $this->assertContains($jQueryCdnPath, $this->jquery->__toString());
  376. }
  377. /**
  378. * @group ZF-6594
  379. */
  380. public function testJQueryUiGoogleCdnPathIsBuiltCorrectly()
  381. {
  382. $jQueryCdnPath = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js';
  383. $this->jquery->setVersion('1.3.1');
  384. $this->jquery->enable();
  385. $this->jquery->setUiVersion('1.7.1');
  386. $this->jquery->uiEnable();
  387. $this->assertContains($jQueryCdnPath, $this->jquery->__toString());
  388. }
  389. /**
  390. * @group ZF-6594
  391. */
  392. public function testJQueryGoogleCdnSslPathIsBuiltCorrectly()
  393. {
  394. $jQueryCdnPath = 'https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js';
  395. $this->jquery->setCdnSsl(true);
  396. $this->jquery->setVersion('1.3.1');
  397. $this->jquery->enable();
  398. $this->assertContains($jQueryCdnPath, $this->jquery->__toString());
  399. }
  400. /**
  401. * @group ZF-6594
  402. */
  403. public function testJQueryUiGoogleCdnSslPathIsBuiltCorrectly()
  404. {
  405. $jQueryCdnPath = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js';
  406. $this->jquery->setCdnSsl(true);
  407. $this->jquery->setVersion('1.3.1');
  408. $this->jquery->enable();
  409. $this->jquery->setUiVersion('1.7.1');
  410. $this->jquery->uiEnable();
  411. $this->assertContains($jQueryCdnPath, $this->jquery->__toString());
  412. }
  413. }