2009-10-11 02:06:58 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* See {@link SSViewerTest->testCastingHelpers()} for more tests related to casting and ViewableData behaviour,
|
2012-02-29 20:35:48 +01:00
|
|
|
* from a template-parsing perspective.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-11 02:06:58 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ViewableDataTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function testRequiresCasting() {
|
|
|
|
$caster = new ViewableDataTest_Castable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertTrue($caster->obj('alwaysCasted') instanceof ViewableDataTest_RequiresCasting);
|
|
|
|
$this->assertTrue($caster->obj('noCastingInformation') instanceof ViewableData_Caster);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertTrue($caster->obj('alwaysCasted', null, false) instanceof ViewableDataTest_RequiresCasting);
|
|
|
|
$this->assertFalse($caster->obj('noCastingInformation', null, false) instanceof ViewableData_Caster);
|
|
|
|
}
|
2010-10-13 05:38:23 +02:00
|
|
|
|
|
|
|
public function testFailoverRequiresCasting() {
|
|
|
|
$caster = new ViewableDataTest_Castable();
|
|
|
|
$container = new ViewableDataTest_Container($caster);
|
|
|
|
|
|
|
|
$this->assertTrue($container->obj('alwaysCasted') instanceof ViewableDataTest_RequiresCasting);
|
|
|
|
$this->assertTrue($caster->obj('alwaysCasted', null, false) instanceof ViewableDataTest_RequiresCasting);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/* @todo This currently fails, because the default_cast static variable is always taken from the topmost
|
|
|
|
* object, not the failover object the field actually came from. Should we fix this, or declare current
|
|
|
|
* behaviour as correct?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* $this->assertTrue($container->obj('noCastingInformation') instanceof ViewableData_Caster);
|
|
|
|
* $this->assertFalse($caster->obj('noCastingInformation', null, false) instanceof ViewableData_Caster);
|
2010-10-13 05:38:23 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function testCastingXMLVal() {
|
|
|
|
$caster = new ViewableDataTest_Castable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertEquals('casted', $caster->XML_val('alwaysCasted'));
|
|
|
|
$this->assertEquals('noCastingInformation', $caster->XML_val('noCastingInformation'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// test automatic escaping is only applied by casted classes
|
|
|
|
$this->assertEquals('<foo>', $caster->XML_val('unsafeXML'));
|
|
|
|
$this->assertEquals('<foo>', $caster->XML_val('castedUnsafeXML'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-15 23:48:24 +02:00
|
|
|
public function testUncastedXMLVal() {
|
|
|
|
$caster = new ViewableDataTest_Castable();
|
|
|
|
$this->assertEquals($caster->XML_val('uncastedZeroValue'), 0);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function testArrayCustomise() {
|
|
|
|
$viewableData = new ViewableDataTest_Castable();
|
|
|
|
$newViewableData = $viewableData->customise(array (
|
|
|
|
'test' => 'overwritten',
|
|
|
|
'alwaysCasted' => 'overwritten'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertEquals('test', $viewableData->XML_val('test'));
|
|
|
|
$this->assertEquals('casted', $viewableData->XML_val('alwaysCasted'));
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertEquals('overwritten', $newViewableData->XML_val('test'));
|
|
|
|
$this->assertEquals('overwritten', $newViewableData->XML_val('alwaysCasted'));
|
2012-02-09 19:57:27 +01:00
|
|
|
|
|
|
|
$this->assertEquals('castable', $viewableData->forTemplate());
|
|
|
|
$this->assertEquals('castable', $newViewableData->forTemplate());
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function testObjectCustomise() {
|
|
|
|
$viewableData = new ViewableDataTest_Castable();
|
|
|
|
$newViewableData = $viewableData->customise(new ViewableDataTest_RequiresCasting());
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertEquals('test', $viewableData->XML_val('test'));
|
|
|
|
$this->assertEquals('casted', $viewableData->XML_val('alwaysCasted'));
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->assertEquals('overwritten', $newViewableData->XML_val('test'));
|
|
|
|
$this->assertEquals('casted', $newViewableData->XML_val('alwaysCasted'));
|
2012-02-09 19:57:27 +01:00
|
|
|
|
|
|
|
$this->assertEquals('castable', $viewableData->forTemplate());
|
|
|
|
$this->assertEquals('casted', $newViewableData->forTemplate());
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2013-06-21 18:19:06 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2010-10-13 02:56:04 +02:00
|
|
|
public function testRAWVal() {
|
|
|
|
$data = new ViewableDataTest_Castable();
|
|
|
|
$data->test = 'This & This';
|
|
|
|
$this->assertEquals($data->RAW_val('test'), 'This & This');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 02:56:04 +02:00
|
|
|
public function testSQLVal() {
|
|
|
|
$data = new ViewableDataTest_Castable();
|
|
|
|
$this->assertEquals($data->SQL_val('test'), 'test');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testJSVal() {
|
|
|
|
$data = new ViewableDataTest_Castable();
|
|
|
|
$data->test = '"this is a test"';
|
|
|
|
$this->assertEquals($data->JS_val('test'), '\"this is a test\"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testATTVal() {
|
|
|
|
$data = new ViewableDataTest_Castable();
|
|
|
|
$data->test = '"this is a test"';
|
|
|
|
$this->assertEquals($data->ATT_val('test'), '"this is a test"');
|
|
|
|
}
|
|
|
|
|
2010-10-15 01:56:11 +02:00
|
|
|
public function testCastingClass() {
|
|
|
|
$expected = array(
|
|
|
|
'NonExistant' => null,
|
|
|
|
'Field' => 'CastingType',
|
|
|
|
'Argument' => 'ArgumentType',
|
|
|
|
'ArrayArgument' => 'ArrayArgumentType'
|
|
|
|
);
|
|
|
|
$obj = new ViewableDataTest_CastingClass();
|
|
|
|
|
|
|
|
foreach($expected as $field => $class) {
|
|
|
|
$this->assertEquals(
|
|
|
|
$class,
|
|
|
|
$obj->castingClass($field),
|
|
|
|
"castingClass() returns correct results for ::\$$field"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 00:47:47 +02:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
class ViewableDataTest_Castable extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_cast = 'ViewableData_Caster';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array (
|
2009-10-11 02:06:58 +02:00
|
|
|
'alwaysCasted' => 'ViewableDataTest_RequiresCasting',
|
|
|
|
'castedUnsafeXML' => 'ViewableData_UnescaptedCaster'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public $test = 'test';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-15 23:48:24 +02:00
|
|
|
public $uncastedZeroValue = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function alwaysCasted() {
|
|
|
|
return 'alwaysCasted';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function noCastingInformation() {
|
|
|
|
return 'noCastingInformation';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function unsafeXML() {
|
|
|
|
return '<foo>';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function castedUnsafeXML() {
|
|
|
|
return $this->unsafeXML();
|
|
|
|
}
|
2012-02-09 19:57:27 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
public function forTemplate() {
|
|
|
|
return 'castable';
|
|
|
|
}
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ViewableDataTest_RequiresCasting extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public $test = 'overwritten';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function forTemplate() {
|
|
|
|
return 'casted';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function setValue() {}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ViewableData_UnescaptedCaster extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
protected $value;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function setValue($value) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function forTemplate() {
|
|
|
|
return Convert::raw2xml($this->value);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ViewableData_Caster extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function forTemplate() {
|
|
|
|
return 'casted';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function setValue() {}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
2010-10-13 05:38:23 +02:00
|
|
|
class ViewableDataTest_Container extends ViewableData {
|
|
|
|
|
|
|
|
public function __construct($failover) {
|
|
|
|
$this->failover = $failover;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-15 01:56:11 +02:00
|
|
|
class ViewableDataTest_CastingClass extends ViewableData {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2010-10-15 01:56:11 +02:00
|
|
|
'Field' => 'CastingType',
|
|
|
|
'Argument' => 'ArgumentType(Argument)',
|
|
|
|
'ArrayArgument' => 'ArrayArgumentType(array(foo, bar))'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-19 00:47:47 +02:00
|
|
|
class ViewableDataTest_NoCastingInformation extends ViewableData {
|
|
|
|
public function noCastingInformation() {
|
|
|
|
return "No casting information";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
/**#@-*/
|