Skip to content

Commit

Permalink
automatically set refreshTimestamp on set
Browse files Browse the repository at this point in the history
If a user sets the timestamp, but refreshTimestamp is truthy, the set
value will effectivley be ignored
  • Loading branch information
AD7six committed Nov 30, 2013
1 parent 5272919 commit 75342bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Cake/Model/Behavior/TimestampBehavior.php
Expand Up @@ -108,13 +108,18 @@ public function implementedEvents() {
* Get or set the timestamp to be used
*
* Set the timestamp to the given DateTime object, or if not passed a new DateTime object
* If an explicit date time is passed, the config option `refreshTimestamp` is
* automatically set to false.
*
* @param \DateTime $ts
* @param bool $refreshTimestamp
* @return \DateTime
*/
public function timestamp(\DateTime $ts = null, $refreshTimestamp = false) {
if ($ts) {
if ($this->_config['refreshTimestamp']) {
$this->_config['refreshTimestamp'] = false;
}
$this->_ts = $ts;
} elseif ($this->_ts === null || $refreshTimestamp) {
$this->_ts = new \DateTime();
Expand Down
15 changes: 7 additions & 8 deletions Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php
Expand Up @@ -83,7 +83,7 @@ public function testImplementedEventsCustom() {
*/
public function testCreatedAbsent() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand All @@ -102,7 +102,7 @@ public function testCreatedAbsent() {
*/
public function testCreatedPresent() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand All @@ -122,7 +122,7 @@ public function testCreatedPresent() {
*/
public function testCreatedNotNew() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand All @@ -142,7 +142,7 @@ public function testCreatedNotNew() {
*/
public function testModifiedAbsent() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand All @@ -162,7 +162,7 @@ public function testModifiedAbsent() {
*/
public function testModifiedPresent() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand Down Expand Up @@ -283,7 +283,7 @@ public function testSetTimestampExplicit() {
*/
public function testTouch() {
$table = $this->getMock('Cake\ORM\Table');
$this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]);
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);

Expand All @@ -306,7 +306,6 @@ public function testTouch() {
public function testTouchNoop() {
$table = $this->getMock('Cake\ORM\Table');
$config = [
'refreshTimestamp' => false,
'events' => [
'Model.beforeSave' => [
'created' => 'new',
Expand All @@ -332,7 +331,7 @@ public function testTouchNoop() {
*/
public function testTouchCustomEvent() {
$table = $this->getMock('Cake\ORM\Table');
$settings = ['events' => ['Something.special' => ['date_specialed' => 'always']], 'refreshTimestamp' => false];
$settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
$this->Behavior = new TimestampBehavior($table, $settings);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand Down

0 comments on commit 75342bb

Please sign in to comment.