Skip to content
Permalink
Browse files

Adjust view __scripts to be a protected instance variable, and add te…

…st methods for View addScript tests.
  • Loading branch information...
predominant committed Apr 5, 2010
1 parent 34c7c15 commit 51d71b45411413ebef8ba8df11a4c4a471e50c38
Showing with 22 additions and 11 deletions.
  1. +6 −6 cake/libs/view/view.php
  2. +16 −5 cake/tests/cases/libs/view/view.test.php
@@ -285,9 +285,9 @@ class View extends Object {
* Scripts (and/or other <head /> tags) for the layout
*
* @var array
* @access private
* @access protected
*/
private $__scripts = array();
protected $_scripts = array();
/**
* Holds an array of paths.
@@ -461,7 +461,7 @@ public function renderLayout($content_for_layout, $layout = null) {
$dataForLayout = array_merge($this->viewVars, array(
'content_for_layout' => $content_for_layout,
'scripts_for_layout' => implode("\n\t", $this->__scripts),
'scripts_for_layout' => implode("\n\t", $this->_scripts),
));
if (!isset($dataForLayout['title_for_layout'])) {
@@ -579,11 +579,11 @@ public function getVar($var) {
*/
public function addScript($name, $content = null) {
if (empty($content)) {
if (!in_array($name, array_values($this->__scripts))) {
$this->__scripts[] = $name;
if (!in_array($name, array_values($this->_scripts))) {
$this->_scripts[] = $name;
}
} else {
$this->__scripts[$name] = $content;
$this->_scripts[$name] = $content;
}
}
@@ -162,6 +162,16 @@ function cakeError($method, $messages) {
$error =& new ViewTestErrorHandler($method, $messages);
return $error;
}
/**
* Test only function to return instance scripts.
*
* @return array Scripts
* @access public
*/
function scripts() {
return $this->_scripts;
}
}
/**
@@ -457,12 +467,13 @@ function testUUIDGeneration() {
* @return void
*/
function testAddInlineScripts() {
$this->View->addScript('prototype.js');
$this->View->addScript('prototype.js');
$this->assertEqual($this->View->__scripts, array('prototype.js'));
$View = new TestView($this->Controller);
$View->addScript('prototype.js');
$View->addScript('prototype.js');
$this->assertEqual($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);'));
$View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
$this->assertEqual($View->scripts(), array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'));
}
/**

0 comments on commit 51d71b4

Please sign in to comment.
You can’t perform that action at this time.