PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/js.test.php

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