Skip to content

Commit

Permalink
Cleaned up MockResponse. Refactored delete_cookie to work with the ne…
Browse files Browse the repository at this point in the history
…w system. Fixes #1263
  • Loading branch information
lonnieezell committed Oct 3, 2018
1 parent d60d04a commit e0109e8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 81 deletions.
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'] = '';
}
}

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

0 comments on commit e0109e8

Please sign in to comment.