From 1ae07ac2a35fe5e1982f3601c3e89b3e81a7e74c Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Wed, 16 Aug 2017 22:26:47 +0100 Subject: [PATCH 1/2] TEST Prove LastEdited is updated when no changes are made --- tests/model/DataObjectTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index b007864bb..8d7cdf474 100644 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -357,6 +357,23 @@ class DataObjectTest extends SapphireTest { $this->assertEquals(1, $players->limit(5, 3)->count()); } + public function testWriteNoChangesDoesntUpdateLastEdited() { + // set mock now so we can be certain of LastEdited time for our test + SS_Datetime::set_mock_now('2017-01-01 00:00:00'); + $obj = new DataObjectTest_Player(); + $obj->FirstName = 'Test'; + $obj->Surname = 'Plater'; + $obj->Email = 'test.player@example.com'; + $obj->write(); + $writtenObj = DataObjectTest_Player::get()->byID($obj->ID); + $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); + + // set mock now so we get a new LastEdited if, for some reason, it's updated + SS_Datetime::set_mock_now('2017-02-01 00:00:00'); + $writtenObj->write(); + $this->assertEquals('2017-01-01 00:00:00', $writtenObj->LastEdited); + } + /** * Test writing of database columns which don't correlate to a DBField, * e.g. all relation fields on has_one/has_many like "ParentID". From eb80a5f9e89e69480edc7f1c9c66cc7403f547f1 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Wed, 16 Aug 2017 23:12:08 +0100 Subject: [PATCH 2/2] FIX LastEdited no longer updated on skipped writes --- model/DataObject.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 82869dd5d..bec04bb5b 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -1377,6 +1377,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity // Check changes exist, abort if there are none $hasChanges = $this->updateChanges($isNewRecord); if($hasChanges || $forceWrite || $isNewRecord) { + + // Ensure Created and LastEdited are populated + if(!isset($this->record['Created'])) { + $this->record['Created'] = $now; + } + $this->record['LastEdited'] = $now; // New records have their insert into the base data table done first, so that they can pass the // generated primary key on to the rest of the manipulation $baseTable = ClassInfo::baseDataClass($this->class); @@ -1396,12 +1402,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $this->invokeWithExtensions('onAfterSkippedWrite'); } - // Ensure Created and LastEdited are populated - if(!isset($this->record['Created'])) { - $this->record['Created'] = $now; - } - $this->record['LastEdited'] = $now; - // Write relations as necessary if($writeComponents) $this->writeComponents(true);