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

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

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

1
<?php
2
/**
3
 * JsHelper Test Case
4
 *
5
 * TestCase for the JsHelper
6
 *
7
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
16
 * @package       Cake.Test.Case.View.Helper
17
 * @since         CakePHP(tm) v 1.3
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20

    
21
App::uses('HtmlHelper', 'View/Helper');
22
App::uses('JsHelper', 'View/Helper');
23
App::uses('JsBaseEngineHelper', 'View/Helper');
24
App::uses('FormHelper', 'View/Helper');
25
App::uses('View', 'View');
26
App::uses('ClassRegistry', 'Utility');
27

    
28
/**
29
 * Class JsEncodingObject
30
 *
31
 * @package       Cake.Test.Case.View.Helper
32
 */
33
class JsEncodingObject {
34

    
35
        protected $_title = 'Old thing';
36

    
37
        //@codingStandardsIgnoreStart
38
        private $__noshow = 'Never ever';
39
        //@codingStandardsIgnoreEnd
40

    
41
}
42

    
43
/**
44
 * Class OptionEngineHelper
45
 *
46
 * @package       Cake.Test.Case.View.Helper
47
 */
48
class OptionEngineHelper extends JsBaseEngineHelper {
49

    
50
        protected $_optionMap = array(
51
                'request' => array(
52
                        'complete' => 'success',
53
                        'request' => 'beforeSend',
54
                        'type' => 'dataType'
55
                )
56
        );
57

    
58
/**
59
 * test method for testing option mapping
60
 *
61
 * @param array $options
62
 * @return array
63
 */
64
        public function testMap($options = array()) {
65
                return $this->_mapOptions('request', $options);
66
        }
67

    
68
/**
69
 * test method for option parsing
70
 *
71
 * @param $options
72
 * @param array $safe
73
 * @return void
74
 */
75
        public function testParseOptions($options, $safe = array()) {
76
                return $this->_parseOptions($options, $safe);
77
        }
78

    
79
        public function get($selector) {
80
        }
81

    
82
        public function event($type, $callback, $options = array()) {
83
        }
84

    
85
        public function domReady($functionBody) {
86
        }
87

    
88
        public function each($callback) {
89
        }
90

    
91
        public function effect($name, $options = array()) {
92
        }
93

    
94
        public function request($url, $options = array()) {
95
        }
96

    
97
        public function drag($options = array()) {
98
        }
99

    
100
        public function drop($options = array()) {
101
        }
102

    
103
        public function sortable($options = array()) {
104
        }
105

    
106
        public function slider($options = array()) {
107
        }
108

    
109
        public function serializeForm($options = array()) {
110
        }
111

    
112
}
113

    
114
/**
115
 * JsHelper TestCase.
116
 *
117
 * @package       Cake.Test.Case.View.Helper
118
 */
