Skip to content

Commit

Permalink
Merge pull request #11586 from o0h/timestamp-behavior-follow-datetime…
Browse files Browse the repository at this point in the history
…type-set-immutable

Timestamp behavior follow datetimetype set immutable
  • Loading branch information
markstory committed Jan 2, 2018
2 parents 897a9ac + 5c21e5f commit 2d21e3a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/ORM/Behavior/TimestampBehavior.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM\Behavior;

use Cake\Database\Type;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\I18n\Time;
Expand Down Expand Up @@ -193,6 +194,14 @@ protected function _updateField($entity, $field, $refreshTimestamp)
if ($entity->isDirty($field)) {
return;
}
$entity->set($field, $this->timestamp(null, $refreshTimestamp));

$ts = $this->timestamp(null, $refreshTimestamp);

$columnType = $this->getTable()->getSchema()->getColumnType($field);
/** @var \Cake\Database\Type\DateTimeType $type */
$type = Type::build($columnType);
$class = $type->getDateTimeClassName();

$entity->set($field, new $class($ts));
}
}
69 changes: 56 additions & 13 deletions tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php
Expand Up @@ -14,10 +14,12 @@
*/
namespace Cake\Test\TestCase\ORM\Behavior;

use Cake\Database\Type;
use Cake\Event\Event;
use Cake\I18n\Time;
use Cake\ORM\Behavior\TimestampBehavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -88,7 +90,7 @@ public function testImplementedEventsCustom()
*/
public function testCreatedAbsent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -110,7 +112,7 @@ public function testCreatedAbsent()
*/
public function testCreatedPresent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -132,7 +134,7 @@ public function testCreatedPresent()
*/
public function testCreatedNotNew()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -154,7 +156,7 @@ public function testCreatedNotNew()
*/
public function testModifiedAbsent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -177,7 +179,7 @@ public function testModifiedAbsent()
*/
public function testModifiedPresent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -194,6 +196,30 @@ public function testModifiedPresent()
$this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
}

/**
* testUseImmutable
*
* @return void
* @triggers Model.beforeSave
*/
public function testUseImmutable()
{
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$entity = new Entity();
$event = new Event('Model.beforeSave');

Type::build('timestamp')->useImmutable();
$entity->clean();
$this->Behavior->handleEvent($event, $entity);
$this->assertInstanceOf('Cake\I18n\FrozenTime', $entity->modified);

Type::build('timestamp')->useMutable();
$entity->clean();
$this->Behavior->handleEvent($event, $entity);
$this->assertInstanceOf('Cake\I18n\Time', $entity->modified);
}

/**
* testInvalidEventConfig
*
Expand All @@ -204,7 +230,7 @@ public function testInvalidEventConfig()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid');
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
$this->Behavior = new TimestampBehavior($table, $settings);

Expand All @@ -220,7 +246,7 @@ public function testInvalidEventConfig()
*/
public function testGetTimestamp()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$return = $behavior->timestamp();
Expand All @@ -241,7 +267,7 @@ public function testGetTimestamp()
*/
public function testGetTimestampPersists()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$initialValue = $behavior->timestamp();
Expand All @@ -261,7 +287,7 @@ public function testGetTimestampPersists()
*/
public function testGetTimestampRefreshes()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$initialValue = $behavior->timestamp();
Expand All @@ -281,7 +307,7 @@ public function testGetTimestampRefreshes()
*/
public function testSetTimestampExplicit()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);

$ts = new \DateTime();
Expand All @@ -302,7 +328,7 @@ public function testSetTimestampExplicit()
*/
public function testTouch()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -325,7 +351,7 @@ public function testTouch()
*/
public function testTouchNoop()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$config = [
'events' => [
'Model.beforeSave' => [
Expand All @@ -352,7 +378,7 @@ public function testTouchNoop()
*/
public function testTouchCustomEvent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
$this->Behavior = new TimestampBehavior($table, $settings);
$ts = new \DateTime('2000-01-01');
Expand Down Expand Up @@ -398,4 +424,21 @@ public function testSaveTriggersInsert()
$this->assertEquals($now->toDateTimeString(), $row->created->toDateTimeString());
$this->assertEquals($now->toDateTimeString(), $row->updated->toDateTimeString());
}

/**
* Helper method to get Table instance with created/modified column
*
* @return \Cake\ORM\Table
*/
protected function getTable()
{
$schema = [
'created' => ['type' => 'datetime'],
'modified' => ['type' => 'timestamp'],
'date_specialed' => ['type' => 'datetime'],
];
$table = new Table(['schema' => $schema]);

return $table;
}
}

0 comments on commit 2d21e3a

Please sign in to comment.