~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/view/helpers/js.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * JsHelper Test Case
 
4
 *
 
5
 * TestCase for the JsHelper
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 
10
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
11
 *
 
12
 *  Licensed under The Open Group Test Suite License
 
13
 *  Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
16
 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 
17
 * @package       cake
 
18
 * @subpackage    cake.tests.cases.libs.view.helpers
 
19
 * @since         CakePHP(tm) v 1.3
 
20
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 
21
 */
 
22
App::import('Helper', array('Js', 'Html', 'Form'));
 
23
App::import('Core', array('View', 'ClassRegistry'));
 
24
 
 
25
Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
 
26
Mock::generate('View', 'JsHelperMockView');
 
27
 
 
28
class OptionEngineHelper extends JsBaseEngineHelper {
 
29
        var $_optionMap = array(
 
30
                'request' => array(
 
31
                        'complete' => 'success',
 
32
                        'request' => 'beforeSend',
 
33
                        'type' => 'dataType'
 
34
                )
 
35
        );
 
36
 
 
37
/**
 
38
 * test method for testing option mapping
 
39
 *
 
40
 * @return array
 
41
 */
 
42
        function testMap($options = array()) {
 
43
                return $this->_mapOptions('request', $options);
 
44
        }
 
45
/**
 
46
 * test method for option parsing
 
47
 *
 
48
 * @return void
 
49
 */
 
50
        function testParseOptions($options, $safe = array()) {
 
51
                return $this->_parseOptions($options, $safe);
 
52
        }
 
53
}
 
54
 
 
55
/**
 
56
 * JsHelper TestCase.
 
57
 *
 
58
 * @package       cake
 
59
 * @subpackage    cake.tests.cases.libs.view.helpers
 
60
 */
 
61
class JsHelperTestCase extends CakeTestCase {
 
62
/**
 
63
 * Regexp for CDATA start block
 
64
 *
 
65
 * @var string
 
66
 */
 
67
        var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
 
68
 
 
69
/**
 
70
 * Regexp for CDATA end block
 
71
 *
 
72
 * @var string
 
73
 */
 
74
        var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
 
75
 
 
76
/**
 
77
 * startTest method
 
78
 *
 
79
 * @access public
 
80
 * @return void
 
81
 */
 
82
        function startTest() {
 
83
                $this->_asset = Configure::read('Asset.timestamp');
 
84
                Configure::write('Asset.timestamp', false);
 
85
 
 
86
                $this->Js =& new JsHelper('JsBase');
 
87
                $this->Js->Html =& new HtmlHelper();
 
88
                $this->Js->Form =& new FormHelper();
 
89
                $this->Js->Form->Html =& new HtmlHelper();
 
90
                $this->Js->JsBaseEngine =& new JsBaseEngineHelper();
 
91
 
 
92
                $view =& new JsHelperMockView();
 
93
                ClassRegistry::addObject('view', $view);
 
94
        }
 
95
 
 
96
/**
 
97
 * endTest method
 
98
 *
 
99
 * @access public
 
100
 * @return void
 
101
 */
 
102
        function endTest() {
 
103
                Configure::write('Asset.timestamp', $this->_asset);
 
104
                ClassRegistry::removeObject('view');
 
105
                unset($this->Js);
 
106
        }
 
107
 
 
108
/**
 
109
 * Switches $this->Js to a mocked engine.
 
110
 *
 
111
 * @return void
 
112
 */
 
113
        function _useMock() {
 
114
                $this->Js =& new JsHelper(array('TestJs'));
 
115
                $this->Js->TestJsEngine =& new TestJsEngineHelper($this);
 
116
                $this->Js->Html =& new HtmlHelper();
 
117
                $this->Js->Form =& new FormHelper();
 
118
                $this->Js->Form->Html =& new HtmlHelper();
 
119
        }
 
120
 
 
121
/**
 
122
 * test object construction
 
123
 *
 
124
 * @return void
 
125
 */
 
126
        function testConstruction() {
 
127
                $js =& new JsHelper();
 
128
                $this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));
 
129
 
 
130
                $js =& new JsHelper(array('mootools'));
 
131
                $this->assertEqual($js->helpers, array('Html', 'Form', 'mootoolsEngine'));
 
132
 
 
133
                $js =& new JsHelper('prototype');
 
134
                $this->assertEqual($js->helpers, array('Html', 'Form', 'prototypeEngine'));
 
