bug #20910 [HttpFoundation] Fix cookie to string conversion for raw cookies (ro0NL)

This PR was squashed before being merged into the 3.1 branch (closes #20910).

Discussion
----------

[HttpFoundation] Fix cookie to string conversion for raw cookies

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | not really
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/20569#discussion_r92135987
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Separated from #20569

This mimics PHP's `setrawcookie` behavior.

Commits
-------

5e899cd [HttpFoundation] Fix cookie to string conversion for raw cookies
This commit is contained in:
Fabien Potencier 2016-12-14 08:17:24 +01:00
commit 05e83f5a4e
2 changed files with 10 additions and 8 deletions

View File

@ -80,20 +80,20 @@ class Cookie
*/
public function __toString()
{
$str = urlencode($this->getName()).'=';
$str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
if ('' === (string) $this->getValue()) {
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
} else {
$str .= urlencode($this->getValue());
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());
if ($this->getExpiresTime() !== 0) {
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
}
}
if ($this->path) {
$str .= '; path='.$this->path;
if ($this->getPath()) {
$str .= '; path='.$this->getPath();
}
if ($this->getDomain()) {
@ -124,7 +124,7 @@ class Cookie
/**
* Gets the value of the cookie.
*
* @return string
* @return string|null
*/
public function getValue()
{
@ -134,7 +134,7 @@ class Cookie
/**
* Gets the domain that the cookie is available to.
*
* @return string
* @return string|null
*/
public function getDomain()
{

View File

@ -154,10 +154,12 @@ class CookieTest extends \PHPUnit_Framework_TestCase
public function testRawCookie()
{
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
$cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
$this->assertFalse($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true);
$cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
$this->assertTrue($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
}
}