Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
		if (!class_exists('TestJsEngineHelper', false)) {
178
			$this->getMock('JsBaseEngineHelper', array(), array($this->View), 'TestJsEngineHelper');
179
		}
180
 
181
		$this->Js = new JsHelper($this->View, array('TestJs'));
182
		$this->Js->TestJsEngine = new TestJsEngineHelper($this->View);
183
		$this->mockObjects[] = $this->Js->TestJsEngine;
184
		$this->Js->request = $request;
185
		$this->Js->Html = new HtmlHelper($this->View);
186
		$this->Js->Html->request = $request;
187
		$this->Js->Form = new FormHelper($this->View);
188
		$this->Js->Form->request = $request;
189
		$this->Js->Form->Html = new HtmlHelper($this->View);
190
	}
191
 
192
/**
193
 * test object construction
194
 *
195
 * @return void
196
 */
197
	public function testConstruction() {
198
		$js = new JsHelper($this->View);
199
		$this->assertEquals(array('Html', 'Form', 'JqueryEngine'), $js->helpers);
200
 
201
		$js = new JsHelper($this->View, array('mootools'));
202
		$this->assertEquals(array('Html', 'Form', 'mootoolsEngine'), $js->helpers);
203
 
204
		$js = new JsHelper($this->View, 'prototype');
205
		$this->assertEquals(array('Html', 'Form', 'prototypeEngine'), $js->helpers);
206
 
207
		$js = new JsHelper($this->View, 'MyPlugin.Dojo');
208
		$this->assertEquals(array('Html', 'Form', 'MyPlugin.DojoEngine'), $js->helpers);
209
	}
210
 
211
/**
212
 * test that methods dispatch internally and to the engine class
213
 *
214
 * @expectedException PHPUnit_Framework_Error_Warning
215
 * @return void
216
 */
217
	public function testMethodDispatching() {
218
		$this->_useMock();
219
 
220
		$this->Js->TestJsEngine
221
			->expects($this->once())
222
			->method('event')
223
			->with('click', 'callback');
224
 
225
		$this->Js->event('click', 'callback');
226
 
227
		$this->Js->TestJsEngine = new StdClass();
228
		$this->Js->someMethodThatSurelyDoesntExist();
229
	}
230
 
231
/**
232
 * Test that method dispatching for events respects buffer parameters and bufferedMethods Lists.
233
 *
234
 * @return void
235
 */
236
	public function testEventDispatchWithBuffering() {
237
		$this->_useMock();
238
 
239
		$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
240
		$this->Js->TestJsEngine->expects($this->exactly(3))
241
			->method('event')
242
			->will($this->returnValue('This is an event call'));
243
 
244
		$this->Js->event('click', 'foo');
245
		$result = $this->Js->getBuffer();
246
		$this->assertEquals(1, count($result));
247
		$this->assertEquals('This is an event call', $result[0]);
248
 
249
		$result = $this->Js->event('click', 'foo', array('buffer' => false));
250
		$buffer = $this->Js->getBuffer();
251
		$this->assertTrue(empty($buffer));
252
		$this->assertEquals('This is an event call', $result);
253
 
254
		$result = $this->Js->event('click', 'foo', false);
255
		$buffer = $this->Js->getBuffer();
256
		$this->assertTrue(empty($buffer));
257
		$this->assertEquals('This is an event call', $result);
258
	}
259
 
260
/**
261
 * Test that method dispatching for effects respects buffer parameters and bufferedMethods Lists.
262
 *
263
 * @return void
264
 */
265
	public function testEffectDispatchWithBuffering() {
266
		$this->_useMock();
267
		$this->Js->TestJsEngine->expects($this->exactly(4))
268
			->method('effect')
269
			->will($this->returnValue('I am not buffered.'));
270
 
271
		$result = $this->Js->effect('slideIn');
272
		$buffer = $this->Js->getBuffer();
273
		$this->assertTrue(empty($buffer));
274
		$this->assertEquals('I am not buffered.', $result);
275
 
276
		$result = $this->Js->effect('slideIn', true);
277
		$buffer = $this->Js->getBuffer();
278
		$this->assertNull($result);
279
		$this->assertEquals(1, count($buffer));
280
		$this->assertEquals('I am not buffered.', $buffer[0]);
281
 
282
		$result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
283
		$buffer = $this->Js->getBuffer();
284
		$this->assertNull($result);
285
		$this->assertEquals(1, count($buffer));
286
		$this->assertEquals('I am not buffered.', $buffer[0]);
287
 
288
		$result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
289
		$buffer = $this->Js->getBuffer();
290
		$this->assertNull($result);
291
		$this->assertEquals(1, count($buffer));
292
		$this->assertEquals('I am not buffered.', $buffer[0]);
293
	}
