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