Removing AjaxHelper and JavascriptHelper as they were deprecated in 1.3 and are replaced by JsHelper + HtmlHelper.

This commit is contained in:
Mark Story 2010-04-14 00:13:33 -04:00
parent 508e0a2d9a
commit c4d57bd6e7
4 changed files with 0 additions and 3562 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,722 +0,0 @@
<?php
/**
* Javascript Helper class file.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Javascript Helper class for easy use of JavaScript.
*
* JavascriptHelper encloses all methods needed while working with JavaScript.
*
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @link http://book.cakephp.org/view/1450/Javascript
*/
class JavascriptHelper extends AppHelper {
/**
* Determines whether native JSON extension is used for encoding. Set by object constructor.
*
* @var boolean
* @access public
*/
public $useNative = false;
/**
* If true, automatically writes events to the end of a script or to an external JavaScript file
* at the end of page execution
*
* @var boolean
* @access public
*/
public $enabled = true;
/**
* Indicates whether <script /> blocks should be written 'safely,' i.e. wrapped in CDATA blocks
*
* @var boolean
* @access public
*/
public $safe = false;
/**
* HTML tags used by this helper.
*
* @var array
* @access public
*/
public $tags = array(
'javascriptstart' => '<script type="text/javascript">',
'javascriptend' => '</script>',
'javascriptblock' => '<script type="text/javascript">%s</script>',
'javascriptlink' => '<script type="text/javascript" src="%s"></script>'
);
/**
* Holds options passed to codeBlock(), saved for when block is dumped to output
*
* @var array
* @access protected
* @see JavascriptHelper::codeBlock()
*/
protected $_blockOptions = array();
/**
* Caches events written by event() for output at the end of page execution
*
* @var array
* @access protected
* @see JavascriptHelper::event()
*/
protected $_cachedEvents = array();
/**
* Indicates whether generated events should be cached for later output (can be written at the
* end of the page, in the <head />, or to an external file).
*
* @var boolean
* @access protected
* @see JavascriptHelper::event()
* @see JavascriptHelper::writeEvents()
*/
protected $_cacheEvents = false;
/**
* Indicates whether cached events should be written to an external file
*
* @var boolean
* @access protected
* @see JavascriptHelper::event()
* @see JavascriptHelper::writeEvents()
*/
protected $_cacheToFile = false;
/**
* Indicates whether *all* generated JavaScript should be cached for later output
*
* @var boolean
* @access protected
* @see JavascriptHelper::codeBlock()
* @see JavascriptHelper::blockEnd()
*/
protected $_cacheAll = false;
/**
* Contains event rules attached with CSS selectors. Used with the event:Selectors JavaScript
* library.
*
* @var array
* @access protected
* @see JavascriptHelper::event()
* @link http://alternateidea.com/event-selectors/
*/
protected $_rules = array();
/**
* @var string
* @access private
*/
private $__scriptBuffer = null;
/**
* Constructor. Checks for presence of native PHP JSON extension to use for object encoding
*
*/
public function __construct($options = array()) {
if (!empty($options)) {
foreach ($options as $key => $val) {
if (is_numeric($key)) {
$key = $val;
$val = true;
}
switch ($key) {
case 'cache':
break;
case 'safe':
$this->safe = $val;
break;
}
}
}
$this->useNative = function_exists('json_encode');
return parent::__construct($options);
}
/**
* Returns a JavaScript script tag.
*
* Options:
*
* - allowCache: boolean, designates whether this block is cacheable using the
* current cache settings.
* - safe: boolean, whether this block should be wrapped in CDATA tags. Defaults
* to helper's object configuration.
* - inline: whether the block should be printed inline, or written
* to cached for later output (i.e. $scripts_for_layout).
*
* @param string $script The JavaScript to be wrapped in SCRIPT tags.
* @param array $options Set of options:
* @return string The full SCRIPT element, with the JavaScript inside it, or null,
* if 'inline' is set to false.
*/
function codeBlock($script = null, $options = array()) {
if (!empty($options) && !is_array($options)) {
$options = array('allowCache' => $options);
} elseif (empty($options)) {
$options = array();
}
$defaultOptions = array('allowCache' => true, 'safe' => true, 'inline' => true);
$options = array_merge($defaultOptions, $options);
if (empty($script)) {
$this->__scriptBuffer = @ob_get_contents();
$this->_blockOptions = $options;
$this->inBlock = true;
@ob_end_clean();
ob_start();
return null;
}
if ($this->_cacheEvents && $this->_cacheAll && $options['allowCache']) {
$this->_cachedEvents[] = $script;
return null;
}
if ($options['safe'] || $this->safe) {
$script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
}
if ($options['inline']) {
return sprintf($this->tags['javascriptblock'], $script);
} else {
$view =& ClassRegistry::getObject('view');
$view->addScript(sprintf($this->tags['javascriptblock'], $script));
}
}
/**
* Ends a block of cached JavaScript code
*
* @return mixed
*/
function blockEnd() {
if (!isset($this->inBlock) || !$this->inBlock) {
return;
}
$script = @ob_get_contents();
@ob_end_clean();
ob_start();
echo $this->__scriptBuffer;
$this->__scriptBuffer = null;
$options = $this->_blockOptions;
$this->_blockOptions = array();
$this->inBlock = false;
if (empty($script)) {
return null;
}
return $this->codeBlock($script, $options);
}
/**
* Returns a JavaScript include tag (SCRIPT element). If the filename is prefixed with "/",
* the path will be relative to the base path of your application. Otherwise, the path will
* be relative to your JavaScript path, usually webroot/js.
*
* @param mixed $url String URL to JavaScript file, or an array of URLs.
* @param boolean $inline If true, the <script /> tag will be printed inline,
* otherwise it will be printed in the <head />, using $scripts_for_layout
* @see JS_URL
* @return string
*/
function link($url, $inline = true) {
if (is_array($url)) {
$out = '';
foreach ($url as $i) {
$out .= "\n\t" . $this->link($i, $inline);
}
if ($inline) {
return $out . "\n";
}
return;
}
if (strpos($url, '://') === false) {
if ($url[0] !== '/') {
$url = JS_URL . $url;
}
if (strpos($url, '?') === false) {
if (substr($url, -3) !== '.js') {
$url .= '.js';
}
}
$url = $this->assetTimestamp($this->webroot($url));
if (Configure::read('Asset.filter.js')) {
$pos = strpos($url, JS_URL);
if ($pos !== false) {
$url = substr($url, 0, $pos) . 'cjs/' . substr($url, $pos + strlen(JS_URL));
}
}
}
$out = sprintf($this->tags['javascriptlink'], $url);
if ($inline) {
return $out;
} else {
$view =& ClassRegistry::getObject('view');
$view->addScript($out);
}
}
/**
* Escape carriage returns and single and double quotes for JavaScript segments.
*
* @param string $script string that might have javascript elements
* @return string escaped string
*/
function escapeScript($script) {
$script = str_replace(array("\r\n", "\n", "\r"), '\n', $script);
$script = str_replace(array('"', "'"), array('\"', "\\'"), $script);
return $script;
}
/**
* Escape a string to be JavaScript friendly.
*
* List of escaped ellements:
* + "\r\n" => '\n'
* + "\r" => '\n'
* + "\n" => '\n'
* + '"' => '\"'
* + "'" => "\\'"
*
* @param string $script String that needs to get escaped.
* @return string Escaped string.
*/
function escapeString($string) {
App::import('Core', 'Multibyte');
$escape = array("\r\n" => "\n", "\r" => "\n");
$string = str_replace(array_keys($escape), array_values($escape), $string);
return $this->_utf8ToHex($string);
}
/**
* Encode a string into JSON. Converts and escapes necessary characters.
*
* @return void
*/
function _utf8ToHex($string) {
$length = strlen($string);
$return = '';
for ($i = 0; $i < $length; ++$i) {
$ord = ord($string{$i});
switch (true) {
case $ord == 0x08:
$return .= '\b';
break;
case $ord == 0x09:
$return .= '\t';
break;
case $ord == 0x0A:
$return .= '\n';
break;
case $ord == 0x0C:
$return .= '\f';
break;
case $ord == 0x0D:
$return .= '\r';
break;
case $ord == 0x22:
case $ord == 0x2F:
case $ord == 0x5C:
case $ord == 0x27:
$return .= '\\' . $string{$i};
break;
case (($ord >= 0x20) && ($ord <= 0x7F)):
$return .= $string{$i};
break;
case (($ord & 0xE0) == 0xC0):
if ($i + 1 >= $length) {
$i += 1;
$return .= '?';
break;
}
$charbits = $string{$i} . $string{$i + 1};
$char = Multibyte::utf8($charbits);
$return .= sprintf('\u%04s', dechex($char[0]));
$i += 1;
break;
case (($ord & 0xF0) == 0xE0):
if ($i + 2 >= $length) {
$i += 2;
$return .= '?';
break;
}
$charbits = $string{$i} . $string{$i + 1} . $string{$i + 2};
$char = Multibyte::utf8($charbits);
$return .= sprintf('\u%04s', dechex($char[0]));
$i += 2;
break;
case (($ord & 0xF8) == 0xF0):
if ($i + 3 >= $length) {
$i += 3;
$return .= '?';
break;
}
$charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3};
$char = Multibyte::utf8($charbits);
$return .= sprintf('\u%04s', dechex($char[0]));
$i += 3;
break;
case (($ord & 0xFC) == 0xF8):
if ($i + 4 >= $length) {
$i += 4;
$return .= '?';
break;
}
$charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3} . $string{$i + 4};
$char = Multibyte::utf8($charbits);
$return .= sprintf('\u%04s', dechex($char[0]));
$i += 4;
break;
case (($ord & 0xFE) == 0xFC):
if ($i + 5 >= $length) {
$i += 5;
$return .= '?';
break;
}
$charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3} . $string{$i + 4} . $string{$i + 5};
$char = Multibyte::utf8($charbits);
$return .= sprintf('\u%04s', dechex($char[0]));
$i += 5;
break;
}
}
return $return;
}
/**
* Attach an event to an element. Used with the Prototype library.
*
* @param string $object Object to be observed
* @param string $event event to observe
* @param string $observer function to call
* @param array $options Set options: useCapture, allowCache, safe
* @return boolean true on success
*/
function event($object, $event, $observer = null, $options = array()) {
if (!empty($options) && !is_array($options)) {
$options = array('useCapture' => $options);
} else if (empty($options)) {
$options = array();
}
$defaultOptions = array('useCapture' => false);
$options = array_merge($defaultOptions, $options);
if ($options['useCapture'] == true) {
$options['useCapture'] = 'true';
} else {
$options['useCapture'] = 'false';
}
$isObject = (
strpos($object, 'window') !== false || strpos($object, 'document') !== false ||
strpos($object, '$(') !== false || strpos($object, '"') !== false ||
strpos($object, '\'') !== false
);
if ($isObject) {
$b = "Event.observe({$object}, '{$event}', function(event) { {$observer} }, ";
$b .= "{$options['useCapture']});";
} elseif ($object[0] == '\'') {
$b = "Event.observe(" . substr($object, 1) . ", '{$event}', function(event) { ";
$b .= "{$observer} }, {$options['useCapture']});";
} else {
$chars = array('#', ' ', ', ', '.', ':');
$found = false;
foreach ($chars as $char) {
if (strpos($object, $char) !== false) {
$found = true;
break;
}
}
if ($found) {
$this->_rules[$object] = $event;
} else {
$b = "Event.observe(\$('{$object}'), '{$event}', function(event) { ";
$b .= "{$observer} }, {$options['useCapture']});";
}
}
if (isset($b) && !empty($b)) {
if ($this->_cacheEvents === true) {
$this->_cachedEvents[] = $b;
return;
} else {
return $this->codeBlock($b, array_diff_key($options, $defaultOptions));
}
}
}
/**
* Cache JavaScript events created with event()
*
* @param boolean $file If true, code will be written to a file
* @param boolean $all If true, all code written with JavascriptHelper will be sent to a file
* @return null
*/
function cacheEvents($file = false, $all = false) {
$this->_cacheEvents = true;
$this->_cacheToFile = $file;
$this->_cacheAll = $all;
}
/**
* Gets (and clears) the current JavaScript event cache
*
* @param boolean $clear
* @return string
*/
function getCache($clear = true) {
$out = '';
$rules = array();
if (!empty($this->_rules)) {
foreach ($this->_rules as $sel => $event) {
$rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
}
}
$data = implode("\n", $this->_cachedEvents);
if (!empty($rules)) {
$data .= "\nvar Rules = {\n" . implode(",\n\n", $rules) . "\n}";
$data .= "\nEventSelectors.start(Rules);\n";
}
if ($clear) {
$this->_rules = array();
$this->_cacheEvents = false;
$this->_cachedEvents = array();
}
return $data;
}
/**
* Write cached JavaScript events
*
* @param boolean $inline If true, returns JavaScript event code. Otherwise it is added to the
* output of $scripts_for_layout in the layout.
* @param array $options Set options for codeBlock
* @return string
*/
function writeEvents($inline = true, $options = array()) {
$out = '';
$rules = array();
if (!$this->_cacheEvents) {
return;
}
$data = $this->getCache();
if (empty($data)) {
return;
}
if ($this->_cacheToFile) {
$filename = md5($data);
if (!file_exists(JS . $filename . '.js')) {
cache(str_replace(WWW_ROOT, '', JS) . $filename . '.js', $data, '+999 days', 'public');
}
$out = $this->link($filename);
} else {
$out = $this->codeBlock("\n" . $data . "\n", $options);
}
if ($inline) {
return $out;
} else {
$view =& ClassRegistry::getObject('view');
$view->addScript($out);
}
}
/**
* Includes the Prototype Javascript library (and anything else) inside a single script tag.
*
* Note: The recommended approach is to copy the contents of
* javascripts into your application's
* public/javascripts/ directory, and use @see javascriptIncludeTag() to
* create remote script links.
*
* @param string $script Script file to include
* @param array $options Set options for codeBlock
* @return string script with all javascript in/javascripts folder
*/
function includeScript($script = "", $options = array()) {
if ($script == "") {
$files = scandir(JS);
$javascript = '';
foreach ($files as $file) {
if (substr($file, -3) == '.js') {
$javascript .= file_get_contents(JS . "{$file}") . "\n\n";
}
}
} else {
$javascript = file_get_contents(JS . "$script.js") . "\n\n";
}
return $this->codeBlock("\n\n" . $javascript, $options);
}
/**
* Generates a JavaScript object in JavaScript Object Notation (JSON)
* from an array
*
* ### Options
*
* - block - Wraps the return value in a script tag if true. Default is false
* - prefix - Prepends the string to the returned data. Default is ''
* - postfix - Appends the string to the returned data. Default is ''
* - stringKeys - A list of array keys to be treated as a string.
* - quoteKeys - If false treats $stringKeys as a list of keys **not** to be quoted. Default is true.
* - q - The type of quote to use. Default is '"'. This option only affects the keys, not the values.
*
* @param array $data Data to be converted
* @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
* @return string A JSON code block
*/
function object($data = array(), $options = array()) {
if (!empty($options) && !is_array($options)) {
$options = array('block' => $options);
} else if (empty($options)) {
$options = array();
}
$defaultOptions = array(
'block' => false, 'prefix' => '', 'postfix' => '',
'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"'
);
$options = array_merge($defaultOptions, $options, array_filter(compact(array_keys($defaultOptions))));
if (is_object($data)) {
$data = get_object_vars($data);
}
$out = $keys = array();
$numeric = true;
if ($this->useNative) {
$rt = json_encode($data);
} else {
if (is_null($data)) {
return 'null';
}
if (is_bool($data)) {
return $data ? 'true' : 'false';
}
if (is_array($data)) {
$keys = array_keys($data);
}
if (!empty($keys)) {
$numeric = (array_values($keys) === array_keys(array_values($keys)));
}
foreach ($data as $key => $val) {
if (is_array($val) || is_object($val)) {
$val = $this->object(
$val,
array_merge($options, array('block' => false, 'prefix' => '', 'postfix' => ''))
);
} else {
$quoteStrings = (
!count($options['stringKeys']) ||
($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) ||
(!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))
);
$val = $this->value($val, $quoteStrings);
}
if (!$numeric) {
$val = $options['q'] . $this->value($key, false) . $options['q'] . ':' . $val;
}
$out[] = $val;
}
if (!$numeric) {
$rt = '{' . implode(',', $out) . '}';
} else {
$rt = '[' . implode(',', $out) . ']';
}
}
$rt = $options['prefix'] . $rt . $options['postfix'];
if ($options['block']) {
$rt = $this->codeBlock($rt, array_diff_key($options, $defaultOptions));
}
return $rt;
}
/**
* Converts a PHP-native variable of any type to a JSON-equivalent representation
*
* @param mixed $val A PHP variable to be converted to JSON
* @param boolean $quoteStrings If false, leaves string values unquoted
* @return string a JavaScript-safe/JSON representation of $val
*/
function value($val, $quoteStrings = true) {
switch (true) {
case (is_array($val) || is_object($val)):
$val = $this->object($val);
break;
case ($val === null):
$val = 'null';
break;
case (is_bool($val)):
$val = !empty($val) ? 'true' : 'false';
break;
case (is_int($val)):
$val = $val;
break;
case (is_float($val)):
$val = sprintf("%.11f", $val);
break;
default:
$val = $this->escapeString($val);
if ($quoteStrings) {
$val = '"' . $val . '"';
}
break;
}
return $val;
}
/**
* AfterRender callback. Writes any cached events to the view, or to a temp file.
*
* @return null
*/
function afterRender() {
if (!$this->enabled) {
return;
}
echo $this->writeEvents(true);
}
}
?>

