From 5e0de9b90e65c59122de13e04768d6e57d4ad8f6 Mon Sep 17 00:00:00 2001 From: nate Date: Thu, 22 Mar 2007 00:37:45 +0000 Subject: [PATCH] Fixing unique script check in View::addScript() (Ticket #2262), and adding View test case git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4645 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/view.php | 11 ++-- cake/tests/cases/libs/view/view.test.php | 84 ++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 cake/tests/cases/libs/view/view.test.php diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index 35338fa86..3e2b8d251 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -535,8 +535,8 @@ class View extends Object { * @access public */ function addScript($name, $content = null) { - if ($content == null) { - if (!in_array($content, array_values($this->__scripts))) { + if (empty($content)) { + if (!in_array($name, array_values($this->__scripts))) { $this->__scripts[] = $name; } } else { @@ -839,9 +839,10 @@ class View extends Object { if (is_null($plugin) || !loadPluginHelper($plugin, $helper)) { if (!loadHelper($helper)) { $this->cakeError('missingHelperFile', array(array( - 'helper' => $helper, - 'file' => Inflector::underscore($helper) . '.php', - 'base' => $this->base))); + 'helper' => $helper, + 'file' => Inflector::underscore($helper) . '.php', + 'base' => $this->base + ))); exit(); } } diff --git a/cake/tests/cases/libs/view/view.test.php b/cake/tests/cases/libs/view/view.test.php new file mode 100644 index 000000000..d547e44e0 --- /dev/null +++ b/cake/tests/cases/libs/view/view.test.php @@ -0,0 +1,84 @@ + + * 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 + * @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 + */ + require_once LIBS.'view'.DS.'view.php'; + require_once LIBS.'controller'.DS.'controller.php'; + + class PostsController extends Controller { + var $name = 'Posts'; + function index() { + $this->set('testData', 'Some test data'); + $test2 = 'more data'; + $test3 = 'even more data'; + $this->set(compact('test2', 'test3')); + } + } + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs + */ +class ViewTest extends UnitTestCase { + + function setUp() { + $this->PostsController = new PostsController(); + $this->PostsController->index(); + $this->view = new View($this->PostsController); + } + + function testViewVars() { + $this->assertTrue($this->view->viewVars, array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data')); + } + + function testUUIDGeneration() { + $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $this->assertEqual($result, 'form5988016017'); + $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $this->assertEqual($result, 'formc3dc6be854'); + $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $this->assertEqual($result, 'form28f92cc87f'); + } + + function testAddInlineScripts() { + $this->view->addScript('prototype.js'); + $this->view->addScript('prototype.js'); + $this->assertEqual($this->view->__scripts, array('prototype.js')); + + $this->view->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);'); + $this->assertEqual($this->view->__scripts, array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);')); + } + + function tearDown() { + unset($this->view); + unset($this->PostsController); + } +} + +?> \ No newline at end of file