* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource * @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 * @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 */ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } uses( 'view' . DS . 'helpers' . DS . 'app_helper', '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' ); /** * AjaxTestController class * * @package cake * @subpackage cake.tests.cases.libs.view.helpers */ class AjaxTestController extends Controller { /** * name property * * @var string 'AjaxTest' * @access public */ var $name = 'AjaxTest'; /** * uses property * * @var mixed null * @access public */ var $uses = null; } /** * PostAjaxTest class * * @package cake * @subpackage cake.tests.cases.libs.view.helpers */ class PostAjaxTest extends Model { /** * primaryKey property * * @var string 'id' * @access public */ var $primaryKey = 'id'; /** * useTable property * * @var bool false * @access public */ var $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 */ var $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 */ var $cDataStart = '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()); } /** * 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('/^]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result); $this->assertPattern('/