diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index b86950923..cc647dfab 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -363,6 +363,65 @@ after') function assertEqualIgnoringWhitespace($a, $b) { $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b)); } + + /** + * See {@link ViewableDataTest} for more extensive casting tests, + * this test just ensures that basic casting is correctly applied during template parsing. + */ + function testCastingHelpers() { + $vd = new SSViewerTest_ViewableData(); + $vd->TextValue = 'html'; + $vd->HTMLValue = 'html'; + $vd->UncastedValue = 'html'; + + // Value casted as "Text" + $this->assertEquals( + '<b>html</b>', + $t = SSViewer::fromString('$TextValue')->process($vd) + ); + $this->assertEquals( + 'html', + $t = SSViewer::fromString('$TextValue.RAW')->process($vd) + ); + $this->assertEquals( + '<b>html</b>', + $t = SSViewer::fromString('$TextValue.XML')->process($vd) + ); + + // Value casted as "HTMLText" + $this->assertEquals( + 'html', + $t = SSViewer::fromString('$HTMLValue')->process($vd) + ); + $this->assertEquals( + 'html', + $t = SSViewer::fromString('$HTMLValue.RAW')->process($vd) + ); + $this->assertEquals( + '<b>html</b>', + $t = SSViewer::fromString('$HTMLValue.XML')->process($vd) + ); + + // Uncasted value (falls back to ViewableData::$default_cast="HTMLText") + $vd = new SSViewerTest_ViewableData(); // TODO Fix caching + $vd->UncastedValue = 'html'; + $this->assertEquals( + 'html', + $t = SSViewer::fromString('$UncastedValue')->process($vd) + ); + $vd = new SSViewerTest_ViewableData(); // TODO Fix caching + $vd->UncastedValue = 'html'; + $this->assertEquals( + 'html', + $t = SSViewer::fromString('$UncastedValue.RAW')->process($vd) + ); + $vd = new SSViewerTest_ViewableData(); // TODO Fix caching + $vd->UncastedValue = 'html'; + $this->assertEquals( + '<b>html</b>', + $t = SSViewer::fromString('$UncastedValue.XML')->process($vd) + ); + } /** * Test $Up works when the scope $Up refers to was entered with a "with" block @@ -677,6 +736,12 @@ class SSViewerTestFixture extends ViewableData { } class SSViewerTest_ViewableData extends ViewableData implements TestOnly { + + public static $casting = array( + 'TextValue' => 'Text', + 'HTMLValue' => 'HTMLText' + ); + function methodWithOneArgument($arg1) { return "arg1:{$arg1}"; } diff --git a/tests/view/ViewableDataTest.php b/tests/view/ViewableDataTest.php index dfc9b1850..c011280ce 100644 --- a/tests/view/ViewableDataTest.php +++ b/tests/view/ViewableDataTest.php @@ -1,5 +1,8 @@ testCastingHelpers()} for more tests related to casting and ViewableData behaviour, + * from a template-parsing perspective. + * * @package sapphire * @subpackage tests */