294
 
295
/**
296
 * test that writeScripts generates scripts inline.
297
 *
298
 * @return void
299
 */
300
	public function testWriteScriptsNoFile() {
301
		$this->_useMock();
302
		$this->Js->buffer('one = 1;');
303
		$this->Js->buffer('two = 2;');
304
		$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
305
		$expected = array(
306
			'script' => array('type' => 'text/javascript'),
307
			$this->cDataStart,
308
			"one = 1;\ntwo = 2;",
309
			$this->cDataEnd,
310
			'/script',
311
		);
312
		$this->assertTags($result, $expected);
313
 
314
		$this->Js->TestJsEngine->expects($this->atLeastOnce())->method('domReady');
315
		$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
316
 
317
		$this->View->expects($this->once())
318
			->method('append')
319
			->with('script', $this->matchesRegularExpression('/one\s\=\s1;\ntwo\s\=\s2;/'));
320
		$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
321
	}
322
 
323
/**
324
 * test that writing the buffer with inline = false includes a script tag.
325
 *
326
 * @return void
327
 */
328
	public function testWriteBufferNotInline() {
329
		$this->Js->set('foo', 1);
330
 
331
		$this->View->expects($this->once())
332
			->method('append')
333
			->with('script', $this->matchesRegularExpression('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#'));
334
 
335
		$this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
336
	}
337
 
338
/**
339
 * test that writeBuffer() sets domReady = false when the request is done by XHR.
340
 * Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries
341
 *
342
 * @return void
343
 */
344
	public function testWriteBufferAndXhr() {
345
		$this->_useMock();
346
		$requestWith = null;
347
		if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
348
			$requestWith = $_SERVER['HTTP_X_REQUESTED_WITH'];
349
		}
350
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
351
 
352
		$this->Js->buffer('alert("test");');
353
		$this->Js->TestJsEngine->expects($this->never())->method('domReady');
354
		$this->Js->writeBuffer();
355
 
356
		unset($_SERVER['HTTP_X_REQUESTED_WITH']);
357
		if ($requestWith !== null) {
358
			$_SERVER['HTTP_X_REQUESTED_WITH'] = $requestWith;
359
		}
360
	}
361
 
362
/**
363
 * test that writeScripts makes files, and puts the events into them.
364
 *
365
 * @return void
366
 */
367
	public function testWriteScriptsInFile() {
368
		$this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, script caching test has been skipped.');
369
 
370
		Configure::write('Cache.disable', false);
371
		$this->Js->request->webroot = '/';
372
		$this->Js->JsBaseEngine = new TestJsEngineHelper($this->View);
373
		$this->Js->buffer('one = 1;');
374
		$this->Js->buffer('two = 2;');
375
		$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
376
		$expected = array(
377
			'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
378
		);
379
		$this->assertTags($result, $expected);
380
		preg_match('/src="(.*\.js)"/', $result, $filename);
381
		$this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
382
		$contents = file_get_contents(WWW_ROOT . $filename[1]);
383
		$this->assertRegExp('/one\s=\s1;\ntwo\s=\s2;/', $contents);
384
		if (file_exists(WWW_ROOT . $filename[1])) {
385
			unlink(WWW_ROOT . $filename[1]);
386
		}
387
 
388
		Configure::write('Cache.disable', true);
389
		$this->Js->buffer('one = 1;');
390
		$this->Js->buffer('two = 2;');
391
		$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
392
		$this->assertRegExp('/one\s=\s1;\ntwo\s=\s2;/', $result);
393
		$this->assertFalse(file_exists(WWW_ROOT . $filename[1]));
394
	}
395
 
396
/**
397
 * test link()
398
 *
399
 * @return void
400
 */
