Skip to content

Commit

Permalink
Don't hard fail on missing columns.
Browse files Browse the repository at this point in the history
If timestamp behavior is attached to a table that doesn't have the
required columns, it should not hard fail in this way. This failure mode
can be percieved as a compatibility break as it happens deep into
runtime, and not during configuration/attach time.

Refs #11800
  • Loading branch information
markstory committed Mar 7, 2018
1 parent bb5e86f commit 9fbaf7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ORM/Behavior/TimestampBehavior.php
Expand Up @@ -198,6 +198,10 @@ protected function _updateField($entity, $field, $refreshTimestamp)
$ts = $this->timestamp(null, $refreshTimestamp);

$columnType = $this->getTable()->getSchema()->getColumnType($field);
if (!$columnType) {
return;
}

/** @var \Cake\Database\Type\DateTimeType $type */
$type = Type::build($columnType);
$class = $type->getDateTimeClassName();
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php
Expand Up @@ -195,6 +195,28 @@ public function testModifiedPresent()
$this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
}

/**
* test that timestamp creation doesn't fail on missing columns
*
* @return void
*/
public function testModifiedMissingColumn()
{
$table = $this->getTable();
$table->getSchema()->removeColumn('created')->removeColumn('modified');
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

$event = new Event('Model.beforeSave');
$entity = new Entity(['name' => 'Foo']);

$return = $this->Behavior->handleEvent($event, $entity);
$this->assertTrue($return, 'Handle Event is expected to always return true');
$this->assertNull($entity->created);
$this->assertNull($entity->modified);
}

/**
* testUseImmutable
*
Expand Down

0 comments on commit 9fbaf7e

Please sign in to comment.