119
class JsHelperTest extends CakeTestCase {
120

    
121
/**
122
 * Regexp for CDATA start block
123
 *
124
 * @var string
125
 */
126
        public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
127

    
128
/**
129
 * Regexp for CDATA end block
130
 *
131
 * @var string
132
 */
133
        public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
134

    
135
/**
136
 * setUp method
137
 *
138
 * @return void
139
 */
140
        public function setUp() {
141
                parent::setUp();
142

    
143
                Configure::write('Asset.timestamp', false);
144

    
145
                $controller = null;
146
                $this->View = $this->getMock('View', array('append'), array(&$controller));
147
                $this->Js = new JsHelper($this->View, 'Option');
148
                $request = new CakeRequest(null, false);
149
                $this->Js->request = $request;
150
                $this->Js->Html = new HtmlHelper($this->View);
151
                $this->Js->Html->request = $request;
152
                $this->Js->Form = new FormHelper($this->View);
153

    
154
                $this->Js->Form->request = $request;
155
                $this->Js->Form->Html = $this->Js->Html;
156
                $this->Js->OptionEngine = new OptionEngineHelper($this->View);
157
        }
158

    
159
/**
160
 * tearDown method
161
 *
162
 * @return void
163
 */
164
        public function tearDown() {
165
                parent::tearDown();
166
                unset($this->Js);
167
        }
168

    
169
/**
170
 * Switches $this->Js to a mocked engine.
171
 *
172
 * @return void
173
 */
174
        protected function _useMock() {
175
                $request = new CakeRequest(null, false);
176

    
177
                $this->Js = new JsHelper($this->View, array('TestJs'));
178
                $this->Js->TestJsEngine = $this->getMock('JsBaseEngineHelper', array(), array($this->View));
179
                $this->Js->request = $request;
180
                $this->Js->Html = new HtmlHelper($this->View);
181
                $this->Js->Html->request = $request;
182
                $this->Js->Form = new FormHelper($this->View);
183
                $this->Js->Form->request = $request;
184
                $this->Js->Form->Html = new HtmlHelper($this->View);
185
        }
186

    
187
/**
188
 * test object construction
189
 *
190
 * @return void
191
 */
192
        public function testConstruction() {
193
                $js = new JsHelper($this->View);
194
                $this->assertEquals(array('Html', 'Form', 'JqueryEngine'), $js->helpers);
195

    
196
                $js = new JsHelper($this->View, array('mootools'));
197
                $this->assertEquals(array('Html', 'Form', 'mootoolsEngine'), $js->helpers);
198

    
199
                $js = new JsHelper($this->View, 'prototype');
200
                $this->assertEquals(array('Html', 'Form', 'prototypeEngine'), $js->helpers);
201

    
202
                $js = new JsHelper($this->View, 'MyPlugin.Dojo');
203
                $this->assertEquals(array('Html', 'Form', 'MyPlugin.DojoEngine'), $js->helpers);
204
        }
205

    
206
/**
207
 * test that methods dispatch internally and to the engine class
208
 *
209
 * @expectedException PHPUnit_Framework_Error_Warning
210
 * @return void
211
 */
212
        public function testMethodDispatching() {
213
                $this->_useMock();
214

    
215
                $this->Js->TestJsEngine
216
                        ->expects($this->once())
217
                        ->method('event')
218
                        ->with('click', 'callback');
219

    
220
                $this->Js->event('click', 'callback');
221

    
222
                $this->Js->TestJsEngine = new StdClass();
223
                $this->Js->someMethodThatSurelyDoesntExist();
224
        }
225

    
226
/**
227
 * Test that method dispatching for events respects buffer parameters and bufferedMethods Lists.
228
 *
229
 * @return void
230
 */
231
        public function testEventDispatchWithBuffering() {
232
                $this->_useMock();
233

    
234
                $this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
235
                $this->Js->TestJsEngine->expects($this->exactly(3))
236
                        ->method('event')
237
                        ->will($this->returnValue('This is an event call'));
238

    
239
                $this->Js->event('click', 'foo');
240
                $result = $this->Js->getBuffer();
241
                $this->assertEquals(1, count($result));
242
                $this->assertEquals('This is an event call', $result[0]);
243

    
244
                $result = $this->Js->event('click', 'foo', array('buffer' => false));
245
                $buffer = $this->Js->getBuffer();
246
                $this->assertTrue(empty($buffer));
247
                $this->assertEquals('This is an event call', $result);
248

    
249
                $result = $this->Js->event('click', 'foo', false);
250
                $buffer = $this->Js->getBuffer();
251
                $this->assertTrue(empty($buffer));
252
                $this->assertEquals('This is an event call', $result);
253
        }
254

    
255
/**
256
 * Test that method dispatching for effects respects buffer parameters and bufferedMethods Lists.
257
 *
258
 * @return void
259
 */
260
        public function testEffectDispatchWithBuffering() {
261
                $this->_useMock();
262
                $this->Js->TestJsEngine->expects($this->exactly(4))
263
                        ->method('effect')
264
                        ->will($this->returnValue('I am not buffered.'));
265

    
266
                $result = $this->Js->effect('slideIn');
267
                $buffer = $this->Js->getBuffer();
268
                $this->assertTrue(empty($buffer));
269
                $this->assertEquals('I am not buffered.', $result);
270

    
271
                $result = $this->Js->effect('slideIn', true);
272
                $buffer = $this->Js->getBuffer();
273
                $this->assertNull($result);
274
                $this->assertEquals(1, count($buffer));
275
                $this->assertEquals('I am not buffered.', $buffer[0]);
276

    
277
                $result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
278
                $buffer = $this->Js->getBuffer();
279
                $this->assertNull($result);
280
                $this->assertEquals(1, count($buffer));
281
                $this->assertEquals('I am not buffered.', $buffer[0]);
282

    
283
                $result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
284
                $buffer = $this->Js->getBuffer();
285
                $this->assertNull($result);
286
                $this->assertEquals(1, count($buffer));
287
                $this->assertEquals('I am not buffered.', $buffer[0]);
288
        }
289

    
290
/**
291
 * test that writeScripts generates scripts inline.
292
 *
293
 * @return void
294
 */
295
        public function testWriteScriptsNoFile() {
296
                $this->_useMock();
297
                $this->Js->buffer('one = 1;');
298
                $this->Js->buffer('two = 2;');
299
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
300
                $expected = array(
301
                        'script' => array('type' => 'text/javascript'),
302
                        $this->cDataStart,
303
                        "one = 1;\ntwo = 2;",
304
                        $this->cDataEnd,
305
                        '/script',
306
                );
307
                $this->assertTags($result, $expected);
308

    
309
                $this->Js->TestJsEngine->expects($this->atLeastOnce())->method('domReady');
310
                $result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
311

    
312
                $this->View->expects($this->once())
313
                        ->method('append')
314
                        ->with('script', $this->matchesRegularExpression('/one\s\=\s1;\ntwo\s\=\s2;/'));
315
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
316
        }
317

    
318
/**
319
 * test that writing the buffer with inline = false includes a script tag.
320
 *
321
 * @return void
322
 */
323
        public function testWriteBufferNotInline() {
324
                $this->Js->set('foo', 1);
325

    
326
                $this->View->expects($this->once())
327
                        ->method('append')
328
                        ->with('script', $this->matchesRegularExpression('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#'));
329

    
330
                $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
331
        }
332

    
333
/**
334
 * test that writeBuffer() sets domReady = false when the request is done by XHR.
335
 * Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries
336
 *
337
 * @return void
338
 */
339
        public function testWriteBufferAndXhr() {
340
                $this->_useMock();
341
                $requestWith = null;
342
                if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
343
                        $requestWith = $_SERVER['HTTP_X_REQUESTED_WITH'];
344
                }
345
                $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
346

    
347
                $this->Js->buffer('alert("test");');
348
                $this->Js->TestJsEngine->expects($this->never())->method('domReady');
349
                $this->Js->writeBuffer();
350

    
351
                unset($_SERVER['HTTP_X_REQUESTED_WITH']);
352
                if ($requestWith !== null) {
353
                        $_SERVER['HTTP_X_REQUESTED_WITH'] = $requestWith;
354
                }
355
        }
