Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies #1286

Merged
merged 4 commits into from Oct 4, 2018
Merged

Cookies #1286

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion system/HTTP/Response.php
Expand Up @@ -640,7 +640,7 @@ public function send()
{
$this->CSP->finalize($this);
}else{

$this->body = str_replace(['{csp-style-nonce}','{csp-script-nonce}'], '', $this->body);
}

Expand Down Expand Up @@ -911,6 +911,37 @@ public function getCookie(string $name, string $prefix = '')
}
}

/**
* Sets a cookie to be deleted when the response is sent.
*
* @param $name
* @param string $domain
* @param string $path
* @param string $prefix
*/
public function deleteCookie($name, string $domain = '', string $path = '/', string $prefix = '')
{
if ($prefix === '' && $this->cookiePrefix !== '')
{
$prefix = $this->cookiePrefix;
}

$name = $prefix.$name;

foreach ($this->cookies as &$cookie)
{
if ($cookie['name'] == $name)
{
$cookie['value'] = '';
$cookie['expires'] = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break; should can be here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. thanks!


break;
}
}

return $this;
}

/**
* Actually sets the cookies.
*/
Expand Down
2 changes: 1 addition & 1 deletion system/Helpers/cookie_helper.php
Expand Up @@ -126,7 +126,7 @@ function get_cookie($index, bool $xssClean = false)
*/
function delete_cookie($name, string $domain = '', string $path = '/', string $prefix = '')
{
set_cookie($name, '', '', $domain, $path, $prefix);
\Config\Services::response()->deleteCookie($name, $domain, $path, $prefix);
}

}
81 changes: 6 additions & 75 deletions tests/_support/HTTP/MockResponse.php
Expand Up @@ -7,80 +7,11 @@
*/
class MockResponse extends Response
{
public function setCookie(
$name,
$value = '',
$expire = '',
$domain = '',
$path = '/',
$prefix = '',
$secure = false,
$httponly = false
)
{
if (is_array($name))
{
foreach
(
[
'value',
'expire',
'domain',
'path',
'prefix',
'secure',
'httponly',
'name'
] as $item
)
{
if (isset($name[$item]))
{
$$item = $name[$item];
}
}
}


$_COOKIE[$prefix . $name] = $value;

/*
@todo: Find a way to use setcookie()
without it throwing header issues.
setcookie
(
$prefix.$name,
$value,
$expire,
$path,
$domain,
$secure,
$httponly
);
*/
}

//--------------------------------------------------------------------

public function hasCookie(string $name, $value = null, string $prefix = ''): bool
{
return array_key_exists($name, $_COOKIE);
}

//--------------------------------------------------------------------

public function deleteCookie
(
$name,
string $domain = '',
string $path = '/',
string $prefix = ''
)
{
$COOKIE[$name] = null;
unset($COOKIE[$name]);

//set_cookie($name, '', '', $domain, $path, $prefix);
}
/**
* If true, will not write output. Useful during testing.
*
* @var bool
*/
protected $pretend = true;

}
11 changes: 7 additions & 4 deletions tests/system/Helpers/CookieHelperTest.php
Expand Up @@ -53,7 +53,7 @@ public function testSetCookieByArrayParameters()
'expire' => $this->expire
];
set_cookie($cookieAttr);

$this->assertTrue($this->response->hasCookie($this->name, $this->value));

delete_cookie($this->name);
Expand Down Expand Up @@ -84,12 +84,15 @@ public function testSetCookieSecured()

public function testDeleteCookie()
{
set_cookie($this->name, $this->value, $this->expire);
//$this->response->setCookie($this->name, $this->value, $this->expire);
$this->response->setCookie($this->name, $this->value, $this->expire);

delete_cookie($this->name);

$this->assertEmpty($this->response->getCookie($this->name));
$cookie = $this->response->getCookie($this->name);

// The cookie is set to be cleared when the request is sent....
$this->assertEquals('', $cookie['value']);
$this->assertEquals('', $cookie['expires']);
}

//--------------------------------------------------------------------
Expand Down