135
 
 
136
                $js =& new JsHelper('MyPlugin.Dojo');
 
137
                $this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
 
138
        }
 
139
 
 
140
/**
 
141
 * test that methods dispatch internally and to the engine class
 
142
 *
 
143
 * @return void
 
144
 */
 
145
        function testMethodDispatching() {
 
146
                $this->_useMock();
 
147
                $this->Js->TestJsEngine->expectOnce('dispatchMethod', array(new PatternExpectation('/methodOne/i'), array()));
 
148
 
 
149
                $this->Js->methodOne();
 
150
 
 
151
                $this->Js->TestEngine =& new StdClass();
 
152
                $this->expectError();
 
153
                $this->Js->someMethodThatSurelyDoesntExist();
 
154
        }
 
155
 
 
156
/**
 
157
 * Test that method dispatching respects buffer parameters and bufferedMethods Lists.
 
158
 *
 
159
 * @return void
 
160
 */
 
161
        function testMethodDispatchWithBuffering() {
 
162
                $this->_useMock();
 
163
 
 
164
                $this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
 
165
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
 
166
 
 
167
                $this->Js->event('click', 'foo');
 
168
                $result = $this->Js->getBuffer();
 
169
                $this->assertEqual(count($result), 1);
 
170
                $this->assertEqual($result[0], 'This is an event call');
 
171
 
 
172
                $result = $this->Js->event('click', 'foo', array('buffer' => false));
 
173
                $buffer = $this->Js->getBuffer();
 
174
                $this->assertTrue(empty($buffer));
 
175
                $this->assertEqual($result, 'This is an event call');
 
176
 
 
177
                $result = $this->Js->event('click', 'foo', false);
 
178
                $buffer = $this->Js->getBuffer();
 
179
                $this->assertTrue(empty($buffer));
 
180
                $this->assertEqual($result, 'This is an event call');
 
181
 
 
182
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
 
183
 
 
184
                $result = $this->Js->effect('slideIn');
 
185
                $buffer = $this->Js->getBuffer();
 
186
                $this->assertTrue(empty($buffer));
 
187
                $this->assertEqual($result, 'I am not buffered.');
 
188
 
 
189
                $result = $this->Js->effect('slideIn', true);
 
190
                $buffer = $this->Js->getBuffer();
 
191
                $this->assertNull($result);
 
192
                $this->assertEqual(count($buffer), 1);
 
193
                $this->assertEqual($buffer[0], 'I am not buffered.');
 
194
 
 
195
                $result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
 
196
                $buffer = $this->Js->getBuffer();
 
197
                $this->assertNull($result);
 
198
                $this->assertEqual(count($buffer), 1);
 
199
                $this->assertEqual($buffer[0], 'I am not buffered.');
 
200
 
 
201
                $result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
 
202
                $buffer = $this->Js->getBuffer();
 
203
                $this->assertNull($result);
 
204
                $this->assertEqual(count($buffer), 1);
 
205
                $this->assertEqual($buffer[0], 'I am not buffered.');
 
206
        }
 
207
 
 
208
/**
 
209
 * test that writeScripts generates scripts inline.
 
210
 *
 
211
 * @return void
 
212
 */
 
213
        function testWriteScriptsNoFile() {
 
214
                $this->_useMock();
 
215
                $this->Js->buffer('one = 1;');
 
216
                $this->Js->buffer('two = 2;');
 
217
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
 
218
                $expected = array(
 
219
                        'script' => array('type' => 'text/javascript'),
 
220
                        $this->cDataStart,
 
221
                        "one = 1;\ntwo = 2;",
 
222
                        $this->cDataEnd,
 
223
                        '/script',
 
224
                );
 
225
                $this->assertTags($result, $expected, true);
 
226
 
 
227
                $this->Js->TestJsEngine->expectAtLeastOnce('domReady');
 
228
                $result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
 
229
 
 
230
                ClassRegistry::removeObject('view');
 
231
                $view =& new JsHelperMockView();
 
232
                ClassRegistry::addObject('view', $view);
 
233
 
 
234
                $view->expectCallCount('addScript', 1);
 
235
                $view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s\=\s1;\ntwo\s\=\s2;/')));
 
236
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
 
237
        }
 
238
 
 
239
/**
 
240
 * test that writing the buffer with inline = false includes a script tag.
 
241
 *
 
242
 * @return void
 
243
 */
 