356

    
357
/**
358
 * test that writeScripts makes files, and puts the events into them.
359
 *
360
 * @return void
361
 */
362
        public function testWriteScriptsInFile() {
363
                $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, script caching test has been skipped.');
364

    
365
                Configure::write('Cache.disable', false);
366
                $this->Js->request->webroot = '/';
367
                $this->Js->JsBaseEngine = $this->getMock('JsBaseEngineHelper', array(), array($this->View));
368
                $this->Js->buffer('one = 1;');
369
                $this->Js->buffer('two = 2;');
370
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
371
                $expected = array(
372
                        'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
373
                );
374
                $this->assertTags($result, $expected);
375
                preg_match('/src="(.*\.js)"/', $result, $filename);
376
                $this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
377
                $contents = file_get_contents(WWW_ROOT . $filename[1]);
378
                $this->assertRegExp('/one\s=\s1;\ntwo\s=\s2;/', $contents);
379
                if (file_exists(WWW_ROOT . $filename[1])) {
380
                        unlink(WWW_ROOT . $filename[1]);
381
                }
382

    
383
                Configure::write('Cache.disable', true);
384
                $this->Js->buffer('one = 1;');
385
                $this->Js->buffer('two = 2;');
386
                $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
387
                $this->assertRegExp('/one\s=\s1;\ntwo\s=\s2;/', $result);
388
                $this->assertFalse(file_exists(WWW_ROOT . $filename[1]));
389
        }
390

    
391
/**
392
 * test link()
393
 *
394
 * @return void
395
 */
