From 130b827e6f7a9d131b2674431e7d9c9dfde6e2f9 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 4 Nov 2011 21:47:55 -0430 Subject: [PATCH] Implementing the modified() method in CakeResponse to have an easier way of setting the modification time --- lib/Cake/Network/CakeResponse.php | 28 +++++++++++++- .../Test/Case/Network/CakeResponseTest.php | 38 +++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index 29ebcd93b..e2545e161 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -676,10 +676,10 @@ class CakeResponse { } $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 @@ class CakeResponse { * * `$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 @@ -708,6 +708,30 @@ class CakeResponse { 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 diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 445598f06..e9522db67 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -271,7 +271,7 @@ class CakeResponseTest extends CakeTestCase { $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 @@ class CakeResponseTest extends CakeTestCase { $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 @@ class CakeResponseTest extends CakeTestCase { $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 @@ class CakeResponseTest extends CakeTestCase { ->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(); + } }