2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-07 00:51:13 +00:00
|
|
|
* JsHelper Test Case
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-07 00:51:13 +00:00
|
|
|
* TestCase for the JsHelper
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2009-03-19 21:10:13 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.view.helpers
|
2009-07-17 04:09:06 +00:00
|
|
|
* @since CakePHP(tm) v 1.3
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-25 02:13:54 +00:00
|
|
|
App::import('Helper', array('Js', 'Html', 'Form'));
|
2009-03-08 00:34:05 +00:00
|
|
|
App::import('Core', array('View', 'ClassRegistry'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
class OptionEngineHelper extends JsBaseEngineHelper {
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_optionMap = array(
|
2009-03-14 21:01:49 +00:00
|
|
|
'request' => array(
|
|
|
|
'complete' => 'success',
|
|
|
|
'request' => 'beforeSend',
|
|
|
|
'type' => 'dataType'
|
|
|
|
)
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
/**
|
|
|
|
* test method for testing option mapping
|
|
|
|
*
|
|
|
|
* @return array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-14 21:01:49 +00:00
|
|
|
function testMap($options = array()) {
|
|
|
|
return $this->_mapOptions('request', $options);
|
|
|
|
}
|
2009-03-15 16:26:12 +00:00
|
|
|
/**
|
|
|
|
* test method for option parsing
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-15 16:26:12 +00:00
|
|
|
function testParseOptions($options, $safe = array()) {
|
|
|
|
return $this->_parseOptions($options, $safe);
|
|
|
|
}
|
2010-06-21 02:31:24 +00:00
|
|
|
|
|
|
|
function get($selector) {}
|
|
|
|
function event($type, $callback, $options = array()) {}
|
|
|
|
function domReady($functionBody) {}
|
|
|
|
function each($callback) {}
|
2010-07-13 16:34:08 +00:00
|
|
|
function effect($name, $options = array()) {}
|
2010-06-21 02:31:24 +00:00
|
|
|
function request($url, $options = array()) {}
|
|
|
|
function drag($options = array()) {}
|
|
|
|
function drop($options = array()) {}
|
2010-07-13 16:34:08 +00:00
|
|
|
function sortable($options = array()) {}
|
|
|
|
function slider($options = array()) {}
|
|
|
|
function serializeForm($options = array()) {}
|
2009-03-14 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-07 00:51:13 +00:00
|
|
|
* JsHelper TestCase.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.view.helpers
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-13 14:01:37 +00:00
|
|
|
class JsHelperTest extends CakeTestCase {
|
2009-03-13 03:48:19 +00:00
|
|
|
/**
|
|
|
|
* Regexp for CDATA start block
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-13 03:48:19 +00:00
|
|
|
/**
|
|
|
|
* Regexp for CDATA end block
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-05-19 04:26:50 +00:00
|
|
|
* setUp method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-19 04:26:50 +00:00
|
|
|
function setUp() {
|
2009-09-18 00:23:36 +00:00
|
|
|
$this->_asset = Configure::read('Asset.timestamp');
|
|
|
|
Configure::write('Asset.timestamp', false);
|
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$controller = null;
|
|
|
|
$this->View = $this->getMock('View', array('addScript'), array(&$controller));
|
|
|
|
$this->Js = new JsHelper($this->View, 'Option');
|
2010-05-15 03:29:16 +00:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$this->Js->request = $request;
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->Js->Html = new HtmlHelper($this->View);
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->Html->request = $request;
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->Js->Form = new FormHelper($this->View);
|
2010-06-28 03:21:11 +00:00
|
|
|
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->Form->request = $request;
|
|
|
|
$this->Js->Form->Html = $this->Js->Html;
|
2010-06-21 02:31:24 +00:00
|
|
|
$this->Js->OptionEngine = new OptionEngineHelper();
|
2009-03-13 03:48:19 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
ClassRegistry::addObject('view', $this->View);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2010-05-19 04:26:50 +00:00
|
|
|
* tearDown method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-19 04:26:50 +00:00
|
|
|
function tearDown() {
|
2009-09-18 00:23:36 +00:00
|
|
|
Configure::write('Asset.timestamp', $this->_asset);
|
2009-03-08 00:34:05 +00:00
|
|
|
ClassRegistry::removeObject('view');
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($this->Js);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
/**
|
|
|
|
* Switches $this->Js to a mocked engine.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-12 22:42:36 +00:00
|
|
|
function _useMock() {
|
2010-05-15 03:29:16 +00:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
if (!class_exists('TestJsEngineHelper', false)) {
|
|
|
|
$this->getMock('JsBaseEngineHelper', array(), array(), 'TestJsEngineHelper');
|
|
|
|
}
|
2010-08-28 04:01:41 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js = new JsHelper($this->View, array('TestJs'));
|
|
|
|
$this->Js->TestJsEngine = new TestJsEngineHelper($this->View);
|
|
|
|
$this->mockObjects[] = $this->Js->TestJsEngine;
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->request = $request;
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->Js->Html = new HtmlHelper($this->View);
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->Html->request = $request;
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->Js->Form = new FormHelper($this->View);
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->Form->request = $request;
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->Js->Form->Html = new HtmlHelper($this->View);
|
2009-07-12 22:42:36 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-07 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* test object construction
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-07 07:26:19 +00:00
|
|
|
function testConstruction() {
|
2010-07-03 17:06:11 +00:00
|
|
|
$js = new JsHelper($this->View);
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));
|
2009-03-13 03:48:19 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$js = new JsHelper($this->View, array('mootools'));
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'Form', 'mootoolsEngine'));
|
2009-03-13 03:48:19 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$js = new JsHelper($this->View, 'prototype');
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'Form', 'prototypeEngine'));
|
2009-03-13 03:48:19 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$js = new JsHelper($this->View, 'MyPlugin.Dojo');
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-07 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* test that methods dispatch internally and to the engine class
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-07 07:26:19 +00:00
|
|
|
function testMethodDispatching() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
2009-03-07 18:10:50 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine
|
|
|
|
->expects($this->once())
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
|
|
|
->with('click', 'callback');
|
|
|
|
|
|
|
|
$this->Js->event('click', 'callback');
|
2009-03-07 18:10:50 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine = new StdClass();
|
2009-03-07 18:10:50 +00:00
|
|
|
$this->expectError();
|
2009-09-18 00:23:36 +00:00
|
|
|
$this->Js->someMethodThatSurelyDoesntExist();
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-29 20:10:04 +00:00
|
|
|
/**
|
2010-05-19 04:26:50 +00:00
|
|
|
* Test that method dispatching for events respects buffer parameters and bufferedMethods Lists.
|
2009-03-29 20:10:04 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-05-19 04:26:50 +00:00
|
|
|
function testEventDispatchWithBuffering() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->exactly(3))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
2010-05-19 04:26:50 +00:00
|
|
|
->will($this->returnValue('This is an event call'));
|
2009-03-29 20:10:04 +00:00
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->event('click', 'foo');
|
|
|
|
$result = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertEqual(count($result), 1);
|
|
|
|
$this->assertEqual($result[0], 'This is an event call');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->event('click', 'foo', array('buffer' => false));
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertTrue(empty($buffer));
|
|
|
|
$this->assertEqual($result, 'This is an event call');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->event('click', 'foo', false);
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertTrue(empty($buffer));
|
|
|
|
$this->assertEqual($result, 'This is an event call');
|
2010-05-19 04:26:50 +00:00
|
|
|
}
|
2009-03-29 20:10:04 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
/**
|
|
|
|
* Test that method dispatching for effects respects buffer parameters and bufferedMethods Lists.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testEffectDispatchWithBuffering() {
|
|
|
|
$this->_useMock();
|
|
|
|
$this->Js->TestJsEngine->expects($this->exactly(4))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('effect')
|
2010-05-19 04:26:50 +00:00
|
|
|
->will($this->returnValue('I am not buffered.'));
|
2009-03-29 20:10:04 +00:00
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->effect('slideIn');
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertTrue(empty($buffer));
|
|
|
|
$this->assertEqual($result, 'I am not buffered.');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->effect('slideIn', true);
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertNull($result);
|
|
|
|
$this->assertEqual(count($buffer), 1);
|
|
|
|
$this->assertEqual($buffer[0], 'I am not buffered.');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertNull($result);
|
|
|
|
$this->assertEqual(count($buffer), 1);
|
|
|
|
$this->assertEqual($buffer[0], 'I am not buffered.');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
|
|
|
|
$buffer = $this->Js->getBuffer();
|
2009-03-29 20:10:04 +00:00
|
|
|
$this->assertNull($result);
|
|
|
|
$this->assertEqual(count($buffer), 1);
|
|
|
|
$this->assertEqual($buffer[0], 'I am not buffered.');
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
/**
|
2009-03-13 03:48:19 +00:00
|
|
|
* test that writeScripts generates scripts inline.
|
2009-03-07 18:10:50 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-13 03:48:19 +00:00
|
|
|
function testWriteScriptsNoFile() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
2009-03-29 18:23:42 +00:00
|
|
|
$this->Js->buffer('one = 1;');
|
|
|
|
$this->Js->buffer('two = 2;');
|
2009-12-17 04:48:29 +00:00
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false, 'clear' => false));
|
2009-03-08 03:06:00 +00:00
|
|
|
$expected = array(
|
2009-03-13 03:48:19 +00:00
|
|
|
'script' => array('type' => 'text/javascript'),
|
|
|
|
$this->cDataStart,
|
|
|
|
"one = 1;\ntwo = 2;",
|
|
|
|
$this->cDataEnd,
|
2009-03-08 03:06:00 +00:00
|
|
|
'/script',
|
|
|
|
);
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->assertTags($result, $expected, true);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->atLeastOnce())->method('domReady');
|
2009-12-17 04:48:29 +00:00
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false, 'clear' => false));
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2010-07-31 20:20:15 +00:00
|
|
|
$this->View->expects($this->once())
|
2010-05-19 04:26:50 +00:00
|
|
|
->method('addScript')
|
|
|
|
->with($this->matchesRegularExpression('/one\s\=\s1;\ntwo\s\=\s2;/'));
|
2009-03-29 18:23:42 +00:00
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2009-12-21 14:29:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that writing the buffer with inline = false includes a script tag.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testWriteBufferNotInline() {
|
|
|
|
$this->Js->set('foo', 1);
|
|
|
|
|
2010-07-31 20:20:15 +00:00
|
|
|
$this->View->expects($this->once())
|
2010-05-19 04:26:50 +00:00
|
|
|
->method('addScript')
|
|
|
|
->with($this->matchesRegularExpression('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#'));
|
2009-12-21 14:29:28 +00:00
|
|
|
|
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2010-03-09 03:51:46 +00:00
|
|
|
/**
|
|
|
|
* test that writeBuffer() sets domReady = false when the request is done by XHR.
|
|
|
|
* Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testWriteBufferAndXhr() {
|
|
|
|
$this->_useMock();
|
|
|
|
$this->Js->params['isAjax'] = true;
|
|
|
|
$this->Js->buffer('alert("test");');
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->never())->method('domReady');
|
2010-03-09 03:51:46 +00:00
|
|
|
$result = $this->Js->writeBuffer();
|
|
|
|
}
|
|
|
|
|
2009-03-20 02:02:18 +00:00
|
|
|
/**
|
|
|
|
* test that writeScripts makes files, and puts the events into them.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-20 02:02:18 +00:00
|
|
|
function testWriteScriptsInFile() {
|
|
|
|
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-05-15 03:29:16 +00:00
|
|
|
$this->Js->request->webroot = '/';
|
2009-03-20 02:02:18 +00:00
|
|
|
$this->Js->JsBaseEngine = new TestJsEngineHelper();
|
2009-03-29 18:23:42 +00:00
|
|
|
$this->Js->buffer('one = 1;');
|
|
|
|
$this->Js->buffer('two = 2;');
|
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
|
2009-03-20 02:02:18 +00:00
|
|
|
$expected = array(
|
|
|
|
'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
preg_match('/src="(.*\.js)"/', $result, $filename);
|
|
|
|
$this->assertTrue(file_exists(WWW_ROOT . $filename[1]));
|
|
|
|
$contents = file_get_contents(WWW_ROOT . $filename[1]);
|
|
|
|
$this->assertPattern('/one\s=\s1;\ntwo\s=\s2;/', $contents);
|
2009-03-20 02:05:06 +00:00
|
|
|
|
|
|
|
@unlink(WWW_ROOT . $filename[1]);
|
2009-03-20 02:02:18 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-12 21:30:41 +00:00
|
|
|
/**
|
|
|
|
* test link()
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-12 23:06:45 +00:00
|
|
|
function testLinkWithMock() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
2010-05-31 02:13:09 +00:00
|
|
|
|
2009-07-12 23:06:45 +00:00
|
|
|
$options = array('update' => '#content');
|
2009-07-12 22:42:36 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(0))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('get');
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('request')
|
|
|
|
->with('/posts/view/1', $options)
|
|
|
|
->will($this->returnValue('--ajax code--'));
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
|
|
|
->with('click', '--ajax code--', $options + array('buffer' => null));
|
2009-07-12 23:06:45 +00:00
|
|
|
|
|
|
|
$result = $this->Js->link('test link', '/posts/view/1', $options);
|
2009-07-12 21:30:41 +00:00
|
|
|
$expected = array(
|
|
|
|
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
|
|
|
|
'test link',
|
|
|
|
'/a'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2010-05-31 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test link with a mock and confirmation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testLinkWithMockAndConfirm() {
|
|
|
|
$this->_useMock();
|
2009-07-25 02:13:54 +00:00
|
|
|
|
2009-07-12 23:06:45 +00:00
|
|
|
$options = array(
|
|
|
|
'confirm' => 'Are you sure?',
|
|
|
|
'update' => '#content',
|
|
|
|
'class' => 'my-class',
|
|
|
|
'id' => 'custom-id',
|
|
|
|
'escape' => false
|
|
|
|
);
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->once())
|
|
|
|
->method('confirmReturn')
|
|
|
|
->with($options['confirm'])
|
|
|
|
->will($this->returnValue('--confirm script--'));
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->once())
|
2010-05-19 04:26:50 +00:00
|
|
|
->method('request')
|
2010-07-03 17:06:11 +00:00
|
|
|
->with('/posts/view/1');
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->once())
|
2010-05-19 04:26:50 +00:00
|
|
|
->method('event')
|
2010-07-03 17:06:11 +00:00
|
|
|
->with('click', '--confirm script--');
|
2010-05-31 02:13:09 +00:00
|
|
|
|
2009-07-12 23:06:45 +00:00
|
|
|
$result = $this->Js->link('test link »', '/posts/view/1', $options);
|
|
|
|
$expected = array(
|
|
|
|
'a' => array('id' => $options['id'], 'class' => $options['class'], 'href' => '/posts/view/1'),
|
|
|
|
'test link »',
|
|
|
|
'/a'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2010-05-31 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test link passing on htmlAttributes
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testLinkWithAribtraryAttributes() {
|
|
|
|
$this->_useMock();
|
2009-07-25 02:13:54 +00:00
|
|
|
|
|
|
|
$options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
|
|
|
|
$result = $this->Js->link('test link', '/posts/view/1', $options);
|
|
|
|
$expected = array(
|
2009-12-07 01:45:12 +00:00
|
|
|
'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
|
2009-07-25 02:13:54 +00:00
|
|
|
'batman' => 'robin'),
|
|
|
|
'test link',
|
|
|
|
'/a'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-25 02:13:54 +00:00
|
|
|
/**
|
|
|
|
* test that link() and no buffering returns an <a> and <script> tags.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-25 02:13:54 +00:00
|
|
|
function testLinkWithNoBuffering() {
|
|
|
|
$this->_useMock();
|
2010-05-31 02:13:09 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('request')
|
|
|
|
->with('/posts/view/1', array('update' => '#content'))
|
2010-05-19 04:26:50 +00:00
|
|
|
->will($this->returnValue('ajax code'));
|
|
|
|
|
2010-05-31 02:13:09 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
2010-05-19 04:26:50 +00:00
|
|
|
->will($this->returnValue('-event handler-'));
|
2009-07-25 02:13:54 +00:00
|
|
|
|
|
|
|
$options = array('update' => '#content', 'buffer' => false);
|
|
|
|
$result = $this->Js->link('test link', '/posts/view/1', $options);
|
|
|
|
$expected = array(
|
|
|
|
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
|
|
|
|
'test link',
|
|
|
|
'/a',
|
|
|
|
'script' => array('type' => 'text/javascript'),
|
|
|
|
$this->cDataStart,
|
|
|
|
'-event handler-',
|
|
|
|
$this->cDataEnd,
|
|
|
|
'/script'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2010-05-31 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test link with buffering off and safe on.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testLinkWithNoBufferingAndSafe() {
|
|
|
|
$this->_useMock();
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('request')
|
|
|
|
->with('/posts/view/1', array('update' => '#content'))
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('ajax code'));
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('-event handler-'));
|
2009-07-25 02:13:54 +00:00
|
|
|
|
|
|
|
$options = array('update' => '#content', 'buffer' => false, 'safe' => false);
|
|
|
|
$result = $this->Js->link('test link', '/posts/view/1', $options);
|
2010-05-31 02:13:09 +00:00
|
|
|
|
2009-07-25 02:13:54 +00:00
|
|
|
$expected = array(
|
|
|
|
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
|
|
|
|
'test link',
|
|
|
|
'/a',
|
|
|
|
'script' => array('type' => 'text/javascript'),
|
|
|
|
'-event handler-',
|
|
|
|
'/script'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2009-07-12 21:30:41 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-21 04:24:58 +00:00
|
|
|
/**
|
|
|
|
* test submit() with a Mock to check Engine method calls
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-21 04:24:58 +00:00
|
|
|
function testSubmitWithMock() {
|
|
|
|
$this->_useMock();
|
2009-07-25 02:54:22 +00:00
|
|
|
|
|
|
|
$options = array('update' => '#content', 'id' => 'test-submit');
|
2009-07-21 04:24:58 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(0))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('get');
|
2009-07-21 04:24:58 +00:00
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('serializeForm')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('serialize-code'));
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('request')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('ajax-code'));
|
2009-07-25 02:54:22 +00:00
|
|
|
|
2009-07-25 03:29:44 +00:00
|
|
|
$params = array(
|
2009-12-07 01:45:12 +00:00
|
|
|
'update' => $options['update'], 'data' => 'serialize-code',
|
2010-05-03 01:17:10 +00:00
|
|
|
'method' => 'post', 'dataExpression' => true, 'buffer' => null
|
2009-07-25 03:29:44 +00:00
|
|
|
);
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(3))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
|
|
|
->with('click', "ajax-code", $params);
|
2009-07-25 02:54:22 +00:00
|
|
|
|
2009-07-21 04:24:58 +00:00
|
|
|
$result = $this->Js->submit('Save', $options);
|
|
|
|
$expected = array(
|
2009-07-25 02:13:54 +00:00
|
|
|
'div' => array('class' => 'submit'),
|
2009-07-21 04:24:58 +00:00
|
|
|
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
|
|
|
|
'/div'
|
|
|
|
);
|
2009-07-25 02:54:22 +00:00
|
|
|
$this->assertTags($result, $expected);
|
2010-07-03 17:06:11 +00:00
|
|
|
}
|
2009-07-25 02:54:22 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
/**
|
|
|
|
* test submit() with a mock
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSubmitWithMockRequestParams() {
|
|
|
|
$this->_useMock();
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(0))
|
|
|
|
->method('get');
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
|
|
|
->method('serializeForm')
|
|
|
|
->will($this->returnValue('serialize-code'));
|
2009-07-25 02:54:22 +00:00
|
|
|
|
|
|
|
$requestParams = array(
|
2010-07-03 17:06:11 +00:00
|
|
|
'update' => '#content',
|
|
|
|
'data' => 'serialize-code',
|
|
|
|
'method' => 'post',
|
|
|
|
'dataExpression' => true
|
2009-07-25 02:54:22 +00:00
|
|
|
);
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
|
|
|
->method('request')
|
|
|
|
->with('/custom/url', $requestParams)
|
|
|
|
->will($this->returnValue('ajax-code'));
|
2009-07-25 02:54:22 +00:00
|
|
|
|
2009-07-25 03:29:44 +00:00
|
|
|
$params = array(
|
2009-12-07 01:45:12 +00:00
|
|
|
'update' => '#content', 'data' => 'serialize-code',
|
2010-05-03 01:17:10 +00:00
|
|
|
'method' => 'post', 'dataExpression' => true, 'buffer' => null
|
2009-07-25 03:29:44 +00:00
|
|
|
);
|
2010-05-19 04:26:50 +00:00
|
|
|
|
2010-07-03 17:06:11 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(3))
|
|
|
|
->method('event')
|
|
|
|
->with('click', "ajax-code", $params);
|
2009-12-07 01:45:12 +00:00
|
|
|
|
2009-07-25 02:54:22 +00:00
|
|
|
$options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
|
|
|
|
$result = $this->Js->submit('Save', $options);
|
|
|
|
$expected = array(
|
|
|
|
'div' => array('class' => 'submit'),
|
|
|
|
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
|
|
|
|
'/div'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2009-07-21 04:24:58 +00:00
|
|
|
}
|
2009-08-10 00:51:59 +00:00
|
|
|
|
2010-04-23 04:04:15 +00:00
|
|
|
/**
|
|
|
|
* test that no buffer works with submit() and that parameters are leaking into the script tag.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSubmitWithNoBuffer() {
|
|
|
|
$this->_useMock();
|
|
|
|
$options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);
|
|
|
|
|
2010-05-19 04:26:50 +00:00
|
|
|
$this->Js->TestJsEngine->expects($this->at(0))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('get');
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(1))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('serializeForm')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('serialize-code'));
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(2))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('request')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('ajax-code'));
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(3))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
2010-05-31 02:13:09 +00:00
|
|
|
->will($this->returnValue('event-handler'));
|
|
|
|
|
2010-04-23 04:04:15 +00:00
|
|
|
$params = array(
|
2010-05-03 01:17:10 +00:00
|
|
|
'update' => $options['update'], 'data' => 'serialize-code',
|
|
|
|
'method' => 'post', 'dataExpression' => true, 'buffer' => false
|
2010-04-23 04:04:15 +00:00
|
|
|
);
|
2010-05-19 04:26:50 +00:00
|
|
|
|
|
|
|
$this->Js->TestJsEngine->expects($this->at(3))
|
2010-07-03 17:06:11 +00:00
|
|
|
->method('event')
|
|
|
|
->with('click', "ajax-code", $params);
|
2010-04-23 04:04:15 +00:00
|
|
|
|
|
|
|
$result = $this->Js->submit('Save', $options);
|
|
|
|
$expected = array(
|
|
|
|
'div' => array('class' => 'submit'),
|
|
|
|
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
|
|
|
|
'/div',
|
|
|
|
'script' => array('type' => 'text/javascript'),
|
|
|
|
'event-handler',
|
|
|
|
'/script'
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
}
|
|
|
|
|
2009-08-10 00:51:59 +00:00
|
|
|
/**
|
|
|
|
* Test that Object::Object() is not breaking json output in JsHelper
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-08-10 00:51:59 +00:00
|
|
|
function testObjectPassThrough() {
|
|
|
|
$result = $this->Js->object(array('one' => 'first', 'two' => 'second'));
|
|
|
|
$expected = '{"one":"first","two":"second"}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-10-15 01:26:43 +00:00
|
|
|
|
2009-12-07 01:45:12 +00:00
|
|
|
/**
|
|
|
|
* Test that inherited Helper::value() is overwritten in JsHelper::value()
|
|
|
|
* and calls JsBaseEngineHelper::value().
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValuePassThrough() {
|
|
|
|
$result = $this->Js->value('string "quote"', true);
|
|
|
|
$expected = '"string \"quote\""';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
|
2009-10-15 01:26:43 +00:00
|
|
|
/**
|
|
|
|
* test set()'ing variables to the Javascript buffer and controlling the output var name.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-15 01:26:43 +00:00
|
|
|
function testSet() {
|
|
|
|
$this->Js->set('loggedIn', true);
|
|
|
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
|
|
|
$result = $this->Js->getBuffer();
|
2009-12-11 04:37:55 +00:00
|
|
|
$expected = 'window.app = {"loggedIn":true,"height":"tall","color":"purple"};';
|
2009-10-15 01:26:43 +00:00
|
|
|
$this->assertEqual($result[0], $expected);
|
2009-10-15 01:43:50 +00:00
|
|
|
|
2009-10-15 01:26:43 +00:00
|
|
|
$this->Js->set('loggedIn', true);
|
|
|
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
|
|
|
$this->Js->setVariable = 'WICKED';
|
2009-10-15 01:43:50 +00:00
|
|
|
$result = $this->Js->getBuffer();
|
2009-12-11 04:37:55 +00:00
|
|
|
$expected = 'window.WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
|
2009-10-15 01:26:43 +00:00
|
|
|
$this->assertEqual($result[0], $expected);
|
2009-12-07 01:45:12 +00:00
|
|
|
|
2009-10-15 01:43:50 +00:00
|
|
|
$this->Js->set('loggedIn', true);
|
|
|
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
|
|
|
$this->Js->setVariable = 'Application.variables';
|
|
|
|
$result = $this->Js->getBuffer();
|
|
|
|
$expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};';
|
|
|
|
$this->assertEqual($result[0], $expected);
|
2009-10-15 01:26:43 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-12-21 14:41:36 +00:00
|
|
|
/**
|
|
|
|
* test that vars set with Js->set() go to the top of the buffered scripts list.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSetVarsAtTopOfBufferedScripts() {
|
|
|
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
|
|
|
$this->Js->alert('hey you!', array('buffer' => true));
|
|
|
|
$this->Js->confirm('Are you sure?', array('buffer' => true));
|
|
|
|
$result = $this->Js->getBuffer(false);
|
2010-02-18 03:27:52 +00:00
|
|
|
|
2009-12-21 14:41:36 +00:00
|
|
|
$expected = 'window.app = {"height":"tall","color":"purple"};';
|
|
|
|
$this->assertEqual($result[0], $expected);
|
|
|
|
$this->assertEqual($result[1], 'alert("hey you!");');
|
|
|
|
$this->assertEqual($result[2], 'confirm("Are you sure?");');
|
|
|
|
}
|
|
|
|
}
|
2009-03-08 03:06:00 +00:00
|
|
|
|
2009-03-08 01:16:11 +00:00
|
|
|
/**
|
|
|
|
* JsBaseEngine Class Test case
|
|
|
|
*
|
|
|
|
* @package cake.tests.view.helpers
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-06-13 14:01:37 +00:00
|
|
|
class JsBaseEngineTest extends CakeTestCase {
|
2009-03-08 01:16:11 +00:00
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setUp method
|
2009-03-08 01:16:11 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
2010-07-03 17:06:11 +00:00
|
|
|
$controller = null;
|
|
|
|
$this->View = new View($controller);
|
|
|
|
$this->JsEngine = new OptionEngineHelper($this->View);
|
2009-03-08 01:16:11 +00:00
|
|
|
}
|
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* tearDown method
|
2009-03-08 01:16:11 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function tearDown() {
|
|
|
|
parent::tearDown();
|
2009-03-08 01:16:11 +00:00
|
|
|
unset($this->JsEngine);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 01:16:11 +00:00
|
|
|
/**
|
|
|
|
* test escape string skills
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 01:16:11 +00:00
|
|
|
function testEscaping() {
|
|
|
|
$result = $this->JsEngine->escape('');
|
|
|
|
$expected = '';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
|
|
|
|
$expected = 'CakePHP\\nRapid Development Framework';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = 'CakePHP\\r\\nRapid Development Framework\\rFor PHP';
|
2009-03-08 01:16:11 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
|
|
|
|
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
2009-07-11 21:21:06 +00:00
|
|
|
$result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
|
|
|
|
$expected = "CakePHP: 'Rapid Development Framework'";
|
2009-03-08 01:16:11 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('my \\"string\\"');
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = 'my \\\\\\"string\\\\\\"';
|
2009-03-08 01:16:11 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* test prompt() creation
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testPrompt() {
|
|
|
|
$result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
|
|
|
|
$expected = 'prompt("Hey, hey you", "hi!");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->prompt('"Hey"', '"hi"');
|
|
|
|
$expected = 'prompt("\"Hey\"", "\"hi\"");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* test alert generation
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testAlert() {
|
|
|
|
$result = $this->JsEngine->alert('Hey there');
|
|
|
|
$expected = 'alert("Hey there");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->alert('"Hey"');
|
|
|
|
$expected = 'alert("\"Hey\"");';
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-08 03:06:00 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* test confirm generation
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testConfirm() {
|
|
|
|
$result = $this->JsEngine->confirm('Are you sure?');
|
|
|
|
$expected = 'confirm("Are you sure?");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->confirm('"Are you sure?"');
|
|
|
|
$expected = 'confirm("\"Are you sure?\"");';
|
2009-07-25 02:13:54 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-08 03:06:00 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
2009-03-13 03:48:19 +00:00
|
|
|
* test Redirect
|
2009-03-08 03:06:00 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testRedirect() {
|
|
|
|
$result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
|
|
|
|
$expected = 'window.location = "/posts/view/1";';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* testObject encoding with non-native methods.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testObject() {
|
|
|
|
$this->JsEngine->useNative = false;
|
|
|
|
|
|
|
|
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
|
|
|
|
$result = $this->JsEngine->object($object);
|
|
|
|
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->object(array('default' => 0));
|
|
|
|
$expected = '{"default":0}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->object(array(
|
|
|
|
'2007' => array(
|
2009-03-08 06:01:08 +00:00
|
|
|
'Spring' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
),
|
|
|
|
'Fall' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
)
|
2009-07-25 02:13:54 +00:00
|
|
|
),
|
2009-03-08 06:01:08 +00:00
|
|
|
'2006' => array(
|
|
|
|
'Spring' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
),
|
|
|
|
'Fall' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
)
|
|
|
|
)
|
2009-03-08 03:06:00 +00:00
|
|
|
));
|
|
|
|
$expected = '{"2007":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}},"2006":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}}}';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-13 23:37:57 +00:00
|
|
|
|
|
|
|
foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
|
|
|
|
$result = $this->JsEngine->object($data);
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2010-02-18 03:27:52 +00:00
|
|
|
|
|
|
|
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
|
|
|
|
$result = $this->JsEngine->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
|
|
|
|
$this->assertPattern('/^PREFIX/', $result);
|
|
|
|
$this->assertPattern('/POSTFIX$/', $result);
|
|
|
|
$this->assertNoPattern('/.PREFIX./', $result);
|
|
|
|
$this->assertNoPattern('/.POSTFIX./', $result);
|
2009-03-14 21:01:49 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-11 19:58:39 +00:00
|
|
|
/**
|
|
|
|
* test compatibility of JsBaseEngineHelper::object() vs. json_encode()
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-11 21:31:54 +00:00
|
|
|
function testObjectAgainstJsonEncode() {
|
2009-07-11 19:58:39 +00:00
|
|
|
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
|
|
|
|
if ($skip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->JsEngine->useNative = false;
|
|
|
|
$data = array();
|
|
|
|
$data['mystring'] = "simple string";
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
|
|
|
|
|
|
|
$data['mystring'] = "strïng with spécial chârs";
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
|
|
|
|
|
|
|
$data['mystring'] = "a two lines\nstring";
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
|
|
|
|
|
|
|
$data['mystring'] = "a \t tabbed \t string";
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
|
|
|
|
|
|
|
$data['mystring'] = "a \"double-quoted\" string";
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
2009-07-11 21:36:48 +00:00
|
|
|
|
2009-07-11 19:58:39 +00:00
|
|
|
$data['mystring'] = 'a \\"double-quoted\\" string';
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
2009-07-11 21:36:48 +00:00
|
|
|
|
|
|
|
unset($data['mystring']);
|
|
|
|
$data[3] = array(1, 2, 3);
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
|
|
|
|
|
|
|
unset($data[3]);
|
|
|
|
$data = array('mystring' => null, 'bool' => false, 'array' => array(1, 44, 66));
|
|
|
|
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
2009-07-11 19:58:39 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-07-11 19:58:39 +00:00
|
|
|
/**
|
|
|
|
* test that JSON made with JsBaseEngineHelper::object() against json_decode()
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-07-11 19:58:39 +00:00
|
|
|
function testObjectAgainstJsonDecode() {
|
|
|
|
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
|
|
|
|
if ($skip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->JsEngine->useNative = false;
|
|
|
|
|
|
|
|
$data = array("simple string");
|
|
|
|
$result = $this->JsEngine->object($data);
|
|
|
|
$this->assertEqual(json_decode($result), $data);
|
|
|
|
|
2009-07-11 21:21:06 +00:00
|
|
|
$data = array('my "string"');
|
2009-07-11 19:58:39 +00:00
|
|
|
$result = $this->JsEngine->object($data);
|
|
|
|
$this->assertEqual(json_decode($result), $data);
|
|
|
|
|
|
|
|
$data = array('my \\"string\\"');
|
|
|
|
$result = $this->JsEngine->object($data);
|
|
|
|
$this->assertEqual(json_decode($result), $data);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
/**
|
|
|
|
* test Mapping of options.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-14 21:01:49 +00:00
|
|
|
function testOptionMapping() {
|
|
|
|
$JsEngine = new OptionEngineHelper();
|
|
|
|
$result = $JsEngine->testMap();
|
|
|
|
$this->assertEqual($result, array());
|
|
|
|
|
|
|
|
$result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
|
|
|
|
$this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
|
2009-03-13 23:37:57 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
|
|
|
|
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
|
2009-07-25 02:13:54 +00:00
|
|
|
|
2009-04-11 04:01:40 +00:00
|
|
|
$result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
|
|
|
|
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
|
2009-03-08 03:06:00 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-15 16:26:12 +00:00
|
|
|
/**
|
|
|
|
* test that option parsing escapes strings and saves what is supposed to be saved.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-03-15 16:26:12 +00:00
|
|
|
function testOptionParsing() {
|
|
|
|
$JsEngine = new OptionEngineHelper();
|
|
|
|
|
|
|
|
$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = 'key:1, url:"\\/posts\\/view\\/1"';
|
2009-03-15 16:26:12 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = 'success:doSuccess, url:"\\/posts\\/view\\/1"';
|
2009-03-15 16:26:12 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 01:16:11 +00:00
|
|
|
}
|