diff --git a/src/Http/Cookie/Cookie.php b/src/Http/Cookie/Cookie.php index a565c1159cb..14bd173b570 100644 --- a/src/Http/Cookie/Cookie.php +++ b/src/Http/Cookie/Cookie.php @@ -81,7 +81,7 @@ class Cookie implements CookieInterface * * @var string */ - protected $path = ''; + protected $path = '/'; /** * Domain @@ -124,7 +124,7 @@ public function __construct( $name, $value = '', $expiresAt = null, - $path = '', + $path = '/', $domain = '', $secure = false, $httpOnly = false diff --git a/src/Http/Response.php b/src/Http/Response.php index d698983e19b..bf96dd4b6ea 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -2175,7 +2175,7 @@ public function cookie($options = null) /** * Create a new response with a cookie set. * - * ### Options + * ### Data * * - `value`: Value of the cookie * - `expire`: Time the cookie expires in diff --git a/tests/TestCase/Http/Cookie/CookieCollectionTest.php b/tests/TestCase/Http/Cookie/CookieCollectionTest.php index cb8e715d3ba..b01a9c68594 100644 --- a/tests/TestCase/Http/Cookie/CookieCollectionTest.php +++ b/tests/TestCase/Http/Cookie/CookieCollectionTest.php @@ -483,7 +483,7 @@ public function testCreateFromServerRequest() $cookie = $cookies->get('name'); $this->assertSame('val', $cookie->getValue()); - $this->assertSame('', $cookie->getPath(), 'No path on request cookies'); + $this->assertSame('/', $cookie->getPath()); $this->assertSame('', $cookie->getDomain(), 'No domain on request cookies'); } } diff --git a/tests/TestCase/Http/Cookie/CookieTest.php b/tests/TestCase/Http/Cookie/CookieTest.php index 8b8adc47ad6..1495aa52140 100644 --- a/tests/TestCase/Http/Cookie/CookieTest.php +++ b/tests/TestCase/Http/Cookie/CookieTest.php @@ -72,7 +72,7 @@ public function testToHeaderValue() { $cookie = new Cookie('cakephp', 'cakephp-rocks'); $result = $cookie->toHeaderValue(); - $this->assertEquals('cakephp=cakephp-rocks', $result); + $this->assertEquals('cakephp=cakephp-rocks; path=/', $result); $date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2027 12:00:00'); @@ -83,7 +83,7 @@ public function testToHeaderValue() ->withSecure(true); $result = $cookie->toHeaderValue(); - $expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; domain=cakephp.org; secure; httponly'; + $expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; path=/; domain=cakephp.org; secure; httponly'; $this->assertEquals($expected, $result); } @@ -212,6 +212,17 @@ public function testWithPath() $this->assertContains('path=/api', $new->toHeaderValue()); } + /** + * Test default path in cookies + * + * @return void + */ + public function testDefaultPath() + { + $cookie = new Cookie('cakephp', 'cakephp-rocks'); + $this->assertContains('path=/', $cookie->toHeaderValue()); + } + /** * Test setting httponly in cookies * @@ -550,10 +561,10 @@ public function testToHeaderValueCollapsesComplexData() public function testGetId() { $cookie = new Cookie('cakephp', 'cakephp-rocks'); - $this->assertEquals('cakephp;;', $cookie->getId()); + $this->assertEquals('cakephp;;/', $cookie->getId()); $cookie = new Cookie('CAKEPHP', 'cakephp-rocks'); - $this->assertEquals('cakephp;;', $cookie->getId()); + $this->assertEquals('cakephp;;/', $cookie->getId()); $cookie = new Cookie('test', 'val', null, '/path', 'example.com'); $this->assertEquals('test;example.com;/path', $cookie->getId()); diff --git a/tests/TestCase/Http/ResponseTest.php b/tests/TestCase/Http/ResponseTest.php index 2c36667e8f9..03010358907 100644 --- a/tests/TestCase/Http/ResponseTest.php +++ b/tests/TestCase/Http/ResponseTest.php @@ -1773,7 +1773,7 @@ public function testGetCookiesArrayValue() 'name' => 'urmc', 'value' => '{"user_id":1,"token":"abc123"}', 'expire' => null, - 'path' => '', + 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => true