diff --git a/dev/FixtureBlueprint.php b/dev/FixtureBlueprint.php index 532c16b56..265623bbe 100644 --- a/dev/FixtureBlueprint.php +++ b/dev/FixtureBlueprint.php @@ -183,14 +183,8 @@ class FixtureBlueprint { // If LastEdited was set in the fixture, set it here if($data && array_key_exists('LastEdited', $data)) { - $edited = $this->parseValue($data['LastEdited'], $fixtures); - DB::manipulate(array( - $class => array( - "command" => "update", "id" => $obj->id, - "fields" => array("LastEdited" => "'".$edited."'") - ) - )); - } + $this->overrideField($obj, 'LastEdited', $data['LastEdited'], $fixtures); + } } catch(Exception $e) { Config::inst()->update('DataObject', 'validation_enabled', $validationenabled); throw $e; @@ -288,4 +282,17 @@ class FixtureBlueprint { $obj->$name = $this->parseValue($value, $fixtures); } + protected function overrideField($obj, $fieldName, $value, $fixtures = null) { + $table = ClassInfo::table_for_object_field(get_class($obj), $fieldName); + $value = $this->parseValue($value, $fixtures); + + DB::manipulate(array( + $table => array( + "command" => "update", "id" => $obj->ID, + "fields" => array($fieldName => is_string($value) ? "'$value'" : $value) + ) + )); + $obj->$fieldName = $value; + } + } \ No newline at end of file diff --git a/tests/dev/FixtureBlueprintTest.php b/tests/dev/FixtureBlueprintTest.php index 591ab6e36..358a50faf 100644 --- a/tests/dev/FixtureBlueprintTest.php +++ b/tests/dev/FixtureBlueprintTest.php @@ -9,7 +9,9 @@ class FixtureBlueprintTest extends SapphireTest { protected $extraDataObjects = array( 'FixtureFactoryTest_DataObject', - 'FixtureFactoryTest_DataObjectRelation' + 'FixtureFactoryTest_DataObjectRelation', + 'FixtureBlueprintTest_SiteTree', + 'FixtureBlueprintTest_Page' ); public function testCreateWithRelationshipExtraFields() { @@ -180,6 +182,40 @@ class FixtureBlueprintTest extends SapphireTest { $this->assertEquals(99, $obj->ID); } + public function testCreateWithLastEdited() { + $extpectedDate = '2010-12-14 16:18:20'; + $blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); + $obj = $blueprint->createObject('lastedited', array('LastEdited' => $extpectedDate)); + $this->assertNotNull($obj); + $this->assertEquals($extpectedDate, $obj->LastEdited); + + $obj = FixtureFactoryTest_DataObject::get()->byID($obj->ID); + $this->assertEquals($extpectedDate, $obj->LastEdited); + } + + public function testCreateWithClassAncestry() { + $data = array( + 'Title' => 'My Title', + 'Created' => '2010-12-14 16:18:20', + 'LastEdited' => '2010-12-14 16:18:20', + 'PublishDate' => '2015-12-09 06:03:00' + ); + $blueprint = new FixtureBlueprint('FixtureBlueprintTest_Article'); + $obj = $blueprint->createObject('home', $data); + $this->assertNotNull($obj); + $this->assertEquals($data['Title'], $obj->Title); + $this->assertEquals($data['Created'], $obj->Created); + $this->assertEquals($data['LastEdited'], $obj->LastEdited); + $this->assertEquals($data['PublishDate'], $obj->PublishDate); + + $obj = FixtureBlueprintTest_Article::get()->byID($obj->ID); + $this->assertNotNull($obj); + $this->assertEquals($data['Title'], $obj->Title); + $this->assertEquals($data['Created'], $obj->Created); + $this->assertEquals($data['LastEdited'], $obj->LastEdited); + $this->assertEquals($data['PublishDate'], $obj->PublishDate); + } + public function testCallbackOnBeforeCreate() { $blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); $this->_called = 0; @@ -231,4 +267,33 @@ class FixtureBlueprintTest extends SapphireTest { $this->assertEquals('Override Name', $obj2->Name); } -} \ No newline at end of file +} + +/** + * @package framework + * @subpackage tests + */ +class FixtureBlueprintTest_SiteTree extends DataObject implements TestOnly { + + private static $db = array( + "Title" => "Varchar" + ); +} + +/** + * @package framework + * @subpackage tests + */ +class FixtureBlueprintTest_Page extends FixtureBlueprintTest_SiteTree { + + private static $db = array( + 'PublishDate' => 'SS_DateTime' + ); +} + +/** + * @package framework + * @subpackage tests + */ +class FixtureBlueprintTest_Article extends FixtureBlueprintTest_Page { +}