396
        public function testLinkWithMock() {
397
                $this->_useMock();
398

    
399
                $options = array('update' => '#content');
400

    
401
                $this->Js->TestJsEngine->expects($this->at(0))
402
                        ->method('get');
403

    
404
                $this->Js->TestJsEngine->expects($this->at(1))
405
                        ->method('request')
406
                        ->with('/posts/view/1', $options)
407
                        ->will($this->returnValue('--ajax code--'));
408

    
409
                $this->Js->TestJsEngine->expects($this->at(2))
410
                        ->method('event')
411
                        ->with('click', '--ajax code--', $options + array('buffer' => null));
412

    
413
                $result = $this->Js->link('test link', '/posts/view/1', $options);
414
                $expected = array(
415
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
416
                        'test link',
417
                        '/a'
418
                );
419
                $this->assertTags($result, $expected);
420
        }
421

    
422
/**
423
 * test link with a mock and confirmation
424
 *
425
 * @return void
426
 */
427
        public function testLinkWithMockAndConfirm() {
428
                $this->_useMock();
429

    
430
                $options = array(
431
                        'confirm' => 'Are you sure?',
432
                        'update' => '#content',
433
                        'class' => 'my-class',
434
                        'id' => 'custom-id',
435
                        'escape' => false
436
                );
437
                $this->Js->TestJsEngine->expects($this->once())
438
                        ->method('confirmReturn')
439
                        ->with($options['confirm'])
440
                        ->will($this->returnValue('--confirm script--'));
441

    
442
                $this->Js->TestJsEngine->expects($this->once())
443
                        ->method('request')
444
                        ->with('/posts/view/1');
445

    
446
                $this->Js->TestJsEngine->expects($this->once())
447
                        ->method('event')
448
                        ->with('click', '--confirm script--');
449

    
450
                $result = $this->Js->link('test link »', '/posts/view/1', $options);
451
                $expected = array(
452
                        'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
453
                        'test link »',
454
                        '/a'
455
                );
456
                $this->assertTags($result, $expected);
457
        }
458

    
459
/**
460
 * test link passing on htmlAttributes
461
 *
462
 * @return void
463
 */
464
        public function testLinkWithAribtraryAttributes() {
465
                $this->_useMock();
466

    
467
                $options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
468
                $result = $this->Js->link('test link', '/posts/view/1', $options);
469
                $expected = array(
470
                        'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
471
                                'batman' => 'robin'),
472
                        'test link',
473
                        '/a'
474
                );
475
                $this->assertTags($result, $expected);
476
        }
477

    
478
/**
479
 * test that link() and no buffering returns an <a> and <script> tags.
480
 *
481
 * @return void
482
 */
483
        public function testLinkWithNoBuffering() {
484
                $this->_useMock();
485

    
486
                $this->Js->TestJsEngine->expects($this->at(1))
487
                        ->method('request')
488
                        ->with('/posts/view/1', array('update' => '#content'))
489
                        ->will($this->returnValue('ajax code'));
490

    
491
                $this->Js->TestJsEngine->expects($this->at(2))
492
                        ->method('event')
493
                        ->will($this->returnValue('-event handler-'));
494

    
495
                $options = array('update' => '#content', 'buffer' => false);
496
                $result = $this->Js->link('test link', '/posts/view/1', $options);
497
                $expected = array(
498
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
499
                        'test link',
500
                        '/a',
501
                        'script' => array('type' => 'text/javascript'),
502
                        $this->cDataStart,
503
                        '-event handler-',
504
                        $this->cDataEnd,
505
                        '/script'
506
                );
507
                $this->assertTags($result, $expected);
508
        }
509

    
510
/**
511
 * test link with buffering off and safe on.
512
 *
513
 * @return void
514
 */
515
        public function testLinkWithNoBufferingAndSafe() {
516
                $this->_useMock();
517

    
518
                $this->Js->TestJsEngine->expects($this->at(1))
519
                        ->method('request')
520
                        ->with('/posts/view/1', array('update' => '#content'))
521
                        ->will($this->returnValue('ajax code'));
522

    
523
                $this->Js->TestJsEngine->expects($this->at(2))
524
                        ->method('event')
525
                        ->will($this->returnValue('-event handler-'));
526

    
527
                $options = array('update' => '#content', 'buffer' => false, 'safe' => false);
528
                $result = $this->Js->link('test link', '/posts/view/1', $options);
529

    
530
                $expected = array(
531
                        'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
532
                        'test link',
533
                        '/a',
534
                        'script' => array('type' => 'text/javascript'),
535
                        '-event handler-',
536
                        '/script'
537
                );
538
                $this->assertTags($result, $expected);
539
        }
540

    
541
/**
542
 * test submit() with a Mock to check Engine method calls
543
 *
544
 * @return void
545
 */
