* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests * @package cake * @subpackage cake.tests.cases.libs.view.helpers * @since CakePHP(tm) v 1.2.0.4206 * @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); } App::import('Helper', array('Html', 'Form', 'Javascript', 'Ajax')); /** * 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()); $this->Ajax->Form->params = array( 'plugin' => null, 'action' => 'view', 'controller' => 'users' ); } /** * 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('/]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result); $this->assertPattern('/]+class="auto_complete"[^<>]*><\/div>/', $result); $this->assertPattern('/<\/div>\s+