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
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package 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
|
|
|
*/
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('HtmlHelper', 'View/Helper');
|
|
|
|
App::uses('JsHelper', 'View/Helper');
|
2010-12-22 02:31:38 +00:00
|
|
|
App::uses('JsBaseEngineHelper', 'View/Helper');
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('FormHelper', 'View/Helper');
|
|
|
|
App::uses('View', 'View');
|
|
|
|
App::uses('ClassRegistry', 'Utility');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-26 12:36:20 +00:00
|
|
|
class JsEncodingObject {
|
|
|
|
protected $_title = 'Old thing';
|
|
|
|
|
|
|
|
private $__noshow = 'Never ever';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package 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;
|
2011-01-09 05:09:01 +00:00
|
|
|
$this->Js->OptionEngine = new OptionEngineHelper($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);
|
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)) {
|
2011-01-09 05:09:01 +00:00
|
|
|
$this->getMock('JsBaseEngineHelper', array(), array($this->View), 'TestJsEngineHelper');
|
2010-05-19 04:26:50 +00:00
|
|
|
}
|
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',
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$this->assertTags($result, $expected);
|
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 = '/';
|
2011-01-09 05:09:01 +00:00
|
|
|
$this->Js->JsBaseEngine = new TestJsEngineHelper($this->View);
|
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/'),
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$this->assertTags($result, $expected);
|
2009-03-20 02:02:18 +00:00
|
|
|
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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$this->assertTags($result, $expected);
|
2009-07-25 02:13:54 +00:00
|
|
|
}
|
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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$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
|
|
|
|
2011-03-08 02:58:11 +00:00
|
|
|
$options = array('update' => '#content', 'id' => 'test-submit', 'style' => 'margin: 0');
|
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'),
|
2011-03-08 02:58:11 +00:00
|
|
|
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save', 'style' => 'margin: 0'),
|
2009-07-21 04:24:58 +00:00
|
|
|
'/div'
|
|
|
|
);
|
2011-05-17 04:36: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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$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'));
|
2011-05-16 22:49:00 +00:00
|
|
|
|
2010-05-31 02:13:09 +00:00
|
|
|
$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'));
|
2011-05-16 22:49:00 +00:00
|
|
|
|
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'
|
|
|
|
);
|
2011-05-17 04:36:22 +00:00
|
|
|
$this->assertTags($result, $expected);
|
2010-04-23 04:04:15 +00:00
|
|
|
}
|
|
|
|
|
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"}';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-08-10 00:51:59 +00:00
|
|
|
}
|
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\""';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-12-07 01:45:12 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2011-01-09 05:09:01 +00:00
|
|
|
$this->View = $this->getMock('View', array('addScript'), array(&$controller));
|
2010-07-03 17:06:11 +00:00
|
|
|
$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 = '';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
|
|
|
|
$expected = 'CakePHP\\nRapid Development Framework';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
|
|
|
|
$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';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
|
|
|
|
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
|
2009-07-11 21:21:06 +00:00
|
|
|
$result = $this->JsEngine->escape("CakePHP: 'Rapid Development Framework'");
|
|
|
|
$expected = "CakePHP: 'Rapid Development Framework'";
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('my \\"string\\"');
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = 'my \\\\\\"string\\\\\\"';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 01:16:11 +00:00
|
|
|
}
|
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!");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->prompt('"Hey"', '"hi"');
|
|
|
|
$expected = 'prompt("\"Hey\"", "\"hi\"");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
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 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");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->alert('"Hey"');
|
|
|
|
$expected = 'alert("\"Hey\"");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
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?");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
$result = $this->JsEngine->confirm('"Are you sure?"');
|
|
|
|
$expected = 'confirm("\"Are you sure?\"");';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
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";';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
}
|
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() {
|
|
|
|
|
|
|
|
$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]}';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
2011-04-26 12:36:20 +00:00
|
|
|
$object = new JsEncodingObject();
|
|
|
|
$object->title = 'New thing';
|
|
|
|
$object->indexes = array(5,6,7,8);
|
|
|
|
$result = $this->JsEngine->object($object);
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2011-04-26 12:36:20 +00:00
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
$result = $this->JsEngine->object(array('default' => 0));
|
|
|
|
$expected = '{"default":0}';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
$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"}}}}';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-13 23:37:57 +00:00
|
|
|
|
|
|
|
foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
|
|
|
|
$result = $this->JsEngine->object($data);
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-13 23:37:57 +00:00
|
|
|
}
|
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-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() {
|
2011-01-09 05:09:01 +00:00
|
|
|
$JsEngine = new OptionEngineHelper($this->View);
|
2009-03-14 21:01:49 +00:00
|
|
|
$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() {
|
2011-01-09 05:09:01 +00:00
|
|
|
$JsEngine = new OptionEngineHelper($this->View);
|
2009-03-15 16:26:12 +00:00
|
|
|
|
|
|
|
$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"';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-15 16:26:12 +00:00
|
|
|
|
|
|
|
$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"';
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEqual($expected, $result);
|
2009-03-15 16:26:12 +00:00
|
|
|
}
|
2009-08-03 18:47:36 +00:00
|
|
|
|
2009-03-08 01:16:11 +00:00
|
|
|
}
|