Skip to content
Browse files

Adding test coverage to `\template\Helper`, refactoring to improve te…

…stability.
  • Loading branch information...
1 parent b6a2d05 commit 76771f00f021f5f829893a849c8ee8995ef9cd39 @nateabele nateabele committed Oct 30, 2010
Showing with 47 additions and 8 deletions.
  1. +8 −4 libraries/lithium/template/Helper.php
  2. +39 −4 libraries/lithium/tests/cases/template/HelperTest.php
View
12 libraries/lithium/template/Helper.php
@@ -129,10 +129,14 @@ protected function _options(array $defaults, array $scope) {
* @return string Rendered HTML
*/
protected function _render($method, $string, $params, array $options = array()) {
- foreach ($params as $key => $value) {
- $params[$key] = $this->_context->applyHandler($this, $method, $key, $value, $options);
+ $strings = $this->_strings;
+
+ if ($this->_context) {
+ foreach ($params as $key => $value) {
+ $params[$key] = $this->_context->applyHandler($this, $method, $key, $value, $options);
+ }
+ $strings = $this->_context->strings();
}
- $strings = $this->_context ? $this->_context->strings() : $this->_strings;
return String::insert(isset($strings[$string]) ? $strings[$string] : $string, $params);
}
@@ -146,7 +150,7 @@ protected function _render($method, $string, $params, array $options = array())
*/
protected function _attributes($params, $method = null, array $options = array()) {
if (!is_array($params)) {
- return empty($params) ? '' : ' ' . $params;
+ return !$params ? '' : ' ' . $params;
}
$defaults = array('escape' => true, 'prepend' => ' ', 'append' => '');
$options += $defaults;
View
43 libraries/lithium/tests/cases/template/HelperTest.php
@@ -8,10 +8,10 @@
namespace lithium\tests\cases\template;
-use \stdClass;
-use \lithium\template\Helper;
-use \lithium\tests\mocks\template\MockHelper;
-use \lithium\tests\mocks\template\MockRenderer;
+use stdClass;
+use lithium\template\Helper;
+use lithium\tests\mocks\template\MockHelper;
+use lithium\tests\mocks\template\MockRenderer;
class HelperTest extends \lithium\test\Unit {
@@ -51,6 +51,15 @@ public function testDefaultEscaping() {
));
$expected = '<script>//alert("XSS!");</script>';
$this->assertEqual($expected, $result);
+
+ $result = $this->helper->escape(array(
+ '<script>alert("XSS!");</script>', '<script>alert("XSS!");</script>'
+ ));
+ $expected = array(
+ '&lt;script&gt;alert(&quot;XSS!&quot;);&lt;/script&gt;',
+ '&lt;script&gt;alert(&quot;XSS!&quot;);&lt;/script&gt;'
+ );
+ $this->assertEqual($expected, $result);
}
/**
@@ -82,10 +91,30 @@ public function testAttributes() {
$result = $this->helper->testAttributes($attributes);
$this->assertEqual($expected, $result);
+ $attributes = ' value="1" title="one"';
+ $result = $this->helper->testAttributes('value="1" title="one"');
+ $this->assertEqual($expected, $result);
+
$attributes = array('checked' => true, 'title' => 'one');
$expected = ' checked="checked" title="one"';
$result = $this->helper->testAttributes($attributes);
$this->assertEqual($expected, $result);
+
+ $attributes = array('checked' => false);
+ $result = $this->helper->testAttributes($attributes);
+ $this->assertEqual(' ', $result);
+ }
+
+ public function testAttributeEscaping() {
+ $attributes = array('checked' => true, 'title' => '<foo>');
+ $expected = ' checked="checked" title="&lt;foo&gt;"';
+ $result = $this->helper->testAttributes($attributes);
+ $this->assertEqual($expected, $result);
+
+ $attributes = array('checked' => true, 'title' => '<foo>');
+ $expected = ' checked="checked" title="<foo>"';
+ $result = $this->helper->testAttributes($attributes, null, array('escape' => false));
+ $this->assertEqual($expected, $result);
}
public function testAttributeMinimization() {
@@ -105,6 +134,12 @@ public function testAttributeMinimization() {
$this->assertEqual($expected, $result);
}
+ public function testInstantiationWithNoContext() {
+ $this->helper = new MockHelper();
+ $result = $this->helper->testRender(null, "foo {:bar}", array('bar' => 'baz'));
+ $this->assertEqual("foo baz", $result);
+ }
+
public function testRender() {
$params = array(
'context' => new MockRenderer(),

0 comments on commit 76771f0

Please sign in to comment.
Something went wrong with that request. Please try again.