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

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

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