JsHelperTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. /**
  3. * JsHelper Test Case
  4. *
  5. * TestCase for the JsHelper
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package Cake.Test.Case.View.Helper
  18. * @since CakePHP(tm) v 1.3
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('HtmlHelper', 'View/Helper');
  22. App::uses('JsHelper', 'View/Helper');
  23. App::uses('JsBaseEngineHelper', 'View/Helper');
  24. App::uses('FormHelper', 'View/Helper');
  25. App::uses('View', 'View');
  26. App::uses('ClassRegistry', 'Utility');
  27. class JsEncodingObject {
  28. protected $_title = 'Old thing';
  29. private $__noshow = 'Never ever';
  30. }
  31. class OptionEngineHelper extends JsBaseEngineHelper {
  32. protected $_optionMap = array(
  33. 'request' => array(
  34. 'complete' => 'success',
  35. 'request' => 'beforeSend',
  36. 'type' => 'dataType'
  37. )
  38. );
  39. /**
  40. * test method for testing option mapping
  41. *
  42. * @return array
  43. */
  44. public function testMap($options = array()) {
  45. return $this->_mapOptions('request', $options);
  46. }
  47. /**
  48. * test method for option parsing
  49. *
  50. * @return void
  51. */
  52. public function testParseOptions($options, $safe = array()) {
  53. return $this->_parseOptions($options, $safe);
  54. }
  55. public function get($selector) {}
  56. public function event($type, $callback, $options = array()) {}
  57. public function domReady($functionBody) {}
  58. public function each($callback) {}
  59. public function effect($name, $options = array()) {}
  60. public function request($url, $options = array()) {}
  61. public function drag($options = array()) {}
  62. public function drop($options = array()) {}
  63. public function sortable($options = array()) {}
  64. public function slider($options = array()) {}
  65. public function serializeForm($options = array()) {}
  66. }
  67. /**
  68. * JsHelper TestCase.
  69. *
  70. * @package Cake.Test.Case.View.Helper
  71. */
  72. class JsHelperTest extends CakeTestCase {
  73. /**
  74. * Regexp for CDATA start block
  75. *
  76. * @var string
  77. */
  78. public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  79. /**
  80. * Regexp for CDATA end block
  81. *
  82. * @var string
  83. */
  84. public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  85. /**
  86. * setUp method
  87. *
  88. * @return void
  89. */
  90. public function setUp() {
  91. $this->_asset = Configure::read('Asset.timestamp');
  92. Configure::write('Asset.timestamp', false);
  93. $controller = null;
  94. $this->View = $this->getMock('View', array('addScript'), array(&$controller));
  95. $this->Js = new JsHelper($this->View, 'Option');
  96. $request = new CakeRequest(null, false);
  97. $this->Js->request = $request;
  98. $this->Js->Html = new HtmlHelper($this->View);
  99. $this->Js->Html->request = $request;
  100. $this->Js->Form = new FormHelper($this->View);
  101. $this->Js->Form->request = $request;
  102. $this->Js->Form->Html = $this->Js->Html;
  103. $this->Js->OptionEngine = new OptionEngineHelper($this->View);
  104. }
  105. /**
  106. * tearDown method
  107. *
  108. * @return void
  109. */
  110. public function tearDown() {
  111. Configure::write('Asset.timestamp', $this->_asset);
  112. unset($this->Js);
  113. }
  114. /**
  115. * Switches $this->Js to a mocked engine.
  116. *
  117. * @return void
  118. */
  119. function _useMock() {
  120. $request = new CakeRequest(null, false);
  121. if (!class_exists('TestJsEngineHelper', false)) {
  122. $this->getMock('JsBaseEngineHelper', array(), array($this->View), 'TestJsEngineHelper');
  123. }
  124. $this->Js = new JsHelper($this->View, array('TestJs'));
  125. $this->Js->TestJsEngine = new TestJsEngineHelper($this->View);
  126. $this->mockObjects[] = $this->Js->TestJsEngine;
  127. $this->Js->request = $request;
  128. $this->Js->Html = new HtmlHelper($this->View);
  129. $this->Js->Html->request = $request;
  130. $this->Js->Form = new FormHelper($this->View);
  131. $this->Js->Form->request = $request;
  132. $this->Js->Form->Html = new HtmlHelper($this->View);
  133. }
  134. /**
  135. * test object construction
  136. *
  137. * @return void
  138. */
  139. public function testConstruction() {
  140. $js = new JsHelper($this->View);
  141. $this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));
  142. $js = new JsHelper($this->View, array('mootools'));
  143. $this->assertEqual($js->helpers, array('Html', 'Form', 'mootoolsEngine'));
  144. $js = new JsHelper($this->View, 'prototype');
  145. $this->assertEqual($js->helpers, array('Html', 'Form', 'prototypeEngine'));
  146. $js = new JsHelper($this->View, 'MyPlugin.Dojo');
  147. $this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
  148. }
  149. /**
  150. * test that methods dispatch internally and to the engine class
  151. *
  152. * @return void
  153. */
  154. public function testMethodDispatching() {
  155. $this->_useMock();
  156. $this->Js->TestJsEngine
  157. ->expects($this->once())
  158. ->method('event')
  159. ->with('click', 'callback');
  160. $this->Js->event('click', 'callback');
  161. $this->Js->TestJsEngine = new StdClass();
  162. $this->expectError();
  163. $this->Js->someMethodThatSurelyDoesntExist();
  164. }
  165. /**
  166. * Test that method dispatching for events respects buffer parameters and bufferedMethods Lists.
  167. *
  168. * @return void
  169. */
  170. public function testEventDispatchWithBuffering() {
  171. $this->_useMock();
  172. $this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
  173. $this->Js->TestJsEngine->expects($this->exactly(3))
  174. ->method('event')
  175. ->will($this->returnValue('This is an event call'));
  176. $this->Js->event('click', 'foo');
  177. $result = $this->Js->getBuffer();
  178. $this->assertEqual(count($result), 1);
  179. $this->assertEqual($result[0], 'This is an event call');
  180. $result = $this->Js->event('click', 'foo', array('buffer' => false));
  181. $buffer = $this->Js->getBuffer();
  182. $this->assertTrue(empty($buffer));
  183. $this->assertEqual($result, 'This is an event call');
  184. $result = $this->Js->event('click', 'foo', false);
  185. $buffer = $this->Js->getBuffer();
  186. $this->assertTrue(empty($buffer));
  187. $this->assertEqual($result, 'This is an event call');
  188. }
  189. /**
  190. * Test that method dispatching for effects respects buffer parameters and bufferedMethods Lists.
  191. *
  192. * @return void
  193. */
  194. public function testEffectDispatchWithBuffering() {
  195. $this->_useMock();
  196. $this->Js->TestJsEngine->expects($this->exactly(4))
  197. ->method('effect')
  198. ->will($this->returnValue('I am not buffered.'));
  199. $result = $this->Js->effect('slideIn');
  200. $buffer = $this->Js->getBuffer();
  201. $this->assertTrue(empty($buffer));
  202. $this->assertEqual($result, 'I am not buffered.');
  203. $result = $this->Js->effect('slideIn', true);
  204. $buffer = $this->Js->getBuffer();
  205. $this->assertNull($result);
  206. $this->assertEqual(count($buffer), 1);
  207. $this->assertEqual($buffer[0], 'I am not buffered.');
  208. $result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
  209. $buffer = $this->Js->getBuffer();
  210. $this->assertNull($result);
  211. $this->assertEqual(count($buffer), 1);
  212. $this->assertEqual($buffer[0], 'I am not buffered.');
  213. $result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
  214. $buffer = $this->Js->getBuffer();
  215. $this->assertNull($result);
  216. $this->assertEqual(count($buffer), 1);
  217. $this->assertEqual($buffer[0], 'I am not buffered.');
  218. }
  219. /**
  220. * test that writeScripts generates scripts inline.
  221. *
  222. * @return void
  223. */
  224. public function testWriteScriptsNoFile() {
  225. $this->_useMock();
  226. $this->Js->buffer('one = 1;');
  227. $this->Js->buffer('two = 2;');
  228. $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
  229. $expected = array(
  230. 'script' => array('type' => 'text/javascript'),
  231. $this->cDataStart,
  232. "one = 1;\ntwo = 2;",
  233. $this->cDataEnd,
  234. '/script',
  235. );
  236. $this->assertTags($result, $expected);
  237. $this->Js->TestJsEngine->expects($this->atLeastOnce())->method('domReady');
  238. $result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
  239. $this->View->expects($this->once())
  240. ->method('addScript')
  241. ->with($this->matchesRegularExpression('/one\s\=\s1;\ntwo\s\=\s2;/'));
  242. $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
  243. }
  244. /**
  245. * test that writing the buffer with inline = false includes a script tag.
  246. *
  247. * @return void
  248. */
  249. public function testWriteBufferNotInline() {
  250. $this->Js->set('foo', 1);
  251. $this->View->expects($this->once())
  252. ->method('addScript')
  253. ->with($this->matchesRegularExpression('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#'));
  254. $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
  255. }
  256. /**
  257. * test that writeBuffer() sets domReady = false when the request is done by XHR.
  258. * Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries
  259. *
  260. * @return void
  261. */
  262. public function testWriteBufferAndXhr() {
  263. $this->_useMock();
  264. $this->Js->params['isAjax'] = true;
  265. $this->Js->buffer('alert("test");');
  266. $this->Js->TestJsEngine->expects($this->never())->method('domReady');
  267. $result = $this->Js->writeBuffer();
  268. }
  269. /**
  270. * test that writeScripts makes files, and puts the events into them.
  271. *
  272. * @return void
  273. */
  274. public function testWriteScriptsInFile() {
  275. $this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped.');
  276. $this->Js->request->webroot = '/';
  277. $this->Js->JsBaseEngine = new TestJsEngineHelper($this->View);
  278. $this->Js->buffer('one = 1;');
  279. $this->Js->buffer('two = 2;');
  280. $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
  281. $expected = array(
  282. 'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
  283. );
  284. $this->assertTags($result, $expected);
  285. preg_match('/src="(.*\.js)"/', $result, $filename);
  286. $this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
  287. $contents = file_get_contents(WWW_ROOT . $filename[1]);
  288. $this->assertPattern('/one\s=\s1;\ntwo\s=\s2;/', $contents);
  289. @unlink(WWW_ROOT . $filename[1]);
  290. }
  291. /**
  292. * test link()
  293. *
  294. * @return void
  295. */
  296. public function testLinkWithMock() {
  297. $this->_useMock();
  298. $options = array('update' => '#content');
  299. $this->Js->TestJsEngine->expects($this->at(0))
  300. ->method('get');
  301. $this->Js->TestJsEngine->expects($this->at(1))
  302. ->method('request')
  303. ->with('/posts/view/1', $options)
  304. ->will($this->returnValue('--ajax code--'));
  305. $this->Js->TestJsEngine->expects($this->at(2))
  306. ->method('event')
  307. ->with('click', '--ajax code--', $options + array('buffer' => null));
  308. $result = $this->Js->link('test link', '/posts/view/1', $options);
  309. $expected = array(
  310. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  311. 'test link',
  312. '/a'
  313. );
  314. $this->assertTags($result, $expected);
  315. }
  316. /**
  317. * test link with a mock and confirmation
  318. *
  319. * @return void
  320. */
  321. public function testLinkWithMockAndConfirm() {
  322. $this->_useMock();
  323. $options = array(
  324. 'confirm' => 'Are you sure?',
  325. 'update' => '#content',
  326. 'class' => 'my-class',
  327. 'id' => 'custom-id',
  328. 'escape' => false
  329. );
  330. $this->Js->TestJsEngine->expects($this->once())
  331. ->method('confirmReturn')
  332. ->with($options['confirm'])
  333. ->will($this->returnValue('--confirm script--'));
  334. $this->Js->TestJsEngine->expects($this->once())
  335. ->method('request')
  336. ->with('/posts/view/1');
  337. $this->Js->TestJsEngine->expects($this->once())
  338. ->method('event')
  339. ->with('click', '--confirm script--');
  340. $result = $this->Js->link('test link »', '/posts/view/1', $options);
  341. $expected = array(
  342. 'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
  343. 'test link »',
  344. '/a'
  345. );
  346. $this->assertTags($result, $expected);
  347. }
  348. /**
  349. * test link passing on htmlAttributes
  350. *
  351. * @return void
  352. */
  353. public function testLinkWithAribtraryAttributes() {
  354. $this->_useMock();
  355. $options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
  356. $result = $this->Js->link('test link', '/posts/view/1', $options);
  357. $expected = array(
  358. 'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
  359. 'batman' => 'robin'),
  360. 'test link',
  361. '/a'
  362. );
  363. $this->assertTags($result, $expected);
  364. }
  365. /**
  366. * test that link() and no buffering returns an <a> and <script> tags.
  367. *
  368. * @return void
  369. */
  370. public function testLinkWithNoBuffering() {
  371. $this->_useMock();
  372. $this->Js->TestJsEngine->expects($this->at(1))
  373. ->method('request')
  374. ->with('/posts/view/1', array('update' => '#content'))
  375. ->will($this->returnValue('ajax code'));
  376. $this->Js->TestJsEngine->expects($this->at(2))
  377. ->method('event')
  378. ->will($this->returnValue('-event handler-'));
  379. $options = array('update' => '#content', 'buffer' => false);
  380. $result = $this->Js->link('test link', '/posts/view/1', $options);
  381. $expected = array(
  382. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  383. 'test link',
  384. '/a',
  385. 'script' => array('type' => 'text/javascript'),
  386. $this->cDataStart,
  387. '-event handler-',
  388. $this->cDataEnd,
  389. '/script'
  390. );
  391. $this->assertTags($result, $expected);
  392. }
  393. /**
  394. * test link with buffering off and safe on.
  395. *
  396. * @return void
  397. */
  398. public function testLinkWithNoBufferingAndSafe() {
  399. $this->_useMock();
  400. $this->Js->TestJsEngine->expects($this->at(1))
  401. ->method('request')
  402. ->with('/posts/view/1', array('update' => '#content'))
  403. ->will($this->returnValue('ajax code'));
  404. $this->Js->TestJsEngine->expects($this->at(2))
  405. ->method('event')
  406. ->will($this->returnValue('-event handler-'));
  407. $options = array('update' => '#content', 'buffer' => false, 'safe' => false);
  408. $result = $this->Js->link('test link', '/posts/view/1', $options);
  409. $expected = array(
  410. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  411. 'test link',
  412. '/a',
  413. 'script' => array('type' => 'text/javascript'),
  414. '-event handler-',
  415. '/script'
  416. );
  417. $this->assertTags($result, $expected);
  418. }
  419. /**
  420. * test submit() with a Mock to check Engine method calls
  421. *
  422. * @return void
  423. */
  424. public function testSubmitWithMock() {
  425. $this->_useMock();
  426. $options = array('update' => '#content', 'id' => 'test-submit', 'style' => 'margin: 0');
  427. $this->Js->TestJsEngine->expects($this->at(0))
  428. ->method('get');
  429. $this->Js->TestJsEngine->expects($this->at(1))
  430. ->method('serializeForm')
  431. ->will($this->returnValue('serialize-code'));
  432. $this->Js->TestJsEngine->expects($this->at(2))
  433. ->method('request')
  434. ->will($this->returnValue('ajax-code'));
  435. $params = array(
  436. 'update' => $options['update'], 'data' => 'serialize-code',
  437. 'method' => 'post', 'dataExpression' => true, 'buffer' => null
  438. );
  439. $this->Js->TestJsEngine->expects($this->at(3))
  440. ->method('event')
  441. ->with('click', "ajax-code", $params);
  442. $result = $this->Js->submit('Save', $options);
  443. $expected = array(
  444. 'div' => array('class' => 'submit'),
  445. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save', 'style' => 'margin: 0'),
  446. '/div'
  447. );
  448. $this->assertTags($result, $expected);
  449. }
  450. /**
  451. * test submit() with a mock
  452. *
  453. * @return void
  454. */
  455. public function testSubmitWithMockRequestParams() {
  456. $this->_useMock();
  457. $this->Js->TestJsEngine->expects($this->at(0))
  458. ->method('get');
  459. $this->Js->TestJsEngine->expects($this->at(1))
  460. ->method('serializeForm')
  461. ->will($this->returnValue('serialize-code'));
  462. $requestParams = array(
  463. 'update' => '#content',
  464. 'data' => 'serialize-code',
  465. 'method' => 'post',
  466. 'dataExpression' => true
  467. );
  468. $this->Js->TestJsEngine->expects($this->at(2))
  469. ->method('request')
  470. ->with('/custom/url', $requestParams)
  471. ->will($this->returnValue('ajax-code'));
  472. $params = array(
  473. 'update' => '#content', 'data' => 'serialize-code',
  474. 'method' => 'post', 'dataExpression' => true, 'buffer' => null
  475. );
  476. $this->Js->TestJsEngine->expects($this->at(3))
  477. ->method('event')
  478. ->with('click', "ajax-code", $params);
  479. $options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
  480. $result = $this->Js->submit('Save', $options);
  481. $expected = array(
  482. 'div' => array('class' => 'submit'),
  483. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
  484. '/div'
  485. );
  486. $this->assertTags($result, $expected);
  487. }
  488. /**
  489. * test that no buffer works with submit() and that parameters are leaking into the script tag.
  490. *
  491. * @return void
  492. */
  493. public function testSubmitWithNoBuffer() {
  494. $this->_useMock();
  495. $options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
  496. $this->Js->TestJsEngine->expects($this->at(0))
  497. ->method('get');
  498. $this->Js->TestJsEngine->expects($this->at(1))
  499. ->method('serializeForm')
  500. ->will($this->returnValue('serialize-code'));
  501. $this->Js->TestJsEngine->expects($this->at(2))
  502. ->method('request')
  503. ->will($this->returnValue('ajax-code'));
  504. $this->Js->TestJsEngine->expects($this->at(3))
  505. ->method('event')
  506. ->will($this->returnValue('event-handler'));
  507. $params = array(
  508. 'update' => $options['update'], 'data' => 'serialize-code',
  509. 'method' => 'post', 'dataExpression' => true, 'buffer' => false
  510. );
  511. $this->Js->TestJsEngine->expects($this->at(3))
  512. ->method('event')
  513. ->with('click', "ajax-code", $params);
  514. $result = $this->Js->submit('Save', $options);
  515. $expected = array(
  516. 'div' => array('class' => 'submit'),
  517. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
  518. '/div',
  519. 'script' => array('type' => 'text/javascript'),
  520. 'event-handler',
  521. '/script'
  522. );
  523. $this->assertTags($result, $expected);
  524. }
  525. /**
  526. * Test that Object::Object() is not breaking json output in JsHelper
  527. *
  528. * @return void
  529. */
  530. public function testObjectPassThrough() {
  531. $result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
  532. $expected = '{"one":"first","two":"second"}';
  533. $this->assertEqual($expected, $result);
  534. }
  535. /**
  536. * Test that inherited Helper::value() is overwritten in JsHelper::value()
  537. * and calls JsBaseEngineHelper::value().
  538. *
  539. * @return void
  540. */
  541. public function testValuePassThrough() {
  542. $result = $this->Js->value('string "quote"', true);
  543. $expected = '"string \"quote\""';
  544. $this->assertEqual($expected, $result);
  545. }
  546. /**
  547. * test set()'ing variables to the Javascript buffer and controlling the output var name.
  548. *
  549. * @return void
  550. */
  551. public function testSet() {
  552. $this->Js->set('loggedIn', true);
  553. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  554. $result = $this->Js->getBuffer();
  555. $expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
  556. $this->assertEqual($result[0], $expected);
  557. $this->Js->set('loggedIn', true);
  558. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  559. $this->Js->setVariable = 'WICKED';
  560. $result = $this->Js->getBuffer();
  561. $expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
  562. $this->assertEqual($result[0], $expected);
  563. $this->Js->set('loggedIn', true);
  564. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  565. $this->Js->setVariable = 'Application.variables';
  566. $result = $this->Js->getBuffer();
  567. $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
  568. $this->assertEqual($result[0], $expected);
  569. }
  570. /**
  571. * test that vars set with Js->set() go to the top of the buffered scripts list.
  572. *
  573. * @return void
  574. */
  575. public function testSetVarsAtTopOfBufferedScripts() {
  576. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  577. $this->Js->alert('hey you!', array('buffer' => true));
  578. $this->Js->confirm('Are you sure?', array('buffer' => true));
  579. $result = $this->Js->getBuffer(false);
  580. $expected = 'window.app = {"height":"tall","color":"purple"};';
  581. $this->assertEqual($result[0], $expected);
  582. $this->assertEqual($result[1], 'alert("hey you!");');
  583. $this->assertEqual($result[2], 'confirm("Are you sure?");');
  584. }
  585. }
  586. /**
  587. * JsBaseEngine Class Test case
  588. *
  589. * @package Cake.Test.Case.View.Helper
  590. */
  591. class JsBaseEngineTest extends CakeTestCase {
  592. /**
  593. * setUp method
  594. *
  595. * @return void
  596. */
  597. public function setUp() {
  598. parent::setUp();
  599. $controller = null;
  600. $this->View = $this->getMock('View', array('addScript'), array(&$controller));
  601. $this->JsEngine = new OptionEngineHelper($this->View);
  602. }
  603. /**
  604. * tearDown method
  605. *
  606. * @return void
  607. */
  608. public function tearDown() {
  609. parent::tearDown();
  610. unset($this->JsEngine);
  611. }
  612. /**
  613. * test escape string skills
  614. *
  615. * @return void
  616. */
  617. public function testEscaping() {
  618. $result = $this->JsEngine->escape('');
  619. $expected = '';
  620. $this->assertEqual($expected, $result);
  621. $result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
  622. $expected = 'CakePHP\\nRapid Development Framework';
  623. $this->assertEqual($expected, $result);
  624. $result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
  625. $expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
  626. $this->assertEqual($expected, $result);
  627. $result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
  628. $expected = 'CakePHP: \\"Rapid Development Framework\\"';
  629. $this->assertEqual($expected, $result);
  630. $result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
  631. $expected = "CakePHP: 'Rapid Development Framework'";
  632. $this->assertEqual($expected, $result);
  633. $result = $this->JsEngine->escape('my \\"string\\"');
  634. $expected = 'my \\\\\\"string\\\\\\"';
  635. $this->assertEqual($expected, $result);
  636. }
  637. /**
  638. * test prompt() creation
  639. *
  640. * @return void
  641. */
  642. public function testPrompt() {
  643. $result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
  644. $expected = 'prompt("Hey, hey you", "hi!");';
  645. $this->assertEqual($expected, $result);
  646. $result = $this->JsEngine->prompt('"Hey"', '"hi"');
  647. $expected = 'prompt("\"Hey\"", "\"hi\"");';
  648. $this->assertEqual($expected, $result);
  649. }
  650. /**
  651. * test alert generation
  652. *
  653. * @return void
  654. */
  655. public function testAlert() {
  656. $result = $this->JsEngine->alert('Hey there');
  657. $expected = 'alert("Hey there");';
  658. $this->assertEqual($expected, $result);
  659. $result = $this->JsEngine->alert('"Hey"');
  660. $expected = 'alert("\"Hey\"");';
  661. $this->assertEqual($expected, $result);
  662. }
  663. /**
  664. * test confirm generation
  665. *
  666. * @return void
  667. */
  668. public function testConfirm() {
  669. $result = $this->JsEngine->confirm('Are you sure?');
  670. $expected = 'confirm("Are you sure?");';
  671. $this->assertEqual($expected, $result);
  672. $result = $this->JsEngine->confirm('"Are you sure?"');
  673. $expected = 'confirm("\"Are you sure?\"");';
  674. $this->assertEqual($expected, $result);
  675. }
  676. /**
  677. * test Redirect
  678. *
  679. * @return void
  680. */
  681. public function testRedirect() {
  682. $result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
  683. $expected = 'window.location = "/posts/view/1";';
  684. $this->assertEqual($expected, $result);
  685. }
  686. /**
  687. * testObject encoding with non-native methods.
  688. *
  689. * @return void
  690. */
  691. public function testObject() {
  692. $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
  693. $result = $this->JsEngine->object($object);
  694. $expected = '{"title":"New thing","indexes":[5,6,7,8]}';
  695. $this->assertEqual($expected, $result);
  696. $object = new JsEncodingObject();
  697. $object->title = 'New thing';
  698. $object->indexes = array(5,6,7,8);
  699. $result = $this->JsEngine->object($object);
  700. $this->assertEqual($expected, $result);
  701. $result = $this->JsEngine->object(array('default' => 0));
  702. $expected = '{"default":0}';
  703. $this->assertEqual($expected, $result);
  704. $result = $this->JsEngine->object(array(
  705. '2007' => array(
  706. 'Spring' => array(
  707. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  708. ),
  709. 'Fall' => array(
  710. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  711. )
  712. ),
  713. '2006' => array(
  714. 'Spring' => array(
  715. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  716. ),
  717. 'Fall' => array(
  718. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  719. )
  720. )
  721. ));
  722. $expected = '{"2007":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}},"2006":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}}}';
  723. $this->assertEqual($expected, $result);
  724. foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
  725. $result = $this->JsEngine->object($data);
  726. $this->assertEqual($expected, $result);
  727. }
  728. $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
  729. $result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
  730. $this->assertPattern('/^PREFIX/', $result);
  731. $this->assertPattern('/POSTFIX$/', $result);
  732. $this->assertNoPattern('/.PREFIX./', $result);
  733. $this->assertNoPattern('/.POSTFIX./', $result);
  734. }
  735. /**
  736. * test Mapping of options.
  737. *
  738. * @return void
  739. */
  740. public function testOptionMapping() {
  741. $JsEngine = new OptionEngineHelper($this->View);
  742. $result = $JsEngine->testMap();
  743. $this->assertEqual($result, array());
  744. $result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
  745. $this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
  746. $result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
  747. $this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  748. $result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  749. $this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  750. }
  751. /**
  752. * test that option parsing escapes strings and saves what is supposed to be saved.
  753. *
  754. * @return void
  755. */
  756. public function testOptionParsing() {
  757. $JsEngine = new OptionEngineHelper($this->View);
  758. $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
  759. $expected = 'key:1, url:"\\/posts\\/view\\/1"';
  760. $this->assertEqual($expected, $result);
  761. $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
  762. $expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
  763. $this->assertEqual($expected, $result);
  764. }
  765. }