244
        function testWriteBufferNotInline() {
 
245
                $this->Js->set('foo', 1);
 
246
 
 
247
                $view =& new JsHelperMockView();
 
248
                ClassRegistry::removeObject('view');
 
249
                ClassRegistry::addObject('view', $view);
 
250
                $view->expectCallCount('addScript', 1);
 
251
 
 
252
                $pattern = new PatternExpectation('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#');
 
253
                $view->expectAt(0, 'addScript', array($pattern));
 
254
 
 
255
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
 
256
        }
 
257
 
 
258
/**
 
259
 * test that writeBuffer() sets domReady = false when the request is done by XHR.
 
260
 * Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries
 
261
 *
 
262
 * @return void
 
263
 */
 
264
        function testWriteBufferAndXhr() {
 
265
                $this->_useMock();
 
266
                $this->Js->params['isAjax'] = true;
 
267
                $this->Js->buffer('alert("test");');
 
268
                $this->Js->TestJsEngine->expectCallCount('dispatchMethod', 0);
 
269
                $result = $this->Js->writeBuffer();
 
270
        }
 
271
 
 
272
/**
 
273
 * test that writeScripts makes files, and puts the events into them.
 
274
 *
 
275
 * @return void
 
276
 */
 
277
        function testWriteScriptsInFile() {
 
278
                if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) {
 
279
                        return;
 
280
                }
 
281
                $this->Js->JsBaseEngine = new TestJsEngineHelper();
 
282
                $this->Js->buffer('one = 1;');
 
283
                $this->Js->buffer('two = 2;');
 
284
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
 
285
                $expected = array(
 
286
                        'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
 
287
                );
 
288
                $this->assertTags($result, $expected);
 
289
                preg_match('/src="(.*\.js)"/', $result, $filename);
 
290
                $this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
 
291
                $contents = file_get_contents(WWW_ROOT . $filename[1]);
 
292
                $this->assertPattern('/one\s=\s1;\ntwo\s=\s2;/', $contents);
 
293
 
 
294
                @unlink(WWW_ROOT . $filename[1]);
 
295
        }
 
296
 
 
297
/**
 
298
 * test link()
 
299
 *
 
300
 * @return void
 
301
 */
 
302
        function testLinkWithMock() {
 
303
                $this->_useMock();
 
304
                $options = array('update' => '#content');
 
305
 
 
306
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array('request', '*'));
 
307
                $this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', new AnythingExpectation()));
 
308
                $this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array(
 
309
                        'request', array('/posts/view/1', $options)
 
310
                ));
 
311
                $this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array(
 
312
                        'event', array('click', 'ajax code', $options + array('buffer' => null))
 
313
                ));
 
314
 
 
315
                $result = $this->Js->link('test link', '/posts/view/1', $options);
 
316
                $expected = array(
 
317
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
 
318
                        'test link',
 
319
                        '/a'
 
320
                );
 
321
                $this->assertTags($result, $expected);
 
322
 
 
323
                $options = array(
 
324
                        'confirm' => 'Are you sure?',
 
325
                        'update' => '#content',
 
326
                        'class' => 'my-class',
 
327
                        'id' => 'custom-id',
 
328
                        'escape' => false
 
329
                );
 
330
                $this->Js->TestJsEngine->expectAt(0, 'confirm', array($options['confirm']));
 
331
                $this->Js->TestJsEngine->expectAt(1, 'request', array('/posts/view/1', '*'));
 
332
$code = <<<CODE
 
333
var _confirm = confirm("Are you sure?");
 
334
if (!_confirm) {
 
335
        return false;
 
336
}
 
337
CODE;
 
338
                $this->Js->TestJsEngine->expectAt(1, 'event', array('click', $code));
 
339
                $result = $this->Js->link('test link »', '/posts/view/1', $options);
 
340
                $expected = array(
 
341
                        'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
 
342
                        'test link »',
 
343
                        '/a'
 
344
                );
 
345
                $this->assertTags($result, $expected);
 
346
 
 
347
                $options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
 
348
                $result = $this->Js->link('test link', '/posts/view/1', $options);
 
349
                $expected = array(
 
350
                        'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
 
351
                                'batman' => 'robin'),
 
352
                        'test link',
 
353
                        '/a'
 
354
                );
 
