統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / View / Helper / JqueryEngineHelperTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (12.399 KB)

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