401
	public function testLinkWithMock() {
402
		$this->_useMock();
403
 
404
		$options = array('update' => '#content');
405
 
406
		$this->Js->TestJsEngine->expects($this->at(0))
407
			->method('get');
408
 
409
		$this->Js->TestJsEngine->expects($this->at(1))
410
			->method('request')
411
			->with('/posts/view/1', $options)
412
			->will($this->returnValue('--ajax code--'));
413
 
414
		$this->Js->TestJsEngine->expects($this->at(2))
415
			->method('event')
416
			->with('click', '--ajax code--', $options + array('buffer' => null));
417
 
418
		$result = $this->Js->link('test link', '/posts/view/1', $options);
419
		$expected = array(
420
			'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
421
			'test link',
422
			'/a'
423
		);
424
		$this->assertTags($result, $expected);
425
	}
426
 
427
/**
428
 * test link with a mock and confirmation
429
 *
430
 * @return void
431
 */
432
	public function testLinkWithMockAndConfirm() {
433
		$this->_useMock();
434
 
435
		$options = array(
436
			'confirm' => 'Are you sure?',
437
			'update' => '#content',
438
			'class' => 'my-class',
439
			'id' => 'custom-id',
440
			'escape' => false
441
		);
442
		$this->Js->TestJsEngine->expects($this->once())
443
			->method('confirmReturn')
444
			->with($options['confirm'])
445
			->will($this->returnValue('--confirm script--'));
446
 
447
		$this->Js->TestJsEngine->expects($this->once())
448
			->method('request')
449
			->with('/posts/view/1');
450
 
451
		$this->Js->TestJsEngine->expects($this->once())
452
			->method('event')
453
			->with('click', '--confirm script--');
454
 
455
		$result = $this->Js->link('test link »', '/posts/view/1', $options);
456
		$expected = array(
457
			'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
458
			'test link »',
459
			'/a'
460
		);
461
		$this->assertTags($result, $expected);
462
	}
463
 
464
/**
465
 * test link passing on htmlAttributes
466
 *
467
 * @return void
468
 */
469
	public function testLinkWithAribtraryAttributes() {
470
		$this->_useMock();
471
 
472
		$options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
473
		$result = $this->Js->link('test link', '/posts/view/1', $options);
474
		$expected = array(
475
			'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
476
				'batman' => 'robin'),
477
			'test link',
478
			'/a'
479
		);
480
		$this->assertTags($result, $expected);
481
	}
482
 
483
/**
484
 * test that link() and no buffering returns an <a> and <script> tags.
485
 *
486
 * @return void
487
 */
488
	public function testLinkWithNoBuffering() {
489
		$this->_useMock();
490
 
491
		$this->Js->TestJsEngine->expects($this->at(1))
492
			->method('request')
493
			->with('/posts/view/1', array('update' => '#content'))
494
			->will($this->returnValue('ajax code'));
495
 
496
		$this->Js->TestJsEngine->expects($this->at(2))
497
			->method('event')
498
			->will($this->returnValue('-event handler-'));
499
 
500
		$options = array('update' => '#content', 'buffer' => false);
501
		$result = $this->Js->link('test link', '/posts/view/1', $options);
502
		$expected = array(
503
			'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
504
			'test link',
505
			'/a',
506
			'script' => array('type' => 'text/javascript'),
507
			$this->cDataStart,
508
			'-event handler-',
509
			$this->cDataEnd,
510
			'/script'
511
		);
512
		$this->assertTags($result, $expected);
513
	}
514
 
515
/**
516
 * test link with buffering off and safe on.
517
 *
518
 * @return void
519
 */
520
	public function testLinkWithNoBufferingAndSafe() {
521
		$this->_useMock();
522
 
523
		$this->Js->TestJsEngine->expects($this->at(1))
524
			->method('request')
525
			->with('/posts/view/1', array('update' => '#content'))
526
			->will($this->returnValue('ajax code'));
527
 
528
		$this->Js->TestJsEngine->expects($this->at(2))
529
			->method('event')
530
			->will($this->returnValue('-event handler-'));
531
 
532
		$options = array('update' => '#content', 'buffer' => false, 'safe' => false);
533
		$result = $this->Js->link('test link', '/posts/view/1', $options);
534
 
535
		$expected = array(
536
			'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
537
			'test link',
538
			'/a',
539
			'script' => array('type' => 'text/javascript'),
540
			'-event handler-',
541
			'/script'
542
		);
543
		$this->assertTags($result, $expected);
544
	}