355
                $this->assertTags($result, $expected);
 
356
        }
 
357
 
 
358
/**
 
359
 * test that link() and no buffering returns an <a> and <script> tags.
 
360
 *
 
361
 * @return void
 
362
 */
 
363
        function testLinkWithNoBuffering() {
 
364
                $this->_useMock();
 
365
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array(
 
366
                        'request', array('/posts/view/1', array('update' => '#content'))
 
367
                ));
 
368
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', '-event handler-', array('event', '*'));
 
369
 
 
370
                $options = array('update' => '#content', 'buffer' => false);
 
371
                $result = $this->Js->link('test link', '/posts/view/1', $options);
 
372
                $expected = array(
 
373
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
 
374
                        'test link',
 
375
                        '/a',
 
376
                        'script' => array('type' => 'text/javascript'),
 
377
                        $this->cDataStart,
 
378
                        '-event handler-',
 
379
                        $this->cDataEnd,
 
380
                        '/script'
 
381
                );
 
382
                $this->assertTags($result, $expected);
 
383
 
 
384
                $options = array('update' => '#content', 'buffer' => false, 'safe' => false);
 
385
                $result = $this->Js->link('test link', '/posts/view/1', $options);
 
386
                $expected = array(
 
387
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
 
388
                        'test link',
 
389
                        '/a',
 
390
                        'script' => array('type' => 'text/javascript'),
 
391
                        '-event handler-',
 
392
                        '/script'
 
393
                );
 
394
                $this->assertTags($result, $expected);
 
395
        }
 
396
 
 
397
/**
 
398
 * test submit() with a Mock to check Engine method calls
 
399
 *
 
400
 * @return void
 
401
 */
 
402
        function testSubmitWithMock() {
 
403
                $this->_useMock();
 
404
 
 
405
                $options = array('update' => '#content', 'id' => 'test-submit');
 
406
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeform', '*'));
 
407
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeForm', '*'));
 
408
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax-code', array('request', '*'));
 
409
 
 
410
                $this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', '*'));
 
411
                $this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array(new PatternExpectation('/serializeForm/i'), '*'));
 
412
                $this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', '*'));
 
413
 
 
414
                $params = array(
 
415
                        'update' => $options['update'], 'data' => 'serialize-code',
 
416
                        'method' => 'post', 'dataExpression' => true, 'buffer' => null
 
417
                );
 
418
                $this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
 
419
                        'event', array('click', "ajax-code", $params)
 
420
                ));
 
421
 
 
422
                $result = $this->Js->submit('Save', $options);
 
423
                $expected = array(
 
424
                        'div' => array('class' => 'submit'),
 
425
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
 
426
                        '/div'
 
427
                );
 
428
                $this->assertTags($result, $expected);
 
429
 
 
430
 
 
431
                $this->Js->TestJsEngine->expectAt(4, 'dispatchMethod', array('get', '*'));
 
432
                $this->Js->TestJsEngine->expectAt(5, 'dispatchMethod', array(new PatternExpectation('/serializeForm/i'), '*'));
 
433
                $requestParams = array(
 
434
                        '/custom/url', array(
 
435
                                'update' => '#content',
 
436
                                'data' => 'serialize-code',
 
437
                                'method' => 'post',
 
438
                                'dataExpression' => true
 
439
                        )
 
440
                );
 
441
                $this->Js->TestJsEngine->expectAt(6, 'dispatchMethod', array('request', $requestParams));
 
442
 
 
443
                $params = array(
 
444
                        'update' => '#content', 'data' => 'serialize-code',
 
445
                        'method' => 'post', 'dataExpression' => true, 'buffer' => null
 
446
                );
 
447
                $this->Js->TestJsEngine->expectAt(7, 'dispatchMethod', array(
 
448
                        'event', array('click', "ajax-code", $params)
 
449
                ));
 
450
 
 
451
                $options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
 
452
                $result = $this->Js->submit('Save', $options);
 
453
                $expected = array(
 
454
                        'div' => array('class' => 'submit'),
 
455
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
 
456
                        '/div'
 
457
                );
 
458
                $this->assertTags($result, $expected);
 
459
        }
 
460
 
 
461
/**
 
462
 * test that no buffer works with submit() and that parameters are leaking into the script tag.
 
463
 *
 
464
 * @return void
 
465
 */
 
