From 964b3f2d48e15348c9ae3891017b8428019a10d3 Mon Sep 17 00:00:00 2001
From: Jeremy Thomerson <jeremy@thomersonfamily.com>
Date: Sat, 15 Jun 2013 13:40:33 +0000
Subject: [PATCH] FIX: <% if Link %> wasn't working

Since ViewableData was returning a casting helper for Link, but DataObject was
only using $this->$fieldname to set values on that casting helper, you could
not use <% if Link %> (or <% if $Link %>) in your templates because Link is not
a field, and thus had no value to be set on the casting helper, causing
hasValue to think that there was no value.  Since DataObject->dbObject says that
"it only matches fields and not methods", it seems safe to have it call db(..)
to get the field spec, and not call ViewableData->castingHelper at all.
---
 model/DataObject.php        |  4 ++--
 tests/view/SSViewerTest.php | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/model/DataObject.php b/model/DataObject.php
index 70749387f..0b6109860 100644
--- a/model/DataObject.php
+++ b/model/DataObject.php
@@ -2643,8 +2643,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
 		} else if($fieldName == 'ID') {
 			return new PrimaryKey($fieldName, $this);
 			
-		// General casting information for items in $db or $casting
-		} else if($helper = $this->castingHelper($fieldName)) {
+		// General casting information for items in $db
+		} else if($helper = $this->db($fieldName)) {
 			$obj = Object::create_from_string($helper, $fieldName);
 			$obj->setValue($this->$fieldName, $this->record, false);
 			return $obj;
diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php
index 92b181b28..15003e9d9 100644
--- a/tests/view/SSViewerTest.php
+++ b/tests/view/SSViewerTest.php
@@ -165,6 +165,18 @@ SS;
 			'Permissions template functions result correct result');
 	}
 
+	public function testNonFieldCastingHelpersNotUsedInHasValue() {
+		// check if Link without $ in front of variable
+		$result = $this->render(
+			'A<% if Link %>$Link<% end_if %>B', new SSViewerTest_Object());
+		$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if Link %>');
+
+		// check if Link with $ in front of variable
+		$result = $this->render(
+			'A<% if $Link %>$Link<% end_if %>B', new SSViewerTest_Object());
+		$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if $Link %>');
+	}
+
 	public function testLocalFunctionsTakePriorityOverGlobals() {
 		$data = new ArrayData(array(
 			'Page' => new SSViewerTest_Object()
@@ -1274,6 +1286,11 @@ class SSViewerTest_Object extends DataObject {
 
 	public $number = null;
 
+	private static $casting = array(
+		'Link' => 'Text',
+	);
+
+
 	public function __construct($number = null) {
 		parent::__construct();
 		$this->number = $number;
@@ -1290,6 +1307,10 @@ class SSViewerTest_Object extends DataObject {
 	public function lotsOfArguments11($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k) {
 		return $a. $b. $c. $d. $e. $f. $g. $h. $i. $j. $k;
 	}
+
+	public function Link() {
+		return 'some/url.html';
+	}
 }
 
 class SSViewerTest_GlobalProvider implements TemplateGlobalProvider, TestOnly {