Browse Source

Fix cookie expire set to 1 when app timezone is not UTC

Erwane Breton 3 years ago
parent
commit
8e6a6683b3

+ 1 - 1
src/Http/Cookie/Cookie.php

@@ -621,7 +621,7 @@ class Cookie implements CookieInterface
     public function withExpired()
     {
         $new = clone $this;
-        $new->expiresAt = new DateTimeImmutable('1970-01-01 00:00:01');
+        $new->expiresAt = new DateTimeImmutable('1970-01-01 00:00:01', new DateTimeZone('UTC'));
 
         return $new;
     }

+ 15 - 0
tests/TestCase/Http/Cookie/CookieTest.php

@@ -237,6 +237,21 @@ class CookieTest extends TestCase
     }
 
     /**
+     * Test the expired method
+     */
+    public function testWithExpiredNotUtc(): void
+    {
+        date_default_timezone_set('Europe/Paris');
+
+        $cookie = new Cookie('cakephp', 'cakephp-rocks');
+        $new = $cookie->withExpired();
+
+        $this->assertStringContainsString('01-Jan-1970 00:00:01 UTC', $new->toHeaderValue());
+
+        date_default_timezone_set('UTC');
+    }
+
+    /**
      * Test the withExpiry method
      */
     public function testWithExpiry(): void

+ 13 - 0
tests/TestCase/Http/ResponseTest.php

@@ -910,6 +910,19 @@ class ResponseTest extends TestCase
         $this->assertSame(1, $new->getCookie('yay')['expires']);
     }
 
+    public function testWithExpiredCookieNotUtc()
+    {
+        date_default_timezone_set('Europe/Paris');
+
+        $response = new Response();
+        $cookie = new Cookie('yay', 'a value');
+        $response = $response->withExpiredCookie($cookie);
+
+        $this->assertSame(1, $response->getCookie('yay')['expires']);
+
+        date_default_timezone_set('UTC');
+    }
+
     /**
      * Test getCookies() and array data.
      */