466
        function testSubmitWithNoBuffer() {
 
467
                $this->_useMock();
 
468
                $options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
 
469
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeform', '*'));
 
470
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeForm', '*'));
 
471
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax-code', array('request', '*'));
 
472
                $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'event-handler', array('event', '*'));
 
473
 
 
474
                $this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', '*'));
 
475
                $this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array(new PatternExpectation('/serializeForm/i'), '*'));
 
476
                $this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', array(
 
477
                        '', array('update' => $options['update'], 'data' => 'serialize-code', 'method' => 'post', 'dataExpression' => true)
 
478
                )));
 
479
 
 
480
                $params = array(
 
481
                        'update' => $options['update'], 'data' => 'serialize-code',
 
482
                        'method' => 'post', 'dataExpression' => true, 'buffer' => false
 
483
                );
 
484
                $this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
 
485
                        'event', array('click', "ajax-code", $params)
 
486
                ));
 
487
 
 
488
                $result = $this->Js->submit('Save', $options);
 
489
                $expected = array(
 
490
                        'div' => array('class' => 'submit'),
 
491
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
 
492
                        '/div',
 
493
                        'script' => array('type' => 'text/javascript'),
 
494
                        'event-handler',
 
495
                        '/script'
 
496
                );
 
497
                $this->assertTags($result, $expected);
 
498
        }
 
499
 
 
500
/**
 
501
 * Test that Object::Object() is not breaking json output in JsHelper
 
502
 *
 
503
 * @return void
 
504
 */
 
505
        function testObjectPassThrough() {
 
506
                $result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
 
507
                $expected = '{"one":"first","two":"second"}';
 
508
                $this->assertEqual($result, $expected);
 
509
        }
 
510
 
 
511
/**
 
512
 * Test that inherited Helper::value() is overwritten in JsHelper::value()
 
513
 * and calls JsBaseEngineHelper::value().
 
514
 *
 
515
 * @return void
 
516
 */
 
517
        function testValuePassThrough() {
 
518
                $result = $this->Js->value('string "quote"', true);
 
519
                $expected = '"string \"quote\""';
 
520
                $this->assertEqual($result, $expected);
 
521
        }
 
522
 
 
523
/**
 
524
 * test set()'ing variables to the Javascript buffer and controlling the output var name.
 
525
 *
 
526
 * @return void
 
527
 */
 
528
        function testSet() {
 
529
                $this->Js->set('loggedIn', true);
 
530
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
 
531
                $result = $this->Js->getBuffer();
 
532
                $expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
 
533
                $this->assertEqual($result[0], $expected);
 
534
 
 
535
                $this->Js->set('loggedIn', true);
 
536
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
 
537
                $this->Js->setVariable = 'WICKED';
 
538
                $result = $this->Js->getBuffer();
 
539
                $expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
 
540
                $this->assertEqual($result[0], $expected);
 
541
 
 
542
                $this->Js->set('loggedIn', true);
 
543
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
 
544
                $this->Js->setVariable = 'Application.variables';
 
545
                $result = $this->Js->getBuffer();
 
546
                $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
 
547
                $this->assertEqual($result[0], $expected);
 
548
        }
 
549
 
 
550
/**
 
551
 * test that vars set with Js->set() go to the top of the buffered scripts list.
 
552
 *
 
553
 * @return void
 
554
 */
 
555
        function testSetVarsAtTopOfBufferedScripts() {
 
556
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
 
557
                $this->Js->alert('hey you!', array('buffer' => true));
 
558
                $this->Js->confirm('Are you sure?', array('buffer' => true));
 
559
                $result = $this->Js->getBuffer(false);
 
560
 
 
561
                $expected = 'window.app = {"height":"tall","color":"purple"};';
 
562
                $this->assertEqual($result[0], $expected);
 
563
                $this->assertEqual($result[1], 'alert("hey you!");');
 
564
                $this->assertEqual($result[2], 'confirm("Are you sure?");');
 
565
        }
 
566
}
 
567
 
 
568
/**
 
569
 * JsBaseEngine Class Test case
 
570
 *
 
571
 * @package cake.tests.view.helpers
 
572
 */
 
573
class JsBaseEngineTestCase extends CakeTestCase {
 
574
/**
 
575
 * startTest method
 
576
 *
 
577
 * @access public
 
578
 * @return void
 
579
 */
 
580
        function startTest() {
 
581
                $this->JsEngine = new JsBaseEngineHelper();
 
582
        }
 
583
/**
 
584
 * endTest method
 
585
 *
 
586
 * @access public
 
587
 * @return void
 
588
 */
 
589
        function endTest() {
 
590
                ClassRegistry::removeObject('view');
 
591
                unset($this->JsEngine);
 
592
        }
 
593
 
 
594
/**
 
595
 * test escape string skills
 
596
 *
 
597
 * @return void
 
598
 */
 
599
        function testEscaping() {
 
600
                $result = $this->JsEngine->escape('');
 
601
                $expected = '';
 
602
                $this->assertEqual($result, $expected);
 
603
 
 
604
                $result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
 
605
                $expected = 'CakePHP\\nRapid Development Framework';
 
606
                $this->assertEqual($result, $expected);
 
607
 
 
608
                $result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
 
609
                $expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
 
610
                $this->assertEqual($result, $expected);
 
611
 
 
612
                $result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
 
613
                $expected = 'CakePHP: \\"Rapid Development Framework\\"';
 
614
                $this->assertEqual($result, $expected);
 
615
 
 
616
                $result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
 
617
                $expected = "CakePHP: 'Rapid Development Framework'";
 
618
                $this->assertEqual($result, $expected);
 
619
 
 
620
                $result = $this->JsEngine->escape('my \\"string\\"');
 
621
                $expected = 'my \\\\\\"string\\\\\\"';
 
622
                $this->assertEqual($result, $expected);
 
623
        }
 
624
 
 
625
/**
 
626
 * test prompt() creation
 
627
 *
 
628
 * @return void
 
629
 */
 
630
        function testPrompt() {
 
631
                $result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
 
632
                $expected = 'prompt("Hey, hey you", "hi!");';
 
633
                $this->assertEqual($result, $expected);
 
634
 
 
635
                $result = $this->JsEngine->prompt('"Hey"', '"hi"');
 
636
                $expected = 'prompt("\"Hey\"", "\"hi\"");';
 
637
                $this->assertEqual($result, $expected);
 
638
        }
 
639
 
 
640
/**
 
641
 * test alert generation
 
642
 *
 
643
 * @return void
 
644
 */
 
645
        function testAlert() {
 
646
                $result = $this->JsEngine->alert('Hey there');
 
647
                $expected = 'alert("Hey there");';
 
648
                $this->assertEqual($result, $expected);
 
649
 
 
650
                $result = $this->JsEngine->alert('"Hey"');
 
651
                $expected = 'alert("\"Hey\"");';
 
652
                $this->assertEqual($result, $expected);
 
653
        }
 
654
 
 
655
/**
 
656
 * test confirm generation
 
657
 *
 
658
 * @return void
 
659
 */
 
660
        function testConfirm() {
 
661
                $result = $this->JsEngine->confirm('Are you sure?');
 
662
                $expected = 'confirm("Are you sure?");';
 
663
                $this->assertEqual($result, $expected);
 
664
 
 
665
                $result = $this->JsEngine->confirm('"Are you sure?"');
 
666
                $expected = 'confirm("\"Are you sure?\"");';
 
667
                $this->assertEqual($result, $expected);
 
668
        }
 
669
 
 
670
/**
 
671
 * test Redirect
 
672
 *
 
673
 * @return void
 
674
 */
 
675
        function testRedirect() {
 
676
                $result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
 
677
                $expected = 'window.location = "/posts/view/1";';
 
678
                $this->assertEqual($result, $expected);
 
679
        }
 
680
 
 
681
/**
 
682
 * testObject encoding with non-native methods.
 
683
 *
 
684
 * @return void
 
685
 */
 
686
        function testObject() {
 
687
                $this->JsEngine->useNative = false;
 
688
 
 
689
                $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
 
690
                $result = $this->JsEngine->object($object);
 
691
                $expected = '{"title":"New thing","indexes":[5,6,7,8]}';
 
692
                $this->assertEqual($result, $expected);
 
693
 
 
694
                $result = $this->JsEngine->object(array('default' => 0));
 
695
                $expected = '{"default":0}';
 
696
                $this->assertEqual($result, $expected);
 
697
 
 
698
                $result = $this->JsEngine->object(array(
 
699
                        '2007' => array(
 
700
                                'Spring' => array(
 
701
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
 
702
                                ),
 
703
                                'Fall' => array(
 
704
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
 
705
                                )
 
706
                        ),
 
707
                        '2006' => array(
 
708
                                'Spring' => array(
 
709
                                    '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
 
710
                                ),
 
711
                                'Fall' => array(
 
712
                                    '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
 
713
                                )
 
714
                        )
 
715
                ));
 
716
                $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"}}}}';
 
717
                $this->assertEqual($result, $expected);
 
718
 
 
719
                foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
 
720
                        $result = $this->JsEngine->object($data);
 
721
                        $this->assertEqual($result, $expected);
 
722
                }
 
723
 
 
724
                $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
 
725
                $result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
 
726
                $this->assertPattern('/^PREFIX/', $result);
 
727
                $this->assertPattern('/POSTFIX$/', $result);
 
728
                $this->assertNoPattern('/.PREFIX./', $result);
 
729
                $this->assertNoPattern('/.POSTFIX./', $result);
 
730
        }
 
731
 
 
732
/**
 
733
 * test compatibility of JsBaseEngineHelper::object() vs. json_encode()
 
734
 *
 
735
 * @return void
 
736
 */
 
737
        function testObjectAgainstJsonEncode() {
 
738
                $skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
 
739
                if ($skip) {
 
740
                        return;
 
741
                }
 
742
                $this->JsEngine->useNative = false;
 
743
                $data = array();
 
744
                $data['mystring'] = "simple string";
 
745
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
746
 
 
747
                $data['mystring'] = "strïng with spécial chârs";
 
748
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
749
 
 
750
                $data['mystring'] = "a two lines\nstring";
 
751
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
752
 
 
753
                $data['mystring'] = "a \t tabbed \t string";
 
754
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
755
 
 
756
                $data['mystring'] = "a \"double-quoted\" string";
 
757
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
758
 
 
759
                $data['mystring'] = 'a \\"double-quoted\\" string';
 
760
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
761
 
 
762
                unset($data['mystring']);
 
763
                $data[3] = array(1, 2, 3);
 
764
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
765
 
 
766
                unset($data[3]);
 
767
                $data = array('mystring' => null, 'bool' => false, 'array' => array(1, 44, 66));
 
768
                $this->assertEqual(json_encode($data), $this->JsEngine->object($data));
 
769
        }
 
770
 
 
771
/**
 
772
 * test that JSON made with JsBaseEngineHelper::object() against json_decode()
 
773
 *
 
774
 * @return void
 
775
 */
 
776
        function testObjectAgainstJsonDecode() {
 
777
                $skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
 
778
                if ($skip) {
 
779
                        return;
 
780
                }
 
781
                $this->JsEngine->useNative = false;
 
782
 
 
783
                $data = array("simple string");
 
784
                $result = $this->JsEngine->object($data);
 
785
                $this->assertEqual(json_decode($result), $data);
 
786
 
 
787
                $data = array('my "string"');
 
788
                $result = $this->JsEngine->object($data);
 
789
                $this->assertEqual(json_decode($result), $data);
 
790
 
 
791
                $data = array('my \\"string\\"');
 
792
                $result = $this->JsEngine->object($data);
 
793
                $this->assertEqual(json_decode($result), $data);
 
794
        }
 
795
 
 
796
/**
 
797
 * test Mapping of options.
 
798
 *
 
799
 * @return void
 
800
 */
 
801
        function testOptionMapping() {
 
802
                $JsEngine = new OptionEngineHelper();
 
803
                $result = $JsEngine->testMap();
 
804
                $this->assertEqual($result, array());
 
805
 
 
806
                $result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
 
807
                $this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
 
808
 
 
809
                $result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
 
810
                $this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
 
811
 
 
812
                $result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
 
813
                $this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
 
814
        }
 
815
 
 
816
/**
 
817
 * test that option parsing escapes strings and saves what is supposed to be saved.
 
818
 *
 
819
 * @return void
 
820
 */
 
821
        function testOptionParsing() {
 
822
                $JsEngine = new OptionEngineHelper();
 
823
 
 
824
                $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
 
825
                $expected = 'key:1, url:"\\/posts\\/view\\/1"';
 
826
                $this->assertEqual($result, $expected);
 
827
 
 
828
                $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
 
829
                $expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
 
830
                $this->assertEqual($result, $expected);
 
831
        }
 
832
 
 
833
}