546
        public function testSubmitWithMock() {
547
                $this->_useMock();
548

    
549
                $options = array('update' => '#content', 'id' => 'test-submit', 'style' => 'margin: 0');
550

    
551
                $this->Js->TestJsEngine->expects($this->at(0))
552
                        ->method('get');
553

    
554
                $this->Js->TestJsEngine->expects($this->at(1))
555
                        ->method('serializeForm')
556
                        ->will($this->returnValue('serialize-code'));
557

    
558
                $this->Js->TestJsEngine->expects($this->at(2))
559
                        ->method('request')
560
                        ->will($this->returnValue('ajax-code'));
561

    
562
                $params = array(
563
                        'update' => $options['update'], 'data' => 'serialize-code',
564
                        'method' => 'post', 'dataExpression' => true, 'buffer' => null
565
                );
566

    
567
                $this->Js->TestJsEngine->expects($this->at(3))
568
                        ->method('event')
569
                        ->with('click', "ajax-code", $params);
570

    
571
                $result = $this->Js->submit('Save', $options);
572
                $expected = array(
573
                        'div' => array('class' => 'submit'),
574
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save', 'style' => 'margin: 0'),
575
                        '/div'
576
                );
577
                $this->assertTags($result, $expected);
578
        }
579

    
580
/**
581
 * test submit() with a mock
582
 *
583
 * @return void
584
 */
585
        public function testSubmitWithMockRequestParams() {
586
                $this->_useMock();
587

    
588
                $this->Js->TestJsEngine->expects($this->at(0))
589
                        ->method('get');
590

    
591
                $this->Js->TestJsEngine->expects($this->at(1))
592
                        ->method('serializeForm')
593
                        ->will($this->returnValue('serialize-code'));
594

    
595
                $requestParams = array(
596
                        'update' => '#content',
597
                        'data' => 'serialize-code',
598
                        'method' => 'post',
599
                        'dataExpression' => true
600
                );
601

    
602
                $this->Js->TestJsEngine->expects($this->at(2))
603
                        ->method('request')
604
                        ->with('/custom/url', $requestParams)
605
                        ->will($this->returnValue('ajax-code'));
606

    
607
                $params = array(
608
                        'update' => '#content', 'data' => 'serialize-code',
609
                        'method' => 'post', 'dataExpression' => true, 'buffer' => null
610
                );
611

    
612
                $this->Js->TestJsEngine->expects($this->at(3))
613
                        ->method('event')
614
                        ->with('click', "ajax-code", $params);
615

    
616
                $options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
617
                $result = $this->Js->submit('Save', $options);
618
                $expected = array(
619
                        'div' => array('class' => 'submit'),
620
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
621
                        '/div'
622
                );
623
                $this->assertTags($result, $expected);
624
        }
625

    
626
/**
627
 * test that no buffer works with submit() and that parameters are leaking into the script tag.
628
 *
629
 * @return void
630
 */
631
        public function testSubmitWithNoBuffer() {
632
                $this->_useMock();
633
                $options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
634

    
635
                $this->Js->TestJsEngine->expects($this->at(0))
636
                        ->method('get');
637

    
638
                $this->Js->TestJsEngine->expects($this->at(1))
639
                        ->method('serializeForm')
640
                        ->will($this->returnValue('serialize-code'));
641

    
642
                $this->Js->TestJsEngine->expects($this->at(2))
643
                        ->method('request')
644
                        ->will($this->returnValue('ajax-code'));
645

    
646
                $this->Js->TestJsEngine->expects($this->at(3))
647
                        ->method('event')
648
                        ->will($this->returnValue('event-handler'));
649

    
650
                $params = array(
651
                        'update' => $options['update'], 'data' => 'serialize-code',
652
                        'method' => 'post', 'dataExpression' => true, 'buffer' => false
653
                );
654

    
655
                $this->Js->TestJsEngine->expects($this->at(3))
656
                        ->method('event')
657
                        ->with('click', "ajax-code", $params);
658

    
659
                $result = $this->Js->submit('Save', $options);
660
                $expected = array(
661
                        'div' => array('class' => 'submit'),
662
                        'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
663
                        '/div',
664
                        'script' => array('type' => 'text/javascript'),
665
                        'event-handler',
666
                        '/script'
667
                );
668
                $this->assertTags($result, $expected);
669
        }
