PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/LeThanhDat/firstdummyapp
PHP | 386 lines | 252 code | 42 blank | 92 comment | 0 complexity | f930b508f1f25dd940efdf0071322b09 MD5 | raw file
  1. <?php
  2. /**
  3. * JqueryEngineTestCase
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @package Cake.Test.Case.View.Helper
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('HtmlHelper', 'View/Helper');
  20. App::uses('JsHelper', 'View/Helper');
  21. App::uses('JqueryEngineHelper', 'View/Helper');
  22. App::uses('View', 'View');
  23. class JqueryEngineHelperTest extends CakeTestCase {
  24. /**
  25. * setUp
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $controller = null;
  32. $this->View = $this->getMock('View', array('addScript'), array(&$controller));
  33. $this->Jquery = new JqueryEngineHelper($this->View);
  34. }
  35. /**
  36. * tearDown
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. parent::tearDown();
  42. unset($this->Jquery);
  43. }
  44. /**
  45. * test selector method
  46. *
  47. * @return void
  48. */
  49. public function testSelector() {
  50. $result = $this->Jquery->get('#content');
  51. $this->assertEquals($this->Jquery, $result);
  52. $this->assertEquals($this->Jquery->selection, '$("#content")');
  53. $result = $this->Jquery->get('document');
  54. $this->assertEquals($this->Jquery, $result);
  55. $this->assertEquals($this->Jquery->selection, '$(document)');
  56. $result = $this->Jquery->get('window');
  57. $this->assertEquals($this->Jquery, $result);
  58. $this->assertEquals($this->Jquery->selection, '$(window)');
  59. $result = $this->Jquery->get('ul');
  60. $this->assertEquals($this->Jquery, $result);
  61. $this->assertEquals($this->Jquery->selection, '$("ul")');
  62. }
  63. /**
  64. * test event binding
  65. *
  66. * @return void
  67. */
  68. public function testEvent() {
  69. $this->Jquery->get('#myLink');
  70. $result = $this->Jquery->event('click', 'doClick', array('wrap' => false));
  71. $expected = '$("#myLink").bind("click", doClick);';
  72. $this->assertEquals($expected, $result);
  73. $result = $this->Jquery->event('click', '$(this).show();', array('stop' => false));
  74. $expected = '$("#myLink").bind("click", function (event) {$(this).show();});';
  75. $this->assertEquals($expected, $result);
  76. $result = $this->Jquery->event('click', '$(this).hide();');
  77. $expected = '$("#myLink").bind("click", function (event) {$(this).hide();' . "\n" . 'return false;});';
  78. $this->assertEquals($expected, $result);
  79. }
  80. /**
  81. * test dom ready event creation
  82. *
  83. * @return void
  84. */
  85. public function testDomReady() {
  86. $result = $this->Jquery->domReady('foo.name = "bar";');
  87. $expected = '$(document).ready(function () {foo.name = "bar";});';
  88. $this->assertEquals($expected, $result);
  89. }
  90. /**
  91. * test Each method
  92. *
  93. * @return void
  94. */
  95. public function testEach() {
  96. $this->Jquery->get('#foo');
  97. $result = $this->Jquery->each('$(this).hide();');
  98. $expected = '$("#foo").each(function () {$(this).hide();});';
  99. $this->assertEquals($expected, $result);
  100. }
  101. /**
  102. * test Effect generation
  103. *
  104. * @return void
  105. */
  106. public function testEffect() {
  107. $this->Jquery->get('#foo');
  108. $result = $this->Jquery->effect('show');
  109. $expected = '$("#foo").show();';
  110. $this->assertEquals($expected, $result);
  111. $result = $this->Jquery->effect('hide');
  112. $expected = '$("#foo").hide();';
  113. $this->assertEquals($expected, $result);
  114. $result = $this->Jquery->effect('hide', array('speed' => 'fast'));
  115. $expected = '$("#foo").hide("fast");';
  116. $this->assertEquals($expected, $result);
  117. $result = $this->Jquery->effect('fadeIn');
  118. $expected = '$("#foo").fadeIn();';
  119. $this->assertEquals($expected, $result);
  120. $result = $this->Jquery->effect('fadeOut');
  121. $expected = '$("#foo").fadeOut();';
  122. $this->assertEquals($expected, $result);
  123. $result = $this->Jquery->effect('slideIn');
  124. $expected = '$("#foo").slideDown();';
  125. $this->assertEquals($expected, $result);
  126. $result = $this->Jquery->effect('slideOut');
  127. $expected = '$("#foo").slideUp();';
  128. $this->assertEquals($expected, $result);
  129. $result = $this->Jquery->effect('slideDown');
  130. $expected = '$("#foo").slideDown();';
  131. $this->assertEquals($expected, $result);
  132. $result = $this->Jquery->effect('slideUp');
  133. $expected = '$("#foo").slideUp();';
  134. $this->assertEquals($expected, $result);
  135. }
  136. /**
  137. * Test Request Generation
  138. *
  139. * @return void
  140. */
  141. public function testRequest() {
  142. $result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1));
  143. $expected = '$.ajax({url:"\\/posts\\/view\\/1"});';
  144. $this->assertEquals($expected, $result);
  145. $result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1), array(
  146. 'update' => '#content'
  147. ));
  148. $expected = '$.ajax({dataType:"html", success:function (data, textStatus) {$("#content").html(data);}, url:"\/posts\/view\/1"});';
  149. $this->assertEquals($expected, $result);
  150. $result = $this->Jquery->request('/people/edit/1', array(
  151. 'method' => 'post',
  152. 'before' => 'doBefore',
  153. 'complete' => 'doComplete',
  154. 'success' => 'doSuccess',
  155. 'error' => 'handleError',
  156. 'type' => 'json',
  157. 'data' => array('name' => 'jim', 'height' => '185cm'),
  158. 'wrapCallbacks' => false
  159. ));
  160. $expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, success:doSuccess, type:"post", url:"\\/people\\/edit\\/1"});';
  161. $this->assertEquals($expected, $result);
  162. $result = $this->Jquery->request('/people/edit/1', array(
  163. 'update' => '#updated',
  164. 'success' => 'doFoo',
  165. 'method' => 'post',
  166. 'wrapCallbacks' => false
  167. ));
  168. $expected = '$.ajax({dataType:"html", success:function (data, textStatus) {doFoo$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
  169. $this->assertEquals($expected, $result);
  170. $result = $this->Jquery->request('/people/edit/1', array(
  171. 'update' => '#updated',
  172. 'success' => 'doFoo',
  173. 'method' => 'post',
  174. 'dataExpression' => true,
  175. 'data' => '$("#someId").serialize()',
  176. 'wrapCallbacks' => false
  177. ));
  178. $expected = '$.ajax({data:$("#someId").serialize(), dataType:"html", success:function (data, textStatus) {doFoo$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
  179. $this->assertEquals($expected, $result);
  180. $result = $this->Jquery->request('/people/edit/1', array(
  181. 'success' => 'doFoo',
  182. 'before' => 'doBefore',
  183. 'method' => 'post',
  184. 'dataExpression' => true,
  185. 'data' => '$("#someId").serialize()',
  186. ));
  187. $expected = '$.ajax({beforeSend:function (XMLHttpRequest) {doBefore}, data:$("#someId").serialize(), success:function (data, textStatus) {doFoo}, type:"post", url:"\\/people\\/edit\\/1"});';
  188. $this->assertEquals($expected, $result);
  189. }
  190. /**
  191. * Test that querystring arguments are not double escaped.
  192. *
  193. * @return void
  194. */
  195. public function testRequestWithQueryStringArguments() {
  196. $url = '/users/search/sort:User.name/direction:desc?nome=&cpm=&audience=public';
  197. $result = $this->Jquery->request($url);
  198. $expected = '$.ajax({url:"\\/users\\/search\\/sort:User.name\\/direction:desc?nome=&cpm=&audience=public"});';
  199. $this->assertEquals($expected, $result);
  200. }
  201. /**
  202. * test that alternate jQuery object values work for request()
  203. *
  204. * @return void
  205. */
  206. public function testRequestWithAlternateJqueryObject() {
  207. $this->Jquery->jQueryObject = '$j';
  208. $result = $this->Jquery->request('/people/edit/1', array(
  209. 'update' => '#updated',
  210. 'success' => 'doFoo',
  211. 'method' => 'post',
  212. 'dataExpression' => true,
  213. 'data' => '$j("#someId").serialize()',
  214. 'wrapCallbacks' => false
  215. ));
  216. $expected = '$j.ajax({data:$j("#someId").serialize(), dataType:"html", success:function (data, textStatus) {doFoo$j("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
  217. $this->assertEquals($expected, $result);
  218. }
  219. /**
  220. * test sortable list generation
  221. *
  222. * @return void
  223. */
  224. public function testSortable() {
  225. $this->Jquery->get('#myList');
  226. $result = $this->Jquery->sortable(array(
  227. 'distance' => 5,
  228. 'containment' => 'parent',
  229. 'start' => 'onStart',
  230. 'complete' => 'onStop',
  231. 'sort' => 'onSort',
  232. 'wrapCallbacks' => false
  233. ));
  234. $expected = '$("#myList").sortable({containment:"parent", distance:5, sort:onSort, start:onStart, stop:onStop});';
  235. $this->assertEquals($expected, $result);
  236. $result = $this->Jquery->sortable(array(
  237. 'distance' => 5,
  238. 'containment' => 'parent',
  239. 'start' => 'onStart',
  240. 'complete' => 'onStop',
  241. 'sort' => 'onSort',
  242. ));
  243. $expected = '$("#myList").sortable({containment:"parent", distance:5, sort:function (event, ui) {onSort}, start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
  244. $this->assertEquals($expected, $result);
  245. }
  246. /**
  247. * test drag() method
  248. *
  249. * @return void
  250. */
  251. public function testDrag() {
  252. $this->Jquery->get('#element');
  253. $result = $this->Jquery->drag(array(
  254. 'container' => '#content',
  255. 'start' => 'onStart',
  256. 'drag' => 'onDrag',
  257. 'stop' => 'onStop',
  258. 'snapGrid' => array(10, 10),
  259. 'wrapCallbacks' => false
  260. ));
  261. $expected = '$("#element").draggable({containment:"#content", drag:onDrag, grid:[10,10], start:onStart, stop:onStop});';
  262. $this->assertEquals($expected, $result);
  263. $result = $this->Jquery->drag(array(
  264. 'container' => '#content',
  265. 'start' => 'onStart',
  266. 'drag' => 'onDrag',
  267. 'stop' => 'onStop',
  268. 'snapGrid' => array(10, 10),
  269. ));
  270. $expected = '$("#element").draggable({containment:"#content", drag:function (event, ui) {onDrag}, grid:[10,10], start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
  271. $this->assertEquals($expected, $result);
  272. }
  273. /**
  274. * test drop() method
  275. *
  276. * @return void
  277. */
  278. public function testDrop() {
  279. $this->Jquery->get('#element');
  280. $result = $this->Jquery->drop(array(
  281. 'accept' => '.items',
  282. 'hover' => 'onHover',
  283. 'leave' => 'onExit',
  284. 'drop' => 'onDrop',
  285. 'wrapCallbacks' => false
  286. ));
  287. $expected = '$("#element").droppable({accept:".items", drop:onDrop, out:onExit, over:onHover});';
  288. $this->assertEquals($expected, $result);
  289. $result = $this->Jquery->drop(array(
  290. 'accept' => '.items',
  291. 'hover' => 'onHover',
  292. 'leave' => 'onExit',
  293. 'drop' => 'onDrop',
  294. ));
  295. $expected = '$("#element").droppable({accept:".items", drop:function (event, ui) {onDrop}, out:function (event, ui) {onExit}, over:function (event, ui) {onHover}});';
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * test slider generation
  300. *
  301. * @return void
  302. */
  303. public function testSlider() {
  304. $this->Jquery->get('#element');
  305. $result = $this->Jquery->slider(array(
  306. 'complete' => 'onComplete',
  307. 'change' => 'onChange',
  308. 'min' => 0,
  309. 'max' => 10,
  310. 'value' => 2,
  311. 'direction' => 'vertical',
  312. 'wrapCallbacks' => false
  313. ));
  314. $expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
  315. $this->assertEquals($expected, $result);
  316. $result = $this->Jquery->slider(array(
  317. 'complete' => 'onComplete',
  318. 'change' => 'onChange',
  319. 'min' => 0,
  320. 'max' => 10,
  321. 'value' => 2,
  322. 'direction' => 'vertical',
  323. ));
  324. $expected = '$("#element").slider({change:function (event, ui) {onChange}, max:10, min:0, orientation:"vertical", stop:function (event, ui) {onComplete}, value:2});';
  325. $this->assertEquals($expected, $result);
  326. }
  327. /**
  328. * test the serializeForm method
  329. *
  330. * @return void
  331. */
  332. public function testSerializeForm() {
  333. $this->Jquery->get('#element');
  334. $result = $this->Jquery->serializeForm(array('isForm' => false));
  335. $expected = '$("#element").closest("form").serialize();';
  336. $this->assertEquals($expected, $result);
  337. $result = $this->Jquery->serializeForm(array('isForm' => true));
  338. $expected = '$("#element").serialize();';
  339. $this->assertEquals($expected, $result);
  340. $result = $this->Jquery->serializeForm(array('isForm' => false, 'inline' => true));
  341. $expected = '$("#element").closest("form").serialize()';
  342. $this->assertEquals($expected, $result);
  343. }
  344. }