Skip to content

Commit b0975ec

Browse files
committedMay 8, 2018
Initial work on new feature testing features.
1 parent 5e4718a commit b0975ec

20 files changed

+1601
-486
lines changed
 

‎system/CodeIgniter.php

+44-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @filesource
3737
*/
3838
use CodeIgniter\HTTP\RedirectResponse;
39+
use CodeIgniter\HTTP\Request;
3940
use Config\Services;
4041
use Config\Cache;
4142
use CodeIgniter\HTTP\URI;
@@ -180,8 +181,12 @@ public function initialize()
180181
* makes all of the pieces work together.
181182
*
182183
* @param \CodeIgniter\Router\RouteCollectionInterface $routes
184+
* @param bool $returnResponse
185+
*
186+
* @throws \CodeIgniter\HTTP\RedirectException
187+
* @throws \Exception
183188
*/
184-
public function run(RouteCollectionInterface $routes = null)
189+
public function run(RouteCollectionInterface $routes = null, bool $returnResponse = false)
185190
{
186191
$this->startBenchmark();
187192

@@ -201,7 +206,7 @@ public function run(RouteCollectionInterface $routes = null)
201206

202207
try
203208
{
204-
$this->handleRequest($routes, $cacheConfig);
209+
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
205210
} catch (Router\RedirectException $e)
206211
{
207212
$logger = Services::logger();
@@ -225,8 +230,11 @@ public function run(RouteCollectionInterface $routes = null)
225230
*
226231
* @param \CodeIgniter\Router\RouteCollectionInterface $routes
227232
* @param $cacheConfig
233+
* @param bool $returnResponse
234+
*
235+
* @throws \CodeIgniter\Filters\Exceptions\FilterException
228236
*/
229-
protected function handleRequest(RouteCollectionInterface $routes = null, $cacheConfig)
237+
protected function handleRequest(RouteCollectionInterface $routes = null, $cacheConfig, bool $returnResponse = false)
230238
{
231239
$this->tryToRouteIt($routes);
232240

@@ -257,6 +265,11 @@ protected function handleRequest(RouteCollectionInterface $routes = null, $cache
257265
// Handle any redirects
258266
if ($returned instanceof RedirectResponse)
259267
{
268+
if ($returnResponse)
269+
{
270+
return $returned;
271+
}
272+
260273
$this->callExit(EXIT_SUCCESS);
261274
}
262275

@@ -279,12 +292,17 @@ protected function handleRequest(RouteCollectionInterface $routes = null, $cache
279292

280293
unset($uri);
281294

282-
$this->sendResponse();
295+
if (! $returnResponse)
296+
{
297+
$this->sendResponse();
298+
}
283299

284300
//--------------------------------------------------------------------
285301
// Is there a post-system event?
286302
//--------------------------------------------------------------------
287303
Events::trigger('post_system');
304+
305+
return $this->response;
288306
}
289307

290308
//--------------------------------------------------------------------
@@ -358,13 +376,35 @@ protected function startBenchmark()
358376

359377
//--------------------------------------------------------------------
360378

379+
/**
380+
* Sets a Request object to be used for this request.
381+
* Used when running certain tests.
382+
*
383+
* @param \CodeIgniter\HTTP\Request $request
384+
*
385+
* @return \CodeIgniter\CodeIgniter
386+
*/
387+
public function setRequest(Request $request)
388+
{
389+
$this->request = $request;
390+
391+
return $this;
392+
}
393+
394+
//--------------------------------------------------------------------
395+
361396
/**
362397
* Get our Request object, (either IncomingRequest or CLIRequest)
363398
* and set the server protocol based on the information provided
364399
* by the server.
365400
*/
366401
protected function getRequestObject()
367402
{
403+
if ($this->request instanceof Request)
404+
{
405+
return;
406+
}
407+
368408
if (is_cli() && ! (ENVIRONMENT == 'testing'))
369409
{
370410
$this->request = Services::clirequest($this->config);

‎system/Config/Services.php

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838
use CodeIgniter\Database\ConnectionInterface;
3939
use CodeIgniter\Database\MigrationRunner;
40+
use CodeIgniter\HTTP\URI;
4041
use CodeIgniter\View\RendererInterface;
4142

4243
/**

‎system/HTTP/IncomingRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ public function detectPath($protocol)
616616
break;
617617
case 'PATH_INFO':
618618
default:
619-
$path = $_SERVER[$protocol] ?? $this->parseRequestURI();
619+
$path = $this->fetchGlobal('server', $protocol) ?? $this->parseRequestURI();
620620
break;
621621
}
622622

‎system/HTTP/Request.php

+77-28
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class Request extends Message implements RequestInterface
6464
*/
6565
protected $method;
6666

67+
/**
68+
* Stores values we've retrieved from
69+
* PHP globals.
70+
* @var array
71+
*/
72+
protected $globals = [];
73+
6774
//--------------------------------------------------------------------
6875

6976
/**
@@ -302,6 +309,23 @@ public function getEnv($index = null, $filter = null, $flags = null)
302309

303310
//--------------------------------------------------------------------
304311

312+
/**
313+
* Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
314+
*
315+
* @param string $method
316+
* @param $value
317+
*
318+
* @return $this
319+
*/
320+
public function setGlobal(string $method, $value)
321+
{
322+
$this->globals[$method] = $value;
323+
324+
return $this;
325+
}
326+
327+
//--------------------------------------------------------------------
328+
305329
/**
306330
* Fetches one or more items from a global, like cookies, get, post, etc.
307331
* Can optionally filter the input when you retrieve it by passing in
@@ -312,45 +336,37 @@ public function getEnv($index = null, $filter = null, $flags = null)
312336
*
313337
* http://php.net/manual/en/filter.filters.sanitize.php
314338
*
315-
* @param int $type Input filter constant
339+
* @param int $method Input filter constant
316340
* @param string|array $index
317341
* @param int $filter Filter constant
318342
* @param null $flags
319343
*
320344
* @return mixed
321345
*/
322-
protected function fetchGlobal($type, $index = null, $filter = null, $flags = null )
346+
public function fetchGlobal($method, $index = null, $filter = null, $flags = null )
323347
{
324-
// Null filters cause null values to return.
325-
if (is_null($filter))
348+
$method = strtolower($method);
349+
350+
if (! isset($this->globals[$method]))
326351
{
327-
$filter = FILTER_DEFAULT;
352+
$this->populateGlobals($method);
328353
}
329354

330-
$loopThrough = [];
331-
switch ($type)
355+
// Null filters cause null values to return.
356+
if (is_null($filter))
332357
{
333-
case INPUT_GET : $loopThrough = $_GET;
334-
break;
335-
case INPUT_POST : $loopThrough = $_POST;
336-
break;
337-
case INPUT_COOKIE : $loopThrough = $_COOKIE;
338-
break;
339-
case INPUT_SERVER : $loopThrough = $_SERVER;
340-
break;
341-
case INPUT_ENV : $loopThrough = $_ENV;
342-
break;
343-
case INPUT_REQUEST : $loopThrough = $_REQUEST;
344-
break;
358+
$filter = FILTER_DEFAULT;
345359
}
346360

347-
// If $index is null, it means that the whole input type array is requested
361+
// Return all values when $index is null
348362
if (is_null($index))
349363
{
350364
$values = [];
351-
foreach ($loopThrough as $key => $value)
365+
foreach ($this->globals[$method] as $key => $value)
352366
{
353-
$values[$key] = is_array($value) ? $this->fetchGlobal($type, $key, $filter, $flags) : filter_var($value, $filter, $flags);
367+
$values[$key] = is_array($value)
368+
? $this->fetchGlobal($method, $key, $filter, $flags)
369+
: filter_var($value, $filter, $flags);
354370
}
355371

356372
return $values;
@@ -363,7 +379,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
363379

364380
foreach ($index as $key)
365381
{
366-
$output[$key] = $this->fetchGlobal($type, $key, $filter, $flags);
382+
$output[$key] = $this->fetchGlobal($method, $key, $filter, $flags);
367383
}
368384

369385
return $output;
@@ -372,7 +388,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
372388
// Does the index contain array notation?
373389
if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1)
374390
{
375-
$value = $loopThrough;
391+
$value = $this->globals[$method];
376392
for ($i = 0; $i < $count; $i++)
377393
{
378394
$key = trim($matches[0][$i], '[]');
@@ -393,14 +409,12 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
393409
}
394410
}
395411

396-
// Due to issues with FastCGI and testing,
397-
// we need to do these all manually instead
398-
// of the simpler filter_input();
399412
if (empty($value))
400413
{
401-
$value = $loopThrough[$index] ?? null;
414+
$value = $this->globals[$method][$index] ?? null;
402415
}
403416

417+
// Cannot filter these types of data automatically...
404418
if (is_array($value) || is_object($value) || is_null($value))
405419
{
406420
return $value;
@@ -410,4 +424,39 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
410424
}
411425

412426
//--------------------------------------------------------------------
427+
428+
/**
429+
* Saves a copy of the current state of one of several PHP globals
430+
* so we can retrieve them later.
431+
*
432+
* @param string $method
433+
*/
434+
protected function populateGlobals(string $method)
435+
{
436+
if (! isset($this->globals[$method]))
437+
{
438+
$this->globals[$method] = [];
439+
}
440+
441+
// Don't populate ENV as it might contain
442+
// sensitive data that we don't want to get logged.
443+
switch($method)
444+
{
445+
case 'get':
446+
$this->globals['get'] = $_GET;
447+
break;
448+
case 'post':
449+
$this->globals['post'] = $_POST;
450+
break;
451+
case 'request':
452+
$this->globals['request'] = $_REQUEST;
453+
break;
454+
case 'cookie':
455+
$this->globals['cookie'] = $_COOKIE;
456+
break;
457+
case 'server':
458+
$this->globals['server'] = $_SERVER;
459+
break;
460+
}
461+
}
413462
}

0 commit comments

Comments
 (0)
Please sign in to comment.