View file

@ -1,909 +0,0 @@
<?php
/**
* AjaxHelperTest file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
App::import('Helper', array('Html', 'Form', 'Javascript', 'Ajax'));
/**
* AjaxTestController class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class AjaxTestController extends Controller {
/**
* name property
*
* @var string 'AjaxTest'
* @access public
*/
public $name = 'AjaxTest';
/**
* uses property
*
* @var mixed null
* @access public
*/
public $uses = null;
}
/**
* PostAjaxTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class PostAjaxTest extends Model {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
public $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
public $useTable = false;
/**
* schema method
*
* @access public
* @return void
*/
function schema() {
return array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
}
/**
* TestAjaxHelper class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TestAjaxHelper extends AjaxHelper {
/**
* stop method
*
* @access public
* @return void
*/
function _stop() {
}
}
/**
* TestJavascriptHelper class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TestJavascriptHelper extends JavascriptHelper {
/**
* codeBlocks property
*
* @var mixed
* @access public
*/
public $codeBlocks;
/**
* codeBlock method
*
* @param mixed $parameter
* @access public
* @return void
*/
function codeBlock($parameter) {
if (empty($this->codeBlocks)) {
$this->codeBlocks = array();
}
$this->codeBlocks[] = $parameter;
}
}
/**
* AjaxTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class AjaxHelperTest extends CakeTestCase {
/**
* Regexp for CDATA start block
*
* @var string
*/
public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
/**
* Regexp for CDATA end block
*
* @var string
*/
public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
/**
* setUp method
*
* @access public
* @return void
*/
function setUp() {
Router::reload();
$this->Ajax =& new TestAjaxHelper();
$this->Ajax->Html =& new HtmlHelper();
$this->Ajax->Form =& new FormHelper();
$this->Ajax->Javascript =& new JavascriptHelper();
$this->Ajax->Form->Html =& $this->Ajax->Html;
$view =& new View(new AjaxTestController());
ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('PostAjaxTest', new PostAjaxTest());
$this->Ajax->Form->params = array(
'plugin' => null,
'action' => 'view',
'controller' => 'users'
);
}
/**
* tearDown method
*
* @access public
* @return void
*/
function tearDown() {
unset($this->Ajax);
ClassRegistry::flush();
}
/**
* testEvalScripts method
*
* @access public
* @return void
*/
function testEvalScripts() {
$result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content', 'evalScripts' => false));
$expected = array(
'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => 'http://www.cakephp.org'),
'Test Link',
'/a',
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','http://www.cakephp.org', {asynchronous:true, evalScripts:false, requestHeaders:['X-Update', 'content']}) }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content'));
$expected = array(
'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => 'http://www.cakephp.org'),
'Test Link',
'/a',
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','http://www.cakephp.org', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'content']}) }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
/**
* testAutoComplete method
*
* @access public
* @return void
*/
function testAutoComplete() {
$result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('minChars' => 2));
$this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
$this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {minChars:2});')) . '/', $result);
$this->assertPattern('/<\/script>$/', $result);
$result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter'));
$this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
$this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\'});')) . '/', $result);
$this->assertPattern('/<\/script>$/', $result);
$result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter', 'updateElement' => 'elementUpdated', 'afterUpdateElement' => 'function (input, element) { alert("updated"); }'));
$this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
$this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\', updateElement:elementUpdated, afterUpdateElement:function (input, element) { alert("updated"); }});')) . '/', $result);
$this->assertPattern('/<\/script>$/', $result);
$result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('callback' => 'function (input, queryString) { alert("requesting"); }'));
$this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
$this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
$this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {callback:function (input, queryString) { alert("requesting"); }});')) . '/', $result);
$this->assertPattern('/<\/script>$/', $result);
$result = $this->Ajax->autoComplete("PostAjaxText.title", "/post", array("parameters" => "'key=value&key2=value2'"));
$this->assertPattern('/{parameters:\'key=value&key2=value2\'}/', $result);
$result = $this->Ajax->autoComplete("PostAjaxText.title", "/post", array("with" => "'key=value&key2=value2'"));
$this->assertPattern('/{parameters:\'key=value&key2=value2\'}/', $result);
}
/**
* testAsynchronous method
*
* @access public
* @return void
*/
function testAsynchronous() {
$result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content', 'type' => 'synchronous'));
$expected = array(
'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => '/'),
'Test Link',
'/a',
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','/', {asynchronous:false, evalScripts:true, requestHeaders:['X-Update', 'content']}) }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
/**
* testDraggable method
*
* @access public
* @return void
*/
function testDraggable() {
$result = $this->Ajax->drag('id', array('handle' => 'other_id'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Draggable('id', {handle:'other_id'});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->drag('id', array('onDrag' => 'doDrag', 'onEnd' => 'doEnd'));
$this->assertPattern('/onDrag:doDrag/', $result);
$this->assertPattern('/onEnd:doEnd/', $result);
}
/**
* testDroppable method
*
* @access public
* @return void
*/
function testDroppable() {
$result = $this->Ajax->drop('droppable', array('accept' => 'crap'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Droppables.add('droppable', {accept:'crap'});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Droppables.add('droppable', {accept:'crap', onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true})}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->dropRemote('droppable', array('accept' => array('crap1', 'crap2')), array('url' => '/posts'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Droppables.add('droppable', {accept:[\"crap1\",\"crap2\"], onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true})}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts', 'with' => '{drag_id:element.id,drop_id:dropon.id,event:event.whatever_you_want}'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Droppables.add('droppable', {accept:'crap', onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true, parameters:{drag_id:element.id,drop_id:dropon.id,event:event.whatever_you_want}})}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
/**
* testForm method
*
* @access public
* @return void
*/
function testForm() {
$result = $this->Ajax->form('showForm', 'post', array('model' => 'Form', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
$this->assertNoPattern('/model=/', $result);
$result = $this->Ajax->form('showForm', 'post', array('name'=> 'SomeFormName', 'id' => 'MyFormID', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
$this->assertPattern('/id="MyFormID"/', $result);
$this->assertPattern('/name="SomeFormName"/', $result);
}
/**
* testSortable method
*
* @access public
* @return void
*/
function testSortable() {
$result = $this->Ajax->sortable('ull', array('constraint' => false, 'ghosting' => true));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Sortable.create('ull', {constraint:false, ghosting:true});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->sortable('ull', array('constraint' => 'false', 'ghosting' => 'true'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Sortable.create('ull', {constraint:false, ghosting:true});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->sortable('ull', array('constraint'=>'false', 'ghosting'=>'true', 'update' => 'myId'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Sortable.create('ull', {constraint:false, ghosting:true, update:'myId'});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->sortable('faqs', array('url'=>'http://www.cakephp.org',
'update' => 'faqs',
'tag' => 'tbody',
'handle' => 'grip',
'before' => "Element.hide('message')",
'complete' => "Element.show('message');"
));
$expected = 'Sortable.create(\'faqs\', {update:\'faqs\', tag:\'tbody\', handle:\'grip\', onUpdate:function(sortable) {Element.hide(\'message\'); new Ajax.Updater(\'faqs\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {Element.show(\'message\');}, parameters:Sortable.serialize(\'faqs\'), requestHeaders:[\'X-Update\', \'faqs\']})}});';
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$result = $this->Ajax->sortable('div', array('overlap' => 'foo'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"Sortable.create('div', {overlap:'foo'});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->sortable('div', array('block' => false));
$expected = "Sortable.create('div', {});";
$this->assertEqual($result, $expected);
$result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'someID'));
$expected = "Sortable.create('div', {scroll:'someID'});";
$this->assertEqual($result, $expected);
$result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'window'));
$expected = "Sortable.create('div', {scroll:window});";
$this->assertEqual($result, $expected);
$result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => "$('someElement')"));
$expected = "Sortable.create('div', {scroll:$('someElement')});";
$this->assertEqual($result, $expected);
}
/**
* testSubmitWithIndicator method
*
* @access public
* @return void
*/
function testSubmitWithIndicator() {
$result = $this->Ajax->submit('Add', array('div' => false, 'url' => "http://www.cakephp.org", 'indicator' => 'loading', 'loading' => "doSomething()", 'complete' => 'doSomethingElse() '));
$this->assertPattern('/onLoading:function\(request\) {doSomething\(\);\s+Element.show\(\'loading\'\);}/', $result);
$this->assertPattern('/onComplete:function\(request, json\) {doSomethingElse\(\) ;\s+Element.hide\(\'loading\'\);}/', $result);
}
/**
* testLink method
*
* @access public
* @return void
*/
function testLink() {
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads');
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Request\(\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('confirm' => 'Are you sure & positive?'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*if \(confirm\(\'Are you sure & positive\?\'\)\) {\s*new Ajax\.Request\(\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true}\);\s*}\s*else\s*{\s*event.returnValue = false;\s*return false;\s*}\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'complete' => 'myComplete();'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onComplete:function\(request, json\) {myComplete\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'loading' => 'myLoading();', 'complete' => 'myComplete();'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onComplete:function\(request, json\) {myComplete\(\);}, onLoading:function\(request\) {myLoading\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'encoding' => 'utf-8'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, encoding:\'utf-8\', requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'success' => 'success();'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onSuccess:function\(request\) {success\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'failure' => 'failure();'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
$this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
$this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onFailure:function\(request\) {failure\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
$result = $this->Ajax->link('Ajax Link', '/test', array('complete' => 'test'));
$this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern("/Event.observe\('link[0-9]+', [\w\d,'\(\)\s{}]+Ajax\.Request\([\w\d\s,'\(\){}:\/]+onComplete:function\(request, json\) {test}/", $result);
$this->assertNoPattern('/^<a[^<>]+complete="test"[^<>]*>Ajax Link<\/a>/', $result);
$this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
$result = $this->Ajax->link(
'Ajax Link',
array('controller' => 'posts', 'action' => 'index', '?' => array('one' => '1', 'two' => '2')),
array('update' => 'myDiv', 'id' => 'myLink')
);
$this->assertPattern('#/posts\?one\=1\&two\=2#', $result);
}
/**
* testRemoteTimer method
*
* @access public
* @return void
*/
function testRemoteTimer() {
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'frequency' => 25));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 25\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {complete();}})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();', 'create' => 'create();'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {complete();}, onCreate:function(request, xhr) {create();}})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'exception' => 'alert(exception);'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onException:function(request, exception) {alert(exception);}})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'contentType' => 'application/x-www-form-urlencoded'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, contentType:\'application/x-www-form-urlencoded\'})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'method' => 'get', 'encoding' => 'utf-8'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, method:\'get\', encoding:\'utf-8\'})')) . '/', $result);
$result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'postBody' => 'var1=value1'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, postBody:\'var1=value1\'})')) . '/', $result);
}
/**
* testObserveField method
*
* @access public
* @return void
*/
function testObserveField() {
$result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
$result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.Observer\(\'field\', 15, function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
$result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
$result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => 'Form.Element.serialize(\'otherField\')'));
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
$this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'otherField\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
}
/**
* testObserveForm method
*
* @access public
* @return void
*/
function testObserveForm() {
$result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Form.EventObserver('form', function(element, value) {new Ajax.Request('http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form')})})",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Form.Observer('form', 15, function(element, value) {new Ajax.Request('http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form')})})",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Form.EventObserver('form', function(element, value) {new Ajax.Updater('divId','http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form'), requestHeaders:['X-Update', 'divId']})}",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => "Form.serialize('otherForm')"));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Form.EventObserver('form', function(element, value) {new Ajax.Updater('divId','http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('otherForm'), requestHeaders:['X-Update', 'divId']})}",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
/**
* testSlider method
*
* @access public
* @return void
*/
function testSlider() {
$result = $this->Ajax->slider('sliderId', 'trackId');
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {axis:'vertical'});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical', 'minimum' => 60, 'maximum' => 288, 'alignX' => -28, 'alignY' => -5, 'disabled' => true));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {axis:'vertical', minimum:60, maximum:288, alignX:-28, alignY:-5, disabled:true});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('change' => "alert('changed');"));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {onChange:function(value) {alert('changed');}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('change' => "alert('changed');", 'slide' => "alert('sliding');"));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {onChange:function(value) {alert('changed');}, onSlide:function(value) {alert('sliding');}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('values' => array(10, 20, 30)));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {values:[10,20,30]});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->slider('sliderId', 'trackId', array('range' => '$R(10, 30)'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var sliderId = new Control.Slider('sliderId', 'trackId', {range:\$R(10, 30)});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
/**
* testRemoteFunction method
*
* @access public
* @return void
*/
function testRemoteFunction() {
$result = $this->Ajax->remoteFunction(array('complete' => 'testComplete();'));
$expected = "new Ajax.Request('/', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {testComplete();}})";
$this->assertEqual($result, $expected);
$result = $this->Ajax->remoteFunction(array('update' => 'myDiv'));
$expected = "new Ajax.Updater('myDiv','/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'myDiv']})";
$this->assertEqual($result, $expected);
$result = $this->Ajax->remoteFunction(array('update' => array('div1', 'div2')));
$expected = "new Ajax.Updater(document.createElement('div'),'/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'div1 div2']})";
$this->assertEqual($result, $expected);
$result = $this->Ajax->remoteFunction(array('update' => 'myDiv', 'confirm' => 'Are you sure?'));
$expected = "if (confirm('Are you sure?')) { new Ajax.Updater('myDiv','/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'myDiv']}); } else { event.returnValue = false; return false; }";
$this->assertEqual($result, $expected);
}
/**
* testDiv method
*
* @access public
* @return void
*/
function testDiv() {
ob_flush();
$oldXUpdate = env('HTTP_X_UPDATE');
$result = $this->Ajax->div('myDiv');
$this->assertTags($result, array('div' => array('id' => 'myDiv')));
$_SERVER['HTTP_X_UPDATE'] = null;
$result = $this->Ajax->divEnd('myDiv');
$this->assertTags($result, '/div');
$_SERVER['HTTP_X_UPDATE'] = 'secondDiv';
$result = $this->Ajax->div('myDiv');
$this->assertTags($result, array('div' => array('id' => 'myDiv')));
$result = $this->Ajax->divEnd('myDiv');
$this->assertTags($result, '/div');
$_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
$result = $this->Ajax->div('myDiv');
$this->assertTrue(empty($result));
$result = $this->Ajax->divEnd('myDiv');
$this->assertTrue(empty($result));
$_SERVER['HTTP_X_UPDATE'] = $oldXUpdate;
}
/**
* testAfterRender method
*
* @access public
* @return void
*/
function testAfterRender() {
$oldXUpdate = env('HTTP_X_UPDATE');
$this->Ajax->Javascript =& new TestJavascriptHelper();
$_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
$result = $this->Ajax->div('myDiv');
$this->assertTrue(empty($result));
echo 'Contents of myDiv';
$result = $this->Ajax->divEnd('myDiv');
$this->assertTrue(empty($result));
ob_start();
$this->Ajax->afterRender();
$result = array_shift($this->Ajax->Javascript->codeBlocks);
$this->assertPattern('/^\s*' . str_replace('/', '\\/', preg_quote('var __ajaxUpdater__ = {myDiv:"Contents%20of%20myDiv"};')) . '\s*' . str_replace('/', '\\/', preg_quote('for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(decodeURIComponent(__ajaxUpdater__[n]))); }')) . '\s*$/s', $result);
$_SERVER['HTTP_X_UPDATE'] = $oldXUpdate;
}
/**
* testEditor method
*
* @access public
* @return void
*/
function testEditor() {
$result = $this->Ajax->editor('myDiv', '/');
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->editor('myDiv', '/', array('complete' => 'testComplete();'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true, onComplete:function(request, json) {testComplete();}}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->editor('myDiv', '/', array('callback' => 'callback();'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Ajax.InPlaceEditor('myDiv', '/', {callback:function(form, value) {callback();}, ajaxOptions:{asynchronous:true, evalScripts:true}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->editor('myDiv', '/', array('collection' => array(1 => 'first', 2 => 'second')));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"new Ajax.InPlaceCollectionEditor('myDiv', '/', {collection:{\"1\":\"first\",\"2\":\"second\"}, ajaxOptions:{asynchronous:true, evalScripts:true}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Ajax->editor('myDiv', '/', array('var' => 'myVar'));
$expected = array(
array('script' => array('type' => 'text/javascript')),
$this->cDataStart,
"var myVar = new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true}});",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
}
}
?>

View file

@ -1,896 +0,0 @@
<?php
/**
* JavascriptHelperTest file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2006-2010, Cake Software Foundation, Inc.
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', array('Controller', 'View', 'ClassRegistry', 'View'));
App::import('Helper', array('Javascript', 'Html', 'Form'));
/**
* TheJsTestController class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TheJsTestController extends Controller {
/**
* name property
*
* @var string 'TheTest'
* @access public
*/
public $name = 'TheTest';
/**
* uses property
*
* @var mixed null
* @access public
*/
public $uses = null;
}
/**
* TheView class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TheView extends View {
/**
* scripts method
*
* @access public
* @return void
*/
function scripts() {
return $this->__scripts;
}
}
/**
* TestJavascriptObject class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TestJavascriptObject {
/**
* property1 property
*
* @var string 'value1'
* @access public
*/
public $property1 = 'value1';
/**
* property2 property
*
* @var int 2
* @access public
*/
public $property2 = 2;
}
/**
* JavascriptTest class
*
* @package test_suite
* @subpackage test_suite.cases.libs
* @since CakePHP Test Suite v 1.0.0.0
*/
class JavascriptTest extends CakeTestCase {
/**
* Regexp for CDATA start block
*
* @var string
*/
public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
/**
* Regexp for CDATA end block
*
* @var string
*/
public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
/**
* setUp method
*
* @access public
* @return void
*/
function startTest() {
$this->Javascript =& new JavascriptHelper();
$this->Javascript->Html =& new HtmlHelper();
$this->Javascript->Form =& new FormHelper();
$this->View =& new TheView(new TheJsTestController());
ClassRegistry::addObject('view', $this->View);
}
/**
* tearDown method
*
* @access public
* @return void
*/
function endTest() {
unset($this->Javascript->Html);
unset($this->Javascript->Form);
unset($this->Javascript);
ClassRegistry::removeObject('view');
unset($this->View);
}
/**
* testConstruct method
*
* @access public
* @return void
*/
function testConstruct() {
$Javascript =& new JavascriptHelper(array('safe'));
$this->assertTrue($Javascript->safe);
$Javascript =& new JavascriptHelper(array('safe' => false));
$this->assertFalse($Javascript->safe);
}
/**
* testLink method
*
* @access public
* @return void
*/
function testLink() {
Configure::write('Asset.timestamp', false);
$result = $this->Javascript->link('script.js');
$expected = '<script type="text/javascript" src="js/script.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('script');
$expected = '<script type="text/javascript" src="js/script.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('scriptaculous.js?load=effects');
$expected = '<script type="text/javascript" src="js/scriptaculous.js?load=effects"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('some.json.libary');
$expected = '<script type="text/javascript" src="js/some.json.libary.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('jquery-1.1.2');
$expected = '<script type="text/javascript" src="js/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('jquery-1.1.2');
$expected = '<script type="text/javascript" src="js/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('/plugin/js/jquery-1.1.2');
$expected = '<script type="text/javascript" src="/plugin/js/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('/some_other_path/myfile.1.2.2.min.js');
$expected = '<script type="text/javascript" src="/some_other_path/myfile.1.2.2.min.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('some_other_path/myfile.1.2.2.min.js');
$expected = '<script type="text/javascript" src="js/some_other_path/myfile.1.2.2.min.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('some_other_path/myfile.1.2.2.min');
$expected = '<script type="text/javascript" src="js/some_other_path/myfile.1.2.2.min.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('http://example.com/jquery.js');
$expected = '<script type="text/javascript" src="http://example.com/jquery.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link(array('prototype.js', 'scriptaculous.js'));
$this->assertPattern('/^\s*<script\s+type="text\/javascript"\s+src=".*js\/prototype\.js"[^<>]*><\/script>/', $result);
$this->assertPattern('/<\/script>\s*<script[^<>]+>/', $result);
$this->assertPattern('/<script\s+type="text\/javascript"\s+src=".*js\/scriptaculous\.js"[^<>]*><\/script>\s*$/', $result);
$result = $this->Javascript->link('jquery-1.1.2', false);
$resultScripts = $this->View->scripts();
reset($resultScripts);
$expected = '<script type="text/javascript" src="js/jquery-1.1.2.js"></script>';
$this->assertNull($result);
$this->assertEqual(count($resultScripts), 1);
$this->assertEqual(current($resultScripts), $expected);
}
/**
* testFilteringAndTimestamping method
*
* @access public
* @return void
*/
function testFilteringAndTimestamping() {
if ($this->skipIf(!is_writable(JS), 'JavaScript directory not writable, skipping JS asset timestamp tests. %s')) {
return;
}
cache(str_replace(WWW_ROOT, '', JS) . '__cake_js_test.js', 'alert("test")', '+999 days', 'public');
$timestamp = substr(strtotime('now'), 0, 8);
Configure::write('Asset.timestamp', true);
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src=".*js\/__cake_js_test\.js\?' . $timestamp . '[0-9]{2}"[^<>]*>/', $result);
$debug = Configure::read('debug');
Configure::write('debug', 0);
$result = $this->Javascript->link('__cake_js_test');
$expected = '<script type="text/javascript" src="js/__cake_js_test.js"></script>';
$this->assertEqual($result, $expected);
Configure::write('Asset.timestamp', 'force');
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src=".*js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"[^<>]*>/', $result);
Configure::write('debug', $debug);
Configure::write('Asset.timestamp', false);
$old = Configure::read('Asset.filter.js');
Configure::write('Asset.filter.js', 'js.php');
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src=".*cjs\/__cake_js_test\.js"[^<>]*>/', $result);
Configure::write('Asset.filter.js', true);
$result = $this->Javascript->link('jquery-1.1.2');
$expected = '<script type="text/javascript" src="cjs/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);
$result = $this->Javascript->link('folderjs/jquery-1.1.2');
$expected = '<script type="text/javascript" src="cjs/folderjs/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);
if ($old === null) {
Configure::delete('Asset.filter.js');
}
$debug = Configure::read('debug');
$webroot = $this->Javascript->webroot;
Configure::write('debug', 0);
Configure::write('Asset.timestamp', 'force');
$this->Javascript->webroot = '/testing/';
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src="\/testing\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result);
$this->Javascript->webroot = '/testing/longer/';
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src="\/testing\/longer\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result);
$this->Javascript->webroot = $webroot;
Configure::write('debug', $debug);
unlink(JS . '__cake_js_test.js');
}
/**
* testValue method
*
* @access public
* @return void
*/
function testValue() {
$result = $this->Javascript->value(array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8)));
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
$this->assertEqual($result, $expected);
$result = $this->Javascript->value(null);
$this->assertEqual($result, 'null');
$result = $this->Javascript->value(true);
$this->assertEqual($result, 'true');
$result = $this->Javascript->value(false);
$this->assertEqual($result, 'false');
$result = $this->Javascript->value(5);
$this->assertEqual($result, '5');
$result = $this->Javascript->value(floatval(5.3));
$this->assertPattern('/^5.3[0]+$/', $result);
$result = $this->Javascript->value('');
$expected = '""';
$this->assertEqual($result, $expected);
$result = $this->Javascript->value('CakePHP' . "\n" . 'Rapid Development Framework');
$expected = '"CakePHP\\nRapid Development Framework"';
$this->assertEqual($result, $expected);
$result = $this->Javascript->value('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
$expected = '"CakePHP\\nRapid Development Framework\\nFor PHP"';
$this->assertEqual($result, $expected);
$result = $this->Javascript->value('CakePHP: "Rapid Development Framework"');
$expected = '"CakePHP: \\"Rapid Development Framework\\""';
$this->assertEqual($result, $expected);
$result = $this->Javascript->value('CakePHP: \'Rapid Development Framework\'');
$expected = '"CakePHP: \\\'Rapid Development Framework\\\'"';
$this->assertEqual($result, $expected);
}
/**
* testObjectGeneration method
*
* @access public
* @return void
*/
function testObjectGeneration() {
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
$result = $this->Javascript->object($object);
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
$this->assertEqual($result, $expected);
$result = $this->Javascript->object(array('default' => 0));
$expected = '{"default":0}';
$this->assertEqual($result, $expected);
$result = $this->Javascript->object(array(
'2007' => 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'))
), '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')
))
));
$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);
if (ini_get('precision') >= 12) {
$number = 3.141592653589;
if (!$this->Javascript->useNative) {
$number = sprintf("%.11f", $number);
}
$result = $this->Javascript->object(array('Object' => array(true, false, 1, '02101', 0, -1, 3.141592653589, "1")));
$expected = '{"Object":[true,false,1,"02101",0,-1,' . $number . ',"1"]}';
$this->assertEqual($result, $expected);
$result = $this->Javascript->object(array('Object' => array(true => true, false, -3.141592653589, -10)));
$expected = '{"Object":{"1":true,"2":false,"3":' . (-1 * $number) . ',"4":-10}}';
$this->assertEqual($result, $expected);
}
$result = $this->Javascript->object(new TestJavascriptObject());
$expected = '{"property1":"value1","property2":2}';
$this->assertEqual($result, $expected);
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
$result = $this->Javascript->object($object, array('block' => true));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'{"title":"New thing","indexes":[5,6,7,8]}',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
$result = $this->Javascript->object($object);
$expected = '{"title":"New thing","indexes":[5,6,7,8],"object":{"inner":{"value":1}}}';
$this->assertEqual($result, $expected);
foreach (array('true' => true, 'false' => false, 'null' => null) as $expected => $data) {
$result = $this->Javascript->object($data);
$this->assertEqual($result, $expected);
}
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
$result = $this->Javascript->object($object, array('prefix' => 'PREFIX', 'postfix' => 'POSTFIX'));
$this->assertPattern('/^PREFIX/', $result);
$this->assertPattern('/POSTFIX$/', $result);
$this->assertNoPattern('/.PREFIX./', $result);
$this->assertNoPattern('/.POSTFIX./', $result);
if ($this->Javascript->useNative) {
$this->Javascript->useNative = false;
$this->testObjectGeneration();
$this->Javascript->useNative = true;
}
}
/**
* testObjectNonNative method
*
* @access public
* @return void
*/
function testObjectNonNative() {
$oldNative = $this->Javascript->useNative;
$this->Javascript->useNative = false;
$object = array(
'Object' => array(
'key1' => 'val1',
'key2' => 'val2',
'key3' => 'val3'
)
);
$expected = '{"Object":{"key1":val1,"key2":"val2","key3":val3}}';
$result = $this->Javascript->object($object, array('quoteKeys' => false, 'stringKeys' => array('key1', 'key3')));
$this->assertEqual($result, $expected);
$this->Javascript->useNative = $oldNative;
}
/**
* testScriptBlock method
*
* @access public
* @return void
*/
function testScriptBlock() {
$result = $this->Javascript->codeBlock('something');
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'something',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', array('allowCache' => true, 'safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
'something',
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', array('allowCache' => false, 'safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
'something',
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', true);
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'something',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', false);
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'something',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
'something',
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock('something', array('safe' => true));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'something',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock(null, array('safe' => true, 'allowCache' => false));
$this->assertNull($result);
echo 'this is some javascript';
$result = $this->Javascript->blockEnd();
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'this is some javascript',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->codeBlock();
$this->assertNull($result);
echo "alert('hey');";
$result = $this->Javascript->blockEnd();
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"alert('hey');",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$this->Javascript->cacheEvents(false, true);
$this->assertFalse($this->Javascript->inBlock);
$result = $this->Javascript->codeBlock();
$this->assertIdentical($result, null);
$this->assertTrue($this->Javascript->inBlock);
echo 'alert("this is a buffered script");';
$result = $this->Javascript->blockEnd();
$this->assertIdentical($result, null);
$this->assertFalse($this->Javascript->inBlock);
$result = $this->Javascript->getCache();
$this->assertEqual('alert("this is a buffered script");', $result);
}
/**
* testOutOfLineScriptWriting method
*
* @access public
* @return void
*/
function testOutOfLineScriptWriting() {
echo $this->Javascript->codeBlock('$(document).ready(function() { });', array('inline' => false));
$this->Javascript->codeBlock(null, array('inline' => false));
echo '$(function(){ });';
$this->Javascript->blockEnd();
$script = $this->View->scripts();
$this->assertEqual(count($script), 2);
$this->assertPattern('/' . preg_quote('$(document).ready(function() { });', '/') . '/', $script[0]);
$this->assertPattern('/' . preg_quote('$(function(){ });', '/') . '/', $script[1]);
}
/**
* testEvent method
*
* @access public
* @return void
*/
function testEvent() {
$result = $this->Javascript->event('myId', 'click', 'something();');
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('myId', 'click', 'something();', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('myId', 'click');
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('myId', 'click', 'something();', false);
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('myId', 'click', 'something();', array('useCapture' => true));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, true);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('document', 'load');
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe(document, 'load', function(event) { }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('$(\'myId\')', 'click', 'something();', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->event('\'document\'', 'load', 'something();', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
"Event.observe('document', 'load', function(event) { something(); }, false);",
'/script'
);
$this->assertTags($result, $expected);
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
$result = $this->Javascript->getCache();
$this->assertPattern('/^' . str_replace('/', '\\/', preg_quote('Event.observe($(\'myId\'), \'click\', function(event) { something(); }, false);')) . '$/s', $result);
$result = $this->Javascript->event('#myId', 'alert(event);');
$this->assertNull($result);
$result = $this->Javascript->getCache();
$this->assertPattern('/^\s*var Rules = {\s*\'#myId\': function\(element, event\)\s*{\s*alert\(event\);\s*}\s*}\s*EventSelectors\.start\(Rules\);\s*$/s', $result);
}
/**
* testWriteEvents method
*
* @access public
* @return void
*/
function testWriteEvents() {
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
$result = $this->Javascript->writeEvents();
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->getCache();
$this->assertTrue(empty($result));
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
$result = $this->Javascript->writeEvents(false);
$resultScripts = $this->View->scripts();
reset($resultScripts);
$this->assertNull($result);
$this->assertEqual(count($resultScripts), 1);
$result = current($resultScripts);
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->getCache();
$this->assertTrue(empty($result));
}
/**
* testEscapeScript method
*
* @access public
* @return void
*/
function testEscapeScript() {
$result = $this->Javascript->escapeScript('');
$expected = '';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeScript('CakePHP' . "\n" . 'Rapid Development Framework');
$expected = 'CakePHP\\nRapid Development Framework';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeScript('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
$expected = 'CakePHP\\nRapid Development Framework\\nFor PHP';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeScript('CakePHP: "Rapid Development Framework"');
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeScript('CakePHP: \'Rapid Development Framework\'');
$expected = 'CakePHP: \\\'Rapid Development Framework\\\'';
$this->assertEqual($result, $expected);
}
/**
* testEscapeString method
*
* @access public
* @return void
*/
function testEscapeString() {
$result = $this->Javascript->escapeString('');
$expected = '';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('CakePHP' . "\n" . 'Rapid Development Framework');
$expected = 'CakePHP\\nRapid Development Framework';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
$expected = 'CakePHP\\nRapid Development Framework\\nFor PHP';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('CakePHP: "Rapid Development Framework"');
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('CakePHP: \'Rapid Development Framework\'');
$expected = "CakePHP: \\'Rapid Development Framework\\'";
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('my \\"string\\"');
$expected = 'my \\\\\\"string\\\\\\"';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('my string\nanother line');
$expected = 'my string\\\nanother line';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('String with \n string that looks like newline');
$expected = 'String with \\\n string that looks like newline';
$this->assertEqual($result, $expected);
$result = $this->Javascript->escapeString('String with \n string that looks like newline');
$expected = 'String with \\\n string that looks like newline';
$this->assertEqual($result, $expected);
}
/**
* test string escaping and compare to json_encode()
*
* @return void
*/
function testStringJsonEncodeCompliance() {
if (!function_exists('json_encode')) {
return;
}
$this->Javascript->useNative = false;
$data = array();
$data['mystring'] = "simple string";
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
$data['mystring'] = "strïng with spécial chârs";
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
$data['mystring'] = "a two lines\nstring";
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
$data['mystring'] = "a \t tabbed \t string";
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
$data['mystring'] = "a \"double-quoted\" string";
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
$data['mystring'] = 'a \\"double-quoted\\" string';
$this->assertEqual(json_encode($data), $this->Javascript->object($data));
}
/**
* test that text encoded with Javascript::object decodes properly
*
* @return void
*/
function testObjectDecodeCompatibility() {
if (!function_exists('json_decode')) {
return;
}
$this->Javascript->useNative = false;
$data = array("simple string");
$result = $this->Javascript->object($data);
$this->assertEqual(json_decode($result), $data);
$data = array('my \"string\"');
$result = $this->Javascript->object($data);
$this->assertEqual(json_decode($result), $data);
$data = array('my \\"string\\"');
$result = $this->Javascript->object($data);
$this->assertEqual(json_decode($result), $data);
}
/**
* testAfterRender method
*
* @access public
* @return void
*/
function testAfterRender() {
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
ob_start();
$this->Javascript->afterRender();
$result = ob_get_clean();
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
"Event.observe($('myId'), 'click', function(event) { something(); }, false);",
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);
$result = $this->Javascript->getCache();
$this->assertTrue(empty($result));
$old = $this->Javascript->enabled;
$this->Javascript->enabled = false;
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
ob_start();
$this->Javascript->afterRender();
$result = ob_get_clean();
$this->assertTrue(empty($result));
$result = $this->Javascript->getCache();
$this->assertPattern('/^' . str_replace('/', '\\/', preg_quote('Event.observe($(\'myId\'), \'click\', function(event) { something(); }, false);')) . '$/s', $result);
$this->Javascript->enabled = $old;
}
}
?>