2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
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
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
2008-10-30 17:30:26 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.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.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite 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
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-13 03:48:19 +00:00
|
|
|
App::import('Helper', array('Js', 'Html'));
|
2009-03-08 00:34:05 +00:00
|
|
|
App::import('Core', array('View', 'ClassRegistry'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-03-13 03:48:19 +00:00
|
|
|
Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
|
|
|
|
Mock::generate('View', 'JsHelperMockView');
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
class OptionEngineHelper extends JsBaseEngineHelper {
|
|
|
|
var $_optionMap = array(
|
|
|
|
'request' => array(
|
|
|
|
'complete' => 'success',
|
|
|
|
'request' => 'beforeSend',
|
|
|
|
'type' => 'dataType'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
/**
|
|
|
|
* test method for testing option mapping
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
**/
|
|
|
|
function testMap($options = array()) {
|
|
|
|
return $this->_mapOptions('request', $options);
|
|
|
|
}
|
2009-03-15 16:26:12 +00:00
|
|
|
/**
|
|
|
|
* test method for option parsing
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testParseOptions($options, $safe = array()) {
|
|
|
|
return $this->_parseOptions($options, $safe);
|
|
|
|
}
|
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
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
class JsHelperTestCase extends CakeTestCase {
|
2009-03-13 03:48:19 +00:00
|
|
|
/**
|
|
|
|
* Regexp for CDATA start block
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
|
|
|
|
/**
|
|
|
|
* Regexp for CDATA end block
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
function startTest() {
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->Js =& new JsHelper('JsBase');
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->Html =& new HtmlHelper();
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->Js->JsBaseEngine =& new JsBaseEngineHelper();
|
|
|
|
|
|
|
|
$view =& new JsHelperMockView();
|
|
|
|
ClassRegistry::addObject('view', $view);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
function endTest() {
|
2009-03-08 00:34:05 +00:00
|
|
|
ClassRegistry::removeObject('view');
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($this->Js);
|
|
|
|
}
|
2009-07-12 22:42:36 +00:00
|
|
|
/**
|
|
|
|
* Switches $this->Js to a mocked engine.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function _useMock() {
|
|
|
|
$this->Js =& new JsHelper(array('TestJs'));
|
2009-07-12 23:06:45 +00:00
|
|
|
$this->Js->TestJsEngine =& new TestJsEngineHelper($this);
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->Html =& new HtmlHelper();
|
|
|
|
}
|
2009-03-07 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* test object construction
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testConstruction() {
|
2009-03-29 20:10:04 +00:00
|
|
|
$js =& new JsHelper();
|
2009-04-27 02:53:26 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'JqueryEngine'));
|
2009-03-13 03:48:19 +00:00
|
|
|
|
2009-03-29 20:10:04 +00:00
|
|
|
$js =& new JsHelper(array('mootools'));
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'mootoolsEngine'));
|
|
|
|
|
2009-03-29 20:10:04 +00:00
|
|
|
$js =& new JsHelper('prototype');
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'prototypeEngine'));
|
|
|
|
|
2009-03-29 20:10:04 +00:00
|
|
|
$js =& new JsHelper('MyPlugin.Dojo');
|
2009-03-13 03:48:19 +00:00
|
|
|
$this->assertEqual($js->helpers, array('Html', 'MyPlugin.DojoEngine'));
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test that methods dispatch internally and to the engine class
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testMethodDispatching() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
|
|
|
$this->Js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
2009-03-07 18:10:50 +00:00
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->methodOne();
|
2009-03-07 18:10:50 +00:00
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->TestEngine =& new StdClass();
|
2009-03-07 18:10:50 +00:00
|
|
|
$this->expectError();
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->someMethodThatSurelyDoesntExist();
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2009-03-29 20:10:04 +00:00
|
|
|
/**
|
|
|
|
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testMethodDispatchWithBuffering() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
|
|
|
|
|
|
|
$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
|
|
|
|
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
|
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');
|
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
|
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-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-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;');
|
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => 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
|
|
|
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->Js->TestJsEngine->expectAtLeastOnce('domReady');
|
2009-03-29 18:23:42 +00:00
|
|
|
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-13 03:48:19 +00:00
|
|
|
$view =& new JsHelperMockView();
|
|
|
|
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
|
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-03-20 02:02:18 +00:00
|
|
|
/**
|
|
|
|
* test that writeScripts makes files, and puts the events into them.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testWriteScriptsInFile() {
|
|
|
|
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$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-07-12 21:30:41 +00:00
|
|
|
/**
|
|
|
|
* test link()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2009-07-12 23:06:45 +00:00
|
|
|
function testLinkWithMock() {
|
2009-07-12 22:42:36 +00:00
|
|
|
$this->_useMock();
|
2009-07-12 23:06:45 +00:00
|
|
|
$options = array('update' => '#content');
|
2009-07-12 22:42:36 +00:00
|
|
|
|
2009-07-12 23:06:45 +00:00
|
|
|
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array('request', '*'));
|
|
|
|
$this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', new AnythingExpectation()));
|
|
|
|
$this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array(
|
|
|
|
'request', array('/posts/view/1', $options)
|
|
|
|
));
|
|
|
|
$this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array(
|
|
|
|
'event', array('click', 'ajax code', $options)
|
|
|
|
));
|
|
|
|
|
|
|
|
$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);
|
2009-07-12 23:06:45 +00:00
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'confirm' => 'Are you sure?',
|
|
|
|
'update' => '#content',
|
|
|
|
'class' => 'my-class',
|
|
|
|
'id' => 'custom-id',
|
|
|
|
'escape' => false
|
|
|
|
);
|
|
|
|
$this->Js->TestJsEngine->expectAt(0, 'confirm', array($options['confirm']));
|
|
|
|
$this->Js->TestJsEngine->expectAt(1, 'request', array('/posts/view/1', '*'));
|
|
|
|
$code = <<<CODE
|
|
|
|
var _confirm = confirm("Are you sure?");
|
|
|
|
if (!_confirm) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CODE;
|
|
|
|
$this->Js->TestJsEngine->expectAt(1, 'event', array('click', $code));
|
|
|
|
$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);
|
2009-07-12 21:30:41 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
**/
|
|
|
|
class JsBaseEngineTestCase extends CakeTestCase {
|
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function startTest() {
|
|
|
|
$this->JsEngine = new JsBaseEngineHelper();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function endTest() {
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
unset($this->JsEngine);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test escape string skills
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* test prompt() creation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test alert generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-04-27 03:24:39 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-08 03:06:00 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test confirm generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-04-27 03:24:39 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
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
|
|
|
|
**/
|
|
|
|
function testRedirect() {
|
|
|
|
$result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
|
|
|
|
$expected = 'window.location = "/posts/view/1";';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* testObject encoding with non-native methods.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'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);
|
|
|
|
}
|
2009-03-14 21:01:49 +00:00
|
|
|
}
|
2009-07-11 19:58:39 +00:00
|
|
|
/**
|
|
|
|
* test compatibility of JsBaseEngineHelper::object() vs. json_encode()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
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
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test that JSON made with JsBaseEngineHelper::object() against json_decode()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-03-14 21:01:49 +00:00
|
|
|
/**
|
|
|
|
* test Mapping of options.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-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-03-15 16:26:12 +00:00
|
|
|
/**
|
|
|
|
* test that option parsing escapes strings and saves what is supposed to be saved.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
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-03-08 01:16:11 +00:00
|
|
|
}
|
2009-03-28 03:13:50 +00:00
|
|
|
?>
|