Permalink
Browse files

Implementing the modified() method in CakeResponse to have an easier …

…way of setting the modification time
  • Loading branch information...
1 parent bab7e77 commit 130b827e6f7a9d131b2674431e7d9c9dfde6e2f9 @lorenzo lorenzo committed Nov 5, 2011
Showing with 61 additions and 5 deletions.
  1. +26 −2 lib/Cake/Network/CakeResponse.php
  2. +35 −3 lib/Cake/Test/Case/Network/CakeResponseTest.php
View
28 lib/Cake/Network/CakeResponse.php
@@ -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);
}
@@ -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
@@ -709,6 +709,30 @@ public function expires($time = 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
*
View
38 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -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'
@@ -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'
@@ -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'
@@ -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.