Skip to content

Commit

Permalink
Implementing the modified() method in CakeResponse to have an easier …
Browse files Browse the repository at this point in the history
…way of setting the modification time
  • Loading branch information
lorenzo committed Jan 19, 2012
1 parent bab7e77 commit 130b827
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
28 changes: 26 additions & 2 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -676,10 +676,10 @@ public function cache($since, $time = '+1 day') {
}
$this->header(array(
'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Cache-Control' => 'public, max-age=' . ($time - time()),
'Pragma' => 'cache'
));
$this->modified($since);
$this->expires($time);
}

Expand All @@ -692,7 +692,7 @@ public function cache($since, $time = '+1 day') {
*
* `$response->expires('now')` Will Expire the response cache now
* `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
* `$response->expires)` Will return the current expiration header value
* `$response->expires()` Will return the current expiration header value
*
* @param string|DateTime $time
* @return string
Expand All @@ -708,6 +708,30 @@ public function expires($time = null) {
return null;
}

/**
* Sets the Last-Modified header for the response by taking an modification time
* If called with no parameters it will return the current Last-Modified value
*
* ## Examples:
*
* `$response->modified('now')` Will set the Last-Modified to the current time
* `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
* `$response->modified()` Will return the current Last-Modified header value
*
* @param string|DateTime $time
* @return string
*/
public function modified($time = null) {
if ($time !== null) {
$date = $this->_getUTCDate($time);
$this->_headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
}
if (isset($this->_headers['Last-Modified'])) {
return $this->_headers['Last-Modified'];
}
return null;
}

/**
* Returns a DateTime object initialized at the $time param and using UTC
* as timezone
Expand Down
38 changes: 35 additions & 3 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -271,7 +271,7 @@ public function testCache() {
$response->expires('+1 day');
$expected = array(
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
'Pragma' => 'cache'
Expand All @@ -284,7 +284,7 @@ public function testCache() {
$time = '+5 day';
$expected = array(
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
'Pragma' => 'cache'
Expand All @@ -297,7 +297,7 @@ public function testCache() {
$time = time();
$expected = array(
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
'Cache-Control' => 'public, max-age=0',
'Pragma' => 'cache'
Expand Down Expand Up @@ -603,4 +603,36 @@ public function testExpires() {
->method('_sendHeader')->with('Expires', $time->format('D, j M Y H:i:s') . ' GMT');
$response->send();
}

/**
* Tests setting the modification date
*
* @return void
*/
public function testModified() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$now = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
$response->modified($now);
$now->setTimeZone(new DateTimeZone('UTC'));
$this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->modified());
$response->expects($this->at(1))
->method('_sendHeader')->with('Last-Modified', $now->format('D, j M Y H:i:s') . ' GMT');
$response->send();

$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$now = time();
$response->modified($now);
$this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->modified());
$response->expects($this->at(1))
->method('_sendHeader')->with('Last-Modified', gmdate('D, j M Y H:i:s', $now) . ' GMT');
$response->send();

$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$time = new DateTime('+1 day', new DateTimeZone('UTC'));
$response->modified('+1 day');
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
$response->expects($this->at(1))
->method('_sendHeader')->with('Last-Modified', $time->format('D, j M Y H:i:s') . ' GMT');
$response->send();
}
}

0 comments on commit 130b827

Please sign in to comment.