From a142ffd46527a45d86ad6223d8ad4303475456d0 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Mon, 17 Nov 2014 15:44:17 +0000 Subject: [PATCH] FIX: VirtualPages use correct casting for 'virtual' database fields --- code/model/VirtualPage.php | 16 ++++++++++++++++ tests/model/VirtualPageTest.php | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/code/model/VirtualPage.php b/code/model/VirtualPage.php index 460e66f9..e7998f12 100644 --- a/code/model/VirtualPage.php +++ b/code/model/VirtualPage.php @@ -450,6 +450,22 @@ class VirtualPage extends Page { if(parent::hasMethod($method)) return true; return $this->copyContentFrom()->hasMethod($method); } + + /** + * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field + * on this object. + * + * @param string $field + * @return string + */ + public function castingHelper($field) { + if($this->copyContentFrom()) { + return $this->copyContentFrom()->castingHelper($field); + } else { + return parent::castingHelper($field); + } + } + } /** diff --git a/tests/model/VirtualPageTest.php b/tests/model/VirtualPageTest.php index aa6bd301..52f9273f 100644 --- a/tests/model/VirtualPageTest.php +++ b/tests/model/VirtualPageTest.php @@ -591,6 +591,19 @@ class VirtualPageTest extends SapphireTest { 'No field copying from previous original after page type changed' ); } + + public function testVirtualPageFindsCorrectCasting() { + $page = new VirtualPageTest_ClassA(); + $page->CastingTest = "Some content"; + $page->write(); + $virtual = new VirtualPage(); + $virtual->CopyContentFromID = $page->ID; + $virtual->write(); + + $this->assertEquals('VirtualPageTest_TestDBField', $virtual->castingHelper('CastingTest')); + $this->assertEquals('SOME CONTENT', $virtual->obj('CastingTest')->forTemplate()); + } + } class VirtualPageTest_ClassA extends Page implements TestOnly { @@ -599,6 +612,7 @@ class VirtualPageTest_ClassA extends Page implements TestOnly { 'MyInitiallyCopiedField' => 'Text', 'MyVirtualField' => 'Text', 'MyNonVirtualField' => 'Text', + 'CastingTest' => 'VirtualPageTest_TestDBField' ); private static $allowed_children = array('VirtualPageTest_ClassB'); @@ -616,6 +630,12 @@ class VirtualPageTest_NotRoot extends Page implements TestOnly { private static $can_be_root = false; } +class VirtualPageTest_TestDBField extends Varchar implements TestOnly { + public function forTemplate() { + return strtoupper($this->XML()); + } +} + class VirtualPageTest_VirtualPageSub extends VirtualPage implements TestOnly { private static $db = array( 'MyProperty' => 'Varchar',