JqueryEngineHelperTest.php 12 KB

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