Skip to content

Commit

Permalink
add touch method to timestamp behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Nov 30, 2013
1 parent f669d8c commit 5272919
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Cake/Model/Behavior/TimestampBehavior.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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
*
Expand Down
72 changes: 72 additions & 0 deletions Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 5272919

Please sign in to comment.