Skip to content

Commit 558f97b

Browse files
committed
fix: Cookie class option prefix bug
When option prefix is '0', it is not set correctly.
1 parent a1c7ca2 commit 558f97b

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

system/Cookie/Cookie.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
119119
* Set the default attributes to a Cookie instance by injecting
120120
* the values from the `CookieConfig` config or an array.
121121
*
122+
* This method is called from Response::__construct().
123+
*
122124
* @param array<string, mixed>|CookieConfig $config
123125
*
124126
* @return array<string, mixed> The old defaults array. Useful for resetting.
@@ -209,7 +211,7 @@ final public function __construct(string $name, string $value = '', array $optio
209211
}
210212

211213
// to preserve backward compatibility with array-based cookies in previous CI versions
212-
$prefix = $options['prefix'] ?: self::$defaults['prefix'];
214+
$prefix = ($options['prefix'] === '') ? self::$defaults['prefix'] : $options['prefix'];
213215
$path = $options['path'] ?: self::$defaults['path'];
214216
$domain = $options['domain'] ?: self::$defaults['domain'];
215217

tests/system/Cookie/CookieTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Config\Cookie as CookieConfig;
1717
use DateTimeImmutable;
1818
use DateTimeZone;
19+
use Generator;
1920
use LogicException;
2021

2122
/**
@@ -76,6 +77,38 @@ public function testConfigInjectionForDefaults(): void
7677
Cookie::setDefaults($old);
7778
}
7879

80+
/**
81+
* @dataProvider prefixProvider
82+
*/
83+
public function testConfigPrefix(string $configPrefix, string $optionPrefix, string $expected): void
84+
{
85+
$config = new CookieConfig();
86+
$config->prefix = $configPrefix;
87+
Cookie::setDefaults($config);
88+
89+
$cookie = new Cookie(
90+
'test',
91+
'value',
92+
[
93+
'prefix' => $optionPrefix,
94+
]
95+
);
96+
97+
$this->assertSame($expected, $cookie->getPrefixedName());
98+
}
99+
100+
public function prefixProvider(): Generator
101+
{
102+
yield from [
103+
['prefix_', '', 'prefix_test'],
104+
['prefix_', '0', '0test'],
105+
['prefix_', 'new_', 'new_test'],
106+
['', '', 'test'],
107+
['', '0', '0test'],
108+
['', 'new_', 'new_test'],
109+
];
110+
}
111+
79112
public function testValidationOfRawCookieName(): void
80113
{
81114
$this->expectException(CookieException::class);

0 commit comments

Comments
 (0)