545
 
546
/**
547
 * test submit() with a Mock to check Engine method calls
548
 *
549
 * @return void
550
 */
551
	public function testSubmitWithMock() {
552
		$this->_useMock();
553
 
554
		$options = array('update' => '#content', 'id' => 'test-submit', 'style' => 'margin: 0');
555
 
556
		$this->Js->TestJsEngine->expects($this->at(0))
557
			->method('get');
558
 
559
		$this->Js->TestJsEngine->expects($this->at(1))
560
			->method('serializeForm')
561
			->will($this->returnValue('serialize-code'));
562
 
563
		$this->Js->TestJsEngine->expects($this->at(2))
564
			->method('request')
565
			->will($this->returnValue('ajax-code'));
566
 
567
		$params = array(
568
			'update' => $options['update'], 'data' => 'serialize-code',
569
			'method' => 'post', 'dataExpression' => true, 'buffer' => null
570
		);
571
 
572
		$this->Js->TestJsEngine->expects($this->at(3))
573
			->method('event')
574
			->with('click', "ajax-code", $params);
575
 
576
		$result = $this->Js->submit('Save', $options);
577
		$expected = array(
578
			'div' => array('class' => 'submit'),
579
			'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save', 'style' => 'margin: 0'),
580
			'/div'
581
		);
582
		$this->assertTags($result, $expected);
583
	}
584
 
585
/**
586
 * test submit() with a mock
587
 *
588
 * @return void
589
 */
590
	public function testSubmitWithMockRequestParams() {
591
		$this->_useMock();
592
 
593
		$this->Js->TestJsEngine->expects($this->at(0))
594
			->method('get');
595
 
596
		$this->Js->TestJsEngine->expects($this->at(1))
597
			->method('serializeForm')
598
			->will($this->returnValue('serialize-code'));
599
 
600
		$requestParams = array(
601
			'update' => '#content',
602
			'data' => 'serialize-code',
603
			'method' => 'post',
604
			'dataExpression' => true
605
		);
606
 
607
		$this->Js->TestJsEngine->expects($this->at(2))
608
			->method('request')
609
			->with('/custom/url', $requestParams)
610
			->will($this->returnValue('ajax-code'));
611
 
612
		$params = array(
613
			'update' => '#content', 'data' => 'serialize-code',
614
			'method' => 'post', 'dataExpression' => true, 'buffer' => null
615
		);
616
 
617
		$this->Js->TestJsEngine->expects($this->at(3))
618
			->method('event')
619
			->with('click', "ajax-code", $params);
620
 
621
		$options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
622
		$result = $this->Js->submit('Save', $options);
623
		$expected = array(
624
			'div' => array('class' => 'submit'),
625
			'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
626
			'/div'
627
		);
628
		$this->assertTags($result, $expected);
629
	}
630
 
631
/**
632
 * test that no buffer works with submit() and that parameters are leaking into the script tag.
633
 *
634
 * @return void
635
 */
636
	public function testSubmitWithNoBuffer() {
637
		$this->_useMock();
638
		$options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
639
 
640
		$this->Js->TestJsEngine->expects($this->at(0))
641
			->method('get');
642
 
643
		$this->Js->TestJsEngine->expects($this->at(1))
644
			->method('serializeForm')
645
			->will($this->returnValue('serialize-code'));
646
 
647
		$this->Js->TestJsEngine->expects($this->at(2))
648
			->method('request')
649
			->will($this->returnValue('ajax-code'));
650
 
651
		$this->Js->TestJsEngine->expects($this->at(3))
652
			->method('event')
653
			->will($this->returnValue('event-handler'));
654
 
655
		$params = array(
656
			'update' => $options['update'], 'data' => 'serialize-code',
657
			'method' => 'post', 'dataExpression' => true, 'buffer' => false
658
		);
659
 
660
		$this->Js->TestJsEngine->expects($this->at(3))
661
			->method('event')
662
			->with('click', "ajax-code", $params);
663
 
664
		$result = $this->Js->submit('Save', $options);
665
		$expected = array(
666
			'div' => array('class' => 'submit'),
667
			'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
668
			'/div',
669
			'script' => array('type' => 'text/javascript'),
670
			'event-handler',
671
			'/script'
672
		);
673
		$this->assertTags($result, $expected);
674
	}