670

    
671
/**
672
 * Test that Object::Object() is not breaking json output in JsHelper
673
 *
674
 * @return void
675
 */
676
        public function testObjectPassThrough() {
677
                $result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
678
                $expected = '{"one":"first","two":"second"}';
679
                $this->assertEquals($expected, $result);
680
        }
681

    
682
/**
683
 * Test that inherited Helper::value() is overwritten in JsHelper::value()
684
 * and calls JsBaseEngineHelper::value().
685
 *
686
 * @return void
687
 */
688
        public function testValuePassThrough() {
689
                $result = $this->Js->value('string "quote"', true);
690
                $expected = '"string \"quote\""';
691
                $this->assertEquals($expected, $result);
692
        }
693

    
694
/**
695
 * test set()'ing variables to the JavaScript buffer and controlling the output var name.
696
 *
697
 * @return void
698
 */
699
        public function testSet() {
700
                $this->Js->set('loggedIn', true);
701
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
702
                $result = $this->Js->getBuffer();
703
                $expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
704
                $this->assertEquals($expected, $result[0]);
705

    
706
                $this->Js->set('loggedIn', true);
707
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
708
                $this->Js->setVariable = 'WICKED';
709
                $result = $this->Js->getBuffer();
710
                $expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
711
                $this->assertEquals($expected, $result[0]);
712

    
713
                $this->Js->set('loggedIn', true);
714
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
715
                $this->Js->setVariable = 'Application.variables';
716
                $result = $this->Js->getBuffer();
717
                $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
718
                $this->assertEquals($expected, $result[0]);
719
        }
720

    
721
/**
722
 * test that vars set with Js->set() go to the top of the buffered scripts list.
723
 *
724
 * @return void
725
 */
726
        public function testSetVarsAtTopOfBufferedScripts() {
727
                $this->Js->set(array('height' => 'tall', 'color' => 'purple'));
728
                $this->Js->alert('hey you!', array('buffer' => true));
729
                $this->Js->confirm('Are you sure?', array('buffer' => true));
730
                $result = $this->Js->getBuffer(false);
731

    
732
                $expected = 'window.app = {"height":"tall","color":"purple"};';
733
                $this->assertEquals($expected, $result[0]);
734
                $this->assertEquals('alert("hey you!");', $result[1]);
735
                $this->assertEquals('confirm("Are you sure?");', $result[2]);
736
        }
737

    
738
}
739

    
740
/**
741
 * JsBaseEngine Class Test case
742
 *
743
 * @package       Cake.Test.Case.View.Helper
744
 */
