From 91e47a6379b2c4ca8f353b0fa1ac0329bc38840e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 12 Mar 2009 22:59:55 -0400 Subject: [PATCH] Adding scriptBlock() to HtmlHelper. Adding tests. --- cake/libs/view/helpers/html.php | 21 +++++++ .../cases/libs/view/helpers/html.test.php | 63 ++++++++++++++++++- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 04905fe6be9..c9bf16e1e3b 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -453,6 +453,27 @@ function script($url, $inline = true, $once = true) { $view->addScript($out); } } +/** + * Wrap $script in a script tag. + * + * @param string $script The script to wrap + * @param array $options The options to use. + * @return mixed string or null + **/ + function scriptBlock($script, $options = array()) { + $defaultOptions = array('safe' => true, 'inline' => true); + $options = array_merge($defaultOptions, $options); + if ($options['safe']) { + $script = "\n" . '//' . "\n"; + } + if ($options['inline']) { + return sprintf($this->tags['javascriptblock'], $script); + } else { + $view =& ClassRegistry::getObject('view'); + $view->addScript(sprintf($this->tags['javascriptblock'], $script)); + return null; + } + } /** * Builds CSS style data from an array of CSS properties * diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index ec97ee954a0..3d6a36b5fad 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -48,6 +48,9 @@ class TheHtmlTestController extends Controller { */ var $uses = null; } + +Mock::generate('View', 'HtmlHelperMockView'); + /** * HtmlHelperTest class * @@ -55,6 +58,18 @@ class TheHtmlTestController extends Controller { * @subpackage cake.tests.cases.libs.view.helpers */ class HtmlHelperTest extends CakeTestCase { +/** + * Regexp for CDATA start block + * + * @var string + */ + var $cDataStart = 'preg:/^\/\/[\s\r\n]*/'; /** * html property * @@ -68,7 +83,7 @@ class HtmlHelperTest extends CakeTestCase { * @access public * @return void */ - function setUp() { + function startTest() { $this->Html =& new HtmlHelper(); $view =& new View(new TheHtmlTestController()); ClassRegistry::addObject('view', $view); @@ -389,6 +404,47 @@ function testScript() { $result = $this->Html->script('foo', true, false); $this->assertNotNull($result); } +/** + * test Script block generation + * + * @return void + **/ + function testScriptBlock() { + $result = $this->Html->scriptBlock('window.foo = 2;'); + $expected = array( + 'script' => array('type' => 'text/javascript'), + $this->cDataStart, + 'window.foo = 2;', + $this->cDataEnd, + '/script', + ); + $this->assertTags($result, $expected); + + $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false)); + $expected = array( + 'script' => array('type' => 'text/javascript'), + 'window.foo = 2;', + '/script', + ); + $this->assertTags($result, $expected); + + $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true)); + $expected = array( + 'script' => array('type' => 'text/javascript'), + $this->cDataStart, + 'window.foo = 2;', + $this->cDataEnd, + '/script', + ); + $this->assertTags($result, $expected); + + $view =& ClassRegistry::getObject('view'); + $view =& new HtmlHelperMockView(); + $view->expectAt(0, 'addScript', array(new PatternExpectation('/window\.foo\s\=\s2;/'))); + + $result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false)); + $this->assertNull($result); + } /** * testCharsetTag method * @@ -936,12 +992,13 @@ function testPara() { $this->assertTags($result, array('p' => array('class' => 'class-name'), '<text>', '/p')); } /** - * tearDown method + * endTest method * * @access public * @return void */ - function tearDown() { + function endTest() { + ClassRegistry::flush(); unset($this->Html); } }