675
 
676
/**
677
 * Test that Object::Object() is not breaking json output in JsHelper
678
 *
679
 * @return void
680
 */
681
	public function testObjectPassThrough() {
682
		$result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
683
		$expected = '{"one":"first","two":"second"}';
684
		$this->assertEquals($expected, $result);
685
	}
686
 
687
/**
688
 * Test that inherited Helper::value() is overwritten in JsHelper::value()
689
 * and calls JsBaseEngineHelper::value().
690
 *
691
 * @return void
692
 */
693
	public function testValuePassThrough() {
694
		$result = $this->Js->value('string "quote"', true);
695
		$expected = '"string \"quote\""';
696
		$this->assertEquals($expected, $result);
697
	}
698
 
699
/**
700
 * test set()'ing variables to the JavaScript buffer and controlling the output var name.
701
 *
702
 * @return void
703
 */
704
	public function testSet() {
705
		$this->Js->set('loggedIn', true);
706
		$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
707
		$result = $this->Js->getBuffer();
708
		$expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
709
		$this->assertEquals($expected, $result[0]);
710
 
711
		$this->Js->set('loggedIn', true);
712
		$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
713
		$this->Js->setVariable = 'WICKED';
714
		$result = $this->Js->getBuffer();
715
		$expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
716
		$this->assertEquals($expected, $result[0]);
717
 
718
		$this->Js->set('loggedIn', true);
719
		$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
720
		$this->Js->setVariable = 'Application.variables';
721
		$result = $this->Js->getBuffer();
722
		$expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
723
		$this->assertEquals($expected, $result[0]);
724
	}
725
 
726
/**
727
 * test that vars set with Js->set() go to the top of the buffered scripts list.
728
 *
729
 * @return void
730
 */
731
	public function testSetVarsAtTopOfBufferedScripts() {
732
		$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
733
		$this->Js->alert('hey you!', array('buffer' => true));
734
		$this->Js->confirm('Are you sure?', array('buffer' => true));
735
		$result = $this->Js->getBuffer(false);
736
 
737
		$expected = 'window.app = {"height":"tall","color":"purple"};';
738
		$this->assertEquals($expected, $result[0]);
739
		$this->assertEquals('alert("hey you!");', $result[1]);
740
		$this->assertEquals('confirm("Are you sure?");', $result[2]);
741
	}
742
 
743
}
744
 
745
/**
746
 * JsBaseEngine Class Test case
747
 *
748
 * @package       Cake.Test.Case.View.Helper
749
 */
