[HttpFoundation] Expired cookies string representation consistency & tests
This commit is contained in:
parent
127724d519
commit
4f5d5eceb0
@ -62,8 +62,9 @@ class Cookie
|
|||||||
$value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null;
|
$value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null;
|
||||||
|
|
||||||
$data = HeaderUtils::combine($parts) + $data;
|
$data = HeaderUtils::combine($parts) + $data;
|
||||||
|
$data['expires'] = self::expiresTimestamp($data['expires']);
|
||||||
|
|
||||||
if (isset($data['max-age'])) {
|
if (isset($data['max-age']) && ($data['max-age'] > 0 || $data['expires'] > time())) {
|
||||||
$data['expires'] = time() + (int) $data['max-age'];
|
$data['expires'] = time() + (int) $data['max-age'];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +103,7 @@ class Cookie
|
|||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->domain = $domain;
|
$this->domain = $domain;
|
||||||
$this->expire = $this->withExpires($expire)->expire;
|
$this->expire = self::expiresTimestamp($expire);
|
||||||
$this->path = empty($path) ? '/' : $path;
|
$this->path = empty($path) ? '/' : $path;
|
||||||
$this->secure = $secure;
|
$this->secure = $secure;
|
||||||
$this->httpOnly = $httpOnly;
|
$this->httpOnly = $httpOnly;
|
||||||
@ -144,6 +145,21 @@ class Cookie
|
|||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function withExpires($expire = 0): self
|
public function withExpires($expire = 0): self
|
||||||
|
{
|
||||||
|
$cookie = clone $this;
|
||||||
|
$cookie->expire = self::expiresTimestamp($expire);
|
||||||
|
|
||||||
|
return $cookie;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts expires formats to a unix timestamp.
|
||||||
|
*
|
||||||
|
* @param int|string|\DateTimeInterface $expire
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private static function expiresTimestamp($expire = 0)
|
||||||
{
|
{
|
||||||
// convert expiration time to a Unix timestamp
|
// convert expiration time to a Unix timestamp
|
||||||
if ($expire instanceof \DateTimeInterface) {
|
if ($expire instanceof \DateTimeInterface) {
|
||||||
@ -156,10 +172,7 @@ class Cookie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$cookie = clone $this;
|
return 0 < $expire ? (int) $expire : 0;
|
||||||
$cookie->expire = 0 < $expire ? (int) $expire : 0;
|
|
||||||
|
|
||||||
return $cookie;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -363,4 +363,33 @@ class CookieTest extends TestCase
|
|||||||
|
|
||||||
$this->assertFalse($cookie->isSecure());
|
$this->assertFalse($cookie->isSecure());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMaxAge()
|
||||||
|
{
|
||||||
|
$futureDateOneHour = gmdate('D, d-M-Y H:i:s T', time() + 3600);
|
||||||
|
|
||||||
|
$cookie = Cookie::fromString('foo=bar; Max-Age=3600; path=/');
|
||||||
|
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());
|
||||||
|
|
||||||
|
$cookie = Cookie::fromString('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/');
|
||||||
|
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());
|
||||||
|
|
||||||
|
$futureDateHalfHour = gmdate('D, d-M-Y H:i:s T', time() + 1800);
|
||||||
|
|
||||||
|
// Max-Age value takes precedence before expires
|
||||||
|
$cookie = Cookie::fromString('foo=bar; expires='.$futureDateHalfHour.'; Max-Age=3600; path=/');
|
||||||
|
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExpiredWithMaxAge()
|
||||||
|
{
|
||||||
|
$cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/');
|
||||||
|
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/', $cookie->__toString());
|
||||||
|
|
||||||
|
$futureDate = gmdate('D, d-M-Y H:i:s T', time() + 864000);
|
||||||
|
|
||||||
|
$cookie = Cookie::fromString('foo=bar; expires='.$futureDate.'; Max-Age=0; path=/');
|
||||||
|
$this->assertEquals(time(), $cookie->getExpiresTime());
|
||||||
|
$this->assertEquals('foo=bar; expires='.gmdate('D, d-M-Y H:i:s T', $cookie->getExpiresTime()).'; Max-Age=0; path=/', $cookie->__toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user