-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
js.test.php
480 lines (433 loc) · 14.4 KB
/
js.test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/* SVN FILE: $Id$ */
/**
* JsHelper Test Case
*
* TestCase for the JsHelper
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Helper', array('Js', 'Html'));
App::import('Core', array('View', 'ClassRegistry'));
Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
Mock::generate('View', 'JsHelperMockView');
class OptionEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array(
'request' => array(
'complete' => 'success',
'request' => 'beforeSend',
'type' => 'dataType'
)
);
/**
* test method for testing option mapping
*
* @return array
**/
function testMap($options = array()) {
return $this->_mapOptions('request', $options);
}
/**
* test method for option parsing
*
* @return void
**/
function testParseOptions($options, $safe = array()) {
return $this->_parseOptions($options, $safe);
}
}
/**
* JsHelper TestCase.
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class JsHelperTestCase extends CakeTestCase {
/**
* Regexp for CDATA start block
*
* @var string
*/
var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
/**
* Regexp for CDATA end block
*
* @var string
*/
var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
/**
* setUp method
*
* @access public
* @return void
*/
function startTest() {
$this->Js =& new JsHelper('JsBase');
$this->Js->Html =& new HtmlHelper();
$this->Js->JsBaseEngine =& new JsBaseEngineHelper();
$view =& new JsHelperMockView();
ClassRegistry::addObject('view', $view);
}
/**
* tearDown method
*
* @access public
* @return void
*/
function endTest() {
ClassRegistry::removeObject('view');
unset($this->Js);
}
/**
* test object construction
*
* @return void
**/
function testConstruction() {
$js =& new JsHelper();
$this->assertEqual($js->helpers, array('Html', 'JqueryEngine'));
$js =& new JsHelper(array('mootools'));
$this->assertEqual($js->helpers, array('Html', 'mootoolsEngine'));
$js =& new JsHelper('prototype');
$this->assertEqual($js->helpers, array('Html', 'prototypeEngine'));
$js =& new JsHelper('MyPlugin.Dojo');
$this->assertEqual($js->helpers, array('Html', 'MyPlugin.DojoEngine'));
}
/**
* test that methods dispatch internally and to the engine class
*
* @return void
**/
function testMethodDispatching() {
$js =& new JsHelper(array('TestJs'));
$js->TestJsEngine =& new TestJsEngineHelper();
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
$js->methodOne();
$js->TestEngine =& new StdClass();
$this->expectError();
$js->someMethodThatSurelyDoesntExist();
}
/**
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
*
* @return void
**/
function testMethodDispatchWithBuffering() {
$js =& new JsHelper(array('TestJs'));
$js->TestJsEngine = new TestJsEngineHelper();
$js->TestJsEngine->bufferedMethods = array('event', 'sortables');
$js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
$js->event('click', 'foo');
$result = $js->getBuffer();
$this->assertEqual(count($result), 1);
$this->assertEqual($result[0], 'This is an event call');
$result = $js->event('click', 'foo', array('buffer' => false));
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');
$result = $js->event('click', 'foo', false);
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');
$js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
$result = $js->effect('slideIn');
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'I am not buffered.');
$result = $js->effect('slideIn', true);
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
$result = $js->effect('slideIn', array('speed' => 'slow'), true);
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
$result = $js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
}
/**
* test that writeScripts generates scripts inline.
*
* @return void
**/
function testWriteScriptsNoFile() {
$this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"one = 1;\ntwo = 2;",
$this->cDataEnd,
'/script',
);
$this->assertTags($result, $expected, true);
$this->Js->JsBaseEngine->expectAtLeastOnce('domReady');
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));
$view =& new JsHelperMockView();
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
}
/**
* test that writeScripts makes files, and puts the events into them.
*
* @return void
**/
function testWriteScriptsInFile() {
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) {
return;
}
$this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
);
$this->assertTags($result, $expected);
preg_match('/src="(.*\.js)"/', $result, $filename);
$this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
$contents = file_get_contents(WWW_ROOT . $filename[1]);
$this->assertPattern('/one\s=\s1;\ntwo\s=\s2;/', $contents);
@unlink(WWW_ROOT . $filename[1]);
}
}
/**
* JsBaseEngine Class Test case
*
* @package cake.tests.view.helpers
**/
class JsBaseEngineTestCase extends CakeTestCase {
/**
* setUp method
*
* @access public
* @return void
*/
function startTest() {
$this->JsEngine = new JsBaseEngineHelper();
}
/**
* tearDown method
*
* @access public
* @return void
*/
function endTest() {
ClassRegistry::removeObject('view');
unset($this->JsEngine);
}
/**
* test escape string skills
*
* @return void
**/
function testEscaping() {
$result = $this->JsEngine->escape('');
$expected = '';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
$expected = 'CakePHP\\nRapid Development Framework';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
$expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
$expected = "CakePHP: 'Rapid Development Framework'";
$this->assertEqual($result, $expected);
$result = $this->JsEngine->escape('my \\"string\\"');
$expected = 'my \\\\\\"string\\\\\\"';
$this->assertEqual($result, $expected);
}
/**
* test prompt() creation
*
* @return void
**/
function testPrompt() {
$result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
$expected = 'prompt("Hey, hey you", "hi!");';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->prompt('"Hey"', '"hi"');
$expected = 'prompt("\"Hey\"", "\"hi\"");';
$this->assertEqual($result, $expected);
}
/**
* test alert generation
*
* @return void
**/
function testAlert() {
$result = $this->JsEngine->alert('Hey there');
$expected = 'alert("Hey there");';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->alert('"Hey"');
$expected = 'alert("\"Hey\"");';
$this->assertEqual($result, $expected);
}
/**
* test confirm generation
*
* @return void
**/
function testConfirm() {
$result = $this->JsEngine->confirm('Are you sure?');
$expected = 'confirm("Are you sure?");';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->confirm('"Are you sure?"');
$expected = 'confirm("\"Are you sure?\"");';
$this->assertEqual($result, $expected);
}
/**
* test Redirect
*
* @return void
**/
function testRedirect() {
$result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
$expected = 'window.location = "/posts/view/1";';
$this->assertEqual($result, $expected);
}
/**
* testObject encoding with non-native methods.
*
* @return void
**/
function testObject() {
$this->JsEngine->useNative = false;
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
$result = $this->JsEngine->object($object);
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->object(array('default' => 0));
$expected = '{"default":0}';
$this->assertEqual($result, $expected);
$result = $this->JsEngine->object(array(
'2007' => array(
'Spring' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
),
'Fall' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
)
),
'2006' => array(
'Spring' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
),
'Fall' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
)
)
));
$expected = '{"2007":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}},"2006":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}}}';
$this->assertEqual($result, $expected);
foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
$result = $this->JsEngine->object($data);
$this->assertEqual($result, $expected);
}
}
/**
* test compatibility of JsBaseEngineHelper::object() vs. json_encode()
*
* @return void
**/
function XXtestObjectAgainstJsonEncode() {
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
if ($skip) {
return;
}
$this->JsEngine->useNative = false;
$data = array();
$data['mystring'] = "simple string";
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
$data['mystring'] = "strïng with spécial chârs";
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
$data['mystring'] = "a two lines\nstring";
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
$data['mystring'] = "a \t tabbed \t string";
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
$data['mystring'] = "a \"double-quoted\" string";
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
$data['mystring'] = 'a \\"double-quoted\\" string';
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
}
/**
* test that JSON made with JsBaseEngineHelper::object() against json_decode()
*
* @return void
**/
function testObjectAgainstJsonDecode() {
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
if ($skip) {
return;
}
$this->JsEngine->useNative = false;
$data = array("simple string");
$result = $this->JsEngine->object($data);
$this->assertEqual(json_decode($result), $data);
$data = array('my "string"');
$result = $this->JsEngine->object($data);
$this->assertEqual(json_decode($result), $data);
$data = array('my \\"string\\"');
$result = $this->JsEngine->object($data);
$this->assertEqual(json_decode($result), $data);
}
/**
* test Mapping of options.
*
* @return void
**/
function testOptionMapping() {
$JsEngine = new OptionEngineHelper();
$result = $JsEngine->testMap();
$this->assertEqual($result, array());
$result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
$this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
$result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
}
/**
* test that option parsing escapes strings and saves what is supposed to be saved.
*
* @return void
**/
function testOptionParsing() {
$JsEngine = new OptionEngineHelper();
$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
$expected = 'key:1, url:"\\/posts\\/view\\/1"';
$this->assertEqual($result, $expected);
$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
$expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
$this->assertEqual($result, $expected);
}
}
?>