From 527291974c2276164cc641c241243bf1fe110791 Mon Sep 17 00:00:00 2001 From: AD7six Date: Fri, 29 Nov 2013 14:50:42 +0000 Subject: [PATCH] add touch method to timestamp behavior --- Cake/Model/Behavior/TimestampBehavior.php | 39 +++++++++- .../Model/Behavior/TimestampBehaviorTest.php | 72 +++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/Cake/Model/Behavior/TimestampBehavior.php b/Cake/Model/Behavior/TimestampBehavior.php index ef377c8e4b4..e5a7669bfcf 100644 --- a/Cake/Model/Behavior/TimestampBehavior.php +++ b/Cake/Model/Behavior/TimestampBehavior.php @@ -38,7 +38,10 @@ class TimestampBehavior extends Behavior { */ protected static $_defaultConfig = [ 'implementedFinders' => [], - 'implementedMethods' => ['timestamp' => 'timestamp'], + 'implementedMethods' => [ + 'timestamp' => 'timestamp', + 'touch' => 'touch' + ], 'events' => [ 'Model.beforeSave' => [ 'created' => 'new', @@ -120,6 +123,40 @@ public function timestamp(\DateTime $ts = null, $refreshTimestamp = false) { return $this->_ts; } +/** + * Touch an entity + * + * Bumps timestamp fields for an entity. For any fields configured to be updated + * "always" or "existing", update the timestamp value. This method will overwrite + * any pre-existing value. + * + * @param Entity $entity + * @param string $eventName + * @return bool true if a field is updated, false if no action performed + */ + public function touch(Entity $entity, $eventName = 'Model.beforeSave') { + $config = $this->config(); + if (!isset($config['events'][$eventName])) { + return false; + } + + $new = $entity->isNew() !== false; + $return = false; + + foreach ($config['events'][$eventName] as $field => $when) { + if ( + $when === 'always' || + ($when === 'existing' && !$new) + ) { + $return = true; + $entity->dirty($field, false); + $this->_updateField($entity, $field, $config['refreshTimestamp']); + } + } + + return $return; + } + /** * Update a field, if it hasn't been updated already * diff --git a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php index 2c4e80ecf5f..e89c7975011 100644 --- a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php +++ b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php @@ -276,6 +276,78 @@ public function testSetTimestampExplicit() { ); } +/** + * testTouch + * + * @return void + */ + public function testTouch() { + $table = $this->getMock('Cake\ORM\Table'); + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); + $ts = new \DateTime('2000-01-01'); + $this->Behavior->timestamp($ts); + + $entity = new Entity(['username' => 'timestamp test']); + $return = $this->Behavior->touch($entity); + $this->assertTrue($return, 'touch is expected to return true if it sets a field value'); + $this->assertSame( + $ts->format('Y-m-d H:i:s'), + $entity->modified->format('Y-m-d H:i:s'), + 'Modified field is expected to be updated' + ); + $this->assertNull($entity->created, 'Created field is NOT expected to change'); + } + +/** + * testTouchNoop + * + * @return void + */ + public function testTouchNoop() { + $table = $this->getMock('Cake\ORM\Table'); + $config = [ + 'refreshTimestamp' => false, + 'events' => [ + 'Model.beforeSave' => [ + 'created' => 'new', + ] + ] + ]; + + $this->Behavior = new TimestampBehavior($table, $config); + $ts = new \DateTime('2000-01-01'); + $this->Behavior->timestamp($ts); + + $entity = new Entity(['username' => 'timestamp test']); + $return = $this->Behavior->touch($entity); + $this->assertFalse($return, 'touch is expected to do nothing and return false'); + $this->assertNull($entity->modified, 'Modified field is NOT expected to change'); + $this->assertNull($entity->created, 'Created field is NOT expected to change'); + } + +/** + * testTouchCustomEvent + * + * @return void + */ + public function testTouchCustomEvent() { + $table = $this->getMock('Cake\ORM\Table'); + $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']], 'refreshTimestamp' => false]; + $this->Behavior = new TimestampBehavior($table, $settings); + $ts = new \DateTime('2000-01-01'); + $this->Behavior->timestamp($ts); + + $entity = new Entity(['username' => 'timestamp test']); + $return = $this->Behavior->touch($entity, 'Something.special'); + $this->assertTrue($return, 'touch is expected to return true if it sets a field value'); + $this->assertSame( + $ts->format('Y-m-d H:i:s'), + $entity->date_specialed->format('Y-m-d H:i:s'), + 'Modified field is expected to be updated' + ); + $this->assertNull($entity->created, 'Created field is NOT expected to change'); + } + /** * Test that calling save, triggers an insert including the created and updated field values *