Skip to content

Commit

Permalink
BUG Fixed setting LastEdited for DataObject with class ancestry
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Smirnov committed May 22, 2015
1 parent 0ba3ada commit 94f6a13
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
23 changes: 15 additions & 8 deletions dev/FixtureBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,8 @@ public function createObject($identifier, $data = null, $fixtures = null) {

// 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;
Expand Down Expand Up @@ -288,4 +282,17 @@ protected function setValue($obj, $name, $value, $fixtures = null) {
$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;
}

}
69 changes: 67 additions & 2 deletions tests/dev/FixtureBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class FixtureBlueprintTest extends SapphireTest {

protected $extraDataObjects = array(
'FixtureFactoryTest_DataObject',
'FixtureFactoryTest_DataObjectRelation'
'FixtureFactoryTest_DataObjectRelation',
'FixtureBlueprintTest_SiteTree',
'FixtureBlueprintTest_Page'
);

public function testCreateWithRelationshipExtraFields() {
Expand Down Expand Up @@ -180,6 +182,40 @@ public function testCreateWithId() {
$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;
Expand Down Expand Up @@ -231,4 +267,33 @@ public function testDefineWithDefaultCustomSetters() {
$this->assertEquals('Override Name', $obj2->Name);
}

}
}

/**
* @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 {
}

0 comments on commit 94f6a13

Please sign in to comment.