diff --git a/system/Commands/Utilities/Environment.php b/system/Commands/Utilities/Environment.php index 99a90415ceea..2bf9e2186255 100644 --- a/system/Commands/Utilities/Environment.php +++ b/system/Commands/Utilities/Environment.php @@ -86,7 +86,7 @@ final class Environment extends BaseCommand public function run(array $params) { if ($params === []) { - CLI::write(sprintf('Your environment is currently set as %s.', CLI::color($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, 'green'))); + CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(service('superglobals')->server('CI_ENVIRONMENT') ?? ENVIRONMENT, 'green'))); CLI::newLine(); return EXIT_ERROR; @@ -119,7 +119,8 @@ public function run(array $params) // force DotEnv to reload the new environment // however we cannot redefine the ENVIRONMENT constant putenv('CI_ENVIRONMENT'); - unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']); + unset($_ENV['CI_ENVIRONMENT']); + service('superglobals')->unsetServer('CI_ENVIRONMENT'); (new DotEnv((new Paths())->envDirectory ?? ROOTPATH))->load(); CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green'); @@ -149,7 +150,7 @@ private function writeNewEnvironmentToEnvFile(string $newEnv): bool copy($baseEnv, $envFile); } - $pattern = preg_quote($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, '/'); + $pattern = preg_quote(service('superglobals')->server('CI_ENVIRONMENT') ?? ENVIRONMENT, '/'); $pattern = sprintf('/^[#\s]*CI_ENVIRONMENT[=\s]+%s$/m', $pattern); return file_put_contents( diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 2a6122cab01c..4f51c3c29437 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -87,17 +87,14 @@ public function run(array $params) // Set HTTP_HOST if ($host !== null) { - $request = service('request'); - $_SERVER = $request->getServer(); - $_SERVER['HTTP_HOST'] = $host; - $request->setGlobal('server', $_SERVER); + service('superglobals')->setServer('HTTP_HOST', $host); } $collection = service('routes')->loadRoutes(); // Reset HTTP_HOST if ($host !== null) { - unset($_SERVER['HTTP_HOST']); + service('superglobals')->unsetServer('HTTP_HOST'); } $methods = Router::HTTP_METHODS; diff --git a/system/Config/Services.php b/system/Config/Services.php index c2a73f839f28..cc3afa2c5591 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -543,7 +543,7 @@ public static function createRequest(App $config, bool $isCli = false): void $request = AppServices::incomingrequest($config); // guess at protocol if needed - $request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'); + $request->setProtocolVersion(static::superglobals()->server('SERVER_PROTOCOL') ?? 'HTTP/1.1'); } // Inject the request object into Services. @@ -746,13 +746,17 @@ public static function siteurifactory( public static function superglobals( ?array $server = null, ?array $get = null, + ?array $post = null, + ?array $cookie = null, + ?array $files = null, + ?array $request = null, bool $getShared = true, ) { if ($getShared) { - return static::getSharedInstance('superglobals', $server, $get); + return static::getSharedInstance('superglobals', $server, $get, $post, $cookie, $files, $request); } - return new Superglobals($server, $get); + return new Superglobals($server, $get, $post, $cookie, $files, $request); } /** diff --git a/system/HTTP/DownloadResponse.php b/system/HTTP/DownloadResponse.php index f6861d549e9f..5e1816e3afe1 100644 --- a/system/HTTP/DownloadResponse.php +++ b/system/HTTP/DownloadResponse.php @@ -171,9 +171,9 @@ private function getDownloadFileName(): string * * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ */ - // @todo: depend super global - if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) - && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) { + $userAgent = service('superglobals')->server('HTTP_USER_AGENT'); + if (count($x) !== 1 && $userAgent !== null + && preg_match('/Android\s(1|2\.[01])/', $userAgent)) { $x[count($x) - 1] = strtoupper($extension); $filename = implode('.', $x); } diff --git a/system/HTTP/Files/FileCollection.php b/system/HTTP/Files/FileCollection.php index 484b3ff74872..5e3599b89853 100644 --- a/system/HTTP/Files/FileCollection.php +++ b/system/HTTP/Files/FileCollection.php @@ -150,11 +150,13 @@ protected function populateFiles() $this->files = []; - if ($_FILES === []) { + $files = service('superglobals')->getFilesArray(); + + if ($files === []) { return; } - $files = $this->fixFilesArray($_FILES); + $files = $this->fixFilesArray($files); foreach ($files as $name => $file) { $this->files[$name] = $this->createFileObject($file); diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 7fb5cd85bca7..f9a57a4098ba 100644 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -266,7 +266,9 @@ public function isAJAX(): bool */ public function isSecure(): bool { - if (! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { + $https = service('superglobals')->server('HTTPS'); + + if ($https !== null && strtolower($https) !== 'off') { return true; } @@ -599,9 +601,9 @@ public function getPostGet($index = null, $filter = null, $flags = null) // Use $_POST directly here, since filter_has_var only // checks the initial POST data, not anything that might // have been added since. - return isset($_POST[$index]) + return service('superglobals')->post($index) !== null ? $this->getPost($index, $filter, $flags) - : (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags)); + : (service('superglobals')->get($index) !== null ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags)); } /** @@ -622,9 +624,9 @@ public function getGetPost($index = null, $filter = null, $flags = null) // Use $_GET directly here, since filter_has_var only // checks the initial GET data, not anything that might // have been added since. - return isset($_GET[$index]) + return service('superglobals')->get($index) !== null ? $this->getGet($index, $filter, $flags) - : (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags)); + : (service('superglobals')->post($index) !== null ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags)); } /** diff --git a/system/HTTP/MessageTrait.php b/system/HTTP/MessageTrait.php index 50daed9c0f8f..7a061a83301d 100644 --- a/system/HTTP/MessageTrait.php +++ b/system/HTTP/MessageTrait.php @@ -86,19 +86,21 @@ public function appendBody($data): self */ public function populateHeaders(): void { - $contentType = $_SERVER['CONTENT_TYPE'] ?? getenv('CONTENT_TYPE'); + $contentType = service('superglobals')->server('CONTENT_TYPE') ?? getenv('CONTENT_TYPE'); if (! empty($contentType)) { $this->setHeader('Content-Type', $contentType); } unset($contentType); - foreach (array_keys($_SERVER) as $key) { + $serverArray = service('superglobals')->getServerArray(); + + foreach (array_keys($serverArray) as $key) { if (sscanf($key, 'HTTP_%s', $header) === 1) { // take SOME_HEADER and turn it into Some-Header $header = str_replace('_', ' ', strtolower($header)); $header = str_replace(' ', '-', ucwords($header)); - $this->setHeader($header, $_SERVER[$key]); + $this->setHeader($header, $serverArray[$key]); // Add us to the header map, so we can find them case-insensitively $this->headerMap[strtolower($header)] = $header; diff --git a/system/HTTP/RedirectResponse.php b/system/HTTP/RedirectResponse.php index 5703d1d6a78d..50c6b649373e 100644 --- a/system/HTTP/RedirectResponse.php +++ b/system/HTTP/RedirectResponse.php @@ -93,8 +93,8 @@ public function withInput() { $session = service('session'); $session->setFlashdata('_ci_old_input', [ - 'get' => $_GET ?? [], // @phpstan-ignore nullCoalesce.variable - 'post' => $_POST ?? [], // @phpstan-ignore nullCoalesce.variable + 'get' => service('superglobals')->getGetArray(), + 'post' => service('superglobals')->getPostArray(), ]); $this->withErrors(); diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index e0efd46324a1..6adfe3794935 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -47,6 +47,8 @@ trait RequestTrait * Stores values we've retrieved from PHP globals. * * @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array} + * + * @deprecated 4.7.0 Use the Superglobals service instead */ protected $globals = []; @@ -231,8 +233,12 @@ public function getEnv($index = null, $filter = null, $flags = null) */ public function setGlobal(string $name, $value) { + // Keep BC with $globals array $this->globals[$name] = $value; + // Also update Superglobals via service + service('superglobals')->setGlobalArray($name, $value); + return $this; } @@ -342,6 +348,8 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f * @param 'cookie'|'get'|'post'|'request'|'server' $name Superglobal name (lowercase) * * @return void + * + * @deprecated 4.7.0 No longer needs to be called explicitly. Used internally to maintain BC with $globals. */ protected function populateGlobals(string $name) { @@ -349,28 +357,7 @@ protected function populateGlobals(string $name) $this->globals[$name] = []; } - // Don't populate ENV as it might contain - // sensitive data that we don't want to get logged. - switch ($name) { - case 'get': - $this->globals['get'] = $_GET; - break; - - case 'post': - $this->globals['post'] = $_POST; - break; - - case 'request': - $this->globals['request'] = $_REQUEST; - break; - - case 'cookie': - $this->globals['cookie'] = $_COOKIE; - break; - - case 'server': - $this->globals['server'] = $_SERVER; - break; - } + // Get data from Superglobals service instead of direct access + $this->globals[$name] = service('superglobals')->getGlobalArray($name); } } diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index d39e8f9fdfaf..211663bc7987 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -451,21 +451,26 @@ public function sendBody() public function redirect(string $uri, string $method = 'auto', ?int $code = null) { // IIS environment likely? Use 'refresh' for better compatibility + $superglobals = service('superglobals'); + $serverSoftware = $superglobals->server('SERVER_SOFTWARE'); if ( $method === 'auto' - && isset($_SERVER['SERVER_SOFTWARE']) - && str_contains($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') + && $serverSoftware !== null + && str_contains($serverSoftware, 'Microsoft-IIS') ) { $method = 'refresh'; } elseif ($method !== 'refresh' && $code === null) { // override status code for HTTP/1.1 & higher + $serverProtocol = $superglobals->server('SERVER_PROTOCOL'); + $requestMethod = $superglobals->server('REQUEST_METHOD'); if ( - isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) + $serverProtocol !== null + && $requestMethod !== null && $this->getProtocolVersion() >= 1.1 ) { - if ($_SERVER['REQUEST_METHOD'] === Method::GET) { + if ($requestMethod === Method::GET) { $code = 302; - } elseif (in_array($_SERVER['REQUEST_METHOD'], [Method::POST, Method::PUT, Method::DELETE], true)) { + } elseif (in_array($requestMethod, [Method::POST, Method::PUT, Method::DELETE], true)) { // reference: https://en.wikipedia.org/wiki/Post/Redirect/Get $code = 303; } else { diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index a9415821f970..6fa4777e2454 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -290,12 +290,17 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''): // If no selected state was submitted we will attempt to set it automatically if ($selected === []) { + $superglobals = service('superglobals'); if (is_array($data)) { - if (isset($data['name'], $_POST[$data['name']])) { - $selected = [$_POST[$data['name']]]; + $postValue = $superglobals->post($data['name'] ?? ''); + if (isset($data['name']) && $postValue !== null) { + $selected = [$postValue]; + } + } else { + $postValue = $superglobals->post($data); + if ($postValue !== null) { + $selected = [$postValue]; } - } elseif (isset($_POST[$data])) { - $selected = [$_POST[$data]]; } } diff --git a/system/Log/Logger.php b/system/Log/Logger.php index 3770e3a6567f..8308ddaf94f7 100644 --- a/system/Log/Logger.php +++ b/system/Log/Logger.php @@ -310,8 +310,8 @@ protected function interpolate($message, array $context = []) $replace['{' . $key . '}'] = $val; } - $replace['{post_vars}'] = '$_POST: ' . print_r($_POST, true); - $replace['{get_vars}'] = '$_GET: ' . print_r($_GET, true); + $replace['{post_vars}'] = '$_POST: ' . print_r(service('superglobals')->getPostArray(), true); + $replace['{get_vars}'] = '$_GET: ' . print_r(service('superglobals')->getGetArray(), true); $replace['{env}'] = ENVIRONMENT; // Allow us to log the file/line that we are logging from diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 6d0f54d14a00..24d817af99d2 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -279,7 +279,7 @@ public function getPageURI(?int $page = null, string $group = 'default', bool $r } if ($this->only !== null) { - $query = array_intersect_key($_GET, array_flip($this->only)); + $query = array_intersect_key(service('superglobals')->getGetArray(), array_flip($this->only)); if (! $segment) { $query[$this->groups[$group]['pageSelector']] = $page; @@ -411,8 +411,9 @@ protected function ensureGroup(string $group, ?int $perPage = null) $this->calculateCurrentPage($group); - if ($_GET !== []) { - $this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET); + $get = service('superglobals')->getGetArray(); + if ($get !== []) { + $this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($get); } } @@ -433,7 +434,7 @@ protected function calculateCurrentPage(string $group) } else { $pageSelector = $this->groups[$group]['pageSelector']; - $page = (int) ($_GET[$pageSelector] ?? 1); + $page = (int) (service('superglobals')->get($pageSelector) ?? 1); $this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page; } diff --git a/system/Router/Router.php b/system/Router/Router.php index f2b291e58bbb..4c2ba230f6a0 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -167,7 +167,7 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request $this->controller = $this->collection->getDefaultController(); $this->method = $this->collection->getDefaultMethod(); - $this->collection->setHTTPVerb($request->getMethod() === '' ? $_SERVER['REQUEST_METHOD'] : $request->getMethod()); + $this->collection->setHTTPVerb($request->getMethod() === '' ? service('superglobals')->server('REQUEST_METHOD') : $request->getMethod()); $this->translateURIDashes = $this->collection->shouldTranslateURIDashes(); diff --git a/system/Security/Security.php b/system/Security/Security.php index c367d2d3b412..5e7d0bfeb679 100644 --- a/system/Security/Security.php +++ b/system/Security/Security.php @@ -281,10 +281,11 @@ private function removeTokenInRequest(RequestInterface $request): void { assert($request instanceof Request); - if (isset($_POST[$this->config->tokenName])) { + $superglobals = service('superglobals'); + if ($superglobals->post($this->config->tokenName) !== null) { // We kill this since we're done and we don't want to pollute the POST array. - unset($_POST[$this->config->tokenName]); - $request->setGlobal('post', $_POST); + $superglobals->unsetPost($this->config->tokenName); + $request->setGlobal('post', $superglobals->getPostArray()); } else { $body = $request->getBody() ?? ''; $json = json_decode($body); diff --git a/system/Session/Session.php b/system/Session/Session.php index f3dd570556c6..e465733bb8f3 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -243,7 +243,8 @@ public function start() $this->startSession(); // Is session ID auto-regeneration configured? (ignoring ajax requests) - if ((! isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') + $requestedWith = service('superglobals')->server('HTTP_X_REQUESTED_WITH'); + if (($requestedWith === null || strtolower($requestedWith) !== 'xmlhttprequest') && ($regenerateTime = $this->config->timeToUpdate) > 0 ) { if (! isset($_SESSION['__ci_last_regenerate'])) { diff --git a/system/Superglobals.php b/system/Superglobals.php index d804de2bc541..b46ef7079764 100644 --- a/system/Superglobals.php +++ b/system/Superglobals.php @@ -13,51 +13,381 @@ namespace CodeIgniter; +use CodeIgniter\Exceptions\InvalidArgumentException; + /** * Superglobals manipulation. * + * Provides a clean API for accessing and manipulating PHP superglobals + * with support for testing and backward compatibility. + * + * Note on return types: + * - $_SERVER can contain int (argc, REQUEST_TIME) or float (REQUEST_TIME_FLOAT) + * - $_SERVER['argv'] is an array + * - $_GET, $_POST, $_REQUEST can contain nested arrays from query params like ?foo[bar]=value + * - $_COOKIE typically contains strings but can have arrays with cookie[key] notation + * + * @phpstan-type server_items array|float|int|string + * @phpstan-type get_items array|string + * @phpstan-type post_items array|string + * @phpstan-type cookie_items array|string + * @phpstan-type files_items array + * @phpstan-type request_items array|string + * * @internal * @see \CodeIgniter\SuperglobalsTest */ final class Superglobals { - private array $server; - private array $get; - - public function __construct(?array $server = null, ?array $get = null) - { - $this->server = $server ?? $_SERVER; - $this->get = $get ?? $_GET; + /** + * @param array|null $server + * @param array|null $get + * @param array|null $post + * @param array|null $cookie + * @param array|null $files + * @param array|null $request + */ + public function __construct( + private ?array $server = null, + private ?array $get = null, + private ?array $post = null, + private ?array $cookie = null, + private ?array $files = null, + private ?array $request = null, + ) { + $this->server ??= $_SERVER; + $this->get ??= $_GET; + $this->post ??= $_POST; + $this->cookie ??= $_COOKIE; + $this->files ??= $_FILES; + $this->request ??= $_REQUEST; } - public function server(string $key): ?string + /** + * Get a value from $_SERVER. + * + * @param server_items|null $default + * + * @return server_items|null + */ + public function server(string $key, mixed $default = null): array|float|int|string|null { - return $this->server[$key] ?? null; + return $this->server[$key] ?? $default; } - public function setServer(string $key, string $value): void + /** + * Set a value in $_SERVER. + * + * @param server_items $value + */ + public function setServer(string $key, array|float|int|string $value): void { $this->server[$key] = $value; $_SERVER[$key] = $value; } /** - * @return array|string|null + * Remove a key from $_SERVER. + */ + public function unsetServer(string $key): void + { + unset($this->server[$key], $_SERVER[$key]); + } + + /** + * Get all $_SERVER values. + * + * @return array */ - public function get(string $key) + public function getServerArray(): array { - return $this->get[$key] ?? null; + return $this->server; } - public function setGet(string $key, string $value): void + /** + * Set the entire $_SERVER array. + * + * @param array $array + */ + public function setServerArray(array $array): void + { + $this->server = $array; + $_SERVER = $array; + } + + /** + * Get a value from $_GET. + * + * @param get_items|null $default + * + * @return get_items|null + */ + public function get(string $key, mixed $default = null): array|string|null + { + return $this->get[$key] ?? $default; + } + + /** + * Set a value in $_GET. + * + * @param get_items $value + */ + public function setGet(string $key, array|string $value): void { $this->get[$key] = $value; $_GET[$key] = $value; } + /** + * Remove a key from $_GET. + */ + public function unsetGet(string $key): void + { + unset($this->get[$key], $_GET[$key]); + } + + /** + * Get all $_GET values. + * + * @return array + */ + public function getGetArray(): array + { + return $this->get; + } + + /** + * Set the entire $_GET array. + * + * @param array $array + */ public function setGetArray(array $array): void { $this->get = $array; $_GET = $array; } + + /** + * Get a value from $_POST. + * + * @param post_items|null $default + * + * @return post_items|null + */ + public function post(string $key, mixed $default = null): array|string|null + { + return $this->post[$key] ?? $default; + } + + /** + * Set a value in $_POST. + * + * @param post_items $value + */ + public function setPost(string $key, array|string $value): void + { + $this->post[$key] = $value; + $_POST[$key] = $value; + } + + /** + * Remove a key from $_POST. + */ + public function unsetPost(string $key): void + { + unset($this->post[$key], $_POST[$key]); + } + + /** + * Get all $_POST values. + * + * @return array + */ + public function getPostArray(): array + { + return $this->post; + } + + /** + * Set the entire $_POST array. + * + * @param array $array + */ + public function setPostArray(array $array): void + { + $this->post = $array; + $_POST = $array; + } + + /** + * Get a value from $_COOKIE. + * + * @param cookie_items|null $default + * + * @return cookie_items|null + */ + public function cookie(string $key, mixed $default = null): array|string|null + { + return $this->cookie[$key] ?? $default; + } + + /** + * Set a value in $_COOKIE. + * + * @param cookie_items $value + */ + public function setCookie(string $key, array|string $value): void + { + $this->cookie[$key] = $value; + $_COOKIE[$key] = $value; + } + + /** + * Remove a key from $_COOKIE. + */ + public function unsetCookie(string $key): void + { + unset($this->cookie[$key], $_COOKIE[$key]); + } + + /** + * Get all $_COOKIE values. + * + * @return array + */ + public function getCookieArray(): array + { + return $this->cookie; + } + + /** + * Set the entire $_COOKIE array. + * + * @param array $array + */ + public function setCookieArray(array $array): void + { + $this->cookie = $array; + $_COOKIE = $array; + } + + /** + * Get a value from $_REQUEST. + * + * @param request_items|null $default + * + * @return request_items|null + */ + public function request(string $key, mixed $default = null): array|string|null + { + return $this->request[$key] ?? $default; + } + + /** + * Set a value in $_REQUEST. + * + * @param request_items $value + */ + public function setRequest(string $key, array|string $value): void + { + $this->request[$key] = $value; + $_REQUEST[$key] = $value; + } + + /** + * Remove a key from $_REQUEST. + */ + public function unsetRequest(string $key): void + { + unset($this->request[$key], $_REQUEST[$key]); + } + + /** + * Get all $_REQUEST values. + * + * @return array + */ + public function getRequestArray(): array + { + return $this->request; + } + + /** + * Set the entire $_REQUEST array. + * + * @param array $array + */ + public function setRequestArray(array $array): void + { + $this->request = $array; + $_REQUEST = $array; + } + + /** + * Get all $_FILES values. + * + * @return files_items + */ + public function getFilesArray(): array + { + return $this->files; + } + + /** + * Set the entire $_FILES array. + * + * @param files_items $array + */ + public function setFilesArray(array $array): void + { + $this->files = $array; + $_FILES = $array; + } + + /** + * Get a superglobal array by name. + * + * @param string $name The superglobal name (server, get, post, cookie, files, request) + * + * @return array + * + * @throws InvalidArgumentException If the superglobal name is invalid + */ + public function getGlobalArray(string $name): array + { + return match ($name) { + 'server' => $this->server, + 'get' => $this->get, + 'post' => $this->post, + 'cookie' => $this->cookie, + 'files' => $this->files, + 'request' => $this->request, + default => throw new InvalidArgumentException( + "Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.", + ), + }; + } + + /** + * Set a superglobal array by name. + * + * @param string $name The superglobal name (server, get, post, cookie, files, request) + * @param array $array The array to set + * + * @throws InvalidArgumentException If the superglobal name is invalid + */ + public function setGlobalArray(string $name, array $array): void + { + match ($name) { + 'server' => $this->setServerArray($array), + 'get' => $this->setGetArray($array), + 'post' => $this->setPostArray($array), + 'cookie' => $this->setCookieArray($array), + 'files' => $this->setFilesArray($array), + 'request' => $this->setRequestArray($array), + default => throw new InvalidArgumentException( + "Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.", + ), + }; + } } diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index c3b974448e43..a19e396e2392 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -397,7 +397,7 @@ protected function populateGlobals(string $name, Request $request, ?array $param } if ($name === 'post') { - $request->setGlobal($name, $params); + $request->setGlobal($name, $params ?? []); $request->setGlobal( 'request', (array) $request->fetchGlobal('post') + (array) $request->fetchGlobal('get'), diff --git a/tests/system/API/TransformerTest.php b/tests/system/API/TransformerTest.php index 30e4b149de99..9d780d7b437f 100644 --- a/tests/system/API/TransformerTest.php +++ b/tests/system/API/TransformerTest.php @@ -44,6 +44,8 @@ private function createMockRequest(string $query = ''): IncomingRequest if ($query !== '') { parse_str($query, $get); $request->setGlobal('get', $get); + } else { + $request->setGlobal('get', []); } return $request; diff --git a/tests/system/CLI/CLITest.php b/tests/system/CLI/CLITest.php index b29ef7bfc12f..146d1066766e 100644 --- a/tests/system/CLI/CLITest.php +++ b/tests/system/CLI/CLITest.php @@ -13,7 +13,9 @@ namespace CodeIgniter\CLI; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\RuntimeException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\PhpStreamWrapper; use CodeIgniter\Test\StreamFilterTrait; @@ -29,6 +31,13 @@ final class CLITest extends CIUnitTestCase { use StreamFilterTrait; + protected function setUp(): void + { + parent::setUp(); + + Services::injectMock('superglobals', new Superglobals()); + } + public function testNew(): void { $actual = new CLI(); @@ -454,12 +463,12 @@ public function testWrap(): void public function testParseCommand(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'ignored', 'b', 'c', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); CLI::init(); $this->assertNull(CLI::getSegment(3)); @@ -473,7 +482,7 @@ public function testParseCommand(): void public function testParseCommandMixed(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'ignored', 'b', 'c', @@ -485,7 +494,7 @@ public function testParseCommandMixed(): void '--fix', '--opt-in', 'sure', - ]; + ]); CLI::init(); $this->assertNull(CLI::getSegment(7)); @@ -502,14 +511,14 @@ public function testParseCommandMixed(): void public function testParseCommandOption(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'ignored', 'b', 'c', '--parm', 'pvalue', 'd', - ]; + ]); CLI::init(); $this->assertSame(['parm' => 'pvalue'], CLI::getOptions()); @@ -524,7 +533,7 @@ public function testParseCommandOption(): void public function testParseCommandMultipleOptions(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'ignored', 'b', 'c', @@ -534,7 +543,7 @@ public function testParseCommandMultipleOptions(): void '--p2', '--p3', 'value 3', - ]; + ]); CLI::init(); $this->assertSame(['parm' => 'pvalue', 'p2' => null, 'p3' => 'value 3'], CLI::getOptions()); diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index 4d66a48447b4..8110698c4212 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -15,7 +15,9 @@ use CodeIgniter\CodeIgniter; use CodeIgniter\Config\DotEnv; +use CodeIgniter\Config\Services; use CodeIgniter\Events\Events; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCLIConfig; use CodeIgniter\Test\Mock\MockCodeIgniter; @@ -34,12 +36,14 @@ protected function setUp(): void { parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $env = new DotEnv(ROOTPATH); $env->load(); // Set environment values that would otherwise stop the framework from functioning during tests. - if (! isset($_SERVER['app.baseURL'])) { - $_SERVER['app.baseURL'] = 'http://example.com/'; + if (service('superglobals')->server('app.baseURL') === null) { + service('superglobals')->setServer('app.baseURL', 'http://example.com/'); } $this->app = new MockCodeIgniter(new MockCLIConfig()); @@ -173,8 +177,8 @@ public function testHelpArgumentAndHelpOptionCombined(): void */ protected function initCLI(...$command): void { - $_SERVER['argv'] = ['spark', ...$command]; - $_SERVER['argc'] = count($_SERVER['argv']); + service('superglobals')->setServer('argv', ['spark', ...$command]); + service('superglobals')->setServer('argc', count(service('superglobals')->server('argv'))); CLI::init(); } diff --git a/tests/system/Cache/ResponseCacheTest.php b/tests/system/Cache/ResponseCacheTest.php index 198caefbc015..a05d37d96f54 100644 --- a/tests/system/Cache/ResponseCacheTest.php +++ b/tests/system/Cache/ResponseCacheTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Cache; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; @@ -20,6 +21,7 @@ use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCache; use Config\App; @@ -35,6 +37,13 @@ #[Group('Others')] final class ResponseCacheTest extends CIUnitTestCase { + protected function setUp(): void + { + parent::setUp(); + + Services::injectMock('superglobals', new Superglobals()); + } + /** * @param array $query */ @@ -58,7 +67,7 @@ private function createIncomingRequest(string $uri = '', array $query = [], App */ private function createCLIRequest(array $params = [], App $app = new App()): CLIRequest { - $_SERVER['argv'] = ['public/index.php', ...$params]; + service('superglobals')->setServer('argv', ['public/index.php', ...$params]); $superglobals = service('superglobals'); $superglobals->setServer('SCRIPT_NAME', 'public/index.php'); diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index 05e20cd7b361..3b19701e1959 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -55,7 +55,9 @@ protected function setUp(): void parent::setUp(); $this->resetServices(); - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + Services::injectMock('superglobals', new Superglobals()); + + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); $this->codeigniter = new MockCodeIgniter(new App()); @@ -72,8 +74,9 @@ protected function tearDown(): void public function testRunEmptyDefaultRoute(): void { - $_SERVER['argv'] = ['index.php']; - $_SERVER['argc'] = 1; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php']); + $superglobals->setServer('argc', 1); ob_start(); $this->codeigniter->run(); @@ -94,8 +97,9 @@ public function testOutputBufferingControl(): void public function testRunEmptyDefaultRouteReturnResponse(): void { - $_SERVER['argv'] = ['index.php']; - $_SERVER['argc'] = 1; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php']); + $superglobals->setServer('argc', 1); $response = $this->codeigniter->run(null, true); $this->assertInstanceOf(ResponseInterface::class, $response); @@ -105,11 +109,11 @@ public function testRunEmptyDefaultRouteReturnResponse(): void public function testRunClosureRoute(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; - - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', 'pages/about']); + $superglobals->setServer('argc', 2); + $superglobals->setServer('REQUEST_URI', '/pages/about'); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -131,9 +135,10 @@ public function testRunClosureRoute(): void */ public function testRun404Override(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + $superglobals = service('superglobals'); + $superglobals->setServer('REQUEST_METHOD', 'GET'); + $superglobals->setServer('REQUEST_URI', '/pages/about'); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -152,8 +157,9 @@ public function testRun404Override(): void public function testRun404OverrideControllerReturnsResponse(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', '/']); + $superglobals->setServer('argc', 2); // Inject mock router. $routes = service('routes'); @@ -171,8 +177,9 @@ public function testRun404OverrideControllerReturnsResponse(): void public function testRun404OverrideReturnResponse(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', '/']); + $superglobals->setServer('argc', 2); // Inject mock router. $routes = service('routes'); @@ -189,8 +196,9 @@ public function testRun404OverrideReturnResponse(): void public function testRun404OverrideByClosure(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', '/']); + $superglobals->setServer('argc', 2); // Inject mock router. $routes = new RouteCollection(service('locator'), new Modules(), new Routing()); @@ -211,11 +219,11 @@ public function testRun404OverrideByClosure(): void public function testControllersCanReturnString(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; - - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', 'pages/about']); + $superglobals->setServer('argc', 2); + $superglobals->setServer('REQUEST_URI', '/pages/about'); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -235,11 +243,11 @@ public function testControllersCanReturnString(): void public function testControllersCanReturnResponseObject(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; - - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', 'pages/about']); + $superglobals->setServer('argc', 2); + $superglobals->setServer('REQUEST_URI', '/pages/about'); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -264,11 +272,11 @@ public function testControllersCanReturnResponseObject(): void */ public function testControllersCanReturnDownloadResponseObject(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'pages/about']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/pages/about'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -289,11 +297,11 @@ public function testControllersCanReturnDownloadResponseObject(): void public function testRunExecuteFilterByClassName(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'pages/about']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/pages/about'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -317,11 +325,11 @@ public function testRunExecuteFilterByClassName(): void public function testRegisterSameFilterTwiceWithDifferentArgument(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'pages/about']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/pages/about'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); $routes = service('routes'); $routes->add( @@ -355,11 +363,11 @@ public function testRegisterSameFilterTwiceWithDifferentArgument(): void public function testDisableControllerFilters(): void { - $_SERVER['argv'] = ['index.php', 'pages/about']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'pages/about']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/pages/about'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/pages/about'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -383,8 +391,8 @@ public function testDisableControllerFilters(): void public function testResponseConfigEmpty(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); $response = service('response', null, false); @@ -393,8 +401,8 @@ public function testResponseConfigEmpty(): void public function testRoutesIsEmpty(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); // Inject mock router. $router = service('router', null, service('incomingrequest'), false); @@ -409,10 +417,10 @@ public function testRoutesIsEmpty(): void public function testTransfersCorrectHTTPVersion(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/2.0'; + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/2.0'); ob_start(); $this->codeigniter->run(); @@ -425,10 +433,10 @@ public function testTransfersCorrectHTTPVersion(): void public function testSupportsHttp3(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/3.0'; + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/3.0'); ob_start(); $this->codeigniter->run(); @@ -441,8 +449,8 @@ public function testSupportsHttp3(): void public function testIgnoringErrorSuppressedByAt(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); ob_start(); @unlink('inexistent-file'); @@ -454,8 +462,8 @@ public function testIgnoringErrorSuppressedByAt(): void public function testRunForceSecure(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); $filterConfig = config(FiltersConfig::class); $filterConfig->required['before'][] = 'forcehttps'; @@ -480,11 +488,11 @@ public function testRunForceSecure(): void public function testRunRedirectionWithNamed(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -504,11 +512,11 @@ public function testRunRedirectionWithNamed(): void public function testRunRedirectionWithURI(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -531,13 +539,13 @@ public function testRunRedirectionWithURI(): void */ public function testRunRedirectionWithGET(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Inject mock router. $routes = service('routes'); @@ -558,13 +566,13 @@ public function testRunRedirectionWithGET(): void public function testRunRedirectionWithGETAndHTTPCode301(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Inject mock router. $routes = service('routes'); @@ -583,13 +591,13 @@ public function testRunRedirectionWithGETAndHTTPCode301(): void public function testRunRedirectionWithPOSTAndHTTPCode301(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); // Inject mock router. $routes = service('routes'); @@ -608,8 +616,8 @@ public function testRunRedirectionWithPOSTAndHTTPCode301(): void public function testStoresPreviousURL(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); // Inject mock router. $router = service('router', null, service('incomingrequest'), false); @@ -625,13 +633,13 @@ public function testStoresPreviousURL(): void public function testNotStoresPreviousURL(): void { - $_SERVER['argv'] = ['index.php', 'example']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'example']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/example'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Inject mock router. $routes = service('routes'); @@ -649,11 +657,11 @@ public function testNotStoresPreviousURL(): void public function testNotStoresPreviousURLByCheckingContentType(): void { - $_SERVER['argv'] = ['index.php', 'image']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'image']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/image'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/image'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -679,8 +687,8 @@ public function testNotStoresPreviousURLByCheckingContentType(): void */ public function testRunDefaultRoute(): void { - $_SERVER['argv'] = ['index.php', '/']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', '/']); + service('superglobals')->setServer('argc', 2); ob_start(); $this->codeigniter->run(); @@ -691,13 +699,13 @@ public function testRunDefaultRoute(): void public function testRunCLIRoute(): void { - $_SERVER['argv'] = ['index.php', 'cli']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'cli']); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/cli'; - $_SERVER['SCRIPT_NAME'] = 'public/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'CLI'; + service('superglobals')->setServer('REQUEST_URI', '/cli'); + service('superglobals')->setServer('SCRIPT_NAME', 'public/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'CLI'); $routes = service('routes'); $routes->cli('cli', '\Tests\Support\Controllers\Popcorn::index'); @@ -711,15 +719,15 @@ public function testRunCLIRoute(): void public function testSpoofRequestMethodCanUsePUT(): void { - $_SERVER['argv'] = ['index.php']; - $_SERVER['argc'] = 1; + service('superglobals')->setServer('argv', ['index.php']); + service('superglobals')->setServer('argc', 1); - $_SERVER['REQUEST_URI'] = '/'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); - $_POST['_method'] = Method::PUT; + service('superglobals')->setPost('_method', Method::PUT); $routes = service('routes'); $routes->setDefaultNamespace('App\Controllers'); @@ -736,15 +744,15 @@ public function testSpoofRequestMethodCanUsePUT(): void public function testSpoofRequestMethodCannotUseGET(): void { - $_SERVER['argv'] = ['index.php']; - $_SERVER['argc'] = 1; + service('superglobals')->setServer('argv', ['index.php']); + service('superglobals')->setServer('argc', 1); - $_SERVER['REQUEST_URI'] = '/'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); - $_POST['_method'] = 'GET'; + service('superglobals')->setPost('_method', 'GET'); $routes = service('routes'); $routes->setDefaultNamespace('App\Controllers'); @@ -772,8 +780,8 @@ public function testPageCacheSendSecureHeaders(): void // Clear Page cache command('cache:clear'); - $_SERVER['REQUEST_URI'] = '/test'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/test'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); $routes = service('routes'); $routes->add('test', static function () { @@ -850,9 +858,9 @@ public function testPageCacheWithCacheQueryString( // Generate request to each URL from the testing array foreach ($testingUrls as $testingUrl) { $this->resetServices(); - $_SERVER['REQUEST_URI'] = '/' . $testingUrl; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $this->codeigniter = new MockCodeIgniter(new App()); + service('superglobals')->setServer('REQUEST_URI', '/' . $testingUrl); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + $this->codeigniter = new MockCodeIgniter(new App()); $routes = service('routes', true); $routePath = explode('?', $testingUrl)[0]; @@ -931,11 +939,11 @@ public static function providePageCacheWithCacheQueryString(): iterable */ public function testRunControllerNotFoundBeforeFilter(): void { - $_SERVER['argv'] = ['index.php']; - $_SERVER['argc'] = 1; + service('superglobals')->setServer('argv', ['index.php']); + service('superglobals')->setServer('argc', 1); - $_SERVER['REQUEST_URI'] = '/cannotFound'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/cannotFound'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router. $routes = service('routes'); @@ -976,12 +984,12 @@ public function testStartControllerPermitsInvoke(): void public function testRouteAttributeCacheIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/cached']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/cached']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/cached'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); - Services::superglobals()->setServer('REQUEST_METHOD', 'GET'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/cached'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Clear cache before test cache()->clean(); @@ -1008,11 +1016,11 @@ public function testRouteAttributeCacheIntegration(): void // Second request - should return cached version with same timestamp $this->resetServices(); - $_SERVER['argv'] = ['index.php', 'attribute/cached']; - $_SERVER['argc'] = 2; - Services::superglobals()->setServer('REQUEST_URI', '/attribute/cached'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); - Services::superglobals()->setServer('REQUEST_METHOD', 'GET'); + service('superglobals')->setServer('argv', ['index.php', 'attribute/cached']); + service('superglobals')->setServer('argc', 2); + service('superglobals')->setServer('REQUEST_URI', '/attribute/cached'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->codeigniter = new MockCodeIgniter(new App()); $routes = service('routes'); @@ -1036,11 +1044,11 @@ public function testRouteAttributeCacheIntegration(): void public function testRouteAttributeFilterIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/filtered']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/filtered']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/filtered'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/filtered'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Register the test filter $filterConfig = config('Filters'); @@ -1064,11 +1072,11 @@ public function testRouteAttributeFilterIntegration(): void public function testRouteAttributeFilterWithParamsIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/filteredWithParams']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/filteredWithParams']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/filteredWithParams'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/filteredWithParams'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Register the test filter $filterConfig = config('Filters'); @@ -1092,11 +1100,11 @@ public function testRouteAttributeFilterWithParamsIntegration(): void public function testRouteAttributeRestrictIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/restricted']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/restricted']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/restricted'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/restricted'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router $routes = service('routes'); @@ -1114,11 +1122,11 @@ public function testRouteAttributeRestrictIntegration(): void public function testRouteAttributeRestrictThrowsException(): void { - $_SERVER['argv'] = ['index.php', 'attribute/restricted']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/restricted']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/shouldBeRestricted'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/shouldBeRestricted'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router $routes = service('routes'); @@ -1135,11 +1143,11 @@ public function testRouteAttributeRestrictThrowsException(): void public function testRouteAttributeMultipleAttributesIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/multiple']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/multiple']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/multiple'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/multiple'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Register the test filter $filterConfig = config('Filters'); @@ -1163,11 +1171,11 @@ public function testRouteAttributeMultipleAttributesIntegration(): void public function testRouteAttributeNoAttributesIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/none']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/none']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/none'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/none'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); // Inject mock router $routes = service('routes'); @@ -1185,12 +1193,12 @@ public function testRouteAttributeNoAttributesIntegration(): void public function testRouteAttributeCustomCacheKeyIntegration(): void { - $_SERVER['argv'] = ['index.php', 'attribute/customkey']; - $_SERVER['argc'] = 2; + service('superglobals')->setServer('argv', ['index.php', 'attribute/customkey']); + service('superglobals')->setServer('argc', 2); - Services::superglobals()->setServer('REQUEST_URI', '/attribute/customkey'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); - Services::superglobals()->setServer('REQUEST_METHOD', 'GET'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/customkey'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Clear cache before test cache()->clean(); @@ -1219,9 +1227,9 @@ public function testRouteAttributeCustomCacheKeyIntegration(): void public function testRouteAttributesDisabledInConfig(): void { - Services::superglobals()->setServer('REQUEST_URI', '/attribute/filtered'); - Services::superglobals()->setServer('SCRIPT_NAME', '/index.php'); - Services::superglobals()->setServer('REQUEST_METHOD', 'GET'); + service('superglobals')->setServer('REQUEST_URI', '/attribute/filtered'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); // Disable route attributes in config BEFORE creating CodeIgniter instance $routing = config('routing'); diff --git a/tests/system/Commands/EnvironmentCommandTest.php b/tests/system/Commands/EnvironmentCommandTest.php index b62ade32c827..597805ee8a38 100644 --- a/tests/system/Commands/EnvironmentCommandTest.php +++ b/tests/system/Commands/EnvironmentCommandTest.php @@ -13,6 +13,8 @@ namespace CodeIgniter\Commands; +use CodeIgniter\Config\Services; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; use PHPUnit\Framework\Attributes\Group; @@ -35,6 +37,8 @@ protected function setUp(): void if (is_file($this->envPath)) { rename($this->envPath, $this->backupEnvPath); } + + Services::injectMock('superglobals', new Superglobals()); } protected function tearDown(): void @@ -49,7 +53,8 @@ protected function tearDown(): void rename($this->backupEnvPath, $this->envPath); } - $_SERVER['CI_ENVIRONMENT'] = $_ENV['CI_ENVIRONMENT'] = ENVIRONMENT; + service('superglobals')->setServer('CI_ENVIRONMENT', ENVIRONMENT); + $_ENV['CI_ENVIRONMENT'] = ENVIRONMENT; } public function testUsingCommandWithNoArgumentsGivesCurrentEnvironment(): void @@ -90,7 +95,7 @@ public function testDefaultShippedEnvIsMissing(): void public function testSettingNewEnvIsSuccess(): void { // default env file has `production` env in it - $_SERVER['CI_ENVIRONMENT'] = 'production'; + service('superglobals')->setServer('CI_ENVIRONMENT', 'production'); command('env development'); $this->assertStringContainsString('Environment is successfully changed to', $this->getStreamFilterBuffer()); diff --git a/tests/system/Commands/GenerateKeyTest.php b/tests/system/Commands/GenerateKeyTest.php index 6dd7a7330c36..6ed0d6c94b93 100644 --- a/tests/system/Commands/GenerateKeyTest.php +++ b/tests/system/Commands/GenerateKeyTest.php @@ -13,6 +13,8 @@ namespace CodeIgniter\Commands; +use CodeIgniter\Config\Services; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Filters\CITestStreamFilter; use CodeIgniter\Test\StreamFilterTrait; @@ -37,6 +39,8 @@ protected function setUp(): void { parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $this->envPath = ROOTPATH . '.env'; $this->backupEnvPath = ROOTPATH . '.env.backup'; @@ -71,7 +75,8 @@ protected function getBuffer(): string protected function resetEnvironment(): void { putenv('encryption.key'); - unset($_ENV['encryption.key'], $_SERVER['encryption.key']); + unset($_ENV['encryption.key']); + service('superglobals')->unsetServer('encryption.key'); } public function testGenerateKeyShowsEncodedKey(): void diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index c058a270504e..462208100675 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Config\BaseService; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services as CodeIgniterServices; use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Exceptions\RedirectException; @@ -67,6 +68,8 @@ protected function setUp(): void $this->resetServices(); parent::setUp(); + + CodeIgniterServices::injectMock('superglobals', new Superglobals()); } public function testStringifyAttributes(): void @@ -103,7 +106,7 @@ public function testEnvReturnsDefault(): void public function testEnvGetsFromSERVER(): void { - $_SERVER['foo'] = 'bar'; + service('superglobals')->setServer('foo', 'bar'); $this->assertSame('bar', env('foo', 'baz')); } @@ -135,7 +138,7 @@ private function createRouteCollection(): RouteCollection public function testRedirectReturnsRedirectResponse(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $response = $this->createMock(Response::class); Services::injectMock('response', $response); @@ -425,7 +428,7 @@ public function testOldInput(): void { $this->injectSessionMock(); // setup from RedirectResponseTest... - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->config = new App(); $this->config->baseURL = 'http://example.com/'; @@ -437,12 +440,13 @@ public function testOldInput(): void Services::injectMock('request', $this->request); // setup & ask for a redirect... - $_SESSION = []; - $_GET = ['foo' => 'bar']; - $_POST = [ + $_SESSION = []; + $superglobals = service('superglobals'); + $superglobals->setGetArray(['foo' => 'bar']); + $superglobals->setPostArray([ 'bar' => 'baz', 'zibble' => 'fritz', - ]; + ]); $response = new RedirectResponse(new App()); $response->withInput(); @@ -459,7 +463,7 @@ public function testOldInputSerializeData(): void { $this->injectSessionMock(); // setup from RedirectResponseTest... - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->config = new App(); $this->config->baseURL = 'http://example.com/'; @@ -471,11 +475,12 @@ public function testOldInputSerializeData(): void Services::injectMock('request', $this->request); // setup & ask for a redirect... - $_SESSION = []; - $_GET = []; - $_POST = [ + $_SESSION = []; + $superglobals = service('superglobals'); + $superglobals->setGetArray([]); + $superglobals->setPostArray([ 'zibble' => serialize('fritz'), - ]; + ]); $response = new RedirectResponse(new App()); $response->withInput(); @@ -494,7 +499,7 @@ public function testOldInputArray(): void { $this->injectSessionMock(); // setup from RedirectResponseTest... - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->config = new App(); $this->config->baseURL = 'http://example.com/'; @@ -512,9 +517,10 @@ public function testOldInputArray(): void ]; // setup & ask for a redirect... - $_SESSION = []; - $_GET = []; - $_POST = ['location' => $locations]; + $_SESSION = []; + $superglobals = service('superglobals'); + $superglobals->setGetArray([]); + $superglobals->setPostArray(['location' => $locations]); $response = new RedirectResponse(new App()); $response->withInput(); diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 9158db34140f..5cc3d1682617 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Exceptions\RuntimeException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Modules; use Encryption; @@ -56,6 +57,8 @@ protected function setUp(): void } BaseConfig::reset(); + + Services::injectMock('superglobals', new Superglobals()); } protected function tearDown(): void @@ -198,7 +201,8 @@ public function testSetsDefaultValues(): void public function testSetsDefaultValuesEncryptionUsingHex2Bin(): void { putenv('encryption.key'); - unset($_ENV['encryption.key'], $_SERVER['encryption.key']); // @phpstan-ignore codeigniter.superglobalAccess + unset($_ENV['encryption.key']); + service('superglobals')->unsetServer('encryption.key'); $dotenv = new DotEnv($this->fixturesFolder, 'encryption.env'); $dotenv->load(); @@ -214,7 +218,8 @@ public function testSetsDefaultValuesEncryptionUsingHex2Bin(): void public function testSetDefaultValuesEncryptionUsingBase64(): void { putenv('encryption.key'); - unset($_ENV['encryption.key'], $_SERVER['encryption.key']); // @phpstan-ignore codeigniter.superglobalAccess + unset($_ENV['encryption.key']); + service('superglobals')->unsetServer('encryption.key'); $dotenv = new DotEnv($this->fixturesFolder, 'base64encryption.env'); $dotenv->load(); diff --git a/tests/system/Config/DotEnvTest.php b/tests/system/Config/DotEnvTest.php index 4507c8a3e8bf..505f4bedcbcb 100644 --- a/tests/system/Config/DotEnvTest.php +++ b/tests/system/Config/DotEnvTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Config; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamDirectory; @@ -46,6 +47,8 @@ protected function setUp(): void $file = 'unreadable.env'; $path = rtrim($this->fixturesFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file; chmod($path, 0644); + + Services::injectMock('superglobals', new Superglobals()); } protected function tearDown(): void @@ -180,8 +183,8 @@ public function testNamespacedVariables(): void public function testLoadsGetServerVar(): void { - $_SERVER['SER_VAR'] = 'TT'; - $dotenv = new DotEnv($this->fixturesFolder, 'nested.env'); + service('superglobals')->setServer('SER_VAR', 'TT'); + $dotenv = new DotEnv($this->fixturesFolder, 'nested.env'); $dotenv->load(); $this->assertSame('TT', $_ENV['NVAR7']); diff --git a/tests/system/Encryption/EncryptionTest.php b/tests/system/Encryption/EncryptionTest.php index de372e232411..48fd6dc78e73 100644 --- a/tests/system/Encryption/EncryptionTest.php +++ b/tests/system/Encryption/EncryptionTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter\Encryption; +use CodeIgniter\Config\Services as CodeIgniterServices; use CodeIgniter\Encryption\Exceptions\EncryptionException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Encryption as EncryptionConfig; use Config\Services; @@ -31,11 +33,14 @@ public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); + CodeIgniterServices::injectMock('superglobals', new Superglobals()); + if (is_file(ROOTPATH . '.env')) { rename(ROOTPATH . '.env', ROOTPATH . '.env.bak'); putenv('encryption.key'); - unset($_ENV['encryption.key'], $_SERVER['encryption.key']); + unset($_ENV['encryption.key']); + service('superglobals')->unsetServer('encryption.key'); } } diff --git a/tests/system/Filters/DebugToolbarTest.php b/tests/system/Filters/DebugToolbarTest.php index 4968cede04f0..905dd46cf15a 100644 --- a/tests/system/Filters/DebugToolbarTest.php +++ b/tests/system/Filters/DebugToolbarTest.php @@ -13,11 +13,13 @@ namespace CodeIgniter\Filters; +use CodeIgniter\Config\Services; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\ResponseInterface; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Filters as FilterConfig; use PHPUnit\Framework\Attributes\BackupGlobals; @@ -41,13 +43,15 @@ protected function setUp(): void { parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $this->request = service('request'); $this->response = service('response'); } public function testDebugToolbarFilter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = new FilterConfig(); $config->globals = [ diff --git a/tests/system/Filters/FiltersTest.php b/tests/system/Filters/FiltersTest.php index b2daf0ec2b07..bf65d43dd0de 100644 --- a/tests/system/Filters/FiltersTest.php +++ b/tests/system/Filters/FiltersTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Filters; +use CodeIgniter\Config\Services; use CodeIgniter\Filters\Exceptions\FilterException; use CodeIgniter\Filters\fixtures\GoogleCurious; use CodeIgniter\Filters\fixtures\GoogleEmpty; @@ -25,6 +26,7 @@ use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\ResponseInterface; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\ConfigFromArrayTrait; use CodeIgniter\Test\Mock\MockAppConfig; @@ -67,6 +69,7 @@ protected function setUp(): void service('autoloader')->addNamespace($defaults); $_SERVER = []; + Services::injectMock('superglobals', new Superglobals()); $this->response = service('response'); } @@ -80,11 +83,11 @@ private function createFilters(FiltersConfig $config, $request = null): Filters public function testProcessMethodDetectsCLI(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'spark', 'list', - ]; - $_SERVER['argc'] = 2; + ]); + service('superglobals')->setServer('argc', 2); $config = [ 'aliases' => ['foo' => ''], @@ -108,7 +111,7 @@ public function testProcessMethodDetectsCLI(): void public function testProcessMethodDetectsGetRequests(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['foo' => ''], @@ -129,7 +132,7 @@ public function testProcessMethodDetectsGetRequests(): void public function testProcessMethodRespectsMethod(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -154,7 +157,7 @@ public function testProcessMethodRespectsMethod(): void public function testProcessMethodIgnoresMethod(): void { - $_SERVER['REQUEST_METHOD'] = 'DELETE'; + service('superglobals')->setServer('REQUEST_METHOD', 'DELETE'); $config = [ 'aliases' => [ @@ -179,7 +182,7 @@ public function testProcessMethodIgnoresMethod(): void public function testProcessMethodProcessGlobals(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -216,7 +219,7 @@ public function testProcessMethodProcessGlobals(): void #[DataProvider('provideProcessMethodProcessGlobalsWithExcept')] public function testProcessMethodProcessGlobalsWithExcept($except): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -267,7 +270,7 @@ public static function provideProcessMethodProcessGlobalsWithExcept(): iterable public function testProcessMethodProcessesFiltersBefore(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -300,7 +303,7 @@ public function testProcessMethodProcessesFiltersBefore(): void public function testProcessMethodProcessesFiltersAfter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -333,7 +336,7 @@ public function testProcessMethodProcessesFiltersAfter(): void public function testProcessMethodProcessesCombined(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -381,7 +384,7 @@ public function testProcessMethodProcessesCombined(): void public function testProcessMethodProcessesCombinedAfterForToolbar(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -422,7 +425,7 @@ public function testProcessMethodProcessesCombinedAfterForToolbar(): void public function testRunThrowsWithInvalidAlias(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [], @@ -442,7 +445,7 @@ public function testRunThrowsWithInvalidAlias(): void public function testCustomFiltersLoad(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [], @@ -467,7 +470,7 @@ public function testCustomFiltersLoad(): void */ public function testAllCustomFiltersAreDiscoveredInConstructor(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [], @@ -482,7 +485,7 @@ public function testAllCustomFiltersAreDiscoveredInConstructor(): void public function testRunThrowsWithInvalidClassType(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['invalid' => InvalidClass::class], @@ -502,7 +505,7 @@ public function testRunThrowsWithInvalidClassType(): void public function testRunDoesBefore(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -522,7 +525,7 @@ public function testRunDoesBefore(): void public function testRunDoesAfter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -542,7 +545,7 @@ public function testRunDoesAfter(): void public function testShortCircuit(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['banana' => GoogleYou::class], @@ -563,7 +566,7 @@ public function testShortCircuit(): void public function testOtherResult(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -593,7 +596,7 @@ public function testOtherResult(): void #[DataProvider('provideBeforeExcept')] public function testBeforeExcept(string $uri, $except, array $expected): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -703,7 +706,7 @@ public static function provideBeforeExcept(): iterable public function testAfterExceptString(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -736,7 +739,7 @@ public function testAfterExceptString(): void public function testAfterExceptInapplicable(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -772,7 +775,7 @@ public function testAfterExceptInapplicable(): void public function testAddFilter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -793,7 +796,7 @@ public function testAddFilter(): void public function testAddFilterSection(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = []; $filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config); @@ -809,7 +812,7 @@ public function testAddFilterSection(): void public function testInitializeTwice(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = []; $filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config); @@ -826,7 +829,7 @@ public function testInitializeTwice(): void public function testEnableFilter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -847,7 +850,7 @@ public function testEnableFilter(): void public function testFiltersWithArguments(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['role' => Role::class], @@ -879,7 +882,7 @@ public function testFiltersWithArguments(): void public function testFilterWithDiffernetArguments(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['role' => Role::class], @@ -905,7 +908,7 @@ public function testFilterWithDiffernetArguments(): void public function testFilterWithoutArgumentsIsDefined(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['role' => Role::class], @@ -930,7 +933,7 @@ public function testFilterWithoutArgumentsIsDefined(): void public function testEnableFilterWithArguments(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['role' => Role::class], @@ -959,7 +962,7 @@ public function testEnableFilterWithArguments(): void public function testEnableFilterWithNoArguments(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['role' => Role::class], @@ -990,7 +993,7 @@ public function testEnableNonFilter(): void { $this->expectException(FilterException::class); - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -1011,7 +1014,7 @@ public function testEnableNonFilter(): void */ public function testMatchesURICaseInsensitively(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1056,7 +1059,7 @@ public function testMatchesURICaseInsensitively(): void public function testMatchesURIWithUnicode(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1105,7 +1108,7 @@ public function testMatchesURIWithUnicode(): void */ public function testFilterMatching(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1141,7 +1144,7 @@ public function testFilterMatching(): void */ public function testGlobalFilterMatching(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1183,7 +1186,7 @@ public function testGlobalFilterMatching(): void */ public function testCombinedFilterMatching(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1231,7 +1234,7 @@ public function testCombinedFilterMatching(): void */ public function testSegmentedFilterMatching(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1329,7 +1332,7 @@ public function testFilterClass(): void public function testReset(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => [ @@ -1352,7 +1355,7 @@ public function testReset(): void public function testRunRequiredDoesBefore(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], @@ -1371,7 +1374,7 @@ public function testRunRequiredDoesBefore(): void public function testRunRequiredDoesAfter(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $config = [ 'aliases' => ['google' => GoogleMe::class], diff --git a/tests/system/Filters/HoneypotTest.php b/tests/system/Filters/HoneypotTest.php index 7e29177f7d3f..630a882352ec 100644 --- a/tests/system/Filters/HoneypotTest.php +++ b/tests/system/Filters/HoneypotTest.php @@ -13,11 +13,13 @@ namespace CodeIgniter\Filters; +use CodeIgniter\Config\Services; use CodeIgniter\Honeypot\Exceptions\HoneypotException; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\Response; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Honeypot; use PHPUnit\Framework\Attributes\BackupGlobals; @@ -50,9 +52,10 @@ protected function setUp(): void $this->config = new \Config\Filters(); $this->honey = new Honeypot(); - unset($_POST[$this->honey->name]); - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST[$this->honey->name] = 'hey'; + Services::injectMock('superglobals', new Superglobals()); + $superglobals = service('superglobals'); + $superglobals->setServer('REQUEST_METHOD', 'POST'); + $superglobals->setPost($this->honey->name, 'hey'); } public function testBeforeTriggered(): void @@ -79,7 +82,7 @@ public function testBeforeClean(): void 'after' => [], ]; - unset($_POST[$this->honey->name]); + service('superglobals')->unsetPost($this->honey->name); $this->request = service('request', null, false); $this->response = service('response'); diff --git a/tests/system/Filters/InvalidCharsTest.php b/tests/system/Filters/InvalidCharsTest.php index a0546ef586bc..6e2dbe0f6ede 100644 --- a/tests/system/Filters/InvalidCharsTest.php +++ b/tests/system/Filters/InvalidCharsTest.php @@ -13,11 +13,13 @@ namespace CodeIgniter\Filters; +use CodeIgniter\Config\Services; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Security\Exceptions\SecurityException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockAppConfig; use PHPUnit\Framework\Attributes\DataProvider; @@ -41,6 +43,8 @@ protected function setUp(): void $_POST = []; $_COOKIE = []; + Services::injectMock('superglobals', new Superglobals()); + $this->request = $this->createRequest(); $this->invalidChars = new InvalidChars(); } @@ -49,9 +53,10 @@ protected function tearDown(): void { parent::tearDown(); - $_GET = []; - $_POST = []; - $_COOKIE = []; + $superglobals = service('superglobals'); + $superglobals->setGetArray([]); + $superglobals->setPostArray([]); + $superglobals->setCookieArray([]); } private function createRequest(): IncomingRequest @@ -79,10 +84,11 @@ public function testBeforeDoNothingWhenCLIRequest(): void #[DoesNotPerformAssertions] public function testBeforeValidString(): void { - $_POST['val'] = [ + $superglobals = service('superglobals'); + $superglobals->setPost('val', [ 'valid string', - ]; - $_COOKIE['val'] = 'valid string'; + ]); + $superglobals->setCookie('val', 'valid string'); $this->invalidChars->before($this->request); } @@ -92,11 +98,11 @@ public function testBeforeInvalidUTF8StringCausesException(): void $this->expectException(SecurityException::class); $this->expectExceptionMessage('Invalid UTF-8 characters in post:'); - $sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS'); - $_POST['val'] = [ + $sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS'); + service('superglobals')->setPost('val', [ 'valid string', $sjisString, - ]; + ]); $this->invalidChars->before($this->request); } @@ -107,7 +113,7 @@ public function testBeforeInvalidControlCharCausesException(): void $this->expectExceptionMessage('Invalid Control characters in cookie:'); $stringWithNullChar = "String contains null char and line break.\0\n"; - $_COOKIE['val'] = $stringWithNullChar; + service('superglobals')->setCookie('val', $stringWithNullChar); $this->invalidChars->before($this->request); } @@ -116,7 +122,7 @@ public function testBeforeInvalidControlCharCausesException(): void #[DoesNotPerformAssertions] public function testCheckControlStringWithLineBreakAndTabReturnsTheString(string $input): void { - $_GET['val'] = $input; + service('superglobals')->setGet('val', $input); $this->invalidChars->before($this->request); } @@ -138,7 +144,7 @@ public function testCheckControlStringWithControlCharsCausesException(string $in $this->expectException(SecurityException::class); $this->expectExceptionMessage('Invalid Control characters in get:'); - $_GET['val'] = $input; + service('superglobals')->setGet('val', $input); $this->invalidChars->before($this->request); } diff --git a/tests/system/HTTP/CLIRequestTest.php b/tests/system/HTTP/CLIRequestTest.php index 854d8430528b..63e8482559ca 100644 --- a/tests/system/HTTP/CLIRequestTest.php +++ b/tests/system/HTTP/CLIRequestTest.php @@ -13,6 +13,8 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Services; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use PHPUnit\Framework\Attributes\BackupGlobals; @@ -34,22 +36,24 @@ protected function setUp(): void { parent::setUp(); - $this->request = new CLIRequest(new App()); - $_POST = []; $_GET = []; + + Services::injectMock('superglobals', new Superglobals(['argv' => []], [], [], [], [])); + + $this->request = new CLIRequest(new App()); } public function testParsingSegments(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -64,14 +68,14 @@ public function testParsingSegments(): void public function testParsingSegmentsWithHTMLMetaChars(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', 'abc < def', "McDonald's", 'aaa', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -88,7 +92,7 @@ public function testParsingSegmentsWithHTMLMetaChars(): void public function testParsingOptions(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', @@ -97,7 +101,7 @@ public function testParsingOptions(): void 'bar', '--foo-bar', 'yes', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -111,14 +115,14 @@ public function testParsingOptions(): void public function testParsingOptionDetails(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', 'profile', '--foo', 'bar', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -129,7 +133,7 @@ public function testParsingOptionDetails(): void public function testParsingOptionString(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', @@ -138,7 +142,7 @@ public function testParsingOptionString(): void 'bar', '--baz', 'queue some stuff', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -149,12 +153,12 @@ public function testParsingOptionString(): void public function testParsingNoOptions(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', 'profile', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -165,7 +169,7 @@ public function testParsingNoOptions(): void public function testParsingArgs(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'spark', 'command', 'param1', @@ -175,7 +179,7 @@ public function testParsingArgs(): void '--opt-2', 'opt 2 val', 'param3', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -193,14 +197,14 @@ public function testParsingArgs(): void public function testParsingPath(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', 'profile', '--foo', 'bar', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -210,7 +214,7 @@ public function testParsingPath(): void public function testParsingMalformed(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', @@ -219,7 +223,7 @@ public function testParsingMalformed(): void 'bar', '--baz', 'queue some stuff', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -231,7 +235,7 @@ public function testParsingMalformed(): void public function testParsingMalformed2(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', @@ -240,7 +244,7 @@ public function testParsingMalformed2(): void 'oops-bar', '--baz', 'queue some stuff', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -252,7 +256,7 @@ public function testParsingMalformed2(): void public function testParsingMalformed3(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'users', '21', @@ -262,7 +266,7 @@ public function testParsingMalformed3(): void 'bar', '--baz', 'queue some stuff', - ]; + ]); // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); @@ -274,8 +278,8 @@ public function testParsingMalformed3(): void public function testFetchGlobalsSingleValue(): void { - $_POST['foo'] = 'bar'; - $_GET['bar'] = 'baz'; + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setGet('bar', 'baz'); $this->assertSame('bar', $this->request->fetchGlobal('post', 'foo')); $this->assertSame('baz', $this->request->fetchGlobal('get', 'bar')); diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index 2c19f7a7c9af..792304e8087a 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -16,6 +16,7 @@ use CodeIgniter\Config\Factories; use CodeIgniter\Config\Services; use CodeIgniter\HTTP\Exceptions\HTTPException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCURLRequest; use Config\App; @@ -41,6 +42,7 @@ protected function setUp(): void parent::setUp(); $this->resetServices(); + Services::injectMock('superglobals', new Superglobals()); $this->request = $this->getRequest(); } @@ -187,9 +189,9 @@ public function testOptionsHeaders(): void #[BackupGlobals(true)] public function testOptionsHeadersNotUsingPopulate(): void { - $_SERVER['HTTP_HOST'] = 'site1.com'; - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US'; - $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate, br'; + service('superglobals')->setServer('HTTP_HOST', 'site1.com'); + service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'en-US'); + service('superglobals')->setServer('HTTP_ACCEPT_ENCODING', 'gzip, deflate, br'); $options = [ 'baseURI' => 'http://www.foo.com/api/v1/', @@ -247,7 +249,7 @@ public function testHeaderContentLengthNotSharedBetweenRequests(): void #[BackupGlobals(true)] public function testHeaderContentLengthNotSharedBetweenClients(): void { - $_SERVER['HTTP_CONTENT_LENGTH'] = '10'; + service('superglobals')->setServer('HTTP_CONTENT_LENGTH', '10'); $options = [ 'baseURI' => 'http://www.foo.com/api/v1/', diff --git a/tests/system/HTTP/DownloadResponseTest.php b/tests/system/HTTP/DownloadResponseTest.php index 7813a08def49..d9c5e30fe207 100644 --- a/tests/system/HTTP/DownloadResponseTest.php +++ b/tests/system/HTTP/DownloadResponseTest.php @@ -13,8 +13,10 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\DownloadException; use CodeIgniter\Files\Exceptions\FileNotFoundException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use DateTime; use DateTimeZone; @@ -33,12 +35,14 @@ final class DownloadResponseTest extends CIUnitTestCase protected function setUp(): void { parent::setUp(); + + Services::injectMock('superglobals', new Superglobals()); } protected function tearDown(): void { - if (isset($_SERVER['HTTP_USER_AGENT'])) { - unset($_SERVER['HTTP_USER_AGENT']); + if (service('superglobals')->server('HTTP_USER_AGENT') !== null) { + service('superglobals')->unsetServer('HTTP_USER_AGENT'); } } @@ -286,8 +290,8 @@ public function testIfTheCharacterCodeIsOtherThanUtf8ReplaceItWithUtf8AndRawurle public function testFileExtensionIsUpperCaseWhenAndroidOSIs2(): void { - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Linux; U; Android 2.0.3; ja-jp; SC-02C Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'; - $response = new DownloadResponse('unit-test.php', false); + service('superglobals')->setServer('HTTP_USER_AGENT', 'Mozilla/5.0 (Linux; U; Android 2.0.3; ja-jp; SC-02C Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'); + $response = new DownloadResponse('unit-test.php', false); $response->setFilePath(__FILE__); $response->buildHeaders(); diff --git a/tests/system/HTTP/Files/FileCollectionTest.php b/tests/system/HTTP/Files/FileCollectionTest.php index daebf98fc314..636285256d7b 100644 --- a/tests/system/HTTP/Files/FileCollectionTest.php +++ b/tests/system/HTTP/Files/FileCollectionTest.php @@ -26,7 +26,7 @@ final class FileCollectionTest extends CIUnitTestCase protected function setUp(): void { parent::setUp(); - $_FILES = []; + service('superglobals')->setFilesArray([]); } public function testAllReturnsArrayWithNoFiles(): void @@ -38,15 +38,15 @@ public function testAllReturnsArrayWithNoFiles(): void public function testAllReturnsValidSingleFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $files = $collection->all(); @@ -61,7 +61,7 @@ public function testAllReturnsValidSingleFile(): void public function testAllReturnsValidMultipleFilesSameName(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'fileA.txt', @@ -72,8 +72,8 @@ public function testAllReturnsValidMultipleFilesSameName(): void 'text/csv', ], 'size' => [ - '124', - '248', + 124, + 248, ], 'tmp_name' => [ '/tmp/fileA.txt', @@ -81,7 +81,7 @@ public function testAllReturnsValidMultipleFilesSameName(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $files = $collection->all(); @@ -103,7 +103,7 @@ public function testAllReturnsValidMultipleFilesSameName(): void public function testAllReturnsValidMultipleFilesDifferentName(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => 'fileA.txt', 'type' => 'text/plain', @@ -118,7 +118,7 @@ public function testAllReturnsValidMultipleFilesDifferentName(): void 'tmp_name' => '/tmp/fileB.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $files = $collection->all(); @@ -148,7 +148,7 @@ public function testAllReturnsValidMultipleFilesDifferentName(): void public function testExtensionGuessing(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => 'fileA.txt', 'type' => 'text/plain', @@ -184,7 +184,7 @@ public function testExtensionGuessing(): void 'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileE.zip.rar', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -222,7 +222,7 @@ public function testExtensionGuessing(): void public function testAllReturnsValidSingleFileNestedName(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'foo' => [ @@ -246,7 +246,7 @@ public function testAllReturnsValidSingleFileNestedName(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $files = $collection->all(); @@ -267,15 +267,15 @@ public function testAllReturnsValidSingleFileNestedName(): void public function testHasFileWithSingleFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -285,7 +285,7 @@ public function testHasFileWithSingleFile(): void public function testHasFileWithMultipleFilesWithDifferentNames(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => 'fileA.txt', 'type' => 'text/plain', @@ -300,7 +300,7 @@ public function testHasFileWithMultipleFilesWithDifferentNames(): void 'tmp_name' => '/tmp/fileB.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -310,7 +310,7 @@ public function testHasFileWithMultipleFilesWithDifferentNames(): void public function testHasFileWithSingleFileNestedName(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'foo' => [ @@ -334,7 +334,7 @@ public function testHasFileWithSingleFileNestedName(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -345,15 +345,15 @@ public function testHasFileWithSingleFileNestedName(): void public function testErrorString(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => UPLOAD_ERR_INI_SIZE, ], - ]; + ]); $expected = 'The file "someFile.txt" exceeds your upload_max_filesize ini directive.'; @@ -366,15 +366,15 @@ public function testErrorString(): void public function testErrorStringWithUnknownError(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 123, ], - ]; + ]); $expected = 'The file "someFile.txt" was not uploaded due to an unknown error.'; @@ -387,14 +387,14 @@ public function testErrorStringWithUnknownError(): void public function testErrorStringWithNoError(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', ], - ]; + ]); $expected = 'The file uploaded with success.'; @@ -407,15 +407,15 @@ public function testErrorStringWithNoError(): void public function testError(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => UPLOAD_ERR_INI_SIZE, ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -426,14 +426,14 @@ public function testError(): void public function testErrorWithUnknownError(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -444,15 +444,15 @@ public function testErrorWithUnknownError(): void public function testErrorWithNoError(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -463,15 +463,15 @@ public function testErrorWithNoError(): void public function testClientPathReturnsValidFullPath(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'full_path' => 'someDir/someFile.txt', ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -482,14 +482,14 @@ public function testClientPathReturnsValidFullPath(): void public function testClientPathReturnsNullWhenFullPathIsNull(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -500,15 +500,15 @@ public function testClientPathReturnsNullWhenFullPathIsNull(): void public function testFileReturnsValidSingleFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('userfile'); @@ -520,15 +520,15 @@ public function testFileReturnsValidSingleFile(): void public function testFileNoExistSingleFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); $file = $collection->getFile('fileuser'); @@ -537,7 +537,7 @@ public function testFileNoExistSingleFile(): void public function testFileReturnValidMultipleFiles(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'fileA.txt', @@ -548,8 +548,8 @@ public function testFileReturnValidMultipleFiles(): void 'text/csv', ], 'size' => [ - '124', - '248', + 124, + 248, ], 'tmp_name' => [ '/tmp/fileA.txt', @@ -557,7 +557,7 @@ public function testFileReturnValidMultipleFiles(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -580,7 +580,7 @@ public function testFileReturnValidMultipleFiles(): void public function testFileWithMultipleFilesNestedName(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'my-form' => [ 'name' => [ 'details' => [ @@ -623,7 +623,7 @@ public function testFileWithMultipleFilesNestedName(): void ], ], ], - ]; + ]); $collection = new FileCollection(); @@ -646,7 +646,7 @@ public function testFileWithMultipleFilesNestedName(): void public function testDoesntHaveFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'my-form' => [ 'name' => [ 'details' => [ @@ -689,7 +689,7 @@ public function testDoesntHaveFile(): void ], ], ], - ]; + ]); $collection = new FileCollection(); @@ -699,7 +699,7 @@ public function testDoesntHaveFile(): void public function testGetFileMultipleHasNoFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'fileA.txt', @@ -710,8 +710,8 @@ public function testGetFileMultipleHasNoFile(): void 'text/csv', ], 'size' => [ - '124', - '248', + 124, + 248, ], 'tmp_name' => [ '/tmp/fileA.txt', @@ -719,7 +719,7 @@ public function testGetFileMultipleHasNoFile(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -730,7 +730,7 @@ public function testGetFileMultipleHasNoFile(): void public function testGetFileMultipleReturnValidDotNotationSyntax(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'my-form' => [ 'name' => [ 'details' => [ @@ -773,7 +773,7 @@ public function testGetFileMultipleReturnValidDotNotationSyntax(): void ], ], ], - ]; + ]); $collection = new FileCollection(); @@ -798,7 +798,7 @@ public function testGetFileMultipleReturnValidDotNotationSyntax(): void public function testGetFileMultipleReturnInvalidDotNotationSyntax(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'my-form' => [ 'name' => [ 'details' => [ @@ -826,7 +826,7 @@ public function testGetFileMultipleReturnInvalidDotNotationSyntax(): void ], ], ], - ]; + ]); $collection = new FileCollection(); @@ -836,7 +836,7 @@ public function testGetFileMultipleReturnInvalidDotNotationSyntax(): void public function testGetFileMultipleReturnValidMultipleFiles(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'fileA.txt', @@ -847,8 +847,8 @@ public function testGetFileMultipleReturnValidMultipleFiles(): void 'text/csv', ], 'size' => [ - '124', - '248', + 124, + 248, ], 'tmp_name' => [ '/tmp/fileA.txt', @@ -856,7 +856,7 @@ public function testGetFileMultipleReturnValidMultipleFiles(): void ], 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -881,15 +881,15 @@ public function testGetFileMultipleReturnValidMultipleFiles(): void public function testGetFileMultipleReturnInvalidSingleFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'fileA.txt', 'type' => 'text/csv', - 'size' => '248', + 'size' => 248, 'tmp_name' => '/tmp/fileA.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); diff --git a/tests/system/HTTP/Files/FileMovingTest.php b/tests/system/HTTP/Files/FileMovingTest.php index f0e1564db5b7..45a9fd179837 100644 --- a/tests/system/HTTP/Files/FileMovingTest.php +++ b/tests/system/HTTP/Files/FileMovingTest.php @@ -42,7 +42,7 @@ protected function setUp(): void rmdir($this->destination); } - $_FILES = []; + service('superglobals')->setFilesArray([]); // Set the mock's return value to true move_uploaded_file('', '', true); @@ -63,7 +63,7 @@ protected function tearDown(): void public function testMove(): void { $finalFilename = 'fileA'; - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => $finalFilename . '.txt', 'type' => 'text/plain', @@ -78,7 +78,7 @@ public function testMove(): void 'tmp_name' => '/tmp/fileB.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -103,7 +103,7 @@ public function testMove(): void public function testMoveOverwriting(): void { $finalFilename = 'file_with_delimiters_underscore'; - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => $finalFilename . '.txt', 'type' => 'text/plain', @@ -125,7 +125,7 @@ public function testMoveOverwriting(): void 'tmp_name' => '/tmp/fileC.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -153,7 +153,7 @@ public function testMoveOverwriting(): void public function testMoved(): void { $finalFilename = 'fileA'; - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => $finalFilename . '.txt', 'type' => 'text/plain', @@ -161,7 +161,7 @@ public function testMoved(): void 'tmp_name' => '/tmp/fileA.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -186,7 +186,7 @@ public function testMoved(): void public function testStore(): void { $finalFilename = 'fileA'; - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => $finalFilename . '.txt', 'type' => 'text/plain', @@ -194,7 +194,7 @@ public function testStore(): void 'tmp_name' => '/tmp/fileA.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -218,7 +218,7 @@ public function testStore(): void public function testAlreadyMoved(): void { $finalFilename = 'fileA'; - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => $finalFilename . '.txt', 'type' => 'text/plain', @@ -226,7 +226,7 @@ public function testAlreadyMoved(): void 'tmp_name' => '/tmp/fileA.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); @@ -248,15 +248,15 @@ public function testAlreadyMoved(): void public function testInvalidFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => UPLOAD_ERR_INI_SIZE, ], - ]; + ]); $destination = $this->destination; $collection = new FileCollection(); @@ -270,15 +270,15 @@ public function testInvalidFile(): void public function testFailedMoveBecauseOfWarning(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $destination = $this->destination; // Create the destination and make it read only @@ -297,7 +297,7 @@ public function testFailedMoveBecauseOfWarning(): void public function testFailedMoveBecauseOfFalseReturned(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile1' => [ 'name' => 'fileA.txt', 'type' => 'text/plain', @@ -305,7 +305,7 @@ public function testFailedMoveBecauseOfFalseReturned(): void 'tmp_name' => '/tmp/fileA.txt', 'error' => 0, ], - ]; + ]); $collection = new FileCollection(); diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 7829786f7f4e..16bee39071c6 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -14,10 +14,12 @@ namespace CodeIgniter\HTTP; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\HTTP\Files\UploadedFile; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use JsonException; @@ -43,10 +45,11 @@ protected function setUp(): void { parent::setUp(); + $_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = []; + Services::injectMock('superglobals', new Superglobals()); + $config = new App(); $this->request = $this->createRequest($config); - - $_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = []; } private function createRequest(?App $config = null, $body = null, ?string $path = null): IncomingRequest @@ -61,7 +64,7 @@ private function createRequest(?App $config = null, $body = null, ?string $path public function testCanGrabRequestVars(): void { - $_REQUEST['TEST'] = 5; + service('superglobals')->setRequest('TEST', '5'); $this->assertSame('5', $this->request->getVar('TEST')); $this->assertNull($this->request->getVar('TESTY')); @@ -69,7 +72,7 @@ public function testCanGrabRequestVars(): void public function testCanGrabGetVars(): void { - $_GET['TEST'] = 5; + service('superglobals')->setGet('TEST', '5'); $this->assertSame('5', $this->request->getGet('TEST')); $this->assertNull($this->request->getGet('TESTY')); @@ -77,7 +80,7 @@ public function testCanGrabGetVars(): void public function testCanGrabPostVars(): void { - $_POST['TEST'] = 5; + service('superglobals')->setPost('TEST', '5'); $this->assertSame('5', $this->request->getPost('TEST')); $this->assertNull($this->request->getPost('TESTY')); @@ -85,8 +88,8 @@ public function testCanGrabPostVars(): void public function testCanGrabPostBeforeGet(): void { - $_POST['TEST'] = 5; - $_GET['TEST'] = 3; + service('superglobals')->setPost('TEST', '5'); + service('superglobals')->setGet('TEST', '3'); $this->assertSame('5', $this->request->getPostGet('TEST')); $this->assertSame('3', $this->request->getGetPost('TEST')); @@ -183,7 +186,7 @@ public function testCanGrabEnvVars(): void public function testCanGrabCookieVars(): void { - $_COOKIE['TEST'] = 5; + service('superglobals')->setCookie('TEST', '5'); $this->assertSame('5', $this->request->getCookie('TEST')); $this->assertNull($this->request->getCookie('TESTY')); @@ -243,7 +246,7 @@ public function testSetValidLocales(): void */ public function testNegotiatesLocale(): void { - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-FR; q=1.0, en; q=0.5'; + service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR); q=1.0, en; q=0.5'); $config = new App(); $config->negotiateLocale = true; @@ -258,7 +261,7 @@ public function testNegotiatesLocale(): void public function testNegotiatesLocaleOnlyBroad(): void { - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr; q=1.0, en; q=0.5'; + service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr); q=1.0, en; q=0.5'); $config = new App(); $config->negotiateLocale = true; @@ -285,7 +288,7 @@ public function testNegotiatesNot(): void public function testNegotiatesCharset(): void { - // $_SERVER['HTTP_ACCEPT_CHARSET'] = 'iso-8859-5, unicode-1-1;q=0.8'; + // service('superglobals')->setServer('HTTP_ACCEPT_CHARSET', 'iso-8859-5, unicode-1-1);q=0.8'; $this->request->setHeader('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8'); $this->assertSame( @@ -488,8 +491,8 @@ public function testGetVarWorksWithJsonAndGetParams(): void $config->baseURL = 'http://example.com/'; // GET method - $_REQUEST['foo'] = 'bar'; - $_REQUEST['fizz'] = 'buzz'; + service('superglobals')->setRequest('foo', 'bar'); + service('superglobals')->setRequest('fizz', 'buzz'); $request = $this->createRequest($config); $request = $request->withMethod('GET'); @@ -749,7 +752,7 @@ public function testIsAJAX(): void public function testIsSecure(): void { - $_SERVER['HTTPS'] = 'on'; + service('superglobals')->setServer('HTTPS', 'on'); $this->assertTrue($this->request->isSecure()); } @@ -777,15 +780,15 @@ public function testUserAgent(): void public function testFileCollectionFactory(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $files = $this->request->getFiles(); $this->assertCount(1, $files); @@ -799,7 +802,7 @@ public function testFileCollectionFactory(): void public function testGetFileMultiple(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => [ 'someFile.txt', @@ -810,8 +813,8 @@ public function testGetFileMultiple(): void 'text/plain', ], 'size' => [ - '124', - '125', + 124, + 125, ], 'tmp_name' => [ '/tmp/myTempFile.txt', @@ -822,7 +825,7 @@ public function testGetFileMultiple(): void 0, ], ], - ]; + ]); $gotit = $this->request->getFileMultiple('userfile'); $this->assertSame(124, $gotit[0]->getSize()); @@ -831,15 +834,15 @@ public function testGetFileMultiple(): void public function testGetFile(): void { - $_FILES = [ + service('superglobals')->setFilesArray([ 'userfile' => [ 'name' => 'someFile.txt', 'type' => 'text/plain', - 'size' => '124', + 'size' => 124, 'tmp_name' => '/tmp/myTempFile.txt', 'error' => 0, ], - ]; + ]); $gotit = $this->request->getFile('userfile'); $this->assertSame(124, $gotit->getSize()); @@ -856,30 +859,34 @@ public function testSpoofing(): void */ public function testGetPostEmpty(): void { - $_POST['TEST'] = '5'; - $_GET['TEST'] = '3'; - $this->assertSame($_POST, $this->request->getPostGet()); - $this->assertSame($_GET, $this->request->getGetPost()); + service('superglobals')->setPost('TEST', '5'); + service('superglobals')->setGet('TEST', '3'); + + $this->assertSame(['TEST' => '5'], $this->request->getPostGet()); + $this->assertSame(['TEST' => '3'], $this->request->getGetPost()); } public function testPostGetSecondStream(): void { - $_GET['get'] = '3'; - $this->assertSame($_GET, $this->request->getPostGet()); + service('superglobals')->setGet('get', '3'); + + $this->assertSame(['get' => '3'], $this->request->getPostGet()); } public function testGetPostSecondStream(): void { - $_POST['post'] = '5'; - $this->assertSame($_POST, $this->request->getGetPost()); + service('superglobals')->setPost('post', '5'); + + $this->assertSame(['post' => '5'], $this->request->getGetPost()); } public function testGetPostSecondStreams(): void { - $_GET['get'] = '3'; - $_POST['post'] = '5'; - $this->assertSame(array_merge($_GET, $_POST), $this->request->getPostGet()); - $this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost()); + service('superglobals')->setGet('get', '3'); + service('superglobals')->setPost('post', '5'); + + $this->assertSame(['get' => '3', 'post' => '5'], $this->request->getPostGet()); + $this->assertSame(['post' => '5', 'get' => '3'], $this->request->getGetPost()); } public function testGetBodyWithFalseBody(): void @@ -903,8 +910,8 @@ public function testGetBodyWithZero(): void */ public function testGetPostIndexNotExists(): void { - $_POST['TEST'] = 5; - $_GET['TEST'] = 3; + service('superglobals')->setPost('TEST', '5'); + service('superglobals')->setGet('TEST', '3'); $this->assertNull($this->request->getPostGet('gc')); $this->assertNull($this->request->getGetPost('gc')); } @@ -929,8 +936,8 @@ public function testSetPath(): void public function testGetIPAddressNormal(): void { - $expected = '123.123.123.123'; - $_SERVER['REMOTE_ADDR'] = $expected; + $expected = '123.123.123.123'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); $this->request = new Request(new App()); $this->request->populateHeaders(); @@ -942,9 +949,9 @@ public function testGetIPAddressNormal(): void public function testGetIPAddressThruProxy(): void { - $expected = '123.123.123.123'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; - $_SERVER['REMOTE_ADDR'] = '10.0.1.200'; + $expected = '123.123.123.123'; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); + service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200'); $config = new App(); $config->proxyIPs = [ @@ -961,9 +968,9 @@ public function testGetIPAddressThruProxy(): void public function testGetIPAddressThruProxyIPv6(): void { - $expected = '123.123.123.123'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; - $_SERVER['REMOTE_ADDR'] = '2001:db8::2:1'; + $expected = '123.123.123.123'; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); + service('superglobals')->setServer('REMOTE_ADDR', '2001:db8::2:1'); $config = new App(); $config->proxyIPs = [ @@ -979,9 +986,9 @@ public function testGetIPAddressThruProxyIPv6(): void public function testGetIPAddressThruProxyInvalidIPAddress(): void { - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.456.23.123'; - $expected = '10.0.1.200'; - $_SERVER['REMOTE_ADDR'] = $expected; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.456.23.123'); + $expected = '10.0.1.200'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); $config = new App(); $config->proxyIPs = [ @@ -997,9 +1004,9 @@ public function testGetIPAddressThruProxyInvalidIPAddress(): void public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void { - $_SERVER['HTTP_X_FORWARDED_FOR'] = '2001:xyz::1'; - $expected = '2001:db8::2:1'; - $_SERVER['REMOTE_ADDR'] = $expected; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '2001:xyz::1'); + $expected = '2001:db8::2:1'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); $config = new App(); $config->proxyIPs = [ @@ -1014,9 +1021,9 @@ public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void public function testGetIPAddressThruProxyNotWhitelisted(): void { - $expected = '10.10.1.200'; - $_SERVER['REMOTE_ADDR'] = $expected; - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.456.23.123'; + $expected = '10.10.1.200'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.456.23.123'); $config = new App(); $config->proxyIPs = [ @@ -1032,9 +1039,9 @@ public function testGetIPAddressThruProxyNotWhitelisted(): void public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void { - $expected = '2001:db8::2:2'; - $_SERVER['REMOTE_ADDR'] = $expected; - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.456.23.123'; + $expected = '2001:db8::2:2'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.456.23.123'); $config = new App(); $config->proxyIPs = [ @@ -1049,9 +1056,9 @@ public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void public function testGetIPAddressThruProxySubnet(): void { - $expected = '123.123.123.123'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; - $_SERVER['REMOTE_ADDR'] = '192.168.5.21'; + $expected = '123.123.123.123'; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); + service('superglobals')->setServer('REMOTE_ADDR', '192.168.5.21'); $config = new App(); $config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For']; @@ -1065,9 +1072,9 @@ public function testGetIPAddressThruProxySubnet(): void public function testGetIPAddressThruProxySubnetIPv6(): void { - $expected = '123.123.123.123'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; - $_SERVER['REMOTE_ADDR'] = '2001:db8:1234:ffff:ffff:ffff:ffff:ffff'; + $expected = '123.123.123.123'; + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); + service('superglobals')->setServer('REMOTE_ADDR', '2001:db8:1234:ffff:ffff:ffff:ffff:ffff'); $config = new App(); $config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For']; @@ -1081,9 +1088,9 @@ public function testGetIPAddressThruProxySubnetIPv6(): void public function testGetIPAddressThruProxyOutOfSubnet(): void { - $expected = '192.168.5.21'; - $_SERVER['REMOTE_ADDR'] = $expected; - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123'; + $expected = '192.168.5.21'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.123.123.123'); $config = new App(); $config->proxyIPs = ['192.168.5.0/28' => 'X-Forwarded-For']; @@ -1096,9 +1103,9 @@ public function testGetIPAddressThruProxyOutOfSubnet(): void public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void { - $expected = '2001:db8:1235:ffff:ffff:ffff:ffff:ffff'; - $_SERVER['REMOTE_ADDR'] = $expected; - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123'; + $expected = '2001:db8:1235:ffff:ffff:ffff:ffff:ffff'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.123.123.123'); $config = new App(); $config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For']; @@ -1111,9 +1118,9 @@ public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void public function testGetIPAddressThruProxyBothIPv4AndIPv6(): void { - $expected = '2001:db8:1235:ffff:ffff:ffff:ffff:ffff'; - $_SERVER['REMOTE_ADDR'] = $expected; - $_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123'; + $expected = '2001:db8:1235:ffff:ffff:ffff:ffff:ffff'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', '123.123.123.123'); $config = new App(); $config->proxyIPs = [ diff --git a/tests/system/HTTP/MessageTest.php b/tests/system/HTTP/MessageTest.php index 28483674fee9..b847fdc4c4de 100644 --- a/tests/system/HTTP/MessageTest.php +++ b/tests/system/HTTP/MessageTest.php @@ -13,8 +13,10 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\HTTP\Exceptions\HTTPException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; @@ -31,6 +33,8 @@ protected function setUp(): void { parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $this->message = new Message(); } @@ -242,11 +246,12 @@ public function testSetHeaderWithExistingArrayValuesAppendNullValue(): void public function testPopulateHeadersWithoutContentType(): void { - $original = $_SERVER; - $originalEnv = getenv('CONTENT_TYPE'); + $superglobals = service('superglobals'); + $original = $superglobals->getServerArray(); + $originalEnv = getenv('CONTENT_TYPE'); // fail path, if the CONTENT_TYPE doesn't exist - $_SERVER = ['HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.50']; + $superglobals->setServerArray(['HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.50']); putenv('CONTENT_TYPE'); $this->message->populateHeaders(); @@ -254,57 +259,60 @@ public function testPopulateHeadersWithoutContentType(): void $this->assertNull($this->message->header('content-type')); putenv("CONTENT_TYPE={$originalEnv}"); - $_SERVER = $original; // restore so code coverage doesn't break + $superglobals->setServerArray($original); // restore so code coverage doesn't break } public function testPopulateHeadersWithoutHTTP(): void { // fail path, if argument doesn't have the HTTP_* - $original = $_SERVER; - $_SERVER = [ + $superglobals = service('superglobals'); + $original = $superglobals->getServerArray(); + $superglobals->setServerArray([ 'USER_AGENT' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405', 'REQUEST_METHOD' => 'POST', - ]; + ]); $this->message->populateHeaders(); $this->assertNull($this->message->header('user-agent')); $this->assertNull($this->message->header('request-method')); - $_SERVER = $original; // restore so code coverage doesn't break + $superglobals->setServerArray($original); // restore so code coverage doesn't break } public function testPopulateHeadersKeyNotExists(): void { // Success path, if array key is not exists, assign empty string to it's value - $original = $_SERVER; - $_SERVER = [ + $superglobals = service('superglobals'); + $original = $superglobals->getServerArray(); + $superglobals->setServerArray([ 'CONTENT_TYPE' => 'text/html; charset=utf-8', 'HTTP_ACCEPT_CHARSET' => null, - ]; + ]); $this->message->populateHeaders(); $this->assertSame('', $this->message->header('accept-charset')->getValue()); - $_SERVER = $original; // restore so code coverage doesn't break + $superglobals->setServerArray($original); // restore so code coverage doesn't break } public function testPopulateHeaders(): void { // success path - $original = $_SERVER; - $_SERVER = [ + $superglobals = service('superglobals'); + $original = $superglobals->getServerArray(); + $superglobals->setServerArray([ 'CONTENT_TYPE' => 'text/html; charset=utf-8', 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.50', - ]; + ]); $this->message->populateHeaders(); $this->assertSame('text/html; charset=utf-8', $this->message->header('content-type')->getValue()); $this->assertSame('en-us,en;q=0.50', $this->message->header('accept-language')->getValue()); - $_SERVER = $original; // restore so code coverage doesn't break + $superglobals->setServerArray($original); // restore so code coverage doesn't break } public function testAddHeaderAddsFirstHeader(): void diff --git a/tests/system/HTTP/RedirectResponseTest.php b/tests/system/HTTP/RedirectResponseTest.php index d6203aa935fd..2ea700c03d88 100644 --- a/tests/system/HTTP/RedirectResponseTest.php +++ b/tests/system/HTTP/RedirectResponseTest.php @@ -16,6 +16,7 @@ use CodeIgniter\Config\Factories; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\Router\RouteCollection; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockIncomingRequest; use CodeIgniter\Validation\Validation; @@ -49,7 +50,8 @@ protected function setUp(): void $this->resetServices(); - $_SERVER['REQUEST_METHOD'] = 'GET'; + Services::injectMock('superglobals', new Superglobals()); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->config = new App(); $this->config->baseURL = 'http://example.com/'; @@ -134,8 +136,8 @@ public function testRedirectRelativeConvertsToFullURI(): void public function testWithInput(): void { $_SESSION = []; - $_GET = ['foo' => 'bar']; - $_POST = ['bar' => 'baz']; + service('superglobals')->setGet('foo', 'bar'); + service('superglobals')->setPost('bar', 'baz'); $response = new RedirectResponse(new App()); @@ -183,7 +185,7 @@ public function testWith(): void #[RunInSeparateProcess] public function testRedirectBack(): void { - $_SERVER['HTTP_REFERER'] = 'http://somewhere.com'; + service('superglobals')->setServer('HTTP_REFERER', 'http://somewhere.com'); $this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent()); Services::injectMock('request', $this->request); diff --git a/tests/system/HTTP/RequestTest.php b/tests/system/HTTP/RequestTest.php index 6659c6966861..23316c98b98e 100644 --- a/tests/system/HTTP/RequestTest.php +++ b/tests/system/HTTP/RequestTest.php @@ -14,6 +14,8 @@ namespace CodeIgniter\HTTP; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use PHPUnit\Framework\Attributes\BackupGlobals; @@ -32,16 +34,18 @@ protected function setUp(): void { parent::setUp(); - $this->request = new Request(new App()); - $_POST = []; $_GET = []; + + Services::injectMock('superglobals', new Superglobals()); + + $this->request = new Request(new App()); } public function testFetchGlobalsSingleValue(): void { - $_POST['foo'] = 'bar'; - $_GET['bar'] = 'baz'; + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setGet('bar', 'baz'); $this->assertSame('bar', $this->request->fetchGlobal('post', 'foo')); $this->assertSame('baz', $this->request->fetchGlobal('get', 'bar')); @@ -557,9 +561,9 @@ public function testGetIPAddressDefault(): void public function testGetIPAddressNormal(): void { - $expected = '123.123.123.123'; - $_SERVER['REMOTE_ADDR'] = $expected; - $this->request = new Request(new App()); + $expected = '123.123.123.123'; + service('superglobals')->setServer('REMOTE_ADDR', $expected); + $this->request = new Request(new App()); $this->assertSame($expected, $this->request->getIPAddress()); // call a second time to exercise the initial conditional block in getIPAddress() $this->assertSame($expected, $this->request->getIPAddress()); @@ -567,9 +571,9 @@ public function testGetIPAddressNormal(): void public function testGetIPAddressThruProxy(): void { - $expected = '123.123.123.123'; - $_SERVER['REMOTE_ADDR'] = '10.0.1.200'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; + $expected = '123.123.123.123'; + service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200'); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); $config = new App(); $config->proxyIPs = [ @@ -586,11 +590,11 @@ public function testGetIPAddressThruProxy(): void public function testGetIPAddressThruProxyInvalid(): void { - $expected = '123.456.23.123'; - $_SERVER['REMOTE_ADDR'] = '10.0.1.200'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; - $config = new App(); - $config->proxyIPs = [ + $expected = '123.456.23.123'; + service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200'); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); + $config = new App(); + $config->proxyIPs = [ '10.0.1.200' => 'X-Forwarded-For', '192.168.5.0/24' => 'X-Forwarded-For', ]; @@ -604,9 +608,9 @@ public function testGetIPAddressThruProxyInvalid(): void public function testGetIPAddressThruProxyNotWhitelisted(): void { - $expected = '123.456.23.123'; - $_SERVER['REMOTE_ADDR'] = '10.10.1.200'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; + $expected = '123.456.23.123'; + service('superglobals')->setServer('REMOTE_ADDR', '10.10.1.200'); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); $config = new App(); $config->proxyIPs = [ @@ -622,9 +626,9 @@ public function testGetIPAddressThruProxyNotWhitelisted(): void public function testGetIPAddressThruProxySubnet(): void { - $expected = '123.123.123.123'; - $_SERVER['REMOTE_ADDR'] = '192.168.5.21'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; + $expected = '123.123.123.123'; + service('superglobals')->setServer('REMOTE_ADDR', '192.168.5.21'); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); $config = new App(); $config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For']; @@ -638,9 +642,9 @@ public function testGetIPAddressThruProxySubnet(): void public function testGetIPAddressThruProxyOutofSubnet(): void { - $expected = '123.123.123.123'; - $_SERVER['REMOTE_ADDR'] = '192.168.5.21'; - $_SERVER['HTTP_X_FORWARDED_FOR'] = $expected; + $expected = '123.123.123.123'; + service('superglobals')->setServer('REMOTE_ADDR', '192.168.5.21'); + service('superglobals')->setServer('HTTP_X_FORWARDED_FOR', $expected); $config = new App(); $config->proxyIPs = ['192.168.5.0/28' => 'X-Forwarded-For']; diff --git a/tests/system/HTTP/ResponseTest.php b/tests/system/HTTP/ResponseTest.php index bea0944599d8..50c90188c048 100644 --- a/tests/system/HTTP/ResponseTest.php +++ b/tests/system/HTTP/ResponseTest.php @@ -14,7 +14,9 @@ namespace CodeIgniter\HTTP; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\HTTP\Exceptions\HTTPException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockResponse; use Config\App; @@ -33,7 +35,8 @@ final class ResponseTest extends CIUnitTestCase protected function setUp(): void { - $this->server = $_SERVER; + Services::injectMock('superglobals', new Superglobals()); + $this->server = service('superglobals')->getServerArray(); parent::setUp(); @@ -44,7 +47,7 @@ protected function tearDown(): void { Factories::reset('config'); - $_SERVER = $this->server; + service('superglobals')->setServerArray($this->server); } public function testCanSetStatusCode(): void @@ -278,9 +281,9 @@ public function testRedirect( ?int $code, int $expectedCode, ): void { - $_SERVER['SERVER_SOFTWARE'] = $server; - $_SERVER['SERVER_PROTOCOL'] = $protocol; - $_SERVER['REQUEST_METHOD'] = $method; + service('superglobals')->setServer('SERVER_SOFTWARE', $server); + service('superglobals')->setServer('SERVER_PROTOCOL', $protocol); + service('superglobals')->setServer('REQUEST_METHOD', $method); $response = new Response(new App()); $response->redirect('example.com', 'auto', $code); @@ -321,9 +324,9 @@ public function testRedirectWithIIS( ?int $code, int $expectedCode, ): void { - $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS'; - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('SERVER_SOFTWARE', 'Microsoft-IIS'); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $response = new Response(new App()); $response->redirect('example.com', 'auto', $code); @@ -331,7 +334,7 @@ public function testRedirectWithIIS( $this->assertSame('0;url=example.com', $response->getHeaderLine('Refresh')); $this->assertSame($expectedCode, $response->getStatusCode()); - unset($_SERVER['SERVER_SOFTWARE']); + service('superglobals')->unsetServer('SERVER_SOFTWARE'); } public static function provideRedirectWithIIS(): iterable @@ -518,9 +521,9 @@ public function testMisbehaving(): void public function testTemporaryRedirectHTTP11(): void { - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'POST'; - $response = new Response(new App()); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + $response = new Response(new App()); $response->setProtocolVersion('HTTP/1.1'); $response->redirect('/foo'); @@ -530,9 +533,9 @@ public function testTemporaryRedirectHTTP11(): void public function testTemporaryRedirectGetHTTP11(): void { - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; - $response = new Response(new App()); + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); + $response = new Response(new App()); $response->setProtocolVersion('HTTP/1.1'); $response->redirect('/foo'); diff --git a/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php b/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php index cbd309dffa7c..393f9dd954e8 100644 --- a/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php +++ b/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Services; use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -32,14 +33,15 @@ protected function setUp(): void parent::setUp(); $_GET = $_SERVER = []; + + Services::injectMock('superglobals', new Superglobals()); } private function createSiteURIFactory(array $server, ?App $appConfig = null): SiteURIFactory { $appConfig ??= new App(); - $_SERVER = $server; - $superglobals = new Superglobals(); + $superglobals = new Superglobals($server); return new SiteURIFactory($appConfig, $superglobals); } @@ -47,10 +49,10 @@ private function createSiteURIFactory(array $server, ?App $appConfig = null): Si public function testDefault(): void { // /index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath()); @@ -59,10 +61,10 @@ public function testDefault(): void public function testDefaultEmpty(): void { // / - $_SERVER['REQUEST_URI'] = '/'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = '/'; $this->assertSame($expected, $factory->detectRoutePath()); @@ -71,10 +73,10 @@ public function testDefaultEmpty(): void public function testRequestURI(): void { // /index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -91,10 +93,10 @@ public function testRequestURINested(): void // So I don't remove this test case. // /ci/index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -103,10 +105,10 @@ public function testRequestURINested(): void public function testRequestURISubfolder(): void { // /ci/index.php/popcorn/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/ci/index.php/popcorn/woot'; - $_SERVER['SCRIPT_NAME'] = '/ci/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/ci/index.php/popcorn/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'popcorn/woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -115,10 +117,10 @@ public function testRequestURISubfolder(): void public function testRequestURINoIndex(): void { // /sub/example - $_SERVER['REQUEST_URI'] = '/sub/example'; - $_SERVER['SCRIPT_NAME'] = '/sub/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/sub/example'); + service('superglobals')->setServer('SCRIPT_NAME', '/sub/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'example'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -127,10 +129,10 @@ public function testRequestURINoIndex(): void public function testRequestURINginx(): void { // /ci/index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot?code=good'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot?code=good'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -139,10 +141,10 @@ public function testRequestURINginx(): void public function testRequestURINginxRedirecting(): void { // /?/ci/index.php/woot - $_SERVER['REQUEST_URI'] = '/?/ci/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/?/ci/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'ci/woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -151,10 +153,10 @@ public function testRequestURINginxRedirecting(): void public function testRequestURISuppressed(): void { // /woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/woot'; - $_SERVER['SCRIPT_NAME'] = '/'; + service('superglobals')->setServer('REQUEST_URI', '/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); @@ -163,10 +165,10 @@ public function testRequestURISuppressed(): void public function testRequestURIGetPath(): void { // /index.php/fruits/banana - $_SERVER['REQUEST_URI'] = '/index.php/fruits/banana'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/fruits/banana'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $this->assertSame('fruits/banana', $factory->detectRoutePath('REQUEST_URI')); } @@ -174,10 +176,10 @@ public function testRequestURIGetPath(): void public function testRequestURIPathIsRelative(): void { // /sub/folder/index.php/fruits/banana - $_SERVER['REQUEST_URI'] = '/sub/folder/index.php/fruits/banana'; - $_SERVER['SCRIPT_NAME'] = '/sub/folder/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/sub/folder/index.php/fruits/banana'); + service('superglobals')->setServer('SCRIPT_NAME', '/sub/folder/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $this->assertSame('fruits/banana', $factory->detectRoutePath('REQUEST_URI')); } @@ -185,24 +187,24 @@ public function testRequestURIPathIsRelative(): void public function testRequestURIStoresDetectedPath(): void { // /fruits/banana - $_SERVER['REQUEST_URI'] = '/fruits/banana'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/fruits/banana'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); - $_SERVER['REQUEST_URI'] = '/candy/snickers'; + service('superglobals')->setServer('REQUEST_URI', '/candy/snickers'); $this->assertSame('fruits/banana', $factory->detectRoutePath('REQUEST_URI')); } public function testRequestURIPathIsNeverRediscovered(): void { - $_SERVER['REQUEST_URI'] = '/fruits/banana'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/fruits/banana'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); - $_SERVER['REQUEST_URI'] = '/candy/snickers'; + service('superglobals')->setServer('REQUEST_URI', '/candy/snickers'); $factory->detectRoutePath('REQUEST_URI'); $this->assertSame('fruits/banana', $factory->detectRoutePath('REQUEST_URI')); @@ -211,13 +213,13 @@ public function testRequestURIPathIsNeverRediscovered(): void public function testQueryString(): void { // /index.php?/ci/woot - $_SERVER['REQUEST_URI'] = '/index.php?/ci/woot'; - $_SERVER['QUERY_STRING'] = '/ci/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php?/ci/woot'); + service('superglobals')->setServer('QUERY_STRING', '/ci/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $_GET['/ci/woot'] = ''; + service('superglobals')->setGet('/ci/woot', ''); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'ci/woot'; $this->assertSame($expected, $factory->detectRoutePath('QUERY_STRING')); @@ -226,13 +228,13 @@ public function testQueryString(): void public function testQueryStringWithQueryString(): void { // /index.php?/ci/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php?/ci/woot?code=good'; - $_SERVER['QUERY_STRING'] = '/ci/woot?code=good'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php?/ci/woot?code=good'); + service('superglobals')->setServer('QUERY_STRING', '/ci/woot?code=good'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $_GET['/ci/woot?code'] = 'good'; + service('superglobals')->setGet('/ci/woot?code', 'good'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'ci/woot'; $this->assertSame($expected, $factory->detectRoutePath('QUERY_STRING')); @@ -243,10 +245,10 @@ public function testQueryStringWithQueryString(): void public function testQueryStringEmpty(): void { // /index.php? - $_SERVER['REQUEST_URI'] = '/index.php?'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php?'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = '/'; $this->assertSame($expected, $factory->detectRoutePath('QUERY_STRING')); @@ -255,10 +257,10 @@ public function testQueryStringEmpty(): void public function testPathInfoUnset(): void { // /index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); - $factory = $this->createSiteURIFactory($_SERVER); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('PATH_INFO')); @@ -270,11 +272,11 @@ public function testPathInfoSubfolder(): void $appConfig->baseURL = 'http://localhost:8888/ci431/public/'; // http://localhost:8888/ci431/public/index.php/woot?code=good#pos - $_SERVER['PATH_INFO'] = '/woot'; - $_SERVER['REQUEST_URI'] = '/ci431/public/index.php/woot?code=good'; - $_SERVER['SCRIPT_NAME'] = '/ci431/public/index.php'; + service('superglobals')->setServer('PATH_INFO', '/woot'); + service('superglobals')->setServer('REQUEST_URI', '/ci431/public/index.php/woot?code=good'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci431/public/index.php'); - $factory = $this->createSiteURIFactory($_SERVER, $appConfig); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray(), $appConfig); $expected = 'woot'; $this->assertSame($expected, $factory->detectRoutePath('PATH_INFO')); @@ -290,10 +292,10 @@ public function testExtensionPHP($path, $detectPath): void $config = new App(); $config->baseURL = 'http://example.com/'; - $_SERVER['REQUEST_URI'] = $path; - $_SERVER['SCRIPT_NAME'] = $path; + service('superglobals')->setServer('REQUEST_URI', $path); + service('superglobals')->setServer('SCRIPT_NAME', $path); - $factory = $this->createSiteURIFactory($_SERVER, $config); + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray(), $config); $this->assertSame($detectPath, $factory->detectRoutePath()); } diff --git a/tests/system/HTTP/SiteURIFactoryTest.php b/tests/system/HTTP/SiteURIFactoryTest.php index b26fde04b245..1ac42592f837 100644 --- a/tests/system/HTTP/SiteURIFactoryTest.php +++ b/tests/system/HTTP/SiteURIFactoryTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Services; use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -32,6 +33,8 @@ protected function setUp(): void parent::setUp(); $_GET = $_SERVER = []; + + Services::injectMock('superglobals', new Superglobals()); } private function createSiteURIFactory(?App $config = null, ?Superglobals $superglobals = null): SiteURIFactory @@ -45,13 +48,13 @@ private function createSiteURIFactory(?App $config = null, ?Superglobals $superg public function testCreateFromGlobals(): void { // http://localhost:8080/index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot?code=good'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['QUERY_STRING'] = 'code=good'; - $_SERVER['HTTP_HOST'] = 'localhost:8080'; - $_SERVER['PATH_INFO'] = '/woot'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot?code=good'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('QUERY_STRING', 'code=good'); + service('superglobals')->setServer('HTTP_HOST', 'localhost:8080'); + service('superglobals')->setServer('PATH_INFO', '/woot'); - $_GET['code'] = 'good'; + service('superglobals')->setGet('code', 'good'); $factory = $this->createSiteURIFactory(); @@ -66,13 +69,13 @@ public function testCreateFromGlobals(): void public function testCreateFromGlobalsAllowedHost(): void { // http://users.example.jp/index.php/woot?code=good#pos - $_SERVER['REQUEST_URI'] = '/index.php/woot?code=good'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['QUERY_STRING'] = 'code=good'; - $_SERVER['HTTP_HOST'] = 'users.example.jp'; - $_SERVER['PATH_INFO'] = '/woot'; + service('superglobals')->setServer('REQUEST_URI', '/index.php/woot?code=good'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); + service('superglobals')->setServer('QUERY_STRING', 'code=good'); + service('superglobals')->setServer('HTTP_HOST', 'users.example.jp'); + service('superglobals')->setServer('PATH_INFO', '/woot'); - $_GET['code'] = 'good'; + service('superglobals')->setGet('code', 'good'); $config = new App(); $config->baseURL = 'http://example.jp/'; diff --git a/tests/system/HTTP/URITest.php b/tests/system/HTTP/URITest.php index 3199d9c1cb25..c449d13af65c 100644 --- a/tests/system/HTTP/URITest.php +++ b/tests/system/HTTP/URITest.php @@ -1049,11 +1049,11 @@ public function testSetBadSegmentSilent(): void public function testBasedNoIndex(): void { - $_SERVER['REQUEST_URI'] = '/ci/v4/controller/method'; - $_SERVER['SCRIPT_NAME'] = '/ci/v4/index.php'; - $_SERVER['QUERY_STRING'] = ''; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['PATH_INFO'] = '/controller/method'; + service('superglobals')->setServer('REQUEST_URI', '/ci/v4/controller/method'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci/v4/index.php'); + service('superglobals')->setServer('QUERY_STRING', ''); + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('PATH_INFO', '/controller/method'); $this->resetServices(); @@ -1083,11 +1083,11 @@ public function testBasedNoIndex(): void public function testBasedWithIndex(): void { - $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/controller/method'; - $_SERVER['SCRIPT_NAME'] = '/ci/v4/index.php'; - $_SERVER['QUERY_STRING'] = ''; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['PATH_INFO'] = '/controller/method'; + service('superglobals')->setServer('REQUEST_URI', '/ci/v4/index.php/controller/method'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci/v4/index.php'); + service('superglobals')->setServer('QUERY_STRING', ''); + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('PATH_INFO', '/controller/method'); $this->resetServices(); @@ -1124,11 +1124,11 @@ public function testForceGlobalSecureRequests(): void { $this->resetServices(); - $_SERVER['REQUEST_URI'] = '/ci/v4/controller/method'; - $_SERVER['SCRIPT_NAME'] = '/ci/v4/index.php'; - $_SERVER['QUERY_STRING'] = ''; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['PATH_INFO'] = '/controller/method'; + service('superglobals')->setServer('REQUEST_URI', '/ci/v4/controller/method'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci/v4/index.php'); + service('superglobals')->setServer('QUERY_STRING', ''); + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('PATH_INFO', '/controller/method'); $config = new App(); $config->baseURL = 'http://example.com/ci/v4'; diff --git a/tests/system/Helpers/CookieHelperTest.php b/tests/system/Helpers/CookieHelperTest.php index c6bfba193054..abbc291ceb38 100644 --- a/tests/system/Helpers/CookieHelperTest.php +++ b/tests/system/Helpers/CookieHelperTest.php @@ -19,6 +19,7 @@ use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockResponse; use Config\App; @@ -44,6 +45,8 @@ protected function setUp(): void parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $this->name = 'greetings'; $this->value = 'hello world'; $this->expire = 9999; @@ -152,14 +155,14 @@ public function testDeleteCookie(): void public function testGetCookie(): void { - $_COOKIE['TEST'] = '5'; + service('superglobals')->setCookie('TEST', '5'); $this->assertSame('5', get_cookie('TEST')); } public function testGetCookieDefaultPrefix(): void { - $_COOKIE['prefix_TEST'] = '5'; + service('superglobals')->setCookie('prefix_TEST', '5'); $config = new CookieConfig(); $config->prefix = 'prefix_'; @@ -170,7 +173,7 @@ public function testGetCookieDefaultPrefix(): void public function testGetCookiePrefix(): void { - $_COOKIE['abc_TEST'] = '5'; + service('superglobals')->setCookie('abc_TEST', '5'); $config = new CookieConfig(); $config->prefix = 'prefix_'; @@ -181,7 +184,7 @@ public function testGetCookiePrefix(): void public function testGetCookieNoPrefix(): void { - $_COOKIE['abc_TEST'] = '5'; + service('superglobals')->setCookie('abc_TEST', '5'); $config = new CookieConfig(); $config->prefix = 'prefix_'; diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 91d5aa2e8c7b..58c1cb2a94cf 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -13,12 +13,15 @@ namespace CodeIgniter\Helpers; +use CodeIgniter\Config\Services as CodeIgniterServices; use CodeIgniter\HTTP\SiteURI; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use Config\DocTypes; use Config\Filters; use Config\Services; +use PHPUnit\Framework\Attributes\BackupGlobals; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\PreserveGlobalState; use PHPUnit\Framework\Attributes\RunInSeparateProcess; @@ -27,6 +30,7 @@ /** * @internal */ +#[BackupGlobals(true)] #[Group('SeparateProcess')] final class FormHelperTest extends CIUnitTestCase { @@ -37,6 +41,10 @@ protected function setUp(): void parent::setUp(); + $_POST = $_GET = []; + + CodeIgniterServices::injectMock('superglobals', new Superglobals()); + helper('form'); } @@ -573,9 +581,9 @@ public function testFormDropdownInferred(): void \n EOH; - $_POST['cars'] = 'audi'; + service('superglobals')->setPost('cars', 'audi'); $this->assertSame($expected, form_dropdown('cars', $options)); - unset($_POST['cars']); + service('superglobals')->unsetPost('cars'); } public function testFormDropdownWithSelectedAttribute(): void @@ -975,7 +983,7 @@ public function testSetRadioFromSessionOldInput(): void #[RunInSeparateProcess] public function testSetRadioFromPost(): void { - $_POST['bar'] = 'baz'; + service('superglobals')->setPost('bar', 'baz'); $this->assertSame(' checked="checked"', set_radio('bar', 'baz')); $this->assertSame('', set_radio('bar', 'boop')); @@ -986,12 +994,12 @@ public function testSetRadioFromPost(): void #[RunInSeparateProcess] public function testSetRadioFromPostWithValueZero(): void { - $_POST['bar'] = '0'; + service('superglobals')->setPost('bar', '0'); $this->assertSame(' checked="checked"', set_radio('bar', '0')); $this->assertSame('', set_radio('bar', 'boop')); - $_POST = []; + service('superglobals')->setPostArray([]); $this->assertSame(' checked="checked"', set_radio('bar', '0', true)); } @@ -1033,7 +1041,6 @@ public function testSetRadioFromSessionOldInputPostArrayWithValueZero(): void public function testSetRadioDefault(): void { $_SESSION = []; - $_POST = []; $this->assertSame(' checked="checked"', set_radio('code', 'alpha', true)); $this->assertSame('', set_radio('code', 'beta', false)); diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php index b802db349949..54ac56bb6a54 100644 --- a/tests/system/Helpers/URLHelper/CurrentUrlTest.php +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -50,22 +50,23 @@ protected function setUp(): void $this->config->baseURL = 'http://example.com/'; $this->config->indexPage = 'index.php'; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + Services::injectMock('superglobals', new Superglobals()); + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); } protected function tearDown(): void { parent::tearDown(); - $_SERVER = []; + service('superglobals')->setServerArray([]); } public function testCurrentURLReturnsBasicURL(): void { - $_SERVER['REQUEST_URI'] = '/public/'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('REQUEST_URI', '/public/'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; @@ -76,9 +77,9 @@ public function testCurrentURLReturnsBasicURL(): void public function testCurrentURLReturnsAllowedHostname(): void { - $_SERVER['HTTP_HOST'] = 'www.example.jp'; - $_SERVER['REQUEST_URI'] = '/public/'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.jp'); + service('superglobals')->setServer('REQUEST_URI', '/public/'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; $this->config->allowedHostnames = ['www.example.jp']; @@ -107,9 +108,9 @@ private function createRequest(?App $config = null, $body = null, ?string $path public function testCurrentURLReturnsBaseURLIfNotAllowedHostname(): void { - $_SERVER['HTTP_HOST'] = 'invalid.example.org'; - $_SERVER['REQUEST_URI'] = '/public/'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'invalid.example.org'); + service('superglobals')->setServer('REQUEST_URI', '/public/'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; $this->config->allowedHostnames = ['www.example.jp']; @@ -133,9 +134,9 @@ public function testCurrentURLReturnsObject(): void public function testCurrentURLEquivalence(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/public/'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/public/'); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); $this->config->indexPage = ''; @@ -146,9 +147,9 @@ public function testCurrentURLEquivalence(): void public function testCurrentURLInSubfolder(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; - $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/foo/public/bar?baz=quip'); + service('superglobals')->setServer('SCRIPT_NAME', '/foo/public/index.php'); $this->config->baseURL = 'http://example.com/foo/public/'; @@ -165,10 +166,10 @@ public function testCurrentURLInSubfolder(): void public function testCurrentURLWithPortInSubfolder(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['SERVER_PORT'] = '8080'; - $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; - $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('SERVER_PORT', '8080'); + service('superglobals')->setServer('REQUEST_URI', '/foo/public/bar?baz=quip'); + service('superglobals')->setServer('SCRIPT_NAME', '/foo/public/index.php'); $this->config->baseURL = 'http://example.com:8080/foo/public/'; @@ -187,8 +188,8 @@ public function testCurrentURLWithPortInSubfolder(): void public function testUriString(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/assets/image.jpg'); $this->config->indexPage = ''; @@ -199,8 +200,8 @@ public function testUriString(): void public function testUriStringNoTrailingSlash(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/assets/image.jpg'); $this->config->baseURL = 'http://example.com/'; $this->config->indexPage = ''; @@ -219,8 +220,8 @@ public function testUriStringEmpty(): void public function testUriStringSubfolderAbsolute(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/subfolder/assets/image.jpg'); $this->config->baseURL = 'http://example.com/subfolder/'; @@ -231,9 +232,9 @@ public function testUriStringSubfolderAbsolute(): void public function testUriStringSubfolderRelative(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; - $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/subfolder/assets/image.jpg'); + service('superglobals')->setServer('SCRIPT_NAME', '/subfolder/index.php'); $this->config->baseURL = 'http://example.com/subfolder/'; @@ -245,8 +246,8 @@ public function testUriStringSubfolderRelative(): void #[DataProvider('provideUrlIs')] public function testUrlIs(string $currentPath, string $testPath, bool $expected): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/' . $currentPath); $this->createRequest($this->config); @@ -256,8 +257,8 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected) #[DataProvider('provideUrlIs')] public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/' . $currentPath); $this->config->indexPage = ''; @@ -269,9 +270,9 @@ public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $ex #[DataProvider('provideUrlIs')] public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bool $expected): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; - $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/' . $currentPath); + service('superglobals')->setServer('SCRIPT_NAME', '/subfolder/index.php'); $this->config->baseURL = 'http://example.com/subfolder/'; diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 3224e426914a..cc7830084a96 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -44,6 +44,7 @@ protected function setUp(): void parent::setUp(); Services::reset(true); + Services::injectMock('superglobals', new Superglobals()); service('routes')->loadRoutes(); // Set a common base configuration (overriden by individual tests) @@ -56,7 +57,7 @@ protected function tearDown(): void { parent::tearDown(); - $_SERVER = []; + service('superglobals')->setServerArray([]); } #[Group('SeparateProcess')] @@ -68,7 +69,7 @@ public function testPreviousURLUsesSessionFirst(): void $uri1 = 'http://example.com/one?two'; $uri2 = 'http://example.com/two?foo'; - $_SERVER['HTTP_REFERER'] = $uri1; + service('superglobals')->setServer('HTTP_REFERER', $uri1); session()->set('_ci_previous_url', $uri2); $this->config->baseURL = 'http://example.com/public'; @@ -98,7 +99,7 @@ public function testPreviousURLUsesRefererIfNeeded(): void { $uri1 = 'http://example.com/one?two'; - $_SERVER['HTTP_REFERER'] = $uri1; + service('superglobals')->setServer('HTTP_REFERER', $uri1); $this->config->baseURL = 'http://example.com/public'; @@ -844,7 +845,7 @@ public function testMbUrlTitleExtraDashes(): void #[DataProvider('provideUrlTo')] public function testUrlTo(string $expected, string $input, ...$args): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); $routes = service('routes'); // @TODO Do not put any placeholder after (:any). diff --git a/tests/system/Helpers/URLHelper/SiteUrlTest.php b/tests/system/Helpers/URLHelper/SiteUrlTest.php index b8c1dff4678b..791515a09fab 100644 --- a/tests/system/Helpers/URLHelper/SiteUrlTest.php +++ b/tests/system/Helpers/URLHelper/SiteUrlTest.php @@ -43,6 +43,7 @@ protected function setUp(): void parent::setUp(); Services::reset(true); + Services::injectMock('superglobals', new Superglobals()); $this->config = new App(); } @@ -353,15 +354,15 @@ public function testBaseURLDiscovery(): void { $this->config->baseURL = 'http://example.com/'; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/test'); $this->createRequest($this->config); $this->assertSame('http://example.com/', base_url()); - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test/page'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/test/page'); $this->createRequest($this->config); @@ -371,8 +372,8 @@ public function testBaseURLDiscovery(): void public function testBaseURLService(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/ci/v4/x/y'); $this->config->baseURL = 'http://example.com/ci/v4/'; @@ -390,7 +391,9 @@ public function testBaseURLService(): void public function testBaseURLWithCLIRequest(): void { - unset($_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']); + $superglobals = service('superglobals'); + $superglobals->unsetServer('HTTP_HOST'); + $superglobals->unsetServer('REQUEST_URI'); $this->config->baseURL = 'http://example.com/'; @@ -408,9 +411,9 @@ public function testBaseURLWithCLIRequest(): void public function testSiteURLWithAllowedHostname(): void { - $_SERVER['HTTP_HOST'] = 'www.example.jp'; - $_SERVER['REQUEST_URI'] = '/public'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.jp'); + service('superglobals')->setServer('REQUEST_URI', '/public'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; $this->config->allowedHostnames = ['www.example.jp']; @@ -425,9 +428,9 @@ public function testSiteURLWithAllowedHostname(): void public function testSiteURLWithAltConfig(): void { - $_SERVER['HTTP_HOST'] = 'www.example.jp'; - $_SERVER['REQUEST_URI'] = '/public'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.jp'); + service('superglobals')->setServer('REQUEST_URI', '/public'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; $this->config->allowedHostnames = ['www.example.jp']; @@ -445,9 +448,9 @@ public function testSiteURLWithAltConfig(): void public function testBaseURLWithAllowedHostname(): void { - $_SERVER['HTTP_HOST'] = 'www.example.jp'; - $_SERVER['REQUEST_URI'] = '/public'; - $_SERVER['SCRIPT_NAME'] = '/public/index.php'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.jp'); + service('superglobals')->setServer('REQUEST_URI', '/public'); + service('superglobals')->setServer('SCRIPT_NAME', '/public/index.php'); $this->config->baseURL = 'http://example.com/public/'; $this->config->allowedHostnames = ['www.example.jp']; diff --git a/tests/system/Honeypot/HoneypotTest.php b/tests/system/Honeypot/HoneypotTest.php index b92c2d1158b7..8c85da575a95 100644 --- a/tests/system/Honeypot/HoneypotTest.php +++ b/tests/system/Honeypot/HoneypotTest.php @@ -14,12 +14,14 @@ namespace CodeIgniter\Honeypot; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\Filters\Filters; use CodeIgniter\Honeypot\Exceptions\HoneypotException; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\Response; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use Config\Honeypot as HoneypotConfig; @@ -47,12 +49,14 @@ protected function setUp(): void { parent::setUp(); + Services::injectMock('superglobals', new Superglobals()); + $this->config = new HoneypotConfig(); $this->honeypot = new Honeypot($this->config); - unset($_POST[$this->config->name]); - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST[$this->config->name] = 'hey'; + $superglobals = service('superglobals'); + $superglobals->setServer('REQUEST_METHOD', 'POST'); + $superglobals->setPost($this->config->name, 'hey'); $this->request = service('request', null, false); $this->response = service('response'); @@ -131,7 +135,7 @@ public function testNotAttachHoneypotWithCSP(): void public function testHasntContent(): void { - unset($_POST[$this->config->name]); + service('superglobals')->unsetPost($this->config->name); $this->request = service('request'); $this->assertFalse($this->honeypot->hasContent($this->request)); diff --git a/tests/system/Log/LoggerTest.php b/tests/system/Log/LoggerTest.php index 623b665cfda3..80d42d6c83b3 100644 --- a/tests/system/Log/LoggerTest.php +++ b/tests/system/Log/LoggerTest.php @@ -131,8 +131,8 @@ public function testLogInterpolatesPost(): void Time::setTestNow('2023-11-25 12:00:00'); - $_POST = ['foo' => 'bar']; - $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_POST: ' . print_r($_POST, true); + service('superglobals')->setPost('foo', 'bar'); + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_POST: ' . print_r(service('superglobals')->getPostArray(), true); $logger->log('debug', 'Test message {post_vars}'); @@ -150,8 +150,8 @@ public function testLogInterpolatesGet(): void Time::setTestNow('2023-11-25 12:00:00'); - $_GET = ['bar' => 'baz']; - $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_GET: ' . print_r($_GET, true); + service('superglobals')->setGet('bar', 'baz'); + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_GET: ' . print_r(service('superglobals')->getGetArray(), true); $logger->log('debug', 'Test message {get_vars}'); diff --git a/tests/system/Models/PaginateModelTest.php b/tests/system/Models/PaginateModelTest.php index 43a915bf101e..08c85dfb0590 100644 --- a/tests/system/Models/PaginateModelTest.php +++ b/tests/system/Models/PaginateModelTest.php @@ -86,7 +86,7 @@ public function testPaginatePageOutOfRange(): void public function testMultiplePager(): void { - $_GET = []; + service('superglobals')->setGetArray([]); $validModel = $this->createModel(ValidModel::class); $userModel = $this->createModel(UserModel::class); diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 3e63f555dfb6..fcba5ed27c20 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -19,6 +19,7 @@ use CodeIgniter\HTTP\URI; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Pager\Exceptions\PagerException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\App; use Config\Pager as PagerConfig; @@ -40,14 +41,17 @@ protected function setUp(): void { parent::setUp(); + $_SERVER = $_GET = $_POST = $_COOKIE = $_FILES = $_REQUEST = []; + + Services::injectMock('superglobals', new Superglobals()); + $this->createPager('/'); } private function createPager(string $requestUri): void { - $_SERVER['REQUEST_URI'] = $requestUri; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_GET = []; + service('superglobals')->setServer('REQUEST_URI', $requestUri); + service('superglobals')->setServer('SCRIPT_NAME', '/index.php'); $config = new App(); $config->baseURL = 'http://example.com/'; @@ -78,7 +82,7 @@ public function testSetPathRemembersPath(): void public function testGetDetailsRecognizesPageQueryVar(): void { - $_GET['page'] = 2; + service('superglobals')->setGet('page', '2'); // Need this to create the group. $this->pager->setPath('foo/bar'); @@ -90,7 +94,7 @@ public function testGetDetailsRecognizesPageQueryVar(): void public function testGetDetailsRecognizesGroupedPageQueryVar(): void { - $_GET['page_foo'] = 2; + service('superglobals')->setGet('page_foo', '2'); // Need this to create the group. $this->pager->setPath('foo/bar', 'foo'); @@ -155,8 +159,8 @@ public function testStoreAndHasMoreCanBeFalse(): void public function testStoreWithQueries(): void { - $_GET['page'] = 3; - $_GET['foo'] = 'bar'; + service('superglobals')->setGet('page', '3'); + service('superglobals')->setGet('foo', 'bar'); $this->pager->store('default', 3, 25, 100); @@ -175,8 +179,8 @@ public function testStoreWithQueries(): void public function testStoreWithSegments(): void { - $_GET['page'] = 3; - $_GET['foo'] = 'bar'; + service('superglobals')->setGet('page', '3'); + service('superglobals')->setGet('foo', 'bar'); $this->pager->store('default', 3, 25, 100, 1); @@ -233,14 +237,14 @@ public function testGetCurrentPageRemembersStoredPage(): void public function testGetCurrentPageDetectsURI(): void { - $_GET['page'] = 2; + service('superglobals')->setGet('page', '2'); $this->assertSame(2, $this->pager->getCurrentPage()); } public function testGetCurrentPageDetectsGroupedURI(): void { - $_GET['page_foo'] = 2; + service('superglobals')->setGet('page_foo', '2'); $this->assertSame(2, $this->pager->getCurrentPage('foo')); } @@ -276,7 +280,7 @@ public function testGetTotalPagesCalcsCorrectValue(): void public function testGetNextURIUsesCurrentURI(): void { - $_GET['page_foo'] = 2; + service('superglobals')->setGet('page_foo', '2'); $this->pager->store('foo', 2, 12, 70); @@ -305,7 +309,7 @@ public function testGetNextURICorrectOnFirstPage(): void public function testGetPreviousURIUsesCurrentURI(): void { - $_GET['page_foo'] = 2; + service('superglobals')->setGet('page_foo', '2'); $this->pager->store('foo', 2, 12, 70); @@ -324,48 +328,49 @@ public function testGetNextURIReturnsNullOnFirstPage(): void public function testGetNextURIWithQueryStringUsesCurrentURI(): void { - $_GET = [ - 'page_foo' => 3, - 'status' => 1, - ]; + service('superglobals')->setGet('page_foo', '3'); + service('superglobals')->setGet('status', '1'); + $getArray = service('superglobals')->getGetArray(); $expected = current_url(true); - $expected = (string) $expected->setQueryArray($_GET); + $expected = (string) $expected->setQueryArray($getArray); - $this->pager->store('foo', $_GET['page_foo'] - 1, 12, 70); + $this->pager->store('foo', (int) $getArray['page_foo'] - 1, 12, 70); $this->assertSame($expected, $this->pager->getNextPageURI('foo')); } public function testGetPreviousURIWithQueryStringUsesCurrentURI(): void { - $_GET = [ - 'page_foo' => 1, - 'status' => 1, - ]; + service('superglobals')->setGet('page_foo', '1'); + service('superglobals')->setGet('status', '1'); + + $getArray = service('superglobals')->getGetArray(); $expected = current_url(true); - $expected = (string) $expected->setQueryArray($_GET); + $expected = (string) $expected->setQueryArray($getArray); - $this->pager->store('foo', $_GET['page_foo'] + 1, 12, 70); + $this->pager->store('foo', (int) $getArray['page_foo'] + 1, 12, 70); $this->assertSame($expected, $this->pager->getPreviousPageURI('foo')); } public function testGetOnlyQueries(): void { - $_GET = [ - 'page' => 2, + $getArray = [ + 'page' => '2', 'search' => 'foo', 'order' => 'asc', 'hello' => 'xxx', 'category' => 'baz', ]; + service('superglobals')->setGetArray($getArray); + $onlyQueries = [ 'search', 'order', ]; - $this->pager->store('default', $_GET['page'], 10, 100); + $this->pager->store('default', (int) $getArray['page'], 10, 100); $uri = current_url(true); @@ -473,10 +478,9 @@ public function testHeadLinks(): void public function testBasedURI(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; - $_SERVER['SCRIPT_NAME'] = '/ci/v4/index.php'; - $_GET = []; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); + service('superglobals')->setServer('REQUEST_URI', '/ci/v4/x/y'); + service('superglobals')->setServer('SCRIPT_NAME', '/ci/v4/index.php'); $config = new App(); $config->baseURL = 'http://example.com/ci/v4/'; @@ -495,7 +499,7 @@ public function testBasedURI(): void $this->config = new PagerConfig(); $this->pager = new Pager($this->config, service('renderer')); - $_GET['page_foo'] = 2; + service('superglobals')->setGet('page_foo', '2'); $this->pager->store('foo', 2, 12, 70); diff --git a/tests/system/RESTful/ResourceControllerTest.php b/tests/system/RESTful/ResourceControllerTest.php index fcbe08070a81..c51e4ae39174 100644 --- a/tests/system/RESTful/ResourceControllerTest.php +++ b/tests/system/RESTful/ResourceControllerTest.php @@ -23,6 +23,7 @@ use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Model; use CodeIgniter\Router\RouteCollection; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCodeIgniter; use CodeIgniter\Test\Mock\MockResourceController; @@ -62,11 +63,13 @@ protected function setUp(): void $this->resetServices(true); $this->resetFactories(); + + Services::injectMock('superglobals', new Superglobals()); } private function createCodeigniter(): void { - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); // Inject mock router. $this->routes = service('routes'); @@ -91,14 +94,14 @@ protected function tearDown(): void public function testResourceGet(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', - ]; - $_SERVER['argc'] = 2; + ]); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/work'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -112,15 +115,15 @@ public function testResourceGet(): void public function testResourceGetNew(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'new', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/new'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/new'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -134,16 +137,16 @@ public function testResourceGetNew(): void public function testResourceGetEdit(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', '1', 'edit', - ]; - $_SERVER['argc'] = 4; + ]); + service('superglobals')->setServer('argc', 4); - $_SERVER['REQUEST_URI'] = '/work/1/edit'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/1/edit'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -157,15 +160,15 @@ public function testResourceGetEdit(): void public function testResourceGetOne(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', '1', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -179,14 +182,14 @@ public function testResourceGetOne(): void public function testResourcePost(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', - ]; - $_SERVER['argc'] = 2; + ]); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/work'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/work'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $this->createCodeigniter(); @@ -200,15 +203,15 @@ public function testResourcePost(): void public function testResourcePatch(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', '123', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/123'; - $_SERVER['REQUEST_METHOD'] = 'PATCH'; + service('superglobals')->setServer('REQUEST_URI', '/work/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'PATCH'); $this->createCodeigniter(); @@ -222,15 +225,15 @@ public function testResourcePatch(): void public function testResourcePut(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', '123', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/123'; - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_URI', '/work/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $this->createCodeigniter(); @@ -244,15 +247,15 @@ public function testResourcePut(): void public function testResourceDelete(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', '123', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/123'; - $_SERVER['REQUEST_METHOD'] = 'DELETE'; + service('superglobals')->setServer('REQUEST_URI', '/work/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'DELETE'); $this->createCodeigniter(); diff --git a/tests/system/RESTful/ResourcePresenterTest.php b/tests/system/RESTful/ResourcePresenterTest.php index db10f07f6588..7e002d8e48b7 100644 --- a/tests/system/RESTful/ResourcePresenterTest.php +++ b/tests/system/RESTful/ResourcePresenterTest.php @@ -17,6 +17,7 @@ use CodeIgniter\Config\Services; use CodeIgniter\Model; use CodeIgniter\Router\RouteCollection; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCodeIgniter; use CodeIgniter\Test\Mock\MockResourcePresenter; @@ -56,11 +57,13 @@ protected function setUp(): void $this->resetServices(true); $this->resetFactories(); + + Services::injectMock('superglobals', new Superglobals()); } private function createCodeigniter(): void { - $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + service('superglobals')->setServer('SERVER_PROTOCOL', 'HTTP/1.1'); // Inject mock router. $this->routes = service('routes'); @@ -82,14 +85,14 @@ protected function tearDown(): void public function testResourceGet(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', - ]; - $_SERVER['argc'] = 2; + ]); + service('superglobals')->setServer('argc', 2); - $_SERVER['REQUEST_URI'] = '/work'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -102,16 +105,16 @@ public function testResourceGet(): void public function testResourceShow(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'show', '1', - ]; - $_SERVER['argc'] = 4; + ]); + service('superglobals')->setServer('argc', 4); - $_SERVER['REQUEST_URI'] = '/work/show/1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/show/1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -124,15 +127,15 @@ public function testResourceShow(): void public function testResourceNew(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'new', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/new'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/new'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -145,15 +148,15 @@ public function testResourceNew(): void public function testResourceCreate(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'create', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/create'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/work/create'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $this->createCodeigniter(); @@ -166,16 +169,16 @@ public function testResourceCreate(): void public function testResourceRemove(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'remove', '123', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/remove/123'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/remove/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -188,16 +191,16 @@ public function testResourceRemove(): void public function testResourceDelete(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'delete', '123', - ]; - $_SERVER['argc'] = 3; + ]); + service('superglobals')->setServer('argc', 3); - $_SERVER['REQUEST_URI'] = '/work/delete/123'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/work/delete/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $this->createCodeigniter(); @@ -210,17 +213,17 @@ public function testResourceDelete(): void public function testResourceEdit(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'edit', '1', 'edit', - ]; - $_SERVER['argc'] = 4; + ]); + service('superglobals')->setServer('argc', 4); - $_SERVER['REQUEST_URI'] = '/work/edit/1'; - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_URI', '/work/edit/1'); + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $this->createCodeigniter(); @@ -233,16 +236,16 @@ public function testResourceEdit(): void public function testResourceUpdate(): void { - $_SERVER['argv'] = [ + service('superglobals')->setServer('argv', [ 'index.php', 'work', 'update', '123', - ]; - $_SERVER['argc'] = 4; + ]); + service('superglobals')->setServer('argc', 4); - $_SERVER['REQUEST_URI'] = '/work/update/123'; - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_URI', '/work/update/123'); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $this->createCodeigniter(); diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 34fe4f088d1e..6346697d206a 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -15,9 +15,11 @@ use App\Controllers\Home; use App\Controllers\Product; +use CodeIgniter\Config\Services; use CodeIgniter\Controller; use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\HTTP\Method; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Feature; use Config\Modules; @@ -38,6 +40,8 @@ protected function setUp(): void $this->resetServices(true); $this->resetFactories(); + + Services::injectMock('superglobals', new Superglobals()); } protected function getCollector(array $config = [], array $files = [], $moduleConfig = null): RouteCollection @@ -551,7 +555,7 @@ public static function provideNestedGroupingWorksWithRootPrefix(): iterable public function testHostnameOption(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); $routes = $this->getCollector(); @@ -1161,7 +1165,7 @@ public function testAddRedirectGetMethod(): void */ public function testWithSubdomain(): void { - $_SERVER['HTTP_HOST'] = 'adm.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'adm.example.com'); $routes = $this->getCollector(); @@ -1176,7 +1180,7 @@ public function testWithSubdomain(): void public function testWithSubdomainMissing(): void { - $_SERVER['HTTP_HOST'] = 'www.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.com'); $routes = $this->getCollector(); @@ -1191,7 +1195,7 @@ public function testWithSubdomainMissing(): void public function testWithDifferentSubdomain(): void { - $_SERVER['HTTP_HOST'] = 'adm.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'adm.example.com'); $routes = $this->getCollector(); @@ -1208,7 +1212,7 @@ public function testWithWWWSubdomain(): void { $routes = $this->getCollector(); - $_SERVER['HTTP_HOST'] = 'www.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'www.example.com'); $routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'sales']); $routes->add('/objects/(:alphanum)', 'App::objectsList/$1'); @@ -1224,7 +1228,7 @@ public function testWithDotCoSubdomain(): void { $routes = $this->getCollector(); - $_SERVER['HTTP_HOST'] = 'example.co.uk'; + service('superglobals')->setServer('HTTP_HOST', 'example.co.uk'); $routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'sales']); $routes->add('/objects/(:alphanum)', 'App::objectsList/$1'); @@ -1238,7 +1242,7 @@ public function testWithDotCoSubdomain(): void public function testWithDifferentSubdomainMissing(): void { - $_SERVER['HTTP_HOST'] = 'adm.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'adm.example.com'); $routes = $this->getCollector(); @@ -1257,7 +1261,7 @@ public function testWithDifferentSubdomainMissing(): void */ public function testWithNoSubdomainAndDot(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); $routes = $this->getCollector(); @@ -1271,7 +1275,7 @@ public function testWithNoSubdomainAndDot(): void */ public function testWithSubdomainOrdered(): void { - $_SERVER['HTTP_HOST'] = 'adm.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'adm.example.com'); $routes = $this->getCollector(); @@ -1531,7 +1535,7 @@ public function testOffsetParameters(): void */ public function testRouteToWithSubdomainMatch(): void { - $_SERVER['HTTP_HOST'] = 'doc.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1543,7 +1547,7 @@ public function testRouteToWithSubdomainMatch(): void public function testRouteToWithSubdomainMismatch(): void { - $_SERVER['HTTP_HOST'] = 'dev.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'dev.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1555,7 +1559,7 @@ public function testRouteToWithSubdomainMismatch(): void public function testRouteToWithSubdomainNot(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1567,7 +1571,7 @@ public function testRouteToWithSubdomainNot(): void public function testRouteToWithGenericSubdomainMatch(): void { - $_SERVER['HTTP_HOST'] = 'doc.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1579,7 +1583,7 @@ public function testRouteToWithGenericSubdomainMatch(): void public function testRouteToWithGenericSubdomainMismatch(): void { - $_SERVER['HTTP_HOST'] = 'dev.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'dev.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1591,7 +1595,7 @@ public function testRouteToWithGenericSubdomainMismatch(): void public function testRouteToWithGenericSubdomainNot(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1603,7 +1607,7 @@ public function testRouteToWithGenericSubdomainNot(): void public function testRouteToWithoutSubdomainMatch(): void { - $_SERVER['HTTP_HOST'] = 'doc.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1615,7 +1619,7 @@ public function testRouteToWithoutSubdomainMatch(): void public function testRouteToWithoutSubdomainMismatch(): void { - $_SERVER['HTTP_HOST'] = 'dev.example.com'; + service('superglobals')->setServer('HTTP_HOST', 'dev.example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1627,7 +1631,7 @@ public function testRouteToWithoutSubdomainMismatch(): void public function testRouteToWithoutSubdomainNot(): void { - $_SERVER['HTTP_HOST'] = 'example.com'; + service('superglobals')->setServer('HTTP_HOST', 'example.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1644,7 +1648,7 @@ public function testRouteToWithoutSubdomainNot(): void */ public function testRouteOverwritingDifferentSubdomains(): void { - $_SERVER['HTTP_HOST'] = 'doc.domain.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1665,7 +1669,7 @@ public function testRouteOverwritingDifferentSubdomains(): void public function testRouteOverwritingTwoRules(): void { - $_SERVER['HTTP_HOST'] = 'doc.domain.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1686,7 +1690,7 @@ public function testRouteOverwritingTwoRules(): void public function testRouteOverwritingTwoRulesLastApplies(): void { - $_SERVER['HTTP_HOST'] = 'doc.domain.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1706,7 +1710,7 @@ public function testRouteOverwritingTwoRulesLastApplies(): void public function testRouteOverwritingMatchingSubdomain(): void { - $_SERVER['HTTP_HOST'] = 'doc.domain.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); @@ -1726,7 +1730,7 @@ public function testRouteOverwritingMatchingSubdomain(): void public function testRouteOverwritingMatchingHost(): void { - $_SERVER['HTTP_HOST'] = 'doc.domain.com'; + service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com'); service('request')->setMethod(Method::GET); $routes = $this->getCollector(); diff --git a/tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php b/tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php index 3d528e92f564..81ee36b1a7f0 100644 --- a/tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php +++ b/tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php @@ -14,10 +14,12 @@ namespace CodeIgniter\Security; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\Cookie\Cookie; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockAppConfig; use CodeIgniter\Test\Mock\MockSecurity; @@ -47,6 +49,7 @@ protected function setUp(): void parent::setUp(); $_COOKIE = []; + Services::injectMock('superglobals', new Superglobals()); $this->config = new SecurityConfig(); $this->config->csrfProtection = Security::CSRF_PROTECTION_COOKIE; @@ -54,8 +57,8 @@ protected function setUp(): void Factories::injectMock('config', 'Security', $this->config); // Set Cookie value - $security = new MockSecurity($this->config); - $_COOKIE[$security->getCookieName()] = $this->hash; + $security = new MockSecurity($this->config); + service('superglobals')->setCookie($security->getCookieName(), $this->hash); $this->resetServices(); } @@ -72,9 +75,9 @@ public function testTokenIsReadFromCookie(): void public function testCSRFVerifySetNewCookie(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; - $_POST['csrf_test_name'] = $this->randomizedToken; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setPost('csrf_test_name', $this->randomizedToken); $config = new MockAppConfig(); $request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent()); @@ -83,7 +86,7 @@ public function testCSRFVerifySetNewCookie(): void $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); /** @var Cookie $cookie */ $cookie = $this->getPrivateProperty($security, 'cookie'); diff --git a/tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php b/tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php index 619f55829712..a4a7f67432da 100644 --- a/tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php +++ b/tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php @@ -22,6 +22,7 @@ use CodeIgniter\Session\Handlers\ArrayHandler; use CodeIgniter\Session\Handlers\FileHandler; use CodeIgniter\Session\Session; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockAppConfig; use CodeIgniter\Test\Mock\MockSecurity; @@ -65,6 +66,8 @@ protected function setUp(): void $_SESSION = []; Factories::reset(); + Services::injectMock('superglobals', new Superglobals()); + $this->config = new SecurityConfig(); $this->config->csrfProtection = Security::CSRF_PROTECTION_SESSION; $this->config->tokenRandomize = true; @@ -138,8 +141,7 @@ public function testCSRFVerifyPostNoToken(): void $this->expectException(SecurityException::class); $this->expectExceptionMessage('The action you requested is not allowed.'); - $_SERVER['REQUEST_METHOD'] = 'POST'; - unset($_POST['csrf_test_name']); + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); @@ -159,8 +161,8 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void $this->expectException(SecurityException::class); $this->expectExceptionMessage('The action you requested is not allowed.'); - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005b'); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); @@ -176,8 +178,8 @@ public function testCSRFVerifyPostInvalidToken(): void $this->expectException(SecurityException::class); $this->expectExceptionMessage('The action you requested is not allowed.'); - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '!'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '!'); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); @@ -187,21 +189,21 @@ public function testCSRFVerifyPostInvalidToken(): void public function testCSRFVerifyPostReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; - $_POST['csrf_test_name'] = $this->randomizedToken; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setPost('csrf_test_name', $this->randomizedToken); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyPOSTHeaderThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005b'); @@ -215,8 +217,8 @@ public function testCSRFVerifyPOSTHeaderThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPOSTHeaderReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', $this->randomizedToken); @@ -224,12 +226,12 @@ public function testCSRFVerifyPOSTHeaderReturnsSelfOnMatch(): void $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyPUTHeaderThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005b'); @@ -243,7 +245,7 @@ public function testCSRFVerifyPUTHeaderThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPUTHeaderReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', $this->randomizedToken); @@ -255,7 +257,7 @@ public function testCSRFVerifyPUTHeaderReturnsSelfOnMatch(): void public function testCSRFVerifyPUTBodyReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setBody("csrf_test_name={$this->randomizedToken}&foo=bar"); @@ -270,7 +272,7 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void $this->expectException(SecurityException::class); $this->expectExceptionMessage('The action you requested is not allowed.'); - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setBody('{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005b"}'); @@ -281,7 +283,7 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void public function testCSRFVerifyJsonReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setBody('{"csrf_test_name":"' . $this->randomizedToken . '","foo":"bar"}'); @@ -294,8 +296,8 @@ public function testCSRFVerifyJsonReturnsSelfOnMatch(): void public function testRegenerateWithFalseSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = $this->randomizedToken; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', $this->randomizedToken); $config = Factories::config('Security'); $config->tokenRandomize = true; @@ -314,8 +316,8 @@ public function testRegenerateWithFalseSecurityRegenerateProperty(): void public function testRegenerateWithTrueSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = $this->randomizedToken; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', $this->randomizedToken); $config = Factories::config('Security'); $config->tokenRandomize = true; diff --git a/tests/system/Security/SecurityCSRFSessionTest.php b/tests/system/Security/SecurityCSRFSessionTest.php index 7fb618bef936..61e5d5a666f2 100644 --- a/tests/system/Security/SecurityCSRFSessionTest.php +++ b/tests/system/Security/SecurityCSRFSessionTest.php @@ -22,6 +22,7 @@ use CodeIgniter\Session\Handlers\ArrayHandler; use CodeIgniter\Session\Handlers\FileHandler; use CodeIgniter\Session\Session; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockAppConfig; use CodeIgniter\Test\Mock\MockSession; @@ -59,6 +60,8 @@ protected function setUp(): void $_SESSION = []; Factories::reset(); + Services::injectMock('superglobals', new Superglobals()); + $this->config = new SecurityConfig(); $this->config->csrfProtection = Security::CSRF_PROTECTION_SESSION; Factories::injectMock('config', 'Security', $this->config); @@ -127,8 +130,8 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void { $this->expectException(SecurityException::class); - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005b'); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); @@ -145,21 +148,21 @@ private function createIncomingRequest(?App $config = null): IncomingRequest public function testCSRFVerifyPostReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); $request = $this->createIncomingRequest(); $security = $this->createSecurity(); $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyPOSTHeaderThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005b'); @@ -171,8 +174,8 @@ public function testCSRFVerifyPOSTHeaderThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPOSTHeaderReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a'); @@ -180,12 +183,12 @@ public function testCSRFVerifyPOSTHeaderReturnsSelfOnMatch(): void $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyPUTHeaderThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005b'); @@ -197,7 +200,7 @@ public function testCSRFVerifyPUTHeaderThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPUTHeaderReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a'); @@ -209,7 +212,7 @@ public function testCSRFVerifyPUTHeaderReturnsSelfOnMatch(): void public function testCSRFVerifyPUTBodyReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); $request = $this->createIncomingRequest(); $request->setBody('csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a&foo=bar'); @@ -223,7 +226,7 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void { $this->expectException(SecurityException::class); - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setBody('{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005b"}'); @@ -234,7 +237,7 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void public function testCSRFVerifyJsonReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); $request = $this->createIncomingRequest(); $request->setBody('{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005a","foo":"bar"}'); @@ -247,8 +250,8 @@ public function testCSRFVerifyJsonReturnsSelfOnMatch(): void public function testRegenerateWithFalseSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); $config = Factories::config('Security'); $config->regenerate = false; @@ -266,8 +269,8 @@ public function testRegenerateWithFalseSecurityRegenerateProperty(): void public function testRegenerateWithTrueSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); $config = Factories::config('Security'); $config->regenerate = true; diff --git a/tests/system/Security/SecurityTest.php b/tests/system/Security/SecurityTest.php index 6bc975d16c22..a1d8400bfee9 100644 --- a/tests/system/Security/SecurityTest.php +++ b/tests/system/Security/SecurityTest.php @@ -14,11 +14,13 @@ namespace CodeIgniter\Security; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\Request; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Security\Exceptions\SecurityException; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockAppConfig; use CodeIgniter\Test\Mock\MockSecurity; @@ -41,6 +43,8 @@ protected function setUp(): void $_COOKIE = []; $this->resetServices(); + + Services::injectMock('superglobals', new Superglobals()); } private static function createMockSecurity(SecurityConfig $config = new SecurityConfig()): MockSecurity @@ -72,7 +76,7 @@ public function testBasicConfigIsSaved(): void public function testHashIsReadFromCookie(): void { - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); @@ -86,7 +90,7 @@ public function testGetHashSetsCookieWhenGETWithoutCSRFCookie(): void { $security = $this->createMockSecurity(); - $_SERVER['REQUEST_METHOD'] = 'GET'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); $security->verify(new Request(new MockAppConfig())); @@ -96,21 +100,21 @@ public function testGetHashSetsCookieWhenGETWithoutCSRFCookie(): void public function testGetHashReturnsCSRFCookieWhenGETWithCSRFCookie(): void { - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'GET'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); $security->verify(new Request(new MockAppConfig())); - $this->assertSame($_COOKIE['csrf_cookie_name'], $security->getHash()); + $this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $security->getHash()); } public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005b'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -121,10 +125,10 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPostReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -132,13 +136,13 @@ public function testCSRFVerifyPostReturnsSelfOnMatch(): void $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyHeaderThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005b'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -151,9 +155,9 @@ public function testCSRFVerifyHeaderThrowsExceptionOnNoMatch(): void public function testCSRFVerifyHeaderReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['foo'] = 'bar'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('foo', 'bar'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -163,13 +167,13 @@ public function testCSRFVerifyHeaderReturnsSelfOnMatch(): void $this->assertInstanceOf(Security::class, $security->verify($request)); $this->assertLogged('info', 'CSRF token verified.'); - $this->assertCount(1, $_POST); + $this->assertCount(1, service('superglobals')->getPostArray()); } public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005b'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -184,8 +188,8 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void public function testCSRFVerifyJsonReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -202,8 +206,8 @@ public function testCSRFVerifyJsonReturnsSelfOnMatch(): void public function testCSRFVerifyPutBodyThrowsExceptionOnNoMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005b'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -218,8 +222,8 @@ public function testCSRFVerifyPutBodyThrowsExceptionOnNoMatch(): void public function testCSRFVerifyPutBodyReturnsSelfOnMatch(): void { - $_SERVER['REQUEST_METHOD'] = 'PUT'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'PUT'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $security = $this->createMockSecurity(); $request = $this->createIncomingRequest(); @@ -245,9 +249,9 @@ public function testSanitizeFilename(): void public function testRegenerateWithFalseSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $config = new SecurityConfig(); $config->regenerate = false; @@ -265,9 +269,9 @@ public function testRegenerateWithFalseSecurityRegenerateProperty(): void public function testRegenerateWithFalseSecurityRegeneratePropertyManually(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $config = new SecurityConfig(); $config->regenerate = false; @@ -286,9 +290,9 @@ public function testRegenerateWithFalseSecurityRegeneratePropertyManually(): voi public function testRegenerateWithTrueSecurityRegenerateProperty(): void { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; + service('superglobals')->setServer('REQUEST_METHOD', 'POST'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + service('superglobals')->setCookie('csrf_cookie_name', '8b9218a55906f9dcc1dc263dce7f005a'); $config = new SecurityConfig(); $config->regenerate = true; @@ -317,16 +321,15 @@ public function testGetters(): void public function testGetPostedTokenReturnsTokenFromPost(): void { - $_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a'; - $request = $this->createIncomingRequest(); - $method = self::getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken'); + service('superglobals')->setPost('csrf_test_name', '8b9218a55906f9dcc1dc263dce7f005a'); + $request = $this->createIncomingRequest(); + $method = self::getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken'); $this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request)); } public function testGetPostedTokenReturnsTokenFromHeader(): void { - $_POST = []; $request = $this->createIncomingRequest()->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a'); $method = self::getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken'); @@ -335,7 +338,6 @@ public function testGetPostedTokenReturnsTokenFromHeader(): void public function testGetPostedTokenReturnsTokenFromJsonBody(): void { - $_POST = []; $jsonBody = json_encode(['csrf_test_name' => '8b9218a55906f9dcc1dc263dce7f005a']); $request = $this->createIncomingRequest()->setBody($jsonBody); $method = self::getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken'); @@ -345,7 +347,6 @@ public function testGetPostedTokenReturnsTokenFromJsonBody(): void public function testGetPostedTokenReturnsTokenFromFormBody(): void { - $_POST = []; $formBody = 'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a'; $request = $this->createIncomingRequest()->setBody($formBody); $method = self::getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken'); diff --git a/tests/system/Session/SessionTest.php b/tests/system/Session/SessionTest.php index 78aec295b718..e49a3b126bea 100644 --- a/tests/system/Session/SessionTest.php +++ b/tests/system/Session/SessionTest.php @@ -14,8 +14,10 @@ namespace CodeIgniter\Session; use CodeIgniter\Config\Factories; +use CodeIgniter\Config\Services; use CodeIgniter\Cookie\Exceptions\CookieException; use CodeIgniter\Session\Handlers\FileHandler; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockSession; use CodeIgniter\Test\TestLogger; @@ -42,6 +44,8 @@ protected function setUp(): void $_COOKIE = []; $_SESSION = []; + + Services::injectMock('superglobals', new Superglobals()); } protected function getInstance($options = []): MockSession @@ -154,8 +158,8 @@ public function testGetReturnsNullWhenNotFound(): void public function testGetReturnsNullWhenNotFoundWithXmlHttpRequest(): void { - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; - $_SESSION = []; + service('superglobals')->setServer('HTTP_X_REQUESTED_WITH', 'xmlhttprequest'); + $_SESSION = []; $session = $this->getInstance(); $session->start(); @@ -165,8 +169,8 @@ public function testGetReturnsNullWhenNotFoundWithXmlHttpRequest(): void public function testGetReturnsEmptyArrayWhenWithXmlHttpRequest(): void { - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; - $_SESSION = []; + service('superglobals')->setServer('HTTP_X_REQUESTED_WITH', 'xmlhttprequest'); + $_SESSION = []; $session = $this->getInstance(); $session->start(); diff --git a/tests/system/SuperglobalsTest.php b/tests/system/SuperglobalsTest.php index 904e3e913d21..73eceec1c658 100644 --- a/tests/system/SuperglobalsTest.php +++ b/tests/system/SuperglobalsTest.php @@ -13,21 +13,448 @@ namespace CodeIgniter; +use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\Test\CIUnitTestCase; +use PHPUnit\Framework\Attributes\BackupGlobals; use PHPUnit\Framework\Attributes\Group; /** * @internal */ +#[BackupGlobals(true)] #[Group('Others')] final class SuperglobalsTest extends CIUnitTestCase { - public function testSetGet(): void + private Superglobals $superglobals; + + protected function setUp(): void + { + parent::setUp(); + + // Clear superglobals before each test + $_SERVER = $_GET = $_POST = $_COOKIE = $_FILES = $_REQUEST = []; + + $this->superglobals = new Superglobals(); + } + + // $_SERVER tests + public function testServerGetSet(): void + { + $this->superglobals->setServer('TEST_KEY', 'test_value'); + + $this->assertSame('test_value', $this->superglobals->server('TEST_KEY')); + $this->assertSame('test_value', $_SERVER['TEST_KEY']); + } + + public function testServerGetReturnsNullForNonExistent(): void + { + $this->assertNull($this->superglobals->server('NON_EXISTENT_KEY')); + } + + public function testServerSetWithArray(): void + { + $this->superglobals->setServer('argv', ['arg1', 'arg2']); + + $this->assertSame(['arg1', 'arg2'], $this->superglobals->server('argv')); + } + + public function testServerSetWithInt(): void + { + $this->superglobals->setServer('REQUEST_TIME', 1234567890); + + $this->assertSame(1234567890, $this->superglobals->server('REQUEST_TIME')); + } + + public function testServerSetWithFloat(): void + { + $this->superglobals->setServer('REQUEST_TIME_FLOAT', 1234567890.123); + + $this->assertEqualsWithDelta(1234567890.123, $this->superglobals->server('REQUEST_TIME_FLOAT'), PHP_FLOAT_EPSILON); + } + + public function testServerUnset(): void + { + $this->superglobals->setServer('TEST_KEY', 'value'); + $this->superglobals->unsetServer('TEST_KEY'); + + $this->assertNull($this->superglobals->server('TEST_KEY')); + $this->assertArrayNotHasKey('TEST_KEY', $_SERVER); + } + + public function testServerGetArray(): void + { + $this->superglobals->setServer('KEY1', 'value1'); + $this->superglobals->setServer('KEY2', 'value2'); + + $array = $this->superglobals->getServerArray(); + + $this->assertArrayHasKey('KEY1', $array); + $this->assertArrayHasKey('KEY2', $array); + $this->assertSame('value1', $array['KEY1']); + $this->assertSame('value2', $array['KEY2']); + } + + public function testServerSetArray(): void + { + $data = ['KEY1' => 'value1', 'KEY2' => 'value2']; + + $this->superglobals->setServerArray($data); + + $this->assertSame('value1', $this->superglobals->server('KEY1')); + $this->assertSame('value2', $this->superglobals->server('KEY2')); + $this->assertSame($data, $_SERVER); + } + + // $_GET tests + public function testGetGetSet(): void + { + $this->superglobals->setGet('test', 'value1'); + + $this->assertSame('value1', $this->superglobals->get('test')); + $this->assertSame('value1', $_GET['test']); + } + + public function testGetReturnsNullForNonExistent(): void + { + $this->assertNull($this->superglobals->get('non_existent')); + } + + public function testGetSetWithArray(): void + { + $this->superglobals->setGet('colors', ['red', 'blue']); + + $this->assertSame(['red', 'blue'], $this->superglobals->get('colors')); + } + + public function testGetUnset(): void + { + $this->superglobals->setGet('test', 'value'); + $this->superglobals->unsetGet('test'); + + $this->assertNull($this->superglobals->get('test')); + $this->assertArrayNotHasKey('test', $_GET); + } + + public function testGetGetArray(): void + { + $this->superglobals->setGet('key1', 'value1'); + $this->superglobals->setGet('key2', 'value2'); + + $array = $this->superglobals->getGetArray(); + + $this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $array); + } + + public function testGetSetArray(): void + { + $data = ['key1' => 'value1', 'key2' => 'value2']; + + $this->superglobals->setGetArray($data); + + $this->assertSame('value1', $this->superglobals->get('key1')); + $this->assertSame($data, $_GET); + } + + // $_POST tests + public function testPostGetSet(): void + { + $this->superglobals->setPost('test', 'value1'); + + $this->assertSame('value1', $this->superglobals->post('test')); + $this->assertSame('value1', $_POST['test']); + } + + public function testPostReturnsNullForNonExistent(): void + { + $this->assertNull($this->superglobals->post('non_existent')); + } + + public function testPostSetWithArray(): void + { + $this->superglobals->setPost('user', ['name' => 'John', 'age' => '30']); + + $this->assertSame(['name' => 'John', 'age' => '30'], $this->superglobals->post('user')); + } + + public function testPostUnset(): void + { + $this->superglobals->setPost('test', 'value'); + $this->superglobals->unsetPost('test'); + + $this->assertNull($this->superglobals->post('test')); + $this->assertArrayNotHasKey('test', $_POST); + } + + public function testPostGetArray(): void + { + $this->superglobals->setPost('key1', 'value1'); + $this->superglobals->setPost('key2', 'value2'); + + $array = $this->superglobals->getPostArray(); + + $this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $array); + } + + public function testPostSetArray(): void + { + $data = ['key1' => 'value1', 'key2' => 'value2']; + + $this->superglobals->setPostArray($data); + + $this->assertSame('value1', $this->superglobals->post('key1')); + $this->assertSame($data, $_POST); + } + + // $_COOKIE tests + public function testCookieGetSet(): void + { + $this->superglobals->setCookie('session', 'abc123'); + + $this->assertSame('abc123', $this->superglobals->cookie('session')); + $this->assertSame('abc123', $_COOKIE['session']); + } + + public function testCookieReturnsNullForNonExistent(): void + { + $this->assertNull($this->superglobals->cookie('non_existent')); + } + + public function testCookieSetWithArray(): void + { + $this->superglobals->setCookie('data', ['key' => 'value']); + + $this->assertSame(['key' => 'value'], $this->superglobals->cookie('data')); + } + + public function testCookieUnset(): void + { + $this->superglobals->setCookie('test', 'value'); + $this->superglobals->unsetCookie('test'); + + $this->assertNull($this->superglobals->cookie('test')); + $this->assertArrayNotHasKey('test', $_COOKIE); + } + + public function testCookieGetArray(): void + { + $this->superglobals->setCookie('key1', 'value1'); + $this->superglobals->setCookie('key2', 'value2'); + + $array = $this->superglobals->getCookieArray(); + + $this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $array); + } + + public function testCookieSetArray(): void + { + $data = ['key1' => 'value1', 'key2' => 'value2']; + + $this->superglobals->setCookieArray($data); + + $this->assertSame('value1', $this->superglobals->cookie('key1')); + $this->assertSame($data, $_COOKIE); + } + + // $_REQUEST tests + public function testRequestGetSet(): void + { + $this->superglobals->setRequest('test', 'value1'); + + $this->assertSame('value1', $this->superglobals->request('test')); + $this->assertSame('value1', $_REQUEST['test']); + } + + public function testRequestReturnsNullForNonExistent(): void + { + $this->assertNull($this->superglobals->request('non_existent')); + } + + public function testRequestSetWithArray(): void + { + $this->superglobals->setRequest('data', ['key' => 'value']); + + $this->assertSame(['key' => 'value'], $this->superglobals->request('data')); + } + + public function testRequestUnset(): void + { + $this->superglobals->setRequest('test', 'value'); + $this->superglobals->unsetRequest('test'); + + $this->assertNull($this->superglobals->request('test')); + $this->assertArrayNotHasKey('test', $_REQUEST); + } + + public function testRequestGetArray(): void + { + $this->superglobals->setRequest('key1', 'value1'); + $this->superglobals->setRequest('key2', 'value2'); + + $array = $this->superglobals->getRequestArray(); + + $this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $array); + } + + public function testRequestSetArray(): void + { + $data = ['key1' => 'value1', 'key2' => 'value2']; + + $this->superglobals->setRequestArray($data); + + $this->assertSame('value1', $this->superglobals->request('key1')); + $this->assertSame($data, $_REQUEST); + } + + // $_FILES tests + public function testFilesGetArray(): void + { + $filesData = [ + 'upload' => [ + 'name' => 'document.pdf', + 'type' => 'application/pdf', + 'tmp_name' => '/tmp/phpTest', + 'error' => UPLOAD_ERR_OK, + 'size' => 12345, + ], + ]; + + $this->superglobals->setFilesArray($filesData); + + $this->assertSame($filesData, $this->superglobals->getFilesArray()); + $this->assertSame($filesData, $_FILES); + } + + public function testFilesSetArrayWithMultipleFiles(): void + { + $filesData = [ + 'photos' => [ + 'name' => ['photo1.jpg', 'photo2.jpg'], + 'type' => ['image/jpeg', 'image/jpeg'], + 'tmp_name' => ['/tmp/phpA', '/tmp/phpB'], + 'error' => [UPLOAD_ERR_OK, UPLOAD_ERR_OK], + 'size' => [1234, 5678], + ], + ]; + + $this->superglobals->setFilesArray($filesData); + + $this->assertSame($filesData, $this->superglobals->getFilesArray()); + $this->assertSame($filesData, $_FILES); + } + + public function testFilesSetArrayEmpty(): void + { + $this->superglobals->setFilesArray([ + 'upload' => [ + 'name' => 'test.txt', + 'type' => 'text/plain', + 'tmp_name' => '/tmp/test', + 'error' => UPLOAD_ERR_OK, + 'size' => 100, + ], + ]); + + // Reset to empty + $this->superglobals->setFilesArray([]); + + $this->assertSame([], $this->superglobals->getFilesArray()); + $this->assertSame([], $_FILES); + } + + // Generic methods + public function testGetGlobalArray(): void + { + $this->superglobals->setGet('test', 'value'); + + $this->assertSame(['test' => 'value'], $this->superglobals->getGlobalArray('get')); + } + + public function testGetGlobalArrayForFiles(): void + { + $filesData = [ + 'upload' => [ + 'name' => 'test.pdf', + 'type' => 'application/pdf', + 'tmp_name' => '/tmp/phpTest', + 'error' => UPLOAD_ERR_OK, + 'size' => 999, + ], + ]; + + $this->superglobals->setFilesArray($filesData); + + $this->assertSame($filesData, $this->superglobals->getGlobalArray('files')); + } + + public function testGetGlobalArrayThrowsExceptionForInvalidName(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid superglobal name 'invalid'. Must be one of: server, get, post, cookie, files, request."); + + $this->superglobals->getGlobalArray('invalid'); + } + + public function testSetGlobalArray(): void + { + $data = ['key' => 'value']; + + $this->superglobals->setGlobalArray('post', $data); + + $this->assertSame('value', $this->superglobals->post('key')); + $this->assertSame($data, $_POST); + } + + public function testSetGlobalArrayForFiles(): void + { + $filesData = [ + 'doc' => [ + 'name' => 'file.txt', + 'type' => 'text/plain', + 'tmp_name' => '/tmp/test', + 'error' => UPLOAD_ERR_OK, + 'size' => 555, + ], + ]; + + $this->superglobals->setGlobalArray('files', $filesData); + + $this->assertSame($filesData, $this->superglobals->getFilesArray()); + $this->assertSame($filesData, $_FILES); + } + + public function testSetGlobalArrayThrowsExceptionForInvalidName(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid superglobal name 'invalid'. Must be one of: server, get, post, cookie, files, request."); + + $this->superglobals->setGlobalArray('invalid', ['key' => 'value']); + } + + // Constructor tests + public function testConstructorWithCustomArrays(): void { - $globals = new Superglobals([], []); + $server = ['SERVER_KEY' => 'server_value']; + $get = ['get_key' => 'get_value']; + $post = ['post_key' => 'post_value']; + $cookie = ['cookie_key' => 'cookie_value']; + $request = ['request_key' => 'request_value']; + $files = [ + 'upload' => [ + 'name' => 'custom.pdf', + 'type' => 'application/pdf', + 'tmp_name' => '/tmp/custom', + 'error' => UPLOAD_ERR_OK, + 'size' => 7777, + ], + ]; - $globals->setGet('test', 'value1'); + $superglobals = new Superglobals($server, $get, $post, $cookie, $files, $request); - $this->assertSame('value1', $globals->get('test')); + $this->assertSame('server_value', $superglobals->server('SERVER_KEY')); + $this->assertSame('get_value', $superglobals->get('get_key')); + $this->assertSame('post_value', $superglobals->post('post_key')); + $this->assertSame('cookie_value', $superglobals->cookie('cookie_key')); + $this->assertSame('request_value', $superglobals->request('request_key')); + $this->assertSame($files, $superglobals->getFilesArray()); } } diff --git a/tests/system/Test/FeatureTestTraitTest.php b/tests/system/Test/FeatureTestTraitTest.php index c0e2e2c873d1..870572174204 100644 --- a/tests/system/Test/FeatureTestTraitTest.php +++ b/tests/system/Test/FeatureTestTraitTest.php @@ -22,12 +22,14 @@ use Config\App; use Config\Feature; use Config\Routing; +use PHPUnit\Framework\Attributes\BackupGlobals; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** * @internal */ +#[BackupGlobals(true)] #[Group('DatabaseLive')] final class FeatureTestTraitTest extends CIUnitTestCase { diff --git a/tests/system/Validation/StrictRules/FileRulesTest.php b/tests/system/Validation/StrictRules/FileRulesTest.php index 5bb259b254ea..5f848c22db60 100644 --- a/tests/system/Validation/StrictRules/FileRulesTest.php +++ b/tests/system/Validation/StrictRules/FileRulesTest.php @@ -58,7 +58,7 @@ protected function setUp(): void $this->validation = new Validation((object) $this->config, service('renderer')); $this->validation->reset(); - $_FILES = [ + service('superglobals')->setFilesArray([ 'avatar' => [ 'tmp_name' => TESTPATH . '_support/Validation/uploads/phpUxc0ty', 'name' => 'my-avatar.png', @@ -146,7 +146,13 @@ protected function setUp(): void 400, ], ], - ]; + ]); + } + + protected function tearDown(): void + { + parent::tearDown(); + service('superglobals')->setFilesArray([]); } public function testUploadedTrue(): void diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index b015ab50f363..386266f44391 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -13,11 +13,13 @@ namespace CodeIgniter\Validation; +use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; +use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Validation\Exceptions\ValidationException; use Config\App; @@ -82,6 +84,9 @@ class ValidationTest extends CIUnitTestCase protected function setUp(): void { parent::setUp(); + + Services::injectMock('superglobals', new Superglobals()); + $this->validation = new Validation((object) static::$config, service('renderer')); $this->validation->reset(); } @@ -869,7 +874,7 @@ public function testRawInput(): void public function testJsonInput(): void { - $_SERVER['CONTENT_TYPE'] = 'application/json'; + service('superglobals')->setServer('CONTENT_TYPE', 'application/json'); $data = [ 'username' => 'admin001', @@ -893,7 +898,7 @@ public function testJsonInput(): void $this->assertSame([], $this->validation->getErrors()); $this->assertSame(['role' => 'administrator'], $this->validation->getValidated()); - unset($_SERVER['CONTENT_TYPE']); + service('superglobals')->unsetServer('CONTENT_TYPE'); } public function testJsonInputInvalid(): void @@ -949,7 +954,7 @@ public function testJsonInputObjectArray(): void } EOL; - $_SERVER['CONTENT_TYPE'] = 'application/json'; + service('superglobals')->setServer('CONTENT_TYPE', 'application/json'); $config = new App(); $config->baseURL = 'http://example.com/'; @@ -967,7 +972,7 @@ public function testJsonInputObjectArray(): void $this->assertFalse($result); $this->assertSame(['p' => 'Validation.array_count'], $this->validation->getErrors()); - unset($_SERVER['CONTENT_TYPE']); + service('superglobals')->unsetServer('CONTENT_TYPE'); } public function testHasRule(): void @@ -1214,7 +1219,7 @@ public function testRulesForSingleRuleWithAsteriskWillReturnNoError(): void $config = new App(); $config->baseURL = 'http://example.com/'; - $_REQUEST = [ + service('superglobals')->setRequestArray([ 'id_user' => [ 1, 3, @@ -1223,7 +1228,7 @@ public function testRulesForSingleRuleWithAsteriskWillReturnNoError(): void 'abc123', 'xyz098', ], - ]; + ]); $request = new IncomingRequest($config, new SiteURI($config), 'php://input', new UserAgent()); @@ -1241,7 +1246,7 @@ public function testRulesForSingleRuleWithAsteriskWillReturnError(): void $config = new App(); $config->baseURL = 'http://example.com/'; - $_REQUEST = [ + service('superglobals')->setRequestArray([ 'id_user' => [ '1dfd', 3, @@ -1257,7 +1262,7 @@ public function testRulesForSingleRuleWithAsteriskWillReturnError(): void ['name' => 'John'], ], ], - ]; + ]); $request = new IncomingRequest($config, new SiteURI($config), 'php://input', new UserAgent()); @@ -1291,9 +1296,9 @@ public function testRulesForSingleRuleWithSingleValue(): void $config = new App(); $config->baseURL = 'http://example.com/'; - $_REQUEST = [ + service('superglobals')->setRequestArray([ 'id_user' => 'gh', - ]; + ]); $request = new IncomingRequest($config, new SiteURI($config), 'php://input', new UserAgent()); diff --git a/utils/phpstan-baseline/argument.type.neon b/utils/phpstan-baseline/argument.type.neon index 91faa4168630..dcd1933310dc 100644 --- a/utils/phpstan-baseline/argument.type.neon +++ b/utils/phpstan-baseline/argument.type.neon @@ -1,4 +1,4 @@ -# total 84 errors +# total 85 errors parameters: ignoreErrors: @@ -122,6 +122,11 @@ parameters: count: 1 path: ../../tests/system/HTTP/HeaderTest.php + - + message: '#^Parameter \#1 \$array of method CodeIgniter\\Superglobals\:\:setServerArray\(\) expects array\\|float\|int\|string\>, array\ given\.$#' + count: 1 + path: ../../tests/system/HTTP/MessageTest.php + - message: '#^Parameter \#3 \$expire of method CodeIgniter\\HTTP\\Response\:\:setCookie\(\) expects int, string given\.$#' count: 1 diff --git a/utils/phpstan-baseline/codeigniter.getReassignArray.neon b/utils/phpstan-baseline/codeigniter.getReassignArray.neon index 6c45e13961e4..0acb0251a45f 100644 --- a/utils/phpstan-baseline/codeigniter.getReassignArray.neon +++ b/utils/phpstan-baseline/codeigniter.getReassignArray.neon @@ -1,15 +1,10 @@ -# total 18 errors +# total 9 errors parameters: ignoreErrors: - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' - count: 3 - path: ../../tests/system/CommonFunctionsTest.php - - - - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' - count: 2 + count: 1 path: ../../tests/system/Filters/InvalidCharsTest.php - @@ -22,11 +17,6 @@ parameters: count: 1 path: ../../tests/system/HTTP/IncomingRequestTest.php - - - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RedirectResponseTest.php - - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' count: 1 @@ -45,14 +35,14 @@ parameters: - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' count: 1 - path: ../../tests/system/Log/LoggerTest.php + path: ../../tests/system/Helpers/FormHelperTest.php - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' count: 1 - path: ../../tests/system/Models/PaginateModelTest.php + path: ../../tests/system/Pager/PagerTest.php - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' - count: 5 - path: ../../tests/system/Pager/PagerTest.php + count: 1 + path: ../../tests/system/SuperglobalsTest.php diff --git a/utils/phpstan-baseline/codeigniter.superglobalAccess.neon b/utils/phpstan-baseline/codeigniter.superglobalAccess.neon index ab837de286c5..d53e1e2dd090 100644 --- a/utils/phpstan-baseline/codeigniter.superglobalAccess.neon +++ b/utils/phpstan-baseline/codeigniter.superglobalAccess.neon @@ -1,4 +1,4 @@ -# total 59 errors +# total 27 errors parameters: ignoreErrors: @@ -17,16 +17,6 @@ parameters: count: 1 path: ../../system/Commands/Encryption/GenerateKey.php - - - message: '#^Accessing offset ''CI_ENVIRONMENT'' directly on \$_SERVER is discouraged\.$#' - count: 3 - path: ../../system/Commands/Utilities/Environment.php - - - - message: '#^Accessing offset ''HTTP_HOST'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/Commands/Utilities/Routes.php - - message: '#^Accessing offset ''REMOTE_ADDR'' directly on \$_SERVER is discouraged\.$#' count: 1 @@ -58,74 +48,14 @@ parameters: path: ../../system/Config/DotEnv.php - - message: '#^Accessing offset ''SERVER_PROTOCOL'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/Config/Services.php - - - - message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../system/HTTP/DownloadResponse.php - - - - message: '#^Accessing offset ''HTTPS'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../system/HTTP/IncomingRequest.php - - - - message: '#^Accessing offset array\|string directly on \$_GET is discouraged\.$#' - count: 2 - path: ../../system/HTTP/IncomingRequest.php - - - - message: '#^Accessing offset ''CONTENT_TYPE'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/HTTP/Message.php - - - - message: '#^Accessing offset \(int\|string\) directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/HTTP/Message.php - - - - message: '#^Accessing offset ''REQUEST_METHOD'' directly on \$_SERVER is discouraged\.$#' - count: 3 - path: ../../system/HTTP/Response.php - - - - message: '#^Accessing offset ''SERVER_PROTOCOL'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/HTTP/Response.php - - - - message: '#^Accessing offset ''SERVER_SOFTWARE'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../system/HTTP/Response.php - - - - message: '#^Accessing offset mixed directly on \$_GET is discouraged\.$#' - count: 1 - path: ../../system/Pager/Pager.php - - - - message: '#^Accessing offset ''REQUEST_METHOD'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/Router/Router.php - - - - message: '#^Accessing offset ''HTTP_X_REQUESTED_WITH'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../system/Session/Session.php - - - - message: '#^Accessing offset ''app\.baseURL'' directly on \$_SERVER is discouraged\.$#' + message: '#^Accessing offset string directly on \$_GET is discouraged\.$#' count: 1 - path: ../../tests/system/CLI/ConsoleTest.php + path: ../../system/Superglobals.php - - message: '#^Accessing offset ''encryption\.key'' directly on \$_SERVER is discouraged\.$#' + message: '#^Accessing offset string directly on \$_SERVER is discouraged\.$#' count: 1 - path: ../../tests/system/Commands/GenerateKeyTest.php + path: ../../system/Superglobals.php - message: '#^Accessing offset ''foo'' directly on \$_SERVER is discouraged\.$#' @@ -167,47 +97,17 @@ parameters: count: 1 path: ../../tests/system/Debug/ExceptionsTest.php - - - message: '#^Accessing offset ''encryption\.key'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Encryption/EncryptionTest.php - - - - message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/DownloadResponseTest.php - - - - message: '#^Accessing offset ''SERVER_SOFTWARE'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - message: '#^Accessing offset ''QUERY_STRING'' directly on \$_SERVER is discouraged\.$#' count: 1 path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - message: '#^Accessing offset ''HTTP_HOST'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Accessing offset ''REQUEST_URI'' directly on \$_SERVER is discouraged\.$#' + message: '#^Accessing offset ''TEST_KEY'' directly on \$_SERVER is discouraged\.$#' count: 1 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php + path: ../../tests/system/SuperglobalsTest.php - - message: '#^Accessing offset ''page'' directly on \$_GET is discouraged\.$#' + message: '#^Accessing offset ''test'' directly on \$_GET is discouraged\.$#' count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Accessing offset ''page_foo'' directly on \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Accessing offset ''CONTENT_TYPE'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Validation/ValidationTest.php + path: ../../tests/system/SuperglobalsTest.php diff --git a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon index c557d777faf5..ab0d015d5677 100644 --- a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon +++ b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon @@ -1,922 +1,22 @@ -# total 423 errors +# total 5 errors parameters: ignoreErrors: - - - message: '#^Assigning string directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../system/Commands/Utilities/Routes.php - - message: '#^Assigning string directly on offset string of \$_SERVER is discouraged\.$#' count: 1 path: ../../system/Config/DotEnv.php - - - message: '#^Assigning ''http\://example\.com/'' directly on offset ''app\.baseURL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CLI/ConsoleTest.php - - - - message: '#^Assigning ''/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/cannotFound'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/cli'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/example'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 6 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/image'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 20 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/pages/about'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 8 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''/test'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''CLI'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''HTTP/1\.1'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 8 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''HTTP/2\.0'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''HTTP/3\.0'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning non\-falsy\-string directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Assigning ''production'' directly on offset ''CI_ENVIRONMENT'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Commands/EnvironmentCommandTest.php - - - - message: '#^Assigning string directly on offset ''CI_ENVIRONMENT'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Commands/EnvironmentCommandTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/CommonFunctionsTest.php - - - - message: '#^Assigning ''bar'' directly on offset ''foo'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/CommonFunctionsTest.php - - - - message: '#^Assigning ''TT'' directly on offset ''SER_VAR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Config/DotEnvTest.php - - message: '#^Assigning ''1'' directly on offset ''CODEIGNITER_SCREAM_DEPRECATIONS'' of \$_SERVER is discouraged\.$#' count: 1 path: ../../tests/system/Debug/ExceptionsTest.php - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Filters/DebugToolbarTest.php - - - - message: '#^Assigning ''DELETE'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Filters/FiltersTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 38 - path: ../../tests/system/Filters/FiltersTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Filters/HoneypotTest.php - - - - message: '#^Assigning string directly on offset ''val'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/Filters/InvalidCharsTest.php - - - - message: '#^Assigning ''baz'' directly on offset ''bar'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/CLIRequestTest.php - - - - message: '#^Assigning ''10'' directly on offset ''HTTP_CONTENT_LENGTH'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/CURLRequestTest.php - - - - message: '#^Assigning ''en\-US'' directly on offset ''HTTP_ACCEPT_LANGUAGE'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/CURLRequestTest.php - - - - message: '#^Assigning ''gzip, deflate, br'' directly on offset ''HTTP_ACCEPT_ENCODING'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/CURLRequestTest.php - - - - message: '#^Assigning ''site1\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/CURLRequestTest.php - - - - message: '#^Assigning ''Mozilla/5\.0 \(Linux; U; Android 2\.0\.3; ja\-jp; SC\-02C Build/IML74K\) AppleWebKit/534\.30 \(KHTML, like Gecko\) Version/4\.0 Mobile Safari/534\.30'' directly on offset ''HTTP_USER_AGENT'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/DownloadResponseTest.php - - - - message: '#^Assigning ''10\.0\.1\.200'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''10\.10\.1\.200'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''123\.123\.123\.123'' directly on offset ''HTTP_X_FORWARDED_FOR'' of \$_SERVER is discouraged\.$#' - count: 7 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''123\.123\.123\.123'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''123\.456\.23\.123'' directly on offset ''HTTP_X_FORWARDED_FOR'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''192\.168\.5\.21'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''2001\:db8\:1234\:ffff\:ffff\:ffff\:ffff\:ffff'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''2001\:db8\:1235\:ffff\:ffff\:ffff\:ffff\:ffff'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''2001\:db8\:\:2\:1'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''2001\:db8\:\:2\:2'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''2001\:xyz\:\:1'' directly on offset ''HTTP_X_FORWARDED_FOR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''3'' directly on offset ''TEST'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''3'' directly on offset ''get'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''fr; q\=1\.0, en; q\=0\.5'' directly on offset ''HTTP_ACCEPT_LANGUAGE'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''fr\-FR; q\=1\.0, en; q\=0\.5'' directly on offset ''HTTP_ACCEPT_LANGUAGE'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''on'' directly on offset ''HTTPS'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning 3 directly on offset ''TEST'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning 5 directly on offset ''TEST'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RedirectResponseTest.php - - - - message: '#^Assigning ''http\://somewhere\.com'' directly on offset ''HTTP_REFERER'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RedirectResponseTest.php - - - - message: '#^Assigning ''10\.0\.1\.200'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''10\.10\.1\.200'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''123\.123\.123\.123'' directly on offset ''HTTP_X_FORWARDED_FOR'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''123\.123\.123\.123'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''123\.456\.23\.123'' directly on offset ''HTTP_X_FORWARDED_FOR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''192\.168\.5\.21'' directly on offset ''REMOTE_ADDR'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''baz'' directly on offset ''bar'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/RequestTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning ''HTTP/1\.1'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning ''Microsoft\-IIS'' directly on offset ''SERVER_SOFTWARE'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning string directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning string directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning string directly on offset ''SERVER_SOFTWARE'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/ResponseTest.php - - - - message: '#^Assigning '''' directly on offset ''/ci/woot'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/\?/ci/woot'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/candy/snickers'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci/index\.php/popcorn/woot'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci/woot'' directly on offset ''QUERY_STRING'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci/woot\?code\=good'' directly on offset ''QUERY_STRING'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci431/public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/ci431/public/index\.php/woot\?code\=good'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/fruits/banana'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 13 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php/fruits/banana'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php/woot'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php/woot\?code\=good'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php\?'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php\?/ci/woot'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php\?/ci/woot\?code\=good'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/sub/example'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/sub/folder/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/sub/folder/index\.php/fruits/banana'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/sub/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/woot'' directly on offset ''PATH_INFO'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/woot'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''good'' directly on offset ''/ci/woot\?code'' of \$_GET is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning string directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning string directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''/index\.php/woot\?code\=good'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''/woot'' directly on offset ''PATH_INFO'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''code\=good'' directly on offset ''QUERY_STRING'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''good'' directly on offset ''code'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''localhost\:8080'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning ''users\.example\.jp'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/SiteURIFactoryTest.php - - - - message: '#^Assigning '''' directly on offset ''QUERY_STRING'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''/ci/v4/controller/method'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''/ci/v4/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''/ci/v4/index\.php/controller/method'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''/controller/method'' directly on offset ''PATH_INFO'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/HTTP/URITest.php - - - - message: '#^Assigning ''/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/assets/image\.jpg'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/foo/public/bar\?baz\=quip'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/foo/public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/public/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/subfolder/assets/image\.jpg'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''/subfolder/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''8080'' directly on offset ''SERVER_PORT'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 11 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''invalid\.example\.org'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''www\.example\.jp'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning non\-falsy\-string directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/CurrentUrlTest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/MiscUrlTest.php - - - - message: '#^Assigning ''http\://example\.com/one\?two'' directly on offset ''HTTP_REFERER'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Helpers/URLHelper/MiscUrlTest.php - - - - message: '#^Assigning ''/ci/v4/x/y'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''/public'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''/public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''/test'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''/test/page'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - - - message: '#^Assigning ''www\.example\.jp'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Helpers/URLHelper/SiteUrlTest.php - - message: '#^Assigning ''test'' directly on offset ''HTTPS'' of \$_SERVER is discouraged\.$#' count: 1 path: ../../tests/system/HomeTest.php - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Honeypot/HoneypotTest.php - - - - message: '#^Assigning ''/ci/v4/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning ''/ci/v4/x/y'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning ''bar'' directly on offset ''foo'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning 2 directly on offset ''page'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning 2 directly on offset ''page_foo'' of \$_GET is discouraged\.$#' - count: 5 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning 3 directly on offset ''page'' of \$_GET is discouraged\.$#' - count: 2 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning string directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Pager/PagerTest.php - - - - message: '#^Assigning ''/work'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''/work/1'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''/work/1/edit'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''/work/123'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''/work/new'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''DELETE'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''HTTP/1\.1'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''PATCH'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''PUT'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php - - - - message: '#^Assigning ''/work'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/create'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/delete/123'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/edit/1'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/new'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/remove/123'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/show/1'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''/work/update/123'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 5 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''HTTP/1\.1'' directly on offset ''SERVER_PROTOCOL'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/RESTful/ResourcePresenterTest.php - - - - message: '#^Assigning ''adm\.example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 4 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''dev\.example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''doc\.domain\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 5 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''doc\.example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''example\.co\.uk'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 5 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''www\.example\.com'' directly on offset ''HTTP_HOST'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Router/RouteCollectionTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 10 - path: ../../tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php - - - - message: '#^Assigning ''PUT'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 8 - path: ../../tests/system/Security/SecurityCSRFSessionTest.php - - - - message: '#^Assigning ''PUT'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 3 - path: ../../tests/system/Security/SecurityCSRFSessionTest.php - - - - message: '#^Assigning ''GET'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Security/SecurityTest.php - - - - message: '#^Assigning ''POST'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 9 - path: ../../tests/system/Security/SecurityTest.php - - - - message: '#^Assigning ''PUT'' directly on offset ''REQUEST_METHOD'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Security/SecurityTest.php - - - - message: '#^Assigning ''xmlhttprequest'' directly on offset ''HTTP_X_REQUESTED_WITH'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Session/SessionTest.php - - message: '#^Assigning ''test'' directly on offset ''HTTPS'' of \$_SERVER is discouraged\.$#' count: 1 @@ -926,8 +26,3 @@ parameters: message: '#^Assigning ''test'' directly on offset ''HTTPS'' of \$_SERVER is discouraged\.$#' count: 1 path: ../../tests/system/Test/FeatureTestTraitTest.php - - - - message: '#^Assigning ''application/json'' directly on offset ''CONTENT_TYPE'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/Validation/ValidationTest.php diff --git a/utils/phpstan-baseline/empty.notAllowed.neon b/utils/phpstan-baseline/empty.notAllowed.neon index 65bd911eca30..d0906d6afeb8 100644 --- a/utils/phpstan-baseline/empty.notAllowed.neon +++ b/utils/phpstan-baseline/empty.notAllowed.neon @@ -1,4 +1,4 @@ -# total 228 errors +# total 227 errors parameters: ignoreErrors: @@ -224,7 +224,7 @@ parameters: - message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' - count: 2 + count: 1 path: ../../system/HTTP/IncomingRequest.php - diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 3eb1f3bd917f..70506914c94a 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 2659 errors +# total 2198 errors includes: - argument.type.neon diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index 36fa344cd39e..76e3e88b7e7c 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 1317 errors +# total 1315 errors parameters: ignoreErrors: @@ -322,11 +322,31 @@ parameters: count: 1 path: ../../system/Config/Services.php + - + message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$cookie with no value type specified in iterable type array\.$#' + count: 1 + path: ../../system/Config/Services.php + + - + message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$files with no value type specified in iterable type array\.$#' + count: 1 + path: ../../system/Config/Services.php + - message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$get with no value type specified in iterable type array\.$#' count: 1 path: ../../system/Config/Services.php + - + message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$post with no value type specified in iterable type array\.$#' + count: 1 + path: ../../system/Config/Services.php + + - + message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$request with no value type specified in iterable type array\.$#' + count: 1 + path: ../../system/Config/Services.php + - message: '#^Method CodeIgniter\\Config\\Services\:\:superglobals\(\) has parameter \$server with no value type specified in iterable type array\.$#' count: 1 @@ -4217,36 +4237,6 @@ parameters: count: 1 path: ../../system/Session/Handlers/BaseHandler.php - - - message: '#^Method CodeIgniter\\Superglobals\:\:__construct\(\) has parameter \$get with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - - - message: '#^Method CodeIgniter\\Superglobals\:\:__construct\(\) has parameter \$server with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - - - message: '#^Method CodeIgniter\\Superglobals\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - - - message: '#^Method CodeIgniter\\Superglobals\:\:setGetArray\(\) has parameter \$array with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - - - message: '#^Property CodeIgniter\\Superglobals\:\:\$get type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - - - message: '#^Property CodeIgniter\\Superglobals\:\:\$server type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Superglobals.php - - message: '#^Method CodeIgniter\\Test\\Constraints\\SeeInDatabase\:\:__construct\(\) has parameter \$data with no value type specified in iterable type array\.$#' count: 1 @@ -4973,52 +4963,52 @@ parameters: path: ../../system/View/Parser.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:313\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:315\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:336\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:338\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:362\:\:includeComments\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:364\:\:includeComments\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:362\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:364\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:394\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:396\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:417\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:419\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:445\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:447\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:497\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:499\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:556\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:558\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php - - message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:579\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' + message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:581\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#' count: 1 path: ../../tests/system/API/TransformerTest.php