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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion system/HTTP/Response.php
Original file line number Diff line number Diff line change
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,35 @@ 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!

}
}

return $this;
}

/**
* Actually sets the cookies.
*/
Expand Down
2 changes: 1 addition & 1 deletion system/Helpers/cookie_helper.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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