Skip to content

Commit c1bf268

Browse files
Merge branch '3.1' into 3.2
* 3.1: [ci] Update travis/appveyor [HttpFoundation] Validate/cast cookie expire time
2 parents c3b1dfa + 2c95f60 commit c1bf268

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Cookie.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
6363
} elseif (!is_numeric($expire)) {
6464
$expire = strtotime($expire);
6565

66-
if (false === $expire || -1 === $expire) {
66+
if (false === $expire) {
6767
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
6868
}
6969
}
7070

7171
$this->name = $name;
7272
$this->value = $value;
7373
$this->domain = $domain;
74-
$this->expire = $expire;
74+
$this->expire = 0 < $expire ? (int) $expire : 0;
7575
$this->path = empty($path) ? '/' : $path;
7676
$this->secure = (bool) $secure;
7777
$this->httpOnly = (bool) $httpOnly;
@@ -98,7 +98,7 @@ public function __toString()
9898
} else {
9999
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());
100100

101-
if ($this->getExpiresTime() !== 0) {
101+
if (0 !== $this->getExpiresTime()) {
102102
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
103103
}
104104
}

Tests/CookieTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidChara
5252
*/
5353
public function testInvalidExpiration()
5454
{
55-
$cookie = new Cookie('MyCookie', 'foo', 'bar');
55+
new Cookie('MyCookie', 'foo', 'bar');
56+
}
57+
58+
public function testNegativeExpirationIsNotPossible()
59+
{
60+
$cookie = new Cookie('foo', 'bar', -100);
61+
62+
$this->assertSame(0, $cookie->getExpiresTime());
5663
}
5764

5865
public function testGetValue()
@@ -77,6 +84,13 @@ public function testGetExpiresTime()
7784
$this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
7885
}
7986

87+
public function testGetExpiresTimeIsCastToInt()
88+
{
89+
$cookie = new Cookie('foo', 'bar', 3600.9);
90+
91+
$this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
92+
}
93+
8094
public function testConstructorWithDateTime()
8195
{
8296
$expire = new \DateTime();
@@ -143,13 +157,13 @@ public function testCookieIsCleared()
143157
public function testToString()
144158
{
145159
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
146-
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
160+
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
147161

148162
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
149-
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
163+
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
150164

151165
$cookie = new Cookie('foo', 'bar', 0, '/', '');
152-
$this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
166+
$this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
153167
}
154168

155169
public function testRawCookie()

0 commit comments

Comments
 (0)