745
class JsBaseEngineTest extends CakeTestCase {
746

    
747
/**
748
 * setUp method
749
 *
750
 * @return void
751
 */
752
        public function setUp() {
753
                parent::setUp();
754
                $controller = null;
755
                $this->View = $this->getMock('View', array('append'), array(&$controller));
756
                $this->JsEngine = new OptionEngineHelper($this->View);
757
        }
758

    
759
/**
760
 * tearDown method
761
 *
762
 * @return void
763
 */
764
        public function tearDown() {
765
                parent::tearDown();
766
                unset($this->JsEngine);
767
        }
768

    
769
/**
770
 * test escape string skills
771
 *
772
 * @return void
773
 */
774
        public function testEscaping() {
775
                $result = $this->JsEngine->escape('');
776
                $expected = '';
777
                $this->assertEquals($expected, $result);
778

    
779
                $result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
780
                $expected = 'CakePHP\\nRapid Development Framework';
781
                $this->assertEquals($expected, $result);
782

    
783
                $result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
784
                $expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
785
                $this->assertEquals($expected, $result);
786

    
787
                $result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
788
                $expected = 'CakePHP: \\"Rapid Development Framework\\"';
789
                $this->assertEquals($expected, $result);
790

    
791
                $result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
792
                $expected = "CakePHP: 'Rapid Development Framework'";
793
                $this->assertEquals($expected, $result);
794

    
795
                $result = $this->JsEngine->escape('my \\"string\\"');
796
                $expected = 'my \\\\\\"string\\\\\\"';
797
                $this->assertEquals($expected, $result);
798
        }
799

    
800
/**
801
 * test prompt() creation
802
 *
803
 * @return void
804
 */
805
        public function testPrompt() {
806
                $result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
807
                $expected = 'prompt("Hey, hey you", "hi!");';
808
                $this->assertEquals($expected, $result);
809

    
810
                $result = $this->JsEngine->prompt('"Hey"', '"hi"');
811
                $expected = 'prompt("\"Hey\"", "\"hi\"");';
812
                $this->assertEquals($expected, $result);
813
        }
814

    
815
/**
816
 * test alert generation
817
 *
818
 * @return void
819
 */
820
        public function testAlert() {
821
                $result = $this->JsEngine->alert('Hey there');
822
                $expected = 'alert("Hey there");';
823
                $this->assertEquals($expected, $result);
824

    
825
                $result = $this->JsEngine->alert('"Hey"');
826
                $expected = 'alert("\"Hey\"");';
827
                $this->assertEquals($expected, $result);
828
        }
829

    
830
/**
831
 * test confirm generation
832
 *
833
 * @return void
834
 */
835
        public function testConfirm() {
836
                $result = $this->JsEngine->confirm('Are you sure?');
837
                $expected = 'confirm("Are you sure?");';
838
                $this->assertEquals($expected, $result);
839

    
840
                $result = $this->JsEngine->confirm('"Are you sure?"');
841
                $expected = 'confirm("\"Are you sure?\"");';
842
                $this->assertEquals($expected, $result);
843
        }
844

    
845
/**
846
 * test Redirect
847
 *
848
 * @return void
849
 */
850
        public function testRedirect() {
851
                $result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
852
                $expected = 'window.location = "/posts/view/1";';
853
                $this->assertEquals($expected, $result);
854
        }
855

    
856
/**
857
 * testObject encoding with non-native methods.
858
 *
859
 * @return void
860
 */
861
        public function testObject() {
862
                $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
863
                $result = $this->JsEngine->object($object);
864
                $expected = '{"title":"New thing","indexes":[5,6,7,8]}';
865
                $this->assertEquals($expected, $result);
866

    
867
                $object = new JsEncodingObject();
868
                $object->title = 'New thing';
869
                $object->indexes = array(5, 6, 7, 8);
870
                $result = $this->JsEngine->object($object);
871
                $this->assertEquals($expected, $result);
872

    
873
                $result = $this->JsEngine->object(array('default' => 0));
874
                $expected = '{"default":0}';
875
                $this->assertEquals($expected, $result);
876

    
877
                $result = $this->JsEngine->object(array(
878
                        '2007' => array(
879
                                'Spring' => array(
880
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
881
                                ),
882
                                'Fall' => array(
883
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
884
                                )
885
                        ),
886
                        '2006' => array(
887
                                'Spring' => array(
888
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
889
                                ),
890
                                'Fall' => array(
891
                                        '1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
892
                                )
893
                        )
894
                ));
895
                $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"}}}}';
896
                $this->assertEquals($expected, $result);
897

    
898
                foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
899
                        $result = $this->JsEngine->object($data);
900
                        $this->assertEquals($expected, $result);
901
                }
902

    
903
                $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
904
                $result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
905
                $this->assertRegExp('/^PREFIX/', $result);
906
                $this->assertRegExp('/POSTFIX$/', $result);
907
                $this->assertNotRegExp('/.PREFIX./', $result);
908
                $this->assertNotRegExp('/.POSTFIX./', $result);
909
        }
910

    
911
/**
912
 * test Mapping of options.
913
 *
914
 * @return void
915
 */
916
        public function testOptionMapping() {
917
                $JsEngine = new OptionEngineHelper($this->View);
918
                $result = $JsEngine->testMap();
919
                $this->assertSame(array(), $result);
920

    
921
                $result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
922
                $this->assertEquals(array('foo' => 'bar', 'baz' => 'sho'), $result);
923

    
924
                $result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
925
                $this->assertEquals(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'), $result);
926

    
927
                $result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
928
                $this->assertEquals(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'), $result);
929
        }
930

    
931
/**
932
 * test that option parsing escapes strings and saves what is supposed to be saved.
933
 *
934
 * @return void
935
 */
936
        public function testOptionParsing() {
937
                $JsEngine = new OptionEngineHelper($this->View);
938

    
939
                $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
940
                $expected = 'key:1, url:"\\/posts\\/view\\/1"';
941
                $this->assertEquals($expected, $result);
942

    
943
                $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
944
                $expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
945
                $this->assertEquals($expected, $result);
946
        }
947

    
948
}