2009-03-14 00:50:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JqueryEngineTestCase
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2006-2008, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.views.helpers
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
App::import('Helper', array('Html', 'Js', 'JqueryEngine'));
|
|
|
|
|
|
|
|
class JqueryEngineHelperTestCase extends CakeTestCase {
|
|
|
|
/**
|
|
|
|
* startTest
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function startTest() {
|
|
|
|
$this->Jquery =& new JqueryEngineHelper();
|
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* end test
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function endTest() {
|
|
|
|
unset($this->Jquery);
|
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* test selector method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testSelector() {
|
|
|
|
$result = $this->Jquery->get('#content');
|
|
|
|
$this->assertEqual($result, $this->Jquery);
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->assertEqual($this->Jquery->selection, '$("#content")');
|
2009-07-25 02:46:33 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
$result = $this->Jquery->get('document');
|
|
|
|
$this->assertEqual($result, $this->Jquery);
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->assertEqual($this->Jquery->selection, '$(document)');
|
2009-07-25 02:46:33 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
$result = $this->Jquery->get('window');
|
|
|
|
$this->assertEqual($result, $this->Jquery);
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->assertEqual($this->Jquery->selection, '$(window)');
|
2009-07-25 02:46:33 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
$result = $this->Jquery->get('ul');
|
|
|
|
$this->assertEqual($result, $this->Jquery);
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->assertEqual($this->Jquery->selection, '$("ul")');
|
2009-03-14 00:50:31 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* test event binding
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testEvent() {
|
2009-04-07 01:24:14 +00:00
|
|
|
$this->Jquery->get('#myLink');
|
|
|
|
$result = $this->Jquery->event('click', 'doClick', array('wrap' => false));
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#myLink").bind("click", doClick);';
|
2009-03-14 00:50:31 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-15 16:26:12 +00:00
|
|
|
|
2009-04-07 01:24:14 +00:00
|
|
|
$result = $this->Jquery->event('click', '$(this).show();', array('stop' => false));
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#myLink").bind("click", function (event) {$(this).show();});';
|
2009-03-15 16:10:46 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-15 16:26:12 +00:00
|
|
|
|
2009-04-07 01:24:14 +00:00
|
|
|
$result = $this->Jquery->event('click', '$(this).hide();');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#myLink").bind("click", function (event) {$(this).hide();'."\n".'return false;});';
|
2009-03-14 00:50:31 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* test dom ready event creation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testDomReady() {
|
|
|
|
$result = $this->Jquery->domReady('foo.name = "bar";');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$(document).bind("ready", function (event) {foo.name = "bar";});';
|
2009-03-14 00:50:31 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* test Each method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testEach() {
|
2009-04-07 01:24:14 +00:00
|
|
|
$this->Jquery->get('#foo');
|
|
|
|
$result = $this->Jquery->each('$(this).hide();');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").each(function () {$(this).hide();});';
|
2009-03-14 00:50:31 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 05:57:24 +00:00
|
|
|
/**
|
|
|
|
* test Effect generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testEffect() {
|
2009-04-07 01:24:14 +00:00
|
|
|
$this->Jquery->get('#foo');
|
|
|
|
$result = $this->Jquery->effect('show');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").show();';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Jquery->effect('hide');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").hide();';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-14 04:10:43 +00:00
|
|
|
|
2009-03-14 05:57:24 +00:00
|
|
|
$result = $this->Jquery->effect('hide', array('speed' => 'fast'));
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").hide("fast");';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Jquery->effect('fadeIn');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").fadeIn();';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Jquery->effect('fadeOut');
|
2009-03-16 01:44:40 +00:00
|
|
|
$expected = '$("#foo").fadeOut();';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
2009-03-15 23:59:16 +00:00
|
|
|
$result = $this->Jquery->effect('slideIn');
|
2009-03-30 01:52:44 +00:00
|
|
|
$expected = '$("#foo").slideDown();';
|
2009-03-15 23:59:16 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Jquery->effect('slideOut');
|
2009-03-30 01:52:44 +00:00
|
|
|
$expected = '$("#foo").slideUp();';
|
2009-03-14 05:57:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:37:41 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->effect('slideDown');
|
|
|
|
$expected = '$("#foo").slideDown();';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Jquery->effect('slideUp');
|
|
|
|
$expected = '$("#foo").slideUp();';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-14 05:57:24 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
/**
|
|
|
|
* Test Request Generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testRequest() {
|
|
|
|
$result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1));
|
2009-07-11 21:21:06 +00:00
|
|
|
$expected = '$.ajax({url:"\\/posts\\/view\\/1"});';
|
2009-03-14 21:01:49 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
2009-08-12 13:27:01 +00:00
|
|
|
$result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1), array(
|
|
|
|
'update' => '#content'
|
|
|
|
));
|
|
|
|
$expected = '$.ajax({success:function (data, textStatus) {$("#content").html(data);}, url:"\/posts\/view\/1"});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-08-12 13:24:23 +00:00
|
|
|
|
2009-03-14 21:01:49 +00:00
|
|
|
$result = $this->Jquery->request('/people/edit/1', array(
|
|
|
|
'method' => 'post',
|
2009-07-25 02:46:33 +00:00
|
|
|
'before' => 'doBefore',
|
2009-04-06 03:49:09 +00:00
|
|
|
'complete' => 'doComplete',
|
|
|
|
'success' => 'doSuccess',
|
2009-03-14 21:01:49 +00:00
|
|
|
'error' => 'handleError',
|
|
|
|
'type' => 'json',
|
2009-07-30 02:19:02 +00:00
|
|
|
'data' => array('name' => 'jim', 'height' => '185cm'),
|
|
|
|
'wrapCallbacks' => false
|
2009-03-14 21:01:49 +00:00
|
|
|
));
|
2009-07-25 03:29:44 +00:00
|
|
|
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, success:doSuccess, type:"post", url:"\\/people\\/edit\\/1"});';
|
2009-03-14 21:01:49 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-25 02:46:33 +00:00
|
|
|
|
2009-03-30 03:00:39 +00:00
|
|
|
$result = $this->Jquery->request('/people/edit/1', array(
|
|
|
|
'update' => '#updated',
|
|
|
|
'success' => 'doFoo',
|
2009-07-30 02:19:02 +00:00
|
|
|
'method' => 'post',
|
|
|
|
'wrapCallbacks' => false
|
2009-03-30 03:00:39 +00:00
|
|
|
));
|
2009-08-12 13:24:23 +00:00
|
|
|
$expected = '$.ajax({success:function (data, textStatus) {$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
|
2009-03-30 03:00:39 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
2009-07-25 05:35:36 +00:00
|
|
|
$result = $this->Jquery->request('/people/edit/1', array(
|
|
|
|
'update' => '#updated',
|
|
|
|
'success' => 'doFoo',
|
|
|
|
'method' => 'post',
|
|
|
|
'dataExpression' => true,
|
2009-07-30 02:19:02 +00:00
|
|
|
'data' => '$("#someId").serialize()',
|
|
|
|
'wrapCallbacks' => false
|
2009-07-25 05:35:36 +00:00
|
|
|
));
|
2009-08-12 13:24:23 +00:00
|
|
|
$expected = '$.ajax({data:$("#someId").serialize(), success:function (data, textStatus) {$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
|
2009-07-25 05:35:36 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->request('/people/edit/1', array(
|
|
|
|
'success' => 'doFoo',
|
|
|
|
'before' => 'doBefore',
|
|
|
|
'method' => 'post',
|
|
|
|
'dataExpression' => true,
|
|
|
|
'data' => '$("#someId").serialize()',
|
|
|
|
));
|
|
|
|
$expected = '$.ajax({beforeSend:function (XMLHttpRequest) {doBefore}, data:$("#someId").serialize(), success:function (data, textStatus) {doFoo}, type:"post", url:"\\/people\\/edit\\/1"});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-14 21:01:49 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-28 03:44:57 +00:00
|
|
|
/**
|
|
|
|
* test sortable list generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testSortable() {
|
2009-04-07 01:24:14 +00:00
|
|
|
$this->Jquery->get('#myList');
|
|
|
|
$result = $this->Jquery->sortable(array(
|
2009-03-28 03:44:57 +00:00
|
|
|
'distance' => 5,
|
|
|
|
'containment' => 'parent',
|
|
|
|
'start' => 'onStart',
|
|
|
|
'complete' => 'onStop',
|
|
|
|
'sort' => 'onSort',
|
2009-07-30 02:19:02 +00:00
|
|
|
'wrapCallbacks' => false
|
2009-03-28 03:44:57 +00:00
|
|
|
));
|
2009-03-30 01:52:44 +00:00
|
|
|
$expected = '$("#myList").sortable({containment:"parent", distance:5, sort:onSort, start:onStart, stop:onStop});';
|
2009-03-28 03:44:57 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->sortable(array(
|
|
|
|
'distance' => 5,
|
|
|
|
'containment' => 'parent',
|
|
|
|
'start' => 'onStart',
|
|
|
|
'complete' => 'onStop',
|
|
|
|
'sort' => 'onSort',
|
|
|
|
));
|
|
|
|
$expected = '$("#myList").sortable({containment:"parent", distance:5, sort:function (event, ui) {onSort}, start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-28 03:44:57 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-30 03:00:39 +00:00
|
|
|
/**
|
|
|
|
* test drag() method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testDrag() {
|
2009-04-09 03:31:17 +00:00
|
|
|
$this->Jquery->get('#element');
|
|
|
|
$result = $this->Jquery->drag(array(
|
|
|
|
'container' => '#content',
|
|
|
|
'start' => 'onStart',
|
2009-07-25 02:46:33 +00:00
|
|
|
'drag' => 'onDrag',
|
2009-04-09 03:31:17 +00:00
|
|
|
'stop' => 'onStop',
|
|
|
|
'snapGrid' => array(10, 10),
|
2009-07-30 02:19:02 +00:00
|
|
|
'wrapCallbacks' => false
|
2009-04-09 03:31:17 +00:00
|
|
|
));
|
2009-04-09 03:57:32 +00:00
|
|
|
$expected = '$("#element").draggable({containment:"#content", drag:onDrag, grid:[10,10], start:onStart, stop:onStop});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->drag(array(
|
|
|
|
'container' => '#content',
|
|
|
|
'start' => 'onStart',
|
|
|
|
'drag' => 'onDrag',
|
|
|
|
'stop' => 'onStop',
|
|
|
|
'snapGrid' => array(10, 10),
|
|
|
|
));
|
|
|
|
$expected = '$("#element").draggable({containment:"#content", drag:function (event, ui) {onDrag}, grid:[10,10], start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-30 03:00:39 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-03-30 03:00:39 +00:00
|
|
|
/**
|
|
|
|
* test drop() method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testDrop() {
|
2009-04-09 03:57:32 +00:00
|
|
|
$this->Jquery->get('#element');
|
|
|
|
$result = $this->Jquery->drop(array(
|
|
|
|
'accept' => '.items',
|
2009-07-25 02:46:33 +00:00
|
|
|
'hover' => 'onHover',
|
2009-04-09 03:57:32 +00:00
|
|
|
'leave' => 'onExit',
|
2009-07-30 02:19:02 +00:00
|
|
|
'drop' => 'onDrop',
|
|
|
|
'wrapCallbacks' => false
|
2009-04-09 03:57:32 +00:00
|
|
|
));
|
|
|
|
$expected = '$("#element").droppable({accept:".items", drop:onDrop, out:onExit, over:onHover});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->drop(array(
|
|
|
|
'accept' => '.items',
|
|
|
|
'hover' => 'onHover',
|
|
|
|
'leave' => 'onExit',
|
|
|
|
'drop' => 'onDrop',
|
|
|
|
));
|
|
|
|
$expected = '$("#element").droppable({accept:".items", drop:function (event, ui) {onDrop}, out:function (event, ui) {onExit}, over:function (event, ui) {onHover}});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-03-30 03:00:39 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-04-24 17:44:52 +00:00
|
|
|
/**
|
|
|
|
* test slider generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testSlider() {
|
|
|
|
$this->Jquery->get('#element');
|
|
|
|
$result = $this->Jquery->slider(array(
|
|
|
|
'complete' => 'onComplete',
|
|
|
|
'change' => 'onChange',
|
|
|
|
'min' => 0,
|
|
|
|
'max' => 10,
|
|
|
|
'value' => 2,
|
2009-07-30 02:19:02 +00:00
|
|
|
'direction' => 'vertical',
|
|
|
|
'wrapCallbacks' => false
|
2009-04-24 17:44:52 +00:00
|
|
|
));
|
|
|
|
$expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-30 02:19:02 +00:00
|
|
|
|
|
|
|
$result = $this->Jquery->slider(array(
|
|
|
|
'complete' => 'onComplete',
|
|
|
|
'change' => 'onChange',
|
|
|
|
'min' => 0,
|
|
|
|
'max' => 10,
|
|
|
|
'value' => 2,
|
|
|
|
'direction' => 'vertical',
|
|
|
|
));
|
|
|
|
$expected = '$("#element").slider({change:function (event, ui) {onChange}, max:10, min:0, orientation:"vertical", stop:function (event, ui) {onComplete}, value:2});';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-04-24 17:44:52 +00:00
|
|
|
}
|
2009-07-30 02:37:41 +00:00
|
|
|
|
2009-07-25 02:46:33 +00:00
|
|
|
/**
|
|
|
|
* test the serializeForm method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testSerializeForm() {
|
|
|
|
$this->Jquery->get('#element');
|
2009-07-25 03:29:44 +00:00
|
|
|
$result = $this->Jquery->serializeForm(array('isForm' => false));
|
2009-07-25 02:46:33 +00:00
|
|
|
$expected = '$("#element").closest("form").serialize();';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
2009-07-25 03:29:44 +00:00
|
|
|
$result = $this->Jquery->serializeForm(array('isForm' => true));
|
2009-07-25 02:46:33 +00:00
|
|
|
$expected = '$("#element").serialize();';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
2009-07-25 03:29:44 +00:00
|
|
|
$result = $this->Jquery->serializeForm(array('isForm' => false, 'inline' => true));
|
|
|
|
$expected = '$("#element").closest("form").serialize()';
|
2009-07-25 02:46:33 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-03-14 00:50:31 +00:00
|
|
|
}
|
|
|
|
?>
|