2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
2009-03-07 00:51:13 +00:00
|
|
|
* JsHelper Test Case
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-07 00:51:13 +00:00
|
|
|
* TestCase for the JsHelper
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
2008-10-30 17:30:26 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
App::import('Helper', 'Js');
|
2009-03-08 00:34:05 +00:00
|
|
|
App::import('Core', array('View', 'ClassRegistry'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-03-07 07:26:19 +00:00
|
|
|
Mock::generate('Helper', 'TestJsEngineHelper', array('methodOne'));
|
2009-03-08 00:34:05 +00:00
|
|
|
Mock::generate('View', 'JsHelperView');
|
|
|
|
|
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
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs.view.helpers
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
class JsHelperTestCase extends CakeTestCase {
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
function startTest() {
|
2009-03-08 01:16:11 +00:00
|
|
|
$this->Js = new JsHelper('JsBase');
|
|
|
|
$this->Js->JsBaseEngine = new JsBaseEngineHelper();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-03-07 00:51:13 +00:00
|
|
|
function endTest() {
|
2009-03-08 00:34:05 +00:00
|
|
|
ClassRegistry::removeObject('view');
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($this->Js);
|
|
|
|
}
|
2009-03-07 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* test object construction
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testConstruction() {
|
|
|
|
$js = new JsHelper();
|
|
|
|
$this->assertEqual($js->helpers, array('jqueryEngine'));
|
|
|
|
|
|
|
|
$js = new JsHelper(array('mootools'));
|
|
|
|
$this->assertEqual($js->helpers, array('mootoolsEngine'));
|
|
|
|
|
|
|
|
$js = new JsHelper('prototype');
|
|
|
|
$this->assertEqual($js->helpers, array('prototypeEngine'));
|
|
|
|
|
|
|
|
$js = new JsHelper('MyPlugin.Dojo');
|
|
|
|
$this->assertEqual($js->helpers, array('MyPlugin.DojoEngine'));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test that methods dispatch internally and to the engine class
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testMethodDispatching() {
|
|
|
|
$js = new JsHelper(array('TestJs'));
|
|
|
|
$js->TestJsEngine = new TestJsEngineHelper();
|
|
|
|
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
2009-03-07 18:10:50 +00:00
|
|
|
|
2009-03-07 07:26:19 +00:00
|
|
|
$js->methodOne();
|
2009-03-07 18:10:50 +00:00
|
|
|
|
|
|
|
$js->TestEngine = new StdClass();
|
|
|
|
$this->expectError();
|
|
|
|
$js->someMethodThatSurelyDoesntExist();
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2009-03-07 18:10:50 +00:00
|
|
|
/**
|
|
|
|
* test script tag generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testUses() {
|
|
|
|
$result = $this->Js->uses('foo');
|
|
|
|
$expected = array(
|
|
|
|
'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
$result = $this->Js->uses(array('foobar', 'bar'));
|
|
|
|
$expected = array(
|
|
|
|
array('script' => array('type' => 'text/javascript', 'src' => 'js/foobar.js')),
|
|
|
|
'/script',
|
|
|
|
array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
|
|
|
|
'/script',
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
$result = $this->Js->uses('jquery-1.3');
|
|
|
|
$expected = array(
|
|
|
|
'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
$result = $this->Js->uses('/plugin/js/jquery-1.3.2');
|
2009-03-07 18:10:50 +00:00
|
|
|
$expected = array(
|
2009-03-08 03:06:00 +00:00
|
|
|
'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.2.js')
|
2009-03-07 18:10:50 +00:00
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Js->uses('scriptaculous.js?load=effects');
|
|
|
|
$expected = array(
|
|
|
|
'script' => array('type' => 'text/javascript', 'src' => 'js/scriptaculous.js?load=effects')
|
|
|
|
);
|
|
|
|
$this->assertTags($result, $expected);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
|
|
|
$view = new JsHelperView();
|
|
|
|
ClassRegistry::addObject('view', $view);
|
|
|
|
|
|
|
|
$view->expectOnce('addScript');
|
|
|
|
$result = $this->Js->uses('test', false);
|
|
|
|
$this->assertNull($result);
|
2009-03-07 18:10:50 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test Min/pack version autofinding
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testMinPackAutoUse() {
|
|
|
|
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, min/pack js testing is skipped')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
touch(WWW_ROOT . 'js' . DS. '__cake_js_min_test.min.js');
|
|
|
|
touch(WWW_ROOT . 'js' . DS. '__cake_js_pack_test.pack.js');
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
$result = $this->Js->uses('__cake_js_min_test');
|
|
|
|
$this->assertPattern('/__cake_js_min_test\.min\.js/', $result);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
$result = $this->Js->uses('__cake_js_pack_test');
|
|
|
|
$this->assertPattern('/__cake_js_pack_test\.pack\.js/', $result);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
Configure::write('debug', 2);
|
|
|
|
$result = $this->Js->uses('__cake_js_pack_test');
|
|
|
|
$this->assertNoPattern('/pack\.js/', $result);
|
2009-03-08 00:34:05 +00:00
|
|
|
|
2009-03-07 18:10:50 +00:00
|
|
|
unlink(WWW_ROOT . 'js' . DS. '__cake_js_min_test.min.js');
|
|
|
|
unlink(WWW_ROOT . 'js' . DS. '__cake_js_pack_test.pack.js');
|
|
|
|
}
|
2009-03-08 00:34:05 +00:00
|
|
|
/**
|
|
|
|
* test timestamp enforcement
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testAssetTimestamping() {
|
|
|
|
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, timestamp testing has been skipped')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Configure::write('Asset.timestamp', true);
|
|
|
|
touch(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
|
|
|
$timestamp = substr(strtotime('now'), 0, 8);
|
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
$result = $this->Js->uses('__cake_js_test', true, false);
|
|
|
|
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
2009-03-08 00:34:05 +00:00
|
|
|
|
|
|
|
Configure::write('debug', 0);
|
2009-03-08 03:06:00 +00:00
|
|
|
$result = $this->Js->uses('__cake_js_test', true, false);
|
2009-03-08 00:34:05 +00:00
|
|
|
$this->assertPattern('/__cake_js_test.js"/', $result);
|
|
|
|
|
|
|
|
Configure::write('Asset.timestamp', 'force');
|
2009-03-08 03:06:00 +00:00
|
|
|
$result = $this->Js->uses('__cake_js_test', true, false);
|
|
|
|
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
2009-03-08 00:34:05 +00:00
|
|
|
|
|
|
|
unlink(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
2009-03-08 03:06:00 +00:00
|
|
|
Configure::write('debug', 2);
|
2009-03-08 00:34:05 +00:00
|
|
|
}
|
2009-03-07 00:56:28 +00:00
|
|
|
/**
|
2009-03-08 03:06:00 +00:00
|
|
|
* test that scripts added with uses() are only ever included once.
|
2009-03-07 07:26:19 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2009-03-08 03:06:00 +00:00
|
|
|
function testUniqueScriptInsertion() {
|
|
|
|
$result = $this->Js->uses('foo');
|
|
|
|
$this->assertNotNull($result);
|
|
|
|
|
|
|
|
$result = $this->Js->uses('foo');
|
|
|
|
$this->assertNull($result, 'Script returned upon duplicate inclusion %s');
|
2009-03-07 07:26:19 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 03:06:00 +00:00
|
|
|
|
|
|
|
|
2009-03-08 01:16:11 +00:00
|
|
|
/**
|
|
|
|
* JsBaseEngine Class Test case
|
|
|
|
*
|
|
|
|
* @package cake.tests.view.helpers
|
|
|
|
**/
|
|
|
|
class JsBaseEngineTestCase extends CakeTestCase {
|
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function startTest() {
|
|
|
|
$this->JsEngine = new JsBaseEngineHelper();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function endTest() {
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
unset($this->JsEngine);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test escape string skills
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testEscaping() {
|
|
|
|
$result = $this->JsEngine->escape('');
|
|
|
|
$expected = '';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP' . "\n" . 'Rapid Development Framework');
|
|
|
|
$expected = 'CakePHP\\nRapid Development Framework';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP' . "\r\n" . 'Rapid Development Framework' . "\r" . 'For PHP');
|
|
|
|
$expected = 'CakePHP\\nRapid Development Framework\\nFor PHP';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP: "Rapid Development Framework"');
|
|
|
|
$expected = 'CakePHP: \\"Rapid Development Framework\\"';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('CakePHP: \'Rapid Development Framework\'');
|
|
|
|
$expected = 'CakePHP: \\\'Rapid Development Framework\\\'';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->escape('my \\"string\\"');
|
|
|
|
$expected = 'my \\\"string\\\"';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-03-08 03:06:00 +00:00
|
|
|
/**
|
|
|
|
* test prompt() creation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testPrompt() {
|
|
|
|
$result = $this->JsEngine->prompt('Hey, hey you', 'hi!');
|
|
|
|
$expected = 'prompt("Hey, hey you", "hi!");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->prompt('"Hey"', '"hi"');
|
|
|
|
$expected = 'prompt("\"Hey\"", "\"hi\"");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test alert generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testAlert() {
|
|
|
|
$result = $this->JsEngine->alert('Hey there');
|
|
|
|
$expected = 'alert("Hey there");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->alert('"Hey"');
|
|
|
|
$expected = 'alert("\"Hey\"");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test confirm generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testConfirm() {
|
|
|
|
$result = $this->JsEngine->confirm('Are you sure?');
|
|
|
|
$expected = 'confirm("Are you sure?");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->confirm('"Are you sure?"');
|
|
|
|
$expected = 'confirm("\"Are you sure?\"");';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* test Redirect
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testRedirect() {
|
|
|
|
$result = $this->JsEngine->redirect(array('controller' => 'posts', 'action' => 'view', 1));
|
|
|
|
$expected = 'window.location = "/posts/view/1";';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* testObject encoding with non-native methods.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testObject() {
|
|
|
|
$this->JsEngine->useNative = false;
|
|
|
|
|
|
|
|
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
|
|
|
|
$result = $this->JsEngine->object($object);
|
|
|
|
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->object(array('default' => 0));
|
|
|
|
$expected = '{"default":0}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->JsEngine->object(array(
|
|
|
|
'2007' => array(
|
2009-03-08 06:01:08 +00:00
|
|
|
'Spring' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
),
|
|
|
|
'Fall' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'2006' => array(
|
|
|
|
'Spring' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
),
|
|
|
|
'Fall' => array(
|
|
|
|
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
|
|
|
|
)
|
|
|
|
)
|
2009-03-08 03:06:00 +00:00
|
|
|
));
|
|
|
|
$expected = '{"2007":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}},"2006":{"Spring":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}},"Fall":{"1":{"id":1,"name":"Josh"},"2":{"id":2,"name":"Becky"}}}}';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-03-08 01:16:11 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 08:17:02 +00:00
|
|
|
?>
|