JqueryEngineHelperTest.php 12 KB

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