PageRenderTime 90ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/View/Helper/JsHelperTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 902 lines | 556 code | 129 blank | 217 comment | 3 complexity | 32451cbbf2278b2f734efc802b391473 MD5 | raw file
  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->assertEquals($js->helpers, array('Html', 'Form', 'JqueryEngine'));
  142. $js = new JsHelper($this->View, array('mootools'));
  143. $this->assertEquals($js->helpers, array('Html', 'Form', 'mootoolsEngine'));
  144. $js = new JsHelper($this->View, 'prototype');
  145. $this->assertEquals($js->helpers, array('Html', 'Form', 'prototypeEngine'));
  146. $js = new JsHelper($this->View, 'MyPlugin.Dojo');
  147. $this->assertEquals($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
  148. }
  149. /**
  150. * test that methods dispatch internally and to the engine class
  151. *
  152. * @expectedException PHPUnit_Framework_Error_Warning
  153. * @return void
  154. */
  155. public function testMethodDispatching() {
  156. $this->_useMock();
  157. $this->Js->TestJsEngine
  158. ->expects($this->once())
  159. ->method('event')
  160. ->with('click', 'callback');
  161. $this->Js->event('click', 'callback');
  162. $this->Js->TestJsEngine = new StdClass();
  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->assertEquals(count($result), 1);
  179. $this->assertEquals($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->assertEquals($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->assertEquals($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->assertEquals($result, 'I am not buffered.');
  203. $result = $this->Js->effect('slideIn', true);
  204. $buffer = $this->Js->getBuffer();
  205. $this->assertNull($result);
  206. $this->assertEquals(count($buffer), 1);
  207. $this->assertEquals($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->assertEquals(count($buffer), 1);
  212. $this->assertEquals($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->assertEquals(count($buffer), 1);
  217. $this->assertEquals($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. $requestWith = null;
  265. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  266. $requestWith = $_SERVER['HTTP_X_REQUESTED_WITH'];
  267. }
  268. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  269. $this->Js->buffer('alert("test");');
  270. $this->Js->TestJsEngine->expects($this->never())->method('domReady');
  271. $result = $this->Js->writeBuffer();
  272. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  273. if ($requestWith !== null) {
  274. $_SERVER['HTTP_X_REQUESTED_WITH'] = $requestWith;
  275. }
  276. }
  277. /**
  278. * test that writeScripts makes files, and puts the events into them.
  279. *
  280. * @return void
  281. */
  282. public function testWriteScriptsInFile() {
  283. $this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped.');
  284. $this->Js->request->webroot = '/';
  285. $this->Js->JsBaseEngine = new TestJsEngineHelper($this->View);
  286. $this->Js->buffer('one = 1;');
  287. $this->Js->buffer('two = 2;');
  288. $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
  289. $expected = array(
  290. 'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
  291. );
  292. $this->assertTags($result, $expected);
  293. preg_match('/src="(.*\.js)"/', $result, $filename);
  294. $this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
  295. $contents = file_get_contents(WWW_ROOT . $filename[1]);
  296. $this->assertRegExp('/one\s=\s1;\ntwo\s=\s2;/', $contents);
  297. @unlink(WWW_ROOT . $filename[1]);
  298. }
  299. /**
  300. * test link()
  301. *
  302. * @return void
  303. */
  304. public function testLinkWithMock() {
  305. $this->_useMock();
  306. $options = array('update' => '#content');
  307. $this->Js->TestJsEngine->expects($this->at(0))
  308. ->method('get');
  309. $this->Js->TestJsEngine->expects($this->at(1))
  310. ->method('request')
  311. ->with('/posts/view/1', $options)
  312. ->will($this->returnValue('--ajax code--'));
  313. $this->Js->TestJsEngine->expects($this->at(2))
  314. ->method('event')
  315. ->with('click', '--ajax code--', $options + array('buffer' => null));
  316. $result = $this->Js->link('test link', '/posts/view/1', $options);
  317. $expected = array(
  318. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  319. 'test link',
  320. '/a'
  321. );
  322. $this->assertTags($result, $expected);
  323. }
  324. /**
  325. * test link with a mock and confirmation
  326. *
  327. * @return void
  328. */
  329. public function testLinkWithMockAndConfirm() {
  330. $this->_useMock();
  331. $options = array(
  332. 'confirm' => 'Are you sure?',
  333. 'update' => '#content',
  334. 'class' => 'my-class',
  335. 'id' => 'custom-id',
  336. 'escape' => false
  337. );
  338. $this->Js->TestJsEngine->expects($this->once())
  339. ->method('confirmReturn')
  340. ->with($options['confirm'])
  341. ->will($this->returnValue('--confirm script--'));
  342. $this->Js->TestJsEngine->expects($this->once())
  343. ->method('request')
  344. ->with('/posts/view/1');
  345. $this->Js->TestJsEngine->expects($this->once())
  346. ->method('event')
  347. ->with('click', '--confirm script--');
  348. $result = $this->Js->link('test link Âť', '/posts/view/1', $options);
  349. $expected = array(
  350. 'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
  351. 'test link Âť',
  352. '/a'
  353. );
  354. $this->assertTags($result, $expected);
  355. }
  356. /**
  357. * test link passing on htmlAttributes
  358. *
  359. * @return void
  360. */
  361. public function testLinkWithAribtraryAttributes() {
  362. $this->_useMock();
  363. $options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
  364. $result = $this->Js->link('test link', '/posts/view/1', $options);
  365. $expected = array(
  366. 'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
  367. 'batman' => 'robin'),
  368. 'test link',
  369. '/a'
  370. );
  371. $this->assertTags($result, $expected);
  372. }
  373. /**
  374. * test that link() and no buffering returns an <a> and <script> tags.
  375. *
  376. * @return void
  377. */
  378. public function testLinkWithNoBuffering() {
  379. $this->_useMock();
  380. $this->Js->TestJsEngine->expects($this->at(1))
  381. ->method('request')
  382. ->with('/posts/view/1', array('update' => '#content'))
  383. ->will($this->returnValue('ajax code'));
  384. $this->Js->TestJsEngine->expects($this->at(2))
  385. ->method('event')
  386. ->will($this->returnValue('-event handler-'));
  387. $options = array('update' => '#content', 'buffer' => false);
  388. $result = $this->Js->link('test link', '/posts/view/1', $options);
  389. $expected = array(
  390. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  391. 'test link',
  392. '/a',
  393. 'script' => array('type' => 'text/javascript'),
  394. $this->cDataStart,
  395. '-event handler-',
  396. $this->cDataEnd,
  397. '/script'
  398. );
  399. $this->assertTags($result, $expected);
  400. }
  401. /**
  402. * test link with buffering off and safe on.
  403. *
  404. * @return void
  405. */
  406. public function testLinkWithNoBufferingAndSafe() {
  407. $this->_useMock();
  408. $this->Js->TestJsEngine->expects($this->at(1))
  409. ->method('request')
  410. ->with('/posts/view/1', array('update' => '#content'))
  411. ->will($this->returnValue('ajax code'));
  412. $this->Js->TestJsEngine->expects($this->at(2))
  413. ->method('event')
  414. ->will($this->returnValue('-event handler-'));
  415. $options = array('update' => '#content', 'buffer' => false, 'safe' => false);
  416. $result = $this->Js->link('test link', '/posts/view/1', $options);
  417. $expected = array(
  418. 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
  419. 'test link',
  420. '/a',
  421. 'script' => array('type' => 'text/javascript'),
  422. '-event handler-',
  423. '/script'
  424. );
  425. $this->assertTags($result, $expected);
  426. }
  427. /**
  428. * test submit() with a Mock to check Engine method calls
  429. *
  430. * @return void
  431. */
  432. public function testSubmitWithMock() {
  433. $this->_useMock();
  434. $options = array('update' => '#content', 'id' => 'test-submit', 'style' => 'margin: 0');
  435. $this->Js->TestJsEngine->expects($this->at(0))
  436. ->method('get');
  437. $this->Js->TestJsEngine->expects($this->at(1))
  438. ->method('serializeForm')
  439. ->will($this->returnValue('serialize-code'));
  440. $this->Js->TestJsEngine->expects($this->at(2))
  441. ->method('request')
  442. ->will($this->returnValue('ajax-code'));
  443. $params = array(
  444. 'update' => $options['update'], 'data' => 'serialize-code',
  445. 'method' => 'post', 'dataExpression' => true, 'buffer' => null
  446. );
  447. $this->Js->TestJsEngine->expects($this->at(3))
  448. ->method('event')
  449. ->with('click', "ajax-code", $params);
  450. $result = $this->Js->submit('Save', $options);
  451. $expected = array(
  452. 'div' => array('class' => 'submit'),
  453. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save', 'style' => 'margin: 0'),
  454. '/div'
  455. );
  456. $this->assertTags($result, $expected);
  457. }
  458. /**
  459. * test submit() with a mock
  460. *
  461. * @return void
  462. */
  463. public function testSubmitWithMockRequestParams() {
  464. $this->_useMock();
  465. $this->Js->TestJsEngine->expects($this->at(0))
  466. ->method('get');
  467. $this->Js->TestJsEngine->expects($this->at(1))
  468. ->method('serializeForm')
  469. ->will($this->returnValue('serialize-code'));
  470. $requestParams = array(
  471. 'update' => '#content',
  472. 'data' => 'serialize-code',
  473. 'method' => 'post',
  474. 'dataExpression' => true
  475. );
  476. $this->Js->TestJsEngine->expects($this->at(2))
  477. ->method('request')
  478. ->with('/custom/url', $requestParams)
  479. ->will($this->returnValue('ajax-code'));
  480. $params = array(
  481. 'update' => '#content', 'data' => 'serialize-code',
  482. 'method' => 'post', 'dataExpression' => true, 'buffer' => null
  483. );
  484. $this->Js->TestJsEngine->expects($this->at(3))
  485. ->method('event')
  486. ->with('click', "ajax-code", $params);
  487. $options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
  488. $result = $this->Js->submit('Save', $options);
  489. $expected = array(
  490. 'div' => array('class' => 'submit'),
  491. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
  492. '/div'
  493. );
  494. $this->assertTags($result, $expected);
  495. }
  496. /**
  497. * test that no buffer works with submit() and that parameters are leaking into the script tag.
  498. *
  499. * @return void
  500. */
  501. public function testSubmitWithNoBuffer() {
  502. $this->_useMock();
  503. $options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
  504. $this->Js->TestJsEngine->expects($this->at(0))
  505. ->method('get');
  506. $this->Js->TestJsEngine->expects($this->at(1))
  507. ->method('serializeForm')
  508. ->will($this->returnValue('serialize-code'));
  509. $this->Js->TestJsEngine->expects($this->at(2))
  510. ->method('request')
  511. ->will($this->returnValue('ajax-code'));
  512. $this->Js->TestJsEngine->expects($this->at(3))
  513. ->method('event')
  514. ->will($this->returnValue('event-handler'));
  515. $params = array(
  516. 'update' => $options['update'], 'data' => 'serialize-code',
  517. 'method' => 'post', 'dataExpression' => true, 'buffer' => false
  518. );
  519. $this->Js->TestJsEngine->expects($this->at(3))
  520. ->method('event')
  521. ->with('click', "ajax-code", $params);
  522. $result = $this->Js->submit('Save', $options);
  523. $expected = array(
  524. 'div' => array('class' => 'submit'),
  525. 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
  526. '/div',
  527. 'script' => array('type' => 'text/javascript'),
  528. 'event-handler',
  529. '/script'
  530. );
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * Test that Object::Object() is not breaking json output in JsHelper
  535. *
  536. * @return void
  537. */
  538. public function testObjectPassThrough() {
  539. $result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
  540. $expected = '{"one":"first","two":"second"}';
  541. $this->assertEquals($expected, $result);
  542. }
  543. /**
  544. * Test that inherited Helper::value() is overwritten in JsHelper::value()
  545. * and calls JsBaseEngineHelper::value().
  546. *
  547. * @return void
  548. */
  549. public function testValuePassThrough() {
  550. $result = $this->Js->value('string "quote"', true);
  551. $expected = '"string \"quote\""';
  552. $this->assertEquals($expected, $result);
  553. }
  554. /**
  555. * test set()'ing variables to the Javascript buffer and controlling the output var name.
  556. *
  557. * @return void
  558. */
  559. public function testSet() {
  560. $this->Js->set('loggedIn', true);
  561. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  562. $result = $this->Js->getBuffer();
  563. $expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
  564. $this->assertEquals($result[0], $expected);
  565. $this->Js->set('loggedIn', true);
  566. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  567. $this->Js->setVariable = 'WICKED';
  568. $result = $this->Js->getBuffer();
  569. $expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
  570. $this->assertEquals($result[0], $expected);
  571. $this->Js->set('loggedIn', true);
  572. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  573. $this->Js->setVariable = 'Application.variables';
  574. $result = $this->Js->getBuffer();
  575. $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
  576. $this->assertEquals($result[0], $expected);
  577. }
  578. /**
  579. * test that vars set with Js->set() go to the top of the buffered scripts list.
  580. *
  581. * @return void
  582. */
  583. public function testSetVarsAtTopOfBufferedScripts() {
  584. $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
  585. $this->Js->alert('hey you!', array('buffer' => true));
  586. $this->Js->confirm('Are you sure?', array('buffer' => true));
  587. $result = $this->Js->getBuffer(false);
  588. $expected = 'window.app = {"height":"tall","color":"purple"};';
  589. $this->assertEquals($result[0], $expected);
  590. $this->assertEquals($result[1], 'alert("hey you!");');
  591. $this->assertEquals($result[2], 'confirm("Are you sure?");');
  592. }
  593. }
  594. /**
  595. * JsBaseEngine Class Test case
  596. *
  597. * @package Cake.Test.Case.View.Helper
  598. */
  599. class JsBaseEngineTest extends CakeTestCase {
  600. /**
  601. * setUp method
  602. *
  603. * @return void
  604. */
  605. public function setUp() {
  606. parent::setUp();
  607. $controller = null;
  608. $this->View = $this->getMock('View', array('addScript'), array(&$controller));
  609. $this->JsEngine = new OptionEngineHelper($this->View);
  610. }
  611. /**
  612. * tearDown method
  613. *
  614. * @return void
  615. */
  616. public function tearDown() {
  617. parent::tearDown();
  618. unset($this->JsEngine);
  619. }
  620. /**
  621. * test escape string skills
  622. *
  623. * @return void
  624. */
  625. public function testEscaping() {
  626. $result = $this->JsEngine->escape('');
  627. $expected = '';
  628. $this->assertEquals($expected, $result);
  629. $result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
  630. $expected = 'CakePHP\\nRapid Development Framework';
  631. $this->assertEquals($expected, $result);
  632. $result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
  633. $expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
  634. $this->assertEquals($expected, $result);
  635. $result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
  636. $expected = 'CakePHP: \\"Rapid Development Framework\\"';
  637. $this->assertEquals($expected, $result);
  638. $result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
  639. $expected = "CakePHP: 'Rapid Development Framework'";
  640. $this->assertEquals($expected, $result);
  641. $result = $this->JsEngine->escape('my \\"string\\"');
  642. $expected = 'my \\\\\\"string\\\\\\"';
  643. $this->assertEquals($expected, $result);
  644. }
  645. /**
  646. * test prompt() creation
  647. *
  648. * @return void
  649. */
  650. public function testPrompt() {
  651. $result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
  652. $expected = 'prompt("Hey, hey you", "hi!");';
  653. $this->assertEquals($expected, $result);
  654. $result = $this->JsEngine->prompt('"Hey"', '"hi"');
  655. $expected = 'prompt("\"Hey\"", "\"hi\"");';
  656. $this->assertEquals($expected, $result);
  657. }
  658. /**
  659. * test alert generation
  660. *
  661. * @return void
  662. */
  663. public function testAlert() {
  664. $result = $this->JsEngine->alert('Hey there');
  665. $expected = 'alert("Hey there");';
  666. $this->assertEquals($expected, $result);
  667. $result = $this->JsEngine->alert('"Hey"');
  668. $expected = 'alert("\"Hey\"");';
  669. $this->assertEquals($expected, $result);
  670. }
  671. /**
  672. * test confirm generation
  673. *
  674. * @return void
  675. */
  676. public function testConfirm() {
  677. $result = $this->JsEngine->confirm('Are you sure?');
  678. $expected = 'confirm("Are you sure?");';
  679. $this->assertEquals($expected, $result);
  680. $result = $this->JsEngine->confirm('"Are you sure?"');
  681. $expected = 'confirm("\"Are you sure?\"");';
  682. $this->assertEquals($expected, $result);
  683. }
  684. /**
  685. * test Redirect
  686. *
  687. * @return void
  688. */
  689. public function testRedirect() {
  690. $result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
  691. $expected = 'window.location = "/posts/view/1";';
  692. $this->assertEquals($expected, $result);
  693. }
  694. /**
  695. * testObject encoding with non-native methods.
  696. *
  697. * @return void
  698. */
  699. public function testObject() {
  700. $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
  701. $result = $this->JsEngine->object($object);
  702. $expected = '{"title":"New thing","indexes":[5,6,7,8]}';
  703. $this->assertEquals($expected, $result);
  704. $object = new JsEncodingObject();
  705. $object->title = 'New thing';
  706. $object->indexes = array(5,6,7,8);
  707. $result = $this->JsEngine->object($object);
  708. $this->assertEquals($expected, $result);
  709. $result = $this->JsEngine->object(array('default' => 0));
  710. $expected = '{"default":0}';
  711. $this->assertEquals($expected, $result);
  712. $result = $this->JsEngine->object(array(
  713. '2007' => 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. '2006' => array(
  722. 'Spring' => array(
  723. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  724. ),
  725. 'Fall' => array(
  726. '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
  727. )
  728. )
  729. ));
  730. $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"}}}}';
  731. $this->assertEquals($expected, $result);
  732. foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
  733. $result = $this->JsEngine->object($data);
  734. $this->assertEquals($expected, $result);
  735. }
  736. $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
  737. $result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
  738. $this->assertRegExp('/^PREFIX/', $result);
  739. $this->assertRegExp('/POSTFIX$/', $result);
  740. $this->assertNotRegExp('/.PREFIX./', $result);
  741. $this->assertNotRegExp('/.POSTFIX./', $result);
  742. }
  743. /**
  744. * test Mapping of options.
  745. *
  746. * @return void
  747. */
  748. public function testOptionMapping() {
  749. $JsEngine = new OptionEngineHelper($this->View);
  750. $result = $JsEngine->testMap();
  751. $this->assertEquals($result, array());
  752. $result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
  753. $this->assertEquals($result, array('foo' => 'bar', 'baz' => 'sho'));
  754. $result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
  755. $this->assertEquals($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  756. $result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  757. $this->assertEquals($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
  758. }
  759. /**
  760. * test that option parsing escapes strings and saves what is supposed to be saved.
  761. *
  762. * @return void
  763. */
  764. public function testOptionParsing() {
  765. $JsEngine = new OptionEngineHelper($this->View);
  766. $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
  767. $expected = 'key:1, url:"\\/posts\\/view\\/1"';
  768. $this->assertEquals($expected, $result);
  769. $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
  770. $expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
  771. $this->assertEquals($expected, $result);
  772. }
  773. }