750
class JsBaseEngineTest extends CakeTestCase {
751
 
752
/**
753
 * setUp method
754
 *
755
 * @return void
756
 */
757
	public function setUp() {
758
		parent::setUp();
759
		$controller = null;
760
		$this->View = $this->getMock('View', array('append'), array(&$controller));
761
		$this->JsEngine = new OptionEngineHelper($this->View);
762
	}
763
 
764
/**
765
 * tearDown method
766
 *
767
 * @return void
768
 */
769
	public function tearDown() {
770
		parent::tearDown();
771
		unset($this->JsEngine);
772
	}
773
 
774
/**
775
 * test escape string skills
776
 *
777
 * @return void
778
 */
779
	public function testEscaping() {
780
		$result = $this->JsEngine->escape('');
781
		$expected = '';
782
		$this->assertEquals($expected, $result);
783
 
784
		$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
785
		$expected = 'CakePHP\\nRapid Development Framework';
786
		$this->assertEquals($expected, $result);
787
 
788
		$result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
789
		$expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
790
		$this->assertEquals($expected, $result);
791
 
792
		$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
793
		$expected = 'CakePHP: \\"Rapid Development Framework\\"';
794
		$this->assertEquals($expected, $result);
795
 
796
		$result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
797
		$expected = "CakePHP: 'Rapid Development Framework'";
798
		$this->assertEquals($expected, $result);
799
 
800
		$result = $this->JsEngine->escape('my \\"string\\"');
801
		$expected = 'my \\\\\\"string\\\\\\"';
802
		$this->assertEquals($expected, $result);
803
	}
804
 
805
/**
806
 * test prompt() creation
807
 *
808
 * @return void
809
 */
810
	public function testPrompt() {
811
		$result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
812
		$expected = 'prompt("Hey, hey you", "hi!");';
813
		$this->assertEquals($expected, $result);
814
 
815
		$result = $this->JsEngine->prompt('"Hey"', '"hi"');
816
		$expected = 'prompt("\"Hey\"", "\"hi\"");';
817
		$this->assertEquals($expected, $result);
818
	}
819
 
820
/**
821
 * test alert generation
822
 *
823
 * @return void
824
 */
825
	public function testAlert() {
826
		$result = $this->JsEngine->alert('Hey there');
827
		$expected = 'alert("Hey there");';
828
		$this->assertEquals($expected, $result);
829
 
830
		$result = $this->JsEngine->alert('"Hey"');
831
		$expected = 'alert("\"Hey\"");';
832
		$this->assertEquals($expected, $result);
833
	}
834
 
835
/**
836
 * test confirm generation
837
 *
838
 * @return void
839
 */
840
	public function testConfirm() {
841
		$result = $this->JsEngine->confirm('Are you sure?');
842
		$expected = 'confirm("Are you sure?");';
843
		$this->assertEquals($expected, $result);
844
 
845
		$result = $this->JsEngine->confirm('"Are you sure?"');
846
		$expected = 'confirm("\"Are you sure?\"");';
847
		$this->assertEquals($expected, $result);
848
	}
849
 
850
/**
851
 * test Redirect
852
 *
853
 * @return void
854
 */
855
	public function testRedirect() {
856
		$result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
857
		$expected = 'window.location = "/posts/view/1";';
858
		$this->assertEquals($expected, $result);
859
	}
860
 
861
/**
862
 * testObject encoding with non-native methods.
863
 *
864
 * @return void
865
 */
866
	public function testObject() {
867
		$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
868
		$result = $this->JsEngine->object($object);
869
		$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
870
		$this->assertEquals($expected, $result);
871
 
872
		$object = new JsEncodingObject();
873
		$object->title = 'New thing';
874
		$object->indexes = array(5, 6, 7, 8);
875
		$result = $this->JsEngine->object($object);
876
		$this->assertEquals($expected, $result);
877
 
878
		$result = $this->JsEngine->object(array('default' => 0));
879
		$expected = '{"default":0}';
880
		$this->assertEquals($expected, $result);
881
 
882
		$result = $this->JsEngine->object(array(
883
			'2007' => array(
884
				'Spring' => array(
885
					'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
886
				),
887
				'Fall' => array(
888
					'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
889
				)
890
			),
891
			'2006' => array(
892
				'Spring' => array(
893
					'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
894
				),
895
				'Fall' => array(
896
					'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
897
				)
898
			)
899
		));
900
		$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"}}}}';
901
		$this->assertEquals($expected, $result);
902
 
903
		foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
904
			$result = $this->JsEngine->object($data);
905
			$this->assertEquals($expected, $result);
906
		}
907
 
908
		$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
909
		$result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
910
		$this->assertRegExp('/^PREFIX/', $result);
911
		$this->assertRegExp('/POSTFIX$/', $result);
912
		$this->assertNotRegExp('/.PREFIX./', $result);
913
		$this->assertNotRegExp('/.POSTFIX./', $result);
914
	}
915
 
916
/**
917
 * test Mapping of options.
918
 *
919
 * @return void
920
 */
921
	public function testOptionMapping() {
922
		$JsEngine = new OptionEngineHelper($this->View);
923
		$result = $JsEngine->testMap();
924
		$this->assertSame(array(), $result);
925
 
926
		$result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
927
		$this->assertEquals(array('foo' => 'bar', 'baz' => 'sho'), $result);
928
 
929
		$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
930
		$this->assertEquals(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'), $result);
931
 
932
		$result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
933
		$this->assertEquals(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'), $result);
934
	}
935
 
936
/**
937
 * test that option parsing escapes strings and saves what is supposed to be saved.
938
 *
939
 * @return void
940
 */
941
	public function testOptionParsing() {
942
		$JsEngine = new OptionEngineHelper($this->View);
943
 
944
		$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
945
		$expected = 'key:1, url:"\\/posts\\/view\\/1"';
946
		$this->assertEquals($expected, $result);
947
 
948
		$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
949
		$expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
950
		$this->assertEquals($expected, $result);
951
	}
952
 
953
}