Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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