PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/Cake/Test/Case/View/Helper/PrototypeEngineHelperTest.php

https://gitlab.com/digaotinfo/abear.com.br
PHP | 386 lines | 258 code | 46 blank | 82 comment | 0 complexity | f3b4d5c93ec879617a32a7df6596a1c8 MD5 | raw file
  1. <?php
  2. /**
  3. * PrototypeEngine TestCase
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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('View', 'View');
  19. App::uses('HtmlHelper', 'View/Helper');
  20. App::uses('JsHelper', 'View/Helper');
  21. App::uses('PrototypeEngineHelper', 'View/Helper');
  22. class PrototypeEngineHelperTest 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->Proto = new PrototypeEngineHelper($this->View);
  33. }
  34. /**
  35. * tearDown
  36. *
  37. * @return void
  38. */
  39. public function tearDown() {
  40. parent::tearDown();
  41. unset($this->Proto);
  42. }
  43. /**
  44. * test selector method
  45. *
  46. * @return void
  47. */
  48. public function testSelector() {
  49. $result = $this->Proto->get('#content');
  50. $this->assertEquals($this->Proto, $result);
  51. $this->assertEquals($this->Proto->selection, '$("content")');
  52. $result = $this->Proto->get('a .remove');
  53. $this->assertEquals($this->Proto, $result);
  54. $this->assertEquals($this->Proto->selection, '$$("a .remove")');
  55. $result = $this->Proto->get('document');
  56. $this->assertEquals($this->Proto, $result);
  57. $this->assertEquals($this->Proto->selection, "$(document)");
  58. $result = $this->Proto->get('window');
  59. $this->assertEquals($this->Proto, $result);
  60. $this->assertEquals($this->Proto->selection, "$(window)");
  61. $result = $this->Proto->get('ul');
  62. $this->assertEquals($this->Proto, $result);
  63. $this->assertEquals($this->Proto->selection, '$$("ul")');
  64. $result = $this->Proto->get('#some_long-id.class');
  65. $this->assertEquals($this->Proto, $result);
  66. $this->assertEquals($this->Proto->selection, '$$("#some_long-id.class")');
  67. }
  68. /**
  69. * test event binding
  70. *
  71. * @return void
  72. */
  73. public function testEvent() {
  74. $this->Proto->get('#myLink');
  75. $result = $this->Proto->event('click', 'doClick', array('wrap' => false));
  76. $expected = '$("myLink").observe("click", doClick);';
  77. $this->assertEquals($expected, $result);
  78. $result = $this->Proto->event('click', 'Element.hide(this);', array('stop' => false));
  79. $expected = '$("myLink").observe("click", function (event) {Element.hide(this);});';
  80. $this->assertEquals($expected, $result);
  81. $result = $this->Proto->event('click', 'Element.hide(this);');
  82. $expected = "\$(\"myLink\").observe(\"click\", function (event) {event.stop();\nElement.hide(this);});";
  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->Proto->domReady('foo.name = "bar";');
  92. $expected = 'document.observe("dom:loaded", function (event) {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->Proto->get('#foo li');
  102. $result = $this->Proto->each('item.hide();');
  103. $expected = '$$("#foo li").each(function (item, index) {item.hide();});';
  104. $this->assertEquals($expected, $result);
  105. }
  106. /**
  107. * test Effect generation
  108. *
  109. * @return void
  110. */
  111. public function testEffect() {
  112. $this->Proto->get('#foo');
  113. $result = $this->Proto->effect('show');
  114. $expected = '$("foo").show();';
  115. $this->assertEquals($expected, $result);
  116. $result = $this->Proto->effect('hide');
  117. $expected = '$("foo").hide();';
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Proto->effect('fadeIn');
  120. $expected = '$("foo").appear();';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->Proto->effect('fadeIn', array('speed' => 'fast'));
  123. $expected = '$("foo").appear({duration:0.50000000000});';
  124. $this->assertEquals($expected, $result);
  125. $result = $this->Proto->effect('fadeIn', array('speed' => 'slow'));
  126. $expected = '$("foo").appear({duration:2});';
  127. $this->assertEquals($expected, $result);
  128. $result = $this->Proto->effect('fadeOut');
  129. $expected = '$("foo").fade();';
  130. $this->assertEquals($expected, $result);
  131. $result = $this->Proto->effect('fadeOut', array('speed' => 'fast'));
  132. $expected = '$("foo").fade({duration:0.50000000000});';
  133. $this->assertEquals($expected, $result);
  134. $result = $this->Proto->effect('fadeOut', array('speed' => 'slow'));
  135. $expected = '$("foo").fade({duration:2});';
  136. $this->assertEquals($expected, $result);
  137. $result = $this->Proto->effect('slideIn');
  138. $expected = 'Effect.slideDown($("foo"));';
  139. $this->assertEquals($expected, $result);
  140. $result = $this->Proto->effect('slideOut');
  141. $expected = 'Effect.slideUp($("foo"));';
  142. $this->assertEquals($expected, $result);
  143. $result = $this->Proto->effect('slideOut', array('speed' => 'fast'));
  144. $expected = 'Effect.slideUp($("foo"), {duration:0.50000000000});';
  145. $this->assertEquals($expected, $result);
  146. $result = $this->Proto->effect('slideOut', array('speed' => 'slow'));
  147. $expected = 'Effect.slideUp($("foo"), {duration:2});';
  148. $this->assertEquals($expected, $result);
  149. }
  150. /**
  151. * Test Request Generation
  152. *
  153. * @return void
  154. */
  155. public function testRequest() {
  156. $result = $this->Proto->request(array('controller' => 'posts', 'action' => 'view', 1));
  157. $expected = 'var jsRequest = new Ajax.Request("/posts/view/1");';
  158. $this->assertEquals($expected, $result);
  159. $result = $this->Proto->request('/posts/view/1', array(
  160. 'method' => 'post',
  161. 'complete' => 'doComplete',
  162. 'before' => 'doBefore',
  163. 'success' => 'doSuccess',
  164. 'error' => 'doError',
  165. 'data' => array('name' => 'jim', 'height' => '185cm'),
  166. 'wrapCallbacks' => false
  167. ));
  168. $expected = 'var jsRequest = new Ajax.Request("/posts/view/1", {method:"post", onComplete:doComplete, onCreate:doBefore, onFailure:doError, onSuccess:doSuccess, parameters:{"name":"jim","height":"185cm"}});';
  169. $this->assertEquals($expected, $result);
  170. $result = $this->Proto->request('/posts/view/1', array('update' => 'content'));
  171. $expected = 'var jsRequest = new Ajax.Updater("content", "/posts/view/1");';
  172. $this->assertEquals($expected, $result);
  173. $result = $this->Proto->request('/people/edit/1', array(
  174. 'method' => 'post',
  175. 'complete' => 'doSuccess',
  176. 'update' => '#update-zone',
  177. 'wrapCallbacks' => false
  178. ));
  179. $expected = 'var jsRequest = new Ajax.Updater("update-zone", "/people/edit/1", {method:"post", onComplete:doSuccess});';
  180. $this->assertEquals($expected, $result);
  181. $result = $this->Proto->request('/people/edit/1', array(
  182. 'method' => 'post',
  183. 'complete' => 'doSuccess',
  184. 'error' => 'handleError',
  185. 'type' => 'json',
  186. 'data' => array('name' => 'jim', 'height' => '185cm'),
  187. 'wrapCallbacks' => false
  188. ));
  189. $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:doSuccess, onFailure:handleError, parameters:{"name":"jim","height":"185cm"}});';
  190. $this->assertEquals($expected, $result);
  191. $result = $this->Proto->request('/people/edit/1', array(
  192. 'method' => 'post',
  193. 'complete' => 'doSuccess',
  194. 'error' => 'handleError',
  195. 'type' => 'json',
  196. 'data' => '$("element").serialize()',
  197. 'dataExpression' => true,
  198. 'wrapCallbacks' => false
  199. ));
  200. $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:doSuccess, onFailure:handleError, parameters:$("element").serialize()});';
  201. $this->assertEquals($expected, $result);
  202. $result = $this->Proto->request('/people/edit/1', array(
  203. 'method' => 'post',
  204. 'before' => 'doBefore();',
  205. 'success' => 'doSuccess();',
  206. 'complete' => 'doComplete();',
  207. 'error' => 'handleError();',
  208. ));
  209. $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});';
  210. $this->assertEquals($expected, $result);
  211. $result = $this->Proto->request('/people/edit/1', array(
  212. 'async' => false,
  213. 'method' => 'post',
  214. 'before' => 'doBefore();',
  215. 'success' => 'doSuccess();',
  216. 'complete' => 'doComplete();',
  217. 'error' => 'handleError();',
  218. ));
  219. $expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {asynchronous:false, method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});';
  220. $this->assertEquals($expected, $result);
  221. $this->Proto->get('#submit');
  222. $result = $this->Proto->request('/users/login', array(
  223. 'before' => 'login.create(event)',
  224. 'complete' => 'login.complete(event)',
  225. 'update' => 'auth',
  226. 'data' => $this->Proto->serializeForm(array('isForm' => false, 'inline' => true)),
  227. 'dataExpression' => true
  228. ));
  229. $this->assertTrue(strpos($result, '$($("submit").form).serialize()') > 0);
  230. $this->assertFalse(strpos($result, 'parameters:function () {$($("submit").form).serialize()}') > 0);
  231. }
  232. /**
  233. * test sortable list generation
  234. *
  235. * @return void
  236. */
  237. public function testSortable() {
  238. $this->Proto->get('#myList');
  239. $result = $this->Proto->sortable(array(
  240. 'complete' => 'onComplete',
  241. 'sort' => 'onSort',
  242. 'wrapCallbacks' => false
  243. ));
  244. $expected = 'var jsSortable = Sortable.create($("myList"), {onChange:onSort, onUpdate:onComplete});';
  245. $this->assertEquals($expected, $result);
  246. }
  247. /**
  248. * test drag() method. Scriptaculous lacks the ability to take an Array of Elements
  249. * in new Drag() when selection is a multiple type. Iterate over the array.
  250. *
  251. * @return void
  252. */
  253. public function testDrag() {
  254. $this->Proto->get('#element');
  255. $result = $this->Proto->drag(array(
  256. 'start' => 'onStart',
  257. 'drag' => 'onDrag',
  258. 'stop' => 'onStop',
  259. 'snapGrid' => array(10, 10),
  260. 'wrapCallbacks' => false
  261. ));
  262. $expected = 'var jsDrag = new Draggable($("element"), {onDrag:onDrag, onEnd:onStop, onStart:onStart, snap:[10,10]});';
  263. $this->assertEquals($expected, $result);
  264. $this->Proto->get('div.dragger');
  265. $result = $this->Proto->drag(array(
  266. 'start' => 'onStart',
  267. 'drag' => 'onDrag',
  268. 'stop' => 'onStop',
  269. 'snapGrid' => array(10, 10),
  270. 'wrapCallbacks' => false
  271. ));
  272. $expected = '$$("div.dragger").each(function (item, index) {new Draggable(item, {onDrag:onDrag, onEnd:onStop, onStart:onStart, snap:[10,10]});});';
  273. $this->assertEquals($expected, $result);
  274. }
  275. /**
  276. * test drop() method
  277. *
  278. * @return void
  279. */
  280. public function testDrop() {
  281. $this->Proto->get('#element');
  282. $result = $this->Proto->drop(array(
  283. 'hover' => 'onHover',
  284. 'drop' => 'onDrop',
  285. 'accept' => '.drag-me',
  286. 'wrapCallbacks' => false
  287. ));
  288. $expected = 'Droppables.add($("element"), {accept:".drag-me", onDrop:onDrop, onHover:onHover});';
  289. $this->assertEquals($expected, $result);
  290. }
  291. /**
  292. * ensure that slider() method behaves properly
  293. *
  294. * @return void
  295. */
  296. public function testSlider() {
  297. $this->Proto->get('#element');
  298. $result = $this->Proto->slider(array(
  299. 'handle' => '#handle',
  300. 'direction' => 'horizontal',
  301. 'change' => 'onChange',
  302. 'complete' => 'onComplete',
  303. 'value' => 4,
  304. 'wrapCallbacks' => false
  305. ));
  306. $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {axis:"horizontal", onChange:onComplete, onSlide:onChange, sliderValue:4});';
  307. $this->assertEquals($expected, $result);
  308. $this->Proto->get('#element');
  309. $result = $this->Proto->slider(array(
  310. 'handle' => '#handle',
  311. 'change' => 'change();',
  312. 'complete' => 'complete();',
  313. 'value' => 4,
  314. 'min' => 10,
  315. 'max' => 100
  316. ));
  317. $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {onChange:function (value) {complete();}, onSlide:function (value) {change();}, range:$R(10,100), sliderValue:4});';
  318. $this->assertEquals($expected, $result);
  319. }
  320. /**
  321. * test the serializeForm implementation.
  322. *
  323. * @return void
  324. */
  325. public function testSerializeForm() {
  326. $this->Proto->get('#element');
  327. $result = $this->Proto->serializeForm(array('isForm' => true));
  328. $expected = '$("element").serialize();';
  329. $this->assertEquals($expected, $result);
  330. $result = $this->Proto->serializeForm(array('isForm' => true, 'inline' => true));
  331. $expected = '$("element").serialize()';
  332. $this->assertEquals($expected, $result);
  333. $result = $this->Proto->serializeForm(array('isForm' => false));
  334. $expected = '$($("element").form).serialize();';
  335. $this->assertEquals($expected, $result);
  336. $result = $this->Proto->serializeForm(array('isForm' => false, 'inline' => true));
  337. $expected = '$($("element").form).serialize()';
  338. $this->assertEquals($expected, $result);
  339. }
  340. }