Skip to content

Commit 4d61afd

Browse files
authored
Merge pull request #1286 from bcit-ci/cookies
Cookies
2 parents e7e196b + 3544dfa commit 4d61afd

4 files changed

Lines changed: 46 additions & 81 deletions

File tree

system/HTTP/Response.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public function send()
640640
{
641641
$this->CSP->finalize($this);
642642
}else{
643-
643+
644644
$this->body = str_replace(['{csp-style-nonce}','{csp-script-nonce}'], '', $this->body);
645645
}
646646

@@ -911,6 +911,37 @@ public function getCookie(string $name, string $prefix = '')
911911
}
912912
}
913913

914+
/**
915+
* Sets a cookie to be deleted when the response is sent.
916+
*
917+
* @param $name
918+
* @param string $domain
919+
* @param string $path
920+
* @param string $prefix
921+
*/
922+
public function deleteCookie($name, string $domain = '', string $path = '/', string $prefix = '')
923+
{
924+
if ($prefix === '' && $this->cookiePrefix !== '')
925+
{
926+
$prefix = $this->cookiePrefix;
927+
}
928+
929+
$name = $prefix.$name;
930+
931+
foreach ($this->cookies as &$cookie)
932+
{
933+
if ($cookie['name'] == $name)
934+
{
935+
$cookie['value'] = '';
936+
$cookie['expires'] = '';
937+
938+
break;
939+
}
940+
}
941+
942+
return $this;
943+
}
944+
914945
/**
915946
* Actually sets the cookies.
916947
*/

system/Helpers/cookie_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function get_cookie($index, bool $xssClean = false)
126126
*/
127127
function delete_cookie($name, string $domain = '', string $path = '/', string $prefix = '')
128128
{
129-
set_cookie($name, '', '', $domain, $path, $prefix);
129+
\Config\Services::response()->deleteCookie($name, $domain, $path, $prefix);
130130
}
131131

132132
}

tests/_support/HTTP/MockResponse.php

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,11 @@
77
*/
88
class MockResponse extends Response
99
{
10-
public function setCookie(
11-
$name,
12-
$value = '',
13-
$expire = '',
14-
$domain = '',
15-
$path = '/',
16-
$prefix = '',
17-
$secure = false,
18-
$httponly = false
19-
)
20-
{
21-
if (is_array($name))
22-
{
23-
foreach
24-
(
25-
[
26-
'value',
27-
'expire',
28-
'domain',
29-
'path',
30-
'prefix',
31-
'secure',
32-
'httponly',
33-
'name'
34-
] as $item
35-
)
36-
{
37-
if (isset($name[$item]))
38-
{
39-
$$item = $name[$item];
40-
}
41-
}
42-
}
43-
44-
45-
$_COOKIE[$prefix . $name] = $value;
46-
47-
/*
48-
@todo: Find a way to use setcookie()
49-
without it throwing header issues.
50-
setcookie
51-
(
52-
$prefix.$name,
53-
$value,
54-
$expire,
55-
$path,
56-
$domain,
57-
$secure,
58-
$httponly
59-
);
60-
*/
61-
}
62-
63-
//--------------------------------------------------------------------
64-
65-
public function hasCookie(string $name, $value = null, string $prefix = ''): bool
66-
{
67-
return array_key_exists($name, $_COOKIE);
68-
}
69-
70-
//--------------------------------------------------------------------
71-
72-
public function deleteCookie
73-
(
74-
$name,
75-
string $domain = '',
76-
string $path = '/',
77-
string $prefix = ''
78-
)
79-
{
80-
$COOKIE[$name] = null;
81-
unset($COOKIE[$name]);
82-
83-
//set_cookie($name, '', '', $domain, $path, $prefix);
84-
}
10+
/**
11+
* If true, will not write output. Useful during testing.
12+
*
13+
* @var bool
14+
*/
15+
protected $pretend = true;
8516

8617
}

tests/system/Helpers/CookieHelperTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testSetCookieByArrayParameters()
5353
'expire' => $this->expire
5454
];
5555
set_cookie($cookieAttr);
56-
56+
5757
$this->assertTrue($this->response->hasCookie($this->name, $this->value));
5858

5959
delete_cookie($this->name);
@@ -84,12 +84,15 @@ public function testSetCookieSecured()
8484

8585
public function testDeleteCookie()
8686
{
87-
set_cookie($this->name, $this->value, $this->expire);
88-
//$this->response->setCookie($this->name, $this->value, $this->expire);
87+
$this->response->setCookie($this->name, $this->value, $this->expire);
8988

9089
delete_cookie($this->name);
9190

92-
$this->assertEmpty($this->response->getCookie($this->name));
91+
$cookie = $this->response->getCookie($this->name);
92+
93+
// The cookie is set to be cleared when the request is sent....
94+
$this->assertEquals('', $cookie['value']);
95+
$this->assertEquals('', $cookie['expires']);
9396
}
9497

9598
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)