Skip to content

Commit

Permalink
Merge pull request #2137 from jthomerson/pulls/fix_viewable_data_wrap…
Browse files Browse the repository at this point in the history
…ped_value

FIX: ViewableData wasn't setting values when using default cast
  • Loading branch information
chillu committed Jul 2, 2013
2 parents 21844a8 + e6bfabf commit 429bbc5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 41 additions & 0 deletions tests/view/ViewableDataTest.php
Expand Up @@ -81,6 +81,19 @@ public function testObjectCustomise() {
$this->assertEquals('casted', $newViewableData->forTemplate());
}

public function testDefaultValueWrapping() {
$data = new ArrayData(array('Title' => 'SomeTitleValue'));
// this results in a cached raw string in ViewableData:
$this->assertTrue($data->hasValue('Title'));
$this->assertFalse($data->hasValue('SomethingElse'));
// this should cast the raw string to a StringField since we are
// passing true as the third argument:
$obj = $data->obj('Title', null, true);
$this->assertTrue(is_object($obj));
// and the string field should have the value of the raw string:
$this->assertEquals('SomeTitleValue', $obj->forTemplate());
}

public function testRAWVal() {
$data = new ViewableDataTest_Castable();
$data->test = 'This & This';
Expand Down Expand Up @@ -121,6 +134,28 @@ public function testCastingClass() {
);
}
}

public function testObjWithCachedStringValueReturnsValidObject() {
$obj = new ViewableDataTest_NoCastingInformation();

// Save a literal string into cache
$cache = true;
$uncastedData = $obj->obj('noCastingInformation', null, false, $cache);

// Fetch the cached string as an object
$forceReturnedObject = true;
$castedData = $obj->obj('noCastingInformation', null, $forceReturnedObject);

// Uncasted data should always be the nonempty string
$this->assertNotEmpty($uncastedData, 'Uncasted data was empty.');
$this->assertTrue(is_string($uncastedData), 'Uncasted data should be a string.');

// Casted data should be the string wrapped in a DBField-object.
$this->assertNotEmpty($castedData, 'Casted data was empty.');
$this->assertInstanceOf('DBField', $castedData, 'Casted data should be instance of DBField.');

$this->assertEquals($uncastedData, $castedData->getValue(), 'Casted and uncasted strings are not equal.');
}
}

/**#@+
Expand Down Expand Up @@ -212,4 +247,10 @@ class ViewableDataTest_CastingClass extends ViewableData {
);
}

class ViewableDataTest_NoCastingInformation extends ViewableData {
public function noCastingInformation() {
return "No casting information";
}
}

/**#@-*/
4 changes: 3 additions & 1 deletion view/ViewableData.php
Expand Up @@ -383,7 +383,9 @@ public function obj($fieldName, $arguments = null, $forceReturnedObject = true,

if(!is_object($value) && $forceReturnedObject) {
$default = Config::inst()->get('ViewableData', 'default_cast', Config::FIRST_SET);
$value = new $default($fieldName);
$castedValue = new $default($fieldName);
$castedValue->setValue($value);
$value = $castedValue;
}

return $value;
Expand Down

0 comments on commit 429bbc5

Please sign in to comment.