Browse Source

Merge pull request #1456 'fix-for-3515' into 2.4

Conflicts:
	lib/Cake/Test/Case/View/ViewTest.php

Fixes #3515
mark_story 12 years ago
parent
commit
94b602b684
3 changed files with 222 additions and 75 deletions
  1. 214 55
      lib/Cake/Test/Case/View/ViewTest.php
  2. 5 8
      lib/Cake/View/View.php
  3. 3 12
      lib/Cake/View/ViewBlock.php

+ 214 - 55
lib/Cake/Test/Case/View/ViewTest.php

@@ -220,6 +220,27 @@ class TestAfterHelper extends Helper {
 }
 
 /**
+ * Class TestObjectWithToString
+ *
+ * An object with the magic method __toString() for testing with view blocks.
+ */
+class TestObjectWithToString {
+
+	public function __toString() {
+		return "I'm ObjectWithToString";
+	}
+
+}
+
+/**
+ * Class TestObjectWithoutToString
+ *
+ * An object without the magic method __toString() for testing with view blocks.
+ */
+class TestObjectWithoutToString {
+}
+
+/**
  * ViewTest class
  *
  * @package       Cake.Test.Case.View
@@ -282,7 +303,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testGetTemplate method
+ * Test getViewFileName method
  *
  * @return void
  */
@@ -320,7 +341,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testPluginGetTemplate method
+ * Test getLayoutFileName method on plugin
  *
  * @return void
  */
@@ -342,7 +363,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testPluginGetTemplate method
+ * Test getViewFileName method on plugin
  *
  * @return void
  */
@@ -368,7 +389,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that plugin/$plugin_name is only appended to the paths it should be.
+ * Test that plugin/$plugin_name is only appended to the paths it should be.
  *
  * @return void
  */
@@ -396,7 +417,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that CamelCase plugins still find their view files.
+ * Test that CamelCase'd plugins still find their view files.
  *
  * @return void
  */
@@ -423,7 +444,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testGetTemplate method
+ * Test getViewFileName method
  *
  * @return void
  */
@@ -516,7 +537,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testMissingView method
+ * Test for missing views
  *
  * @expectedException MissingViewException
  * @return void
@@ -545,7 +566,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testMissingLayout method
+ * Test for missing layouts
  *
  * @expectedException MissingLayoutException
  * @return void
@@ -572,7 +593,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testViewVars method
+ * Test viewVars method
  *
  * @return void
  */
@@ -581,7 +602,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testUUIDGeneration method
+ * Test generation of UUIDs method
  *
  * @return void
  */
@@ -595,7 +616,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testAddInlineScripts method
+ * Test addInlineScripts method
  *
  * @return void
  */
@@ -610,7 +631,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementExists method
+ * Test elementExists method
  *
  * @return void
  */
@@ -633,7 +654,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElement method
+ * Test element method
  *
  * @return void
  */
@@ -656,7 +677,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementInexistent method
+ * Test elementInexistent method
  *
  * @expectedException PHPUnit_Framework_Error_Notice
  * @return void
@@ -666,7 +687,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementInexistent2 method
+ * Test elementInexistent2 method
  *
  * @expectedException PHPUnit_Framework_Error_Notice
  * @return void
@@ -676,7 +697,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementInexistent3 method
+ * Test elementInexistent3 method
  *
  * @expectedException PHPUnit_Framework_Error_Notice
  * @return void
@@ -686,8 +707,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that elements can have callbacks
- *
+ * Test that elements can have callbacks
  */
 	public function testElementCallbacks() {
 		$this->getMock('Helper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
@@ -702,7 +722,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that additional element viewVars don't get overwritten with helpers.
+ * Test that additional element viewVars don't get overwritten with helpers.
  *
  * @return void
  */
@@ -720,7 +740,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementCacheHelperNoCache method
+ * Test elementCacheHelperNoCache method
  *
  * @return void
  */
@@ -733,7 +753,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testElementCache method
+ * Test elementCache method
  *
  * @return void
  */
@@ -787,7 +807,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test __get allowing access to helpers.
+ * Test __get allowing access to helpers.
  *
  * @return void
  */
@@ -798,7 +818,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that ctp is used as a fallback file extension for elements
+ * Test that ctp is used as a fallback file extension for elements
  *
  * @return void
  */
@@ -813,7 +833,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testLoadHelpers method
+ * Test loadHelpers method
  *
  * @return void
  */
@@ -828,7 +848,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test lazy loading helpers
+ * Test lazy loading helpers
  *
  * @return void
  */
@@ -841,7 +861,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test the correct triggering of helper callbacks
+ * Test the correct triggering of helper callbacks
  *
  * @return void
  */
@@ -924,7 +944,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testBeforeLayout method
+ * Test beforeLayout method
  *
  * @return void
  */
@@ -936,7 +956,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testAfterLayout method
+ * Test afterLayout method
  *
  * @return void
  */
@@ -954,7 +974,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testRenderLoadHelper method
+ * Test renderLoadHelper method
  *
  * @return void
  */
@@ -980,7 +1000,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testRender method
+ * Test render method
  *
  * @return void
  */
@@ -1023,7 +1043,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that View::$view works
+ * Test that View::$view works
  *
  * @return void
  */
@@ -1054,7 +1074,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * test that view vars can replace the local helper variables
+ * Test that view vars can replace the local helper variables
  * and not overwrite the $this->Helper references
  *
  * @return void
@@ -1071,7 +1091,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testGetViewFileName method
+ * Test getViewFileName method
  *
  * @return void
  */
@@ -1099,7 +1119,7 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * testRenderCache method
+ * Test renderCache method
  *
  * @return void
  */
@@ -1323,6 +1343,55 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
+ * Test setting a block's content to null
+ *
+ * @return void
+ * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
+ */
+	public function testBlockSetNull() {
+		$this->View->assign('testWithNull', null);
+		$result = $this->View->fetch('testWithNull');
+		$this->assertSame('', $result);
+	}
+
+/**
+ * Test setting a block's content to an object with __toString magic method
+ * 
+ * @return void
+ */
+	public function testBlockSetObjectWithToString() {
+		$objectWithToString = new TestObjectWithToString();
+		$this->View->assign('testWithObjectWithToString', $objectWithToString);
+		$result = $this->View->fetch('testWithObjectWithToString');
+		$this->assertSame("I'm ObjectWithToString", $result);
+	}
+
+/**
+ * Test setting a block's content to an object without __toString magic method
+ *
+ * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
+ * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
+ *
+ * @expectedException PHPUnit_Framework_Error
+ * @return void
+ */
+	public function testBlockSetObjectWithoutToString() {
+		$objectWithToString = new TestObjectWithoutToString();
+		$this->View->assign('testWithObjectWithoutToString', $objectWithToString);
+	}
+
+/**
+ * Test setting a block's content to a decimal
+ *
+ * @return void
+ */
+	public function testBlockSetDecimal() {
+		$this->View->assign('testWithDecimal', 1.23456789);
+		$result = $this->View->fetch('testWithDecimal');
+		$this->assertEqual('1.23456789', $result);
+	}
+
+/**
  * Test appending to a block with append.
  *
  * @return void
@@ -1336,7 +1405,62 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * Test prepending to a block with append.
+ * Test appending null to a block with append.
+ *
+ * @return void
+ * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
+ */
+	public function testBlockAppendNull() {
+		$this->View->assign('testWithNull', 'Block');
+		$this->View->append('testWithNull', null);
+
+		$result = $this->View->fetch('testWithNull');
+		$this->assertSame('Block', $result);
+	}
+
+/**
+ * Test appending an object with __toString magic method to a block with append.
+ *
+ * @return void
+ */
+	public function testBlockAppendObjectWithToString() {
+		$objectWithToString = new TestObjectWithToString();
+		$this->View->assign('testWithObjectWithToString', 'Block ');
+		$this->View->append('testWithObjectWithToString', $objectWithToString);
+		$result = $this->View->fetch('testWithObjectWithToString');
+		$this->assertSame("Block I'm ObjectWithToString", $result);
+	}
+
+/**
+ * Test appending an object without __toString magic method to a block with append.
+ *
+ * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
+ * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
+ *
+ * @expectedException PHPUnit_Framework_Error
+ * @return void
+ */
+	public function testBlockAppendObjectWithoutToString() {
+		$objectWithToString = new TestObjectWithoutToString();
+		$this->View->assign('testWithObjectWithoutToString', 'Block ');
+		$this->View->append('testWithObjectWithoutToString', $objectWithToString);
+	}
+
+/**
+ * Test appending a decimal to a block with append.
+ *
+ * @return void
+ */
+	public function testBlockAppendDecimal() {
+		$this->View->assign('testWithDecimal', 'Block ');
+		$this->View->append('testWithDecimal', 1.23456789);
+
+		$result = $this->View->fetch('testWithDecimal');
+		$this->assertSame('Block 1.23456789', $result);
+	}
+
+/**
+ * Test prepending to a block with prepend.
  *
  * @return void
  */
@@ -1349,45 +1473,80 @@ class ViewTest extends CakeTestCase {
 	}
 
 /**
- * You should be able to append to undefined blocks.
+ * Test prepending null to a block with prepend.
  *
  * @return void
+ * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  */
-	public function testBlockAppendUndefined() {
-		$this->View->append('test', 'Unknown');
-		$result = $this->View->fetch('test');
-		$this->assertEquals('Unknown', $result);
+	public function testBlockPrependNull() {
+		$this->View->assign('testWithNull', 'Block');
+		$this->View->prepend('testWithNull', null);
+
+		$result = $this->View->fetch('testWithNull');
+		$this->assertSame('Block', $result);
 	}
 
 /**
- * You should be able to prepend to undefined blocks.
+ * Test prepending an object with __toString magic method to a block with prepend.
  *
  * @return void
  */
-	public function testBlockPrependUndefined() {
-		$this->View->prepend('test', 'Unknown');
-		$result = $this->View->fetch('test');
-		$this->assertEquals('Unknown', $result);
+	public function testBlockPrependObjectWithToString() {
+		$objectWithToString = new TestObjectWithToString();
+		$this->View->assign('testWithObjectWithToString', ' Block');
+		$this->View->prepend('testWithObjectWithToString', $objectWithToString);
+		$result = $this->View->fetch('testWithObjectWithToString');
+		$this->assertSame("I'm ObjectWithToString Block", $result);
 	}
 
 /**
- * setting an array should cause an exception.
+ * Test prepending an object without __toString magic method to a block with prepend.
  *
- * @expectedException CakeException
+ * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
+ * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
+ *
+ * @expectedException PHPUnit_Framework_Error
  * @return void
  */
-	public function testBlockSetArrayException() {
-		$this->View->assign('test', array(1, 2, 3));
+	public function testBlockPrependObjectWithoutToString() {
+		$objectWithToString = new TestObjectWithoutToString();
+		$this->View->assign('testWithObjectWithoutToString', 'Block ');
+		$this->View->prepend('testWithObjectWithoutToString', $objectWithToString);
 	}
 
 /**
- * Appending an array should cause an exception.
+ * Test prepending a decimal to a block with prepend.
  *
- * @expectedException CakeException
  * @return void
  */
-	public function testBlockAppendArrayException() {
-		$this->View->append('test', array(1, 2, 3));
+	public function testBlockPrependDecimal() {
+		$this->View->assign('testWithDecimal', ' Block');
+		$this->View->prepend('testWithDecimal', 1.23456789);
+
+		$result = $this->View->fetch('testWithDecimal');
+		$this->assertSame('1.23456789 Block', $result);
+	}
+
+/**
+ * You should be able to append to undefined blocks.
+ *
+ * @return void
+ */
+	public function testBlockAppendUndefined() {
+		$this->View->append('test', 'Unknown');
+		$result = $this->View->fetch('test');
+		$this->assertEquals('Unknown', $result);
+	}
+
+/**
+ * You should be able to prepend to undefined blocks.
+ *
+ * @return void
+ */
+	public function testBlockPrependUndefined() {
+		$this->View->prepend('test', 'Unknown');
+		$result = $this->View->fetch('test');
+		$this->assertEquals('Unknown', $result);
 	}
 
 /**
@@ -1542,7 +1701,7 @@ TEXT;
 	}
 
 /**
- * test memory leaks that existed in _paths at one point.
+ * Test memory leaks that existed in _paths at one point.
  *
  * @return void
  */
@@ -1565,7 +1724,7 @@ TEXT;
 	}
 
 /**
- * tests that a vew block uses default value when not assigned and uses assigned value when it is
+ * Tests that a vew block uses default value when not assigned and uses assigned value when it is
  *
  * @return void
  */

+ 5 - 8
lib/Cake/View/View.php

@@ -448,7 +448,7 @@ class View extends Object {
  * @param string $view Name of view file to use
  * @param string $layout Layout to use.
  * @return string Rendered Element
- * @throws CakeException if there is an error in the view.
+ * @throws CakeException If there is an error in the view.
  */
 	public function render($view = null, $layout = null) {
 		if ($this->hasRendered) {
@@ -628,9 +628,8 @@ class View extends Object {
  * block will create the block.
  *
  * @param string $name Name of the block
- * @param string $value The content for the block.
+ * @param mixed $value The content for the block.
  * @return void
- * @throws CakeException when you use non-string values.
  * @see ViewBlock::concat()
  */
 	public function append($name, $value = null) {
@@ -642,9 +641,8 @@ class View extends Object {
  * block will create the block.
  *
  * @param string $name Name of the block
- * @param string $value The content for the block.
+ * @param mixed $value The content for the block.
  * @return void
- * @throws CakeException when you use non-string values.
  * @see ViewBlock::concat()
  */
 	public function prepend($name, $value = null) {
@@ -656,9 +654,8 @@ class View extends Object {
  * existing content.
  *
  * @param string $name Name of the block
- * @param string $value The content for the block.
+ * @param mixed $value The content for the block.
  * @return void
- * @throws CakeException when you use non-string values.
  * @see ViewBlock::set()
  */
 	public function assign($name, $value) {
@@ -829,7 +826,7 @@ class View extends Object {
  * Magic accessor for deprecated attributes.
  *
  * @param string $name Name of the attribute to set.
- * @param string $value Value of the attribute to set.
+ * @param mixed $value Value of the attribute to set.
  * @return mixed
  */
 	public function __set($name, $value) {

+ 3 - 12
lib/Cake/View/ViewBlock.php

@@ -131,17 +131,13 @@ class ViewBlock {
  * of the new capturing context will be added to the existing block context.
  *
  * @param string $name Name of the block
- * @param string $value The content for the block
+ * @param mixed $value The content for the block
  * @param string $mode If ViewBlock::APPEND content will be appended to existing content.
  *   If ViewBlock::PREPEND it will be prepended.
  * @return void
- * @throws CakeException when you use non-string values.
  */
 	public function concat($name, $value = null, $mode = ViewBlock::APPEND) {
 		if (isset($value)) {
-			if (!is_string($value)) {
-				throw new CakeException(__d('cake_dev', '%s must be a string.', '$value'));
-			}
 			if (!isset($this->_blocks[$name])) {
 				$this->_blocks[$name] = '';
 			}
@@ -166,7 +162,6 @@ class ViewBlock {
  * @param string $name Name of the block
  * @param string $value The content for the block.
  * @return void
- * @throws CakeException when you use non-string values.
  * @deprecated As of 2.3 use ViewBlock::concat() instead.
  */
 	public function append($name, $value = null) {
@@ -178,15 +173,11 @@ class ViewBlock {
  * existing content.
  *
  * @param string $name Name of the block
- * @param string $value The content for the block.
+ * @param mixed $value The content for the block.
  * @return void
- * @throws CakeException when you use non-string values.
  */
 	public function set($name, $value) {
-		if (!is_string($value)) {
-			throw new CakeException(__d('cake_dev', 'Blocks can only contain strings.'));
-		}
-		$this->_blocks[$name] = $value;
+		$this->_blocks[$name] = (string)$value;
 	}
 
 /**