diff --git a/module/VuFind/src/VuFind/Cookie/CookieManager.php b/module/VuFind/src/VuFind/Cookie/CookieManager.php index 20a5af45e9b208180655a0424e69b82403f4fc7a..67357cbc8675d5c8464050dacdfddc388710f470 100644 --- a/module/VuFind/src/VuFind/Cookie/CookieManager.php +++ b/module/VuFind/src/VuFind/Cookie/CookieManager.php @@ -117,7 +117,20 @@ class CookieManager } /** - * Set a cookie. + * Support method for setGlobalCookie -- proxy PHP's setcookie() function + * for compatibility with unit testing. + * + * @return bool + */ + public function proxySetCookie() + { + // Special case: in test suite -- don't actually write headers! + return defined('VUFIND_PHPUNIT_RUNNING') + ? true : call_user_func_array('setcookie', func_get_args()); + } + + /** + * Support method for set() -- set the actual cookie in PHP. * * @param string $key Name of cookie to set * @param mixed $value Value to set @@ -125,25 +138,41 @@ class CookieManager * * @return bool */ - public function set($key, $value, $expire = 0) + public function setGlobalCookie($key, $value, $expire) { - if (is_array($value)) { - $success = true; - foreach ($value as $i => $curr) { - $lastSuccess = setcookie( - $key . '[' . $i . ']', $curr, $expire, - $this->path, $this->domain, $this->secure - ); - if (!$lastSuccess) { - $success = false; - } - } - } else { - $success = setcookie( + // Simple case: flat value. + if (!is_array($value)) { + return $this->proxySetCookie( $key, $value, $expire, $this->path, $this->domain, $this->secure ); } - if ($success) { + + // Complex case: array of values. + $success = true; + foreach ($value as $i => $curr) { + $lastSuccess = $this->proxySetCookie( + $key . '[' . $i . ']', $curr, $expire, + $this->path, $this->domain, $this->secure + ); + if (!$lastSuccess) { + $success = false; + } + } + return $success; + } + + /** + * Set a cookie. + * + * @param string $key Name of cookie to set + * @param mixed $value Value to set + * @param int $expire Cookie expiration time + * + * @return bool + */ + public function set($key, $value, $expire = 0) + { + if ($success = $this->setGlobalCookie($key, $value, $expire)) { $this->cookies[$key] = $value; } return $success; diff --git a/module/VuFind/tests/bootstrap.php b/module/VuFind/tests/bootstrap.php index e951bd4a78a7c9efff8f7454a84ca4a5d72bf3f9..7b32da1a8507841b71ec1f563d885572695d6d7c 100644 --- a/module/VuFind/tests/bootstrap.php +++ b/module/VuFind/tests/bootstrap.php @@ -45,7 +45,4 @@ if (file_exists('vendor/autoload.php')) { $loader->register(); } -define('PHPUNIT_SEARCH_FIXTURES', realpath(__DIR__ . '/../../VuFindSearch/tests/unit-tests/fixtures')); - -// Use output buffering -- some tests involve HTTP headers and will fail if there is output. -ob_start(); \ No newline at end of file +define('PHPUNIT_SEARCH_FIXTURES', realpath(__DIR__ . '/../../VuFindSearch/tests/unit-tests/fixtures')); \ No newline at end of file