2007-02-03 22:20:13 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @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
|
|
|
|
*/
|
2007-06-23 07:44:39 +00:00
|
|
|
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
|
|
|
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
|
|
|
|
}
|
2007-03-16 23:54:24 +00:00
|
|
|
|
2007-07-08 01:17:57 +00:00
|
|
|
require_once CAKE.'app_helper.php';
|
|
|
|
uses('controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'ajax',
|
|
|
|
'view'.DS.'helpers'.DS.'html', 'view'.DS.'helpers'.DS.'form', 'view'.DS.'helpers'.DS.'javascript');
|
2007-03-16 23:54:24 +00:00
|
|
|
|
2007-06-23 07:44:39 +00:00
|
|
|
class AjaxTestController extends Controller {
|
|
|
|
var $name = 'AjaxTest';
|
|
|
|
var $uses = null;
|
|
|
|
}
|
2007-06-21 16:24:13 +00:00
|
|
|
|
2007-06-23 07:44:39 +00:00
|
|
|
class PostAjaxTest extends Model {
|
|
|
|
var $primaryKey = 'id';
|
|
|
|
var $useTable = false;
|
2007-06-21 16:24:13 +00:00
|
|
|
|
2007-06-23 07:44:39 +00:00
|
|
|
function loadInfo() {
|
|
|
|
return new Set(array(
|
|
|
|
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
|
|
|
|
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
|
|
|
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
|
|
|
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)));
|
2007-03-16 23:54:24 +00:00
|
|
|
}
|
2007-06-23 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
2007-03-16 23:54:24 +00:00
|
|
|
|
2007-02-03 22:20:13 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs.view.helpers
|
|
|
|
*/
|
|
|
|
class AjaxTest extends UnitTestCase {
|
|
|
|
|
|
|
|
function setUp() {
|
2007-06-23 08:40:52 +00:00
|
|
|
Router::reload();
|
2007-03-16 23:54:24 +00:00
|
|
|
$this->Ajax = new AjaxHelper();
|
|
|
|
$this->Ajax->Html = new HtmlHelper();
|
|
|
|
$this->Ajax->Form = new FormHelper();
|
|
|
|
$this->Ajax->Javascript = new JavascriptHelper();
|
|
|
|
$this->Ajax->Form->Html =& $this->Ajax->Html;
|
2007-06-23 07:44:39 +00:00
|
|
|
$view = new View(new AjaxTestController());
|
2007-03-16 23:54:24 +00:00
|
|
|
ClassRegistry::addObject('view', $view);
|
2007-06-23 07:44:39 +00:00
|
|
|
ClassRegistry::addObject('PostAjaxTest', new PostAjaxTest());
|
2007-02-03 22:20:13 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 14:23:36 +00:00
|
|
|
function testEvalScripts() {
|
2007-03-16 23:54:24 +00:00
|
|
|
$result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content', 'evalScripts' => false));
|
2007-06-20 07:51:52 +00:00
|
|
|
$expected = '<a href="/" id="link1" onclick=" return false;">Test Link</a><script type="text/javascript">Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'/\', {asynchronous:true, evalScripts:false, requestHeaders:[\'X-Update\', \'content\']}) }, false);</script>';
|
2007-02-06 14:23:36 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2007-02-06 14:28:04 +00:00
|
|
|
|
2007-03-16 23:54:24 +00:00
|
|
|
$result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content'));
|
2007-06-20 07:51:52 +00:00
|
|
|
$expected = '<a href="/" id="link1" onclick=" return false;">Test Link</a><script type="text/javascript">Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'/\', {asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'content\']}) }, false);</script>';
|
2007-02-06 14:28:04 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2007-02-06 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 22:16:35 +00:00
|
|
|
function testAutoComplete() {
|
2007-06-23 07:44:39 +00:00
|
|
|
$result = $this->Ajax->autoComplete('PostAjaxTest/title' , '/posts', array('minChars' => 2));
|
2007-06-21 16:24:13 +00:00
|
|
|
|
2007-06-23 07:44:39 +00:00
|
|
|
$this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
|
|
|
|
$this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
|
2007-03-16 23:54:24 +00:00
|
|
|
$this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
|
2007-06-23 07:44:39 +00:00
|
|
|
$this->assertPattern('/<\/div>\s+<script type="text\/javascript">new Ajax\.Autocompleter\(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'\/posts\',/', $result);
|
2007-06-20 07:51:52 +00:00
|
|
|
$this->assertPattern('/<script(.*)>(.*) {minChars:2}\);/', $result);
|
2007-03-16 23:54:24 +00:00
|
|
|
$this->assertPattern('/<\/script>$/', $result);
|
2007-02-08 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 14:23:36 +00:00
|
|
|
function testAsynchronous() {
|
2007-03-16 23:54:24 +00:00
|
|
|
$result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content', 'type' => 'synchronous'));
|
2007-06-20 07:51:52 +00:00
|
|
|
$expected = '<a href="/" id="link1" onclick=" return false;">Test Link</a><script type="text/javascript">Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'/\', {asynchronous:false, evalScripts:true, requestHeaders:[\'X-Update\', \'content\']}) }, false);</script>';
|
2007-02-06 14:23:36 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
|
2007-02-03 22:20:13 +00:00
|
|
|
function testDraggable() {
|
2007-03-16 23:54:24 +00:00
|
|
|
$result = $this->Ajax->drag('id', array('handle' => 'other_id'));
|
2007-02-03 22:20:13 +00:00
|
|
|
$expected = '<script type="text/javascript">new Draggable(\'id\', {handle:\'other_id\'});</script>';
|
2007-02-04 09:06:44 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2007-02-03 22:20:13 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 22:16:35 +00:00
|
|
|
function testDroppable() {
|
2007-03-16 23:54:24 +00:00
|
|
|
$result = $this->Ajax->drop('droppable', array('accept' => 'crap'));
|
2007-02-08 22:16:35 +00:00
|
|
|
$expected = '<script type="text/javascript">Droppables.add(\'droppable\', {accept:\'crap\'});</script>';
|
|
|
|
$this->assertEqual($result, $expected);
|
2007-03-16 23:54:24 +00:00
|
|
|
|
|
|
|
$result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts'));
|
2007-06-20 07:51:52 +00:00
|
|
|
$expected = '<script type="text/javascript">Droppables.add(\'droppable\', {accept:\'crap\', onDrop:function(element, droppable) {new Ajax.Request(\'/posts\', {asynchronous:true, evalScripts:true})}});</script>';
|
2007-03-16 23:54:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2007-02-08 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 16:24:13 +00:00
|
|
|
function testSortable() {
|
|
|
|
$result = $this->Ajax->sortable('ull', array('constraint'=>false,'ghosting'=>true));
|
|
|
|
$expected = '<script type="text/javascript">Sortable.create(\'ull\', {constraint:false, ghosting:true});</script>';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Ajax->sortable('ull', array('constraint'=>'false','ghosting'=>'true'));
|
|
|
|
$expected = '<script type="text/javascript">Sortable.create(\'ull\', {constraint:false, ghosting:true});</script>';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
|
|
|
|
2007-03-29 02:16:00 +00:00
|
|
|
function testSubmitWithIndicator() {
|
|
|
|
$result = $this->Ajax->submit('Add', array('div' => false, 'url' => "/controller/action", 'indicator' => 'loading', 'loading' => "doSomething()", 'complete' => 'doSomethingElse() '));
|
2007-06-20 07:51:52 +00:00
|
|
|
$this->assertPattern('/onLoading:function\(request\) {doSomething\(\);\s+Element.show\(\'loading\'\);}/', $result);
|
|
|
|
$this->assertPattern('/onComplete:function\(request, json\) {doSomethingElse\(\) ;\s+Element.hide\(\'loading\'\);}/', $result);
|
2007-03-29 02:16:00 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 22:20:13 +00:00
|
|
|
function tearDown() {
|
2007-03-16 23:54:24 +00:00
|
|
|
unset($this->Ajax);
|
2007-02-03 22:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|