5
* TestCase for the JsHelper
9
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The Open Group Test Suite License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
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
22
App::import('Helper', array('Js', 'Html', 'Form'));
23
App::import('Core', array('View', 'ClassRegistry'));
25
Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
26
Mock::generate('View', 'JsHelperMockView');
28
class OptionEngineHelper extends JsBaseEngineHelper {
29
var $_optionMap = array(
31
'complete' => 'success',
32
'request' => 'beforeSend',
38
* test method for testing option mapping
42
function testMap($options = array()) {
43
return $this->_mapOptions('request', $options);
46
* test method for option parsing
50
function testParseOptions($options, $safe = array()) {
51
return $this->_parseOptions($options, $safe);
59
* @subpackage cake.tests.cases.libs.view.helpers
61
class JsHelperTestCase extends CakeTestCase {
63
* Regexp for CDATA start block
67
var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
70
* Regexp for CDATA end block
74
var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
82
function startTest() {
83
$this->_asset = Configure::read('Asset.timestamp');
84
Configure::write('Asset.timestamp', false);
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();
92
$view =& new JsHelperMockView();
93
ClassRegistry::addObject('view', $view);
103
Configure::write('Asset.timestamp', $this->_asset);
104
ClassRegistry::removeObject('view');
109
* Switches $this->Js to a mocked engine.
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();
122
* test object construction
126
function testConstruction() {
127
$js =& new JsHelper();
128
$this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));
130
$js =& new JsHelper(array('mootools'));
131
$this->assertEqual($js->helpers, array('Html', 'Form', 'mootoolsEngine'));
133
$js =& new JsHelper('prototype');
134
$this->assertEqual($js->helpers, array('Html', 'Form', 'prototypeEngine'));
136
$js =& new JsHelper('MyPlugin.Dojo');
137
$this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
141
* test that methods dispatch internally and to the engine class
145
function testMethodDispatching() {
147
$this->Js->TestJsEngine->expectOnce('dispatchMethod', array(new PatternExpectation('/methodOne/i'), array()));
149
$this->Js->methodOne();
151
$this->Js->TestEngine =& new StdClass();
152
$this->expectError();
153
$this->Js->someMethodThatSurelyDoesntExist();
157
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
161
function testMethodDispatchWithBuffering() {
164
$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
165
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
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');
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');
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');
182
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
184
$result = $this->Js->effect('slideIn');
185
$buffer = $this->Js->getBuffer();
186
$this->assertTrue(empty($buffer));
187
$this->assertEqual($result, 'I am not buffered.');
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.');
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.');
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.');
209
* test that writeScripts generates scripts inline.
213
function testWriteScriptsNoFile() {
215
$this->Js->buffer('one = 1;');
216
$this->Js->buffer('two = 2;');
217
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
219
'script' => array('type' => 'text/javascript'),
221
"one = 1;\ntwo = 2;",
225
$this->assertTags($result, $expected, true);
227
$this->Js->TestJsEngine->expectAtLeastOnce('domReady');
228
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
230
ClassRegistry::removeObject('view');
231
$view =& new JsHelperMockView();
232
ClassRegistry::addObject('view', $view);
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));
240
* test that writing the buffer with inline = false includes a script tag.
244
function testWriteBufferNotInline() {
245
$this->Js->set('foo', 1);
247
$view =& new JsHelperMockView();
248
ClassRegistry::removeObject('view');
249
ClassRegistry::addObject('view', $view);
250
$view->expectCallCount('addScript', 1);
252
$pattern = new PatternExpectation('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#');
253
$view->expectAt(0, 'addScript', array($pattern));
255
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
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
264
function testWriteBufferAndXhr() {
266
$this->Js->params['isAjax'] = true;
267
$this->Js->buffer('alert("test");');
268
$this->Js->TestJsEngine->expectCallCount('dispatchMethod', 0);
269
$result = $this->Js->writeBuffer();
273
* test that writeScripts makes files, and puts the events into them.
277
function testWriteScriptsInFile() {
278
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) {
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));
286
'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
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);
294
@unlink(WWW_ROOT . $filename[1]);
302
function testLinkWithMock() {
304
$options = array('update' => '#content');
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)
311
$this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array(
312
'event', array('click', 'ajax code', $options + array('buffer' => null))
315
$result = $this->Js->link('test link', '/posts/view/1', $options);
317
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
321
$this->assertTags($result, $expected);
324
'confirm' => 'Are you sure?',
325
'update' => '#content',
326
'class' => 'my-class',
330
$this->Js->TestJsEngine->expectAt(0, 'confirm', array($options['confirm']));
331
$this->Js->TestJsEngine->expectAt(1, 'request', array('/posts/view/1', '*'));
333
var _confirm = confirm("Are you sure?");
338
$this->Js->TestJsEngine->expectAt(1, 'event', array('click', $code));
339
$result = $this->Js->link('test link »', '/posts/view/1', $options);
341
'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
345
$this->assertTags($result, $expected);
347
$options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
348
$result = $this->Js->link('test link', '/posts/view/1', $options);
350
'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
351
'batman' => 'robin'),
355
$this->assertTags($result, $expected);
359
* test that link() and no buffering returns an <a> and <script> tags.
363
function testLinkWithNoBuffering() {
365
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array(
366
'request', array('/posts/view/1', array('update' => '#content'))
368
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', '-event handler-', array('event', '*'));
370
$options = array('update' => '#content', 'buffer' => false);
371
$result = $this->Js->link('test link', '/posts/view/1', $options);
373
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
376
'script' => array('type' => 'text/javascript'),
382
$this->assertTags($result, $expected);
384
$options = array('update' => '#content', 'buffer' => false, 'safe' => false);
385
$result = $this->Js->link('test link', '/posts/view/1', $options);
387
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
390
'script' => array('type' => 'text/javascript'),
394
$this->assertTags($result, $expected);
398
* test submit() with a Mock to check Engine method calls
402
function testSubmitWithMock() {
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', '*'));
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', '*'));
415
'update' => $options['update'], 'data' => 'serialize-code',
416
'method' => 'post', 'dataExpression' => true, 'buffer' => null
418
$this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
419
'event', array('click', "ajax-code", $params)
422
$result = $this->Js->submit('Save', $options);
424
'div' => array('class' => 'submit'),
425
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
428
$this->assertTags($result, $expected);
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',
438
'dataExpression' => true
441
$this->Js->TestJsEngine->expectAt(6, 'dispatchMethod', array('request', $requestParams));
444
'update' => '#content', 'data' => 'serialize-code',
445
'method' => 'post', 'dataExpression' => true, 'buffer' => null
447
$this->Js->TestJsEngine->expectAt(7, 'dispatchMethod', array(
448
'event', array('click', "ajax-code", $params)
451
$options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
452
$result = $this->Js->submit('Save', $options);
454
'div' => array('class' => 'submit'),
455
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
458
$this->assertTags($result, $expected);
462
* test that no buffer works with submit() and that parameters are leaking into the script tag.
466
function testSubmitWithNoBuffer() {
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', '*'));
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)
481
'update' => $options['update'], 'data' => 'serialize-code',
482
'method' => 'post', 'dataExpression' => true, 'buffer' => false
484
$this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
485
'event', array('click', "ajax-code", $params)
488
$result = $this->Js->submit('Save', $options);
490
'div' => array('class' => 'submit'),
491
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
493
'script' => array('type' => 'text/javascript'),
497
$this->assertTags($result, $expected);
501
* Test that Object::Object() is not breaking json output in JsHelper
505
function testObjectPassThrough() {
506
$result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
507
$expected = '{"one":"first","two":"second"}';
508
$this->assertEqual($result, $expected);
512
* Test that inherited Helper::value() is overwritten in JsHelper::value()
513
* and calls JsBaseEngineHelper::value().
517
function testValuePassThrough() {
518
$result = $this->Js->value('string "quote"', true);
519
$expected = '"string \"quote\""';
520
$this->assertEqual($result, $expected);
524
* test set()'ing variables to the Javascript buffer and controlling the output var name.
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);
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);
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);
551
* test that vars set with Js->set() go to the top of the buffered scripts list.
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);
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?");');
569
* JsBaseEngine Class Test case
571
* @package cake.tests.view.helpers
573
class JsBaseEngineTestCase extends CakeTestCase {
580
function startTest() {
581
$this->JsEngine = new JsBaseEngineHelper();
590
ClassRegistry::removeObject('view');
591
unset($this->JsEngine);
595
* test escape string skills
599
function testEscaping() {
600
$result = $this->JsEngine->escape('');
602
$this->assertEqual($result, $expected);
604
$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
605
$expected = 'CakePHP\\nRapid Development Framework';
606
$this->assertEqual($result, $expected);
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);
612
$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
613
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
614
$this->assertEqual($result, $expected);
616
$result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
617
$expected = "CakePHP: 'Rapid Development Framework'";
618
$this->assertEqual($result, $expected);
620
$result = $this->JsEngine->escape('my \\"string\\"');
621
$expected = 'my \\\\\\"string\\\\\\"';
622
$this->assertEqual($result, $expected);
626
* test prompt() creation
630
function testPrompt() {
631
$result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
632
$expected = 'prompt("Hey, hey you", "hi!");';
633
$this->assertEqual($result, $expected);
635
$result = $this->JsEngine->prompt('"Hey"', '"hi"');
636
$expected = 'prompt("\"Hey\"", "\"hi\"");';
637
$this->assertEqual($result, $expected);
641
* test alert generation
645
function testAlert() {
646
$result = $this->JsEngine->alert('Hey there');
647
$expected = 'alert("Hey there");';
648
$this->assertEqual($result, $expected);
650
$result = $this->JsEngine->alert('"Hey"');
651
$expected = 'alert("\"Hey\"");';
652
$this->assertEqual($result, $expected);
656
* test confirm generation
660
function testConfirm() {
661
$result = $this->JsEngine->confirm('Are you sure?');
662
$expected = 'confirm("Are you sure?");';
663
$this->assertEqual($result, $expected);
665
$result = $this->JsEngine->confirm('"Are you sure?"');
666
$expected = 'confirm("\"Are you sure?\"");';
667
$this->assertEqual($result, $expected);
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);
682
* testObject encoding with non-native methods.
686
function testObject() {
687
$this->JsEngine->useNative = false;
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);
694
$result = $this->JsEngine->object(array('default' => 0));
695
$expected = '{"default":0}';
696
$this->assertEqual($result, $expected);
698
$result = $this->JsEngine->object(array(
701
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
704
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
709
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
712
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
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);
719
foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
720
$result = $this->JsEngine->object($data);
721
$this->assertEqual($result, $expected);
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);
733
* test compatibility of JsBaseEngineHelper::object() vs. json_encode()
737
function testObjectAgainstJsonEncode() {
738
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
742
$this->JsEngine->useNative = false;
744
$data['mystring'] = "simple string";
745
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
747
$data['mystring'] = "strïng with spécial chârs";
748
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
750
$data['mystring'] = "a two lines\nstring";
751
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
753
$data['mystring'] = "a \t tabbed \t string";
754
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
756
$data['mystring'] = "a \"double-quoted\" string";
757
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
759
$data['mystring'] = 'a \\"double-quoted\\" string';
760
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
762
unset($data['mystring']);
763
$data[3] = array(1, 2, 3);
764
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
767
$data = array('mystring' => null, 'bool' => false, 'array' => array(1, 44, 66));
768
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
772
* test that JSON made with JsBaseEngineHelper::object() against json_decode()
776
function testObjectAgainstJsonDecode() {
777
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
781
$this->JsEngine->useNative = false;
783
$data = array("simple string");
784
$result = $this->JsEngine->object($data);
785
$this->assertEqual(json_decode($result), $data);
787
$data = array('my "string"');
788
$result = $this->JsEngine->object($data);
789
$this->assertEqual(json_decode($result), $data);
791
$data = array('my \\"string\\"');
792
$result = $this->JsEngine->object($data);
793
$this->assertEqual(json_decode($result), $data);
797
* test Mapping of options.
801
function testOptionMapping() {
802
$JsEngine = new OptionEngineHelper();
803
$result = $JsEngine->testMap();
804
$this->assertEqual($result, array());
806
$result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
807
$this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
809
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
810
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
812
$result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
813
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
817
* test that option parsing escapes strings and saves what is supposed to be saved.
821
function testOptionParsing() {
822
$JsEngine = new OptionEngineHelper();
824
$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
825
$expected = 'key:1, url:"\\/posts\\/view\\/1"';
826
$this->assertEqual($result, $expected);
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);