CodeIgniter4/tests/system/CodeIgniterTest.php

961 lines
31 KiB
PHP
Raw Normal View History

2021-06-07 00:15:28 +08:00
<?php
declare(strict_types=1);
2021-07-19 21:32:33 +08:00
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
2021-06-07 00:15:28 +08:00
namespace CodeIgniter;
use App\Controllers\Home;
2021-07-03 00:56:44 +08:00
use CodeIgniter\Config\Services;
use CodeIgniter\Debug\Timer;
use CodeIgniter\Exceptions\PageNotFoundException;
2023-11-19 17:56:33 +09:00
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\RouteCollection;
2021-02-28 06:57:05 +07:00
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use Config\App;
use Config\Cache;
2023-01-31 17:28:47 +09:00
use Config\Filters as FiltersConfig;
2021-02-28 06:57:05 +07:00
use Config\Modules;
use Config\Routing;
2024-05-06 09:19:46 +09:00
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
2024-07-26 20:51:10 +08:00
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
2021-09-24 17:31:19 +09:00
use Tests\Support\Filters\Customfilter;
use Tests\Support\Filters\RedirectFilter;
/**
* @internal
*/
2024-04-23 01:29:05 +08:00
#[BackupGlobals(true)]
#[Group('Others')]
2024-05-08 00:05:51 +08:00
#[RunTestsInSeparateProcesses]
final class CodeIgniterTest extends CIUnitTestCase
{
private CodeIgniter $codeigniter;
2021-06-04 22:51:52 +08:00
2024-07-26 20:51:10 +08:00
#[WithoutErrorHandler]
2021-06-04 22:51:52 +08:00
protected function setUp(): void
{
parent::setUp();
$this->resetServices();
2021-06-04 22:51:52 +08:00
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
2021-06-25 23:35:25 +08:00
$this->codeigniter = new MockCodeIgniter(new App());
2024-11-01 17:00:17 +03:00
$response = service('response');
$response->pretend();
2021-06-04 22:51:52 +08:00
}
protected function tearDown(): void
2021-06-04 22:51:52 +08:00
{
parent::tearDown();
$this->resetServices();
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRunEmptyDefaultRoute(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 1;
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
2024-04-10 11:28:07 +09:00
public function testOutputBufferingControl(): void
2023-05-15 14:58:30 +08:00
{
ob_start();
$this->codeigniter->run();
ob_get_clean();
// 1 phpunit output buffering level
$this->assertSame(1, ob_get_level());
}
2023-07-29 22:59:04 +08:00
public function testRunEmptyDefaultRouteReturnResponse(): void
2022-10-22 19:28:24 +09:00
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$response = $this->codeigniter->run(null, true);
2022-10-22 19:28:24 +09:00
$this->assertStringContainsString('Welcome to CodeIgniter', $response->getBody());
}
2023-07-29 22:59:04 +08:00
public function testRunClosureRoute(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-07-29 22:59:04 +08:00
$routes->add('pages/(:segment)', static function ($segment): void {
2021-06-04 22:51:52 +08:00
echo 'You want to see "' . esc($segment) . '" page.';
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('You want to see "about" page.', $output);
}
/**
* @psalm-suppress UndefinedClass
*/
2023-07-29 22:59:04 +08:00
public function testRun404Override(): void
2021-06-04 22:51:52 +08:00
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2021-06-04 22:51:52 +08:00
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Errors::show404');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run($routes);
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString("Can't find a route for 'GET: pages/about'.", $output);
$this->assertSame(404, response()->getStatusCode());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRun404OverrideControllerReturnsResponse(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Controllers\Popcorn::pop');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
$response = $this->codeigniter->run($routes, true);
$this->assertStringContainsString('Oops', $response->getBody());
$this->assertSame(567, $response->getStatusCode());
}
2023-07-29 22:59:04 +08:00
public function testRun404OverrideReturnResponse(): void
2022-10-22 19:28:24 +09:00
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2022-10-22 19:28:24 +09:00
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Controllers\Popcorn::pop');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2022-10-22 19:28:24 +09:00
Services::injectMock('router', $router);
$response = $this->codeigniter->run($routes, true);
2022-10-22 19:28:24 +09:00
$this->assertStringContainsString('Oops', $response->getBody());
}
2023-07-29 22:59:04 +08:00
public function testRun404OverrideByClosure(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = new RouteCollection(service('locator'), new Modules(), new Routing());
2021-06-04 22:51:52 +08:00
$routes->setAutoRoute(false);
2023-07-29 22:59:04 +08:00
$routes->set404Override(static function (): void {
2021-06-04 22:51:52 +08:00
echo '404 Override by Closure.';
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run($routes);
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('404 Override by Closure.', $output);
$this->assertSame(404, response()->getStatusCode());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testControllersCanReturnString(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-01-31 17:43:32 +09:00
$routes->add(
'pages/(:segment)',
static fn ($segment): string => 'You want to see "' . esc($segment) . '" page.',
2023-01-31 17:43:32 +09:00
);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('You want to see "about" page.', $output);
}
2023-07-29 22:59:04 +08:00
public function testControllersCanReturnResponseObject(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->add('pages/(:segment)', static function ($segment) {
2024-11-01 17:00:17 +03:00
$response = service('response');
2022-09-02 09:32:01 +08:00
$string = "You want to see 'about' page.";
2021-06-04 22:51:52 +08:00
return $response->setBody($string);
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString("You want to see 'about' page.", $output);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6358
*/
2023-07-29 22:59:04 +08:00
public function testControllersCanReturnDownloadResponseObject(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->add('pages/(:segment)', static function ($segment) {
2024-11-01 17:00:17 +03:00
$response = service('response');
return $response->download('some.txt', 'some text', true);
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertSame('some text', $output);
}
public function testRunExecuteFilterByClassName(): void
2021-09-24 17:31:19 +09:00
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-09-24 17:31:19 +09:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-01-31 17:43:32 +09:00
$routes->add(
'pages/about',
2024-11-01 17:00:17 +03:00
static fn () => service('incomingrequest')->getBody(),
['filter' => Customfilter::class],
2023-01-31 17:43:32 +09:00
);
2021-09-24 17:31:19 +09:00
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-09-24 17:31:19 +09:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-09-24 17:31:19 +09:00
$output = ob_get_clean();
$this->assertStringContainsString('http://hellowworld.com', $output);
2022-08-13 09:12:32 +09:00
$this->resetServices();
2021-09-24 17:31:19 +09:00
}
2024-04-10 11:28:07 +09:00
public function testRegisterSameFilterTwiceWithDifferentArgument(): void
2023-01-31 17:28:47 +09:00
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2023-01-31 17:28:47 +09:00
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-01-31 17:28:47 +09:00
$routes->add(
'pages/about',
2024-11-01 17:00:17 +03:00
static fn () => service('incomingrequest')->getBody(),
2023-01-31 17:28:47 +09:00
// Set filter with no argument.
['filter' => 'test-customfilter'],
2023-01-31 17:28:47 +09:00
);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2023-01-31 17:28:47 +09:00
Services::injectMock('router', $router);
/** @var FiltersConfig $filterConfig */
$filterConfig = config('Filters');
$filterConfig->filters = [
// Set filter with argument.
'test-customfilter:arg1' => [
'before' => ['pages/*'],
],
];
2024-11-01 17:00:17 +03:00
service('filters', $filterConfig);
2023-01-31 17:28:47 +09:00
ob_start();
2023-01-31 17:28:47 +09:00
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('http://hellowworld.comhttp://hellowworld.com', $output);
2023-01-31 17:28:47 +09:00
$this->resetServices();
}
2023-07-29 22:59:04 +08:00
public function testDisableControllerFilters(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->add(
'pages/about',
2024-11-01 17:00:17 +03:00
static fn () => service('incomingrequest')->getBody(),
['filter' => Customfilter::class],
);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->disableFilters();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('', $output);
$this->resetServices();
}
2023-07-29 22:59:04 +08:00
public function testResponseConfigEmpty(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
2024-11-01 17:00:17 +03:00
$response = service('response', null, false);
2021-06-04 22:51:52 +08:00
$this->assertInstanceOf(Response::class, $response);
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRoutesIsEmpty(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
// Inject mock router.
2024-11-01 17:00:17 +03:00
$router = service('router', null, service('incomingrequest'), false);
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
2023-07-29 22:59:04 +08:00
public function testTransfersCorrectHTTPVersion(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/2.0';
ob_start();
$this->codeigniter->run();
ob_get_clean();
2021-06-04 22:51:52 +08:00
$response = $this->getPrivateProperty($this->codeigniter, 'response');
2021-06-25 23:35:25 +08:00
$this->assertSame('2.0', $response->getProtocolVersion());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testSupportsHttp3(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/3.0';
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('3.0', $response->getProtocolVersion());
}
2023-07-29 22:59:04 +08:00
public function testIgnoringErrorSuppressedByAt(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
ob_start();
@unlink('inexistent-file');
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
2023-07-29 22:59:04 +08:00
public function testRunForceSecure(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
$filterConfig = config(FiltersConfig::class);
$filterConfig->required['before'][] = 'forcehttps';
2021-06-25 23:35:25 +08:00
$config = config(App::class);
2021-06-04 22:51:52 +08:00
$config->forceGlobalSecureRequests = true;
2021-06-25 23:35:25 +08:00
$codeigniter = new MockCodeIgniter($config);
$codeigniter->setContext('web');
2021-06-04 22:51:52 +08:00
$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
$response = $this->getPrivateProperty($codeigniter, 'response');
$this->assertNull($response->header('Location'));
$response = $codeigniter->run(null, true);
2021-06-04 22:51:52 +08:00
2023-02-23 16:16:40 +09:00
$this->assertSame('https://example.com/index.php/', $response->header('Location')->getValue());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRunRedirectionWithNamed(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-07-29 22:59:04 +08:00
$routes->add('pages/named', static function (): void {
2021-06-04 22:51:52 +08:00
}, ['as' => 'name']);
$routes->addRedirect('example', 'name');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
2021-06-25 23:35:25 +08:00
$this->assertSame('http://example.com/pages/named', $response->header('Location')->getValue());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRunRedirectionWithURI(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2023-07-29 22:59:04 +08:00
$routes->add('pages/uri', static function (): void {
2021-06-04 22:51:52 +08:00
});
$routes->addRedirect('example', 'pages/uri');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
2021-06-25 23:35:25 +08:00
$this->assertSame('http://example.com/pages/uri', $response->header('Location')->getValue());
2021-06-04 22:51:52 +08:00
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3041
*/
2023-07-29 22:59:04 +08:00
public function testRunRedirectionWithGET(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
2021-06-04 22:51:52 +08:00
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
// addRedirect() sets status code 302 by default.
2021-06-04 22:51:52 +08:00
$routes->addRedirect('example', 'pages/notset');
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
ob_get_clean();
2021-06-04 22:51:52 +08:00
$response = $this->getPrivateProperty($this->codeigniter, 'response');
2021-06-25 23:35:25 +08:00
$this->assertSame('http://example.com/pages/notset', $response->header('Location')->getValue());
$this->assertSame(302, $response->getStatusCode());
}
2023-07-29 22:59:04 +08:00
public function testRunRedirectionWithGETAndHTTPCode301(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->addRedirect('example', 'pages/notset', 301);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame(301, $response->getStatusCode());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testRunRedirectionWithPOSTAndHTTPCode301(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2021-06-04 22:51:52 +08:00
$routes->addRedirect('example', 'pages/notset', 301);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
ob_get_clean();
2021-06-25 23:35:25 +08:00
2021-06-04 22:51:52 +08:00
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame(301, $response->getStatusCode());
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testStoresPreviousURL(): void
2021-09-15 01:47:19 +00:00
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
2024-11-01 17:00:17 +03:00
$router = service('router', null, service('incomingrequest'), false);
2021-09-15 01:47:19 +00:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-09-15 01:47:19 +00:00
ob_get_clean();
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
2023-02-23 16:16:40 +09:00
$this->assertSame('http://example.com/index.php/', $_SESSION['_ci_previous_url']);
2021-09-15 01:47:19 +00:00
}
2023-07-29 22:59:04 +08:00
public function testNotStoresPreviousURL(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
2021-06-04 22:51:52 +08:00
$_SERVER['REQUEST_URI'] = '/example';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2021-06-04 22:51:52 +08:00
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
2021-06-04 22:51:52 +08:00
$routes->addRedirect('example', 'pages/notset', 301);
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
2021-06-04 22:51:52 +08:00
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
ob_get_clean();
2021-09-15 01:47:19 +00:00
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
2021-06-04 22:51:52 +08:00
}
2023-07-29 22:59:04 +08:00
public function testNotStoresPreviousURLByCheckingContentType(): void
{
$_SERVER['argv'] = ['index.php', 'image'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/image';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->add('image', static function () {
2024-11-01 17:00:17 +03:00
$response = service('response');
return $response->setContentType('image/jpeg', '');
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
}
2021-06-04 22:51:52 +08:00
/**
* The method after all test, reset Servces:: config
* Can't use static::tearDownAfterClass. This will cause a buffer exception
* need improve
*/
2023-07-29 22:59:04 +08:00
public function testRunDefaultRoute(): void
2021-06-04 22:51:52 +08:00
{
2021-06-25 23:35:25 +08:00
$_SERVER['argv'] = ['index.php', '/'];
2021-06-04 22:51:52 +08:00
$_SERVER['argc'] = 2;
ob_start();
$this->codeigniter->run();
2021-06-04 22:51:52 +08:00
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
2023-07-29 22:59:04 +08:00
public function testRunCLIRoute(): void
{
$_SERVER['argv'] = ['index.php', 'cli'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/cli';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = 'public/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'CLI';
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->cli('cli', '\Tests\Support\Controllers\Popcorn::index');
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Method Not Allowed', $output);
}
2023-07-29 22:59:04 +08:00
public function testSpoofRequestMethodCanUsePUT(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
2023-11-19 17:56:33 +09:00
$_POST['_method'] = Method::PUT;
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->setDefaultNamespace('App\Controllers');
$routes->resetRoutes();
$routes->post('/', 'Home::index');
$routes->put('/', 'Home::index');
ob_start();
$this->codeigniter->run();
ob_get_clean();
2024-11-01 17:00:17 +03:00
$this->assertSame(Method::PUT, service('incomingrequest')->getMethod());
}
2023-07-29 22:59:04 +08:00
public function testSpoofRequestMethodCannotUseGET(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['_method'] = 'GET';
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->setDefaultNamespace('App\Controllers');
$routes->resetRoutes();
$routes->post('/', 'Home::index');
$routes->get('/', 'Home::index');
ob_start();
$this->codeigniter->run();
ob_get_clean();
2024-11-01 17:00:17 +03:00
$this->assertSame('POST', service('incomingrequest')->getMethod());
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6281
*/
2023-07-29 22:59:04 +08:00
public function testPageCacheSendSecureHeaders(): void
{
// Suppress command() output
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();
CITestStreamFilter::addOutputFilter();
// Clear Page cache
command('cache:clear');
$_SERVER['REQUEST_URI'] = '/test';
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->add('test', static function () {
CodeIgniter::cache(3600);
2024-11-01 17:00:17 +03:00
$response = service('response');
2022-09-02 09:32:01 +08:00
$string = 'This is a test page. Elapsed time: {elapsed_time}';
return $response->setBody($string);
});
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);
2023-01-31 17:28:47 +09:00
/** @var FiltersConfig $filterConfig */
$filterConfig = config('Filters');
$filterConfig->globals['after'] = ['secureheaders'];
2024-11-01 17:00:17 +03:00
service('filters', $filterConfig);
// The first response to be cached.
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('This is a test page', $output);
2024-11-01 17:00:17 +03:00
$response = service('response');
$headers = $response->headers();
$this->assertArrayHasKey('X-Frame-Options', $headers);
// The second response from the Page cache.
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('This is a test page', $output);
2024-11-01 17:00:17 +03:00
$response = service('response');
$headers = $response->headers();
$this->assertArrayHasKey('X-Frame-Options', $headers);
// Clear Page cache
command('cache:clear');
// Remove stream filters
CITestStreamFilter::removeErrorFilter();
CITestStreamFilter::removeOutputFilter();
}
/**
* @param array|bool $cacheQueryStringValue
2022-09-04 19:44:31 +08:00
*
* @see https://github.com/codeigniter4/CodeIgniter4/pull/6410
*/
2024-04-23 01:29:05 +08:00
#[DataProvider('providePageCacheWithCacheQueryString')]
2023-01-31 17:43:32 +09:00
public function testPageCacheWithCacheQueryString(
$cacheQueryStringValue,
int $expectedPagesInCache,
array $testingUrls,
): void {
// Suppress command() output
2023-02-21 16:40:08 +09:00
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
CITestStreamFilter::addErrorFilter();
// Create cache config with cacheQueryString value from the dataProvider
$cacheConfig = config(Cache::class);
$cacheConfig->cacheQueryString = $cacheQueryStringValue;
// Clear cache before starting the test
command('cache:clear');
// Calculate amount of items in the cache before the test
$cache = \Config\Services::cache();
$cacheStartCounter = count($cache->getCacheInfo());
// Generate request to each URL from the testing array
foreach ($testingUrls as $testingUrl) {
$this->resetServices();
$_SERVER['REQUEST_URI'] = '/' . $testingUrl;
2023-07-03 12:42:27 +09:00
$_SERVER['SCRIPT_NAME'] = '/index.php';
$this->codeigniter = new MockCodeIgniter(new App());
2024-11-01 17:00:17 +03:00
$routes = service('routes', true);
$routePath = explode('?', $testingUrl)[0];
$string = 'This is a test page, to check cache configuration';
$routes->add($routePath, static function () use ($string) {
2024-11-01 17:00:17 +03:00
service('responsecache')->setTtl(60);
$response = service('response');
return $response->setBody($string);
});
// Inject router
2024-11-01 17:00:17 +03:00
$router = service('router', $routes, service('incomingrequest', null, false));
Services::injectMock('router', $router);
2023-01-31 17:43:32 +09:00
// Cache the page output using default caching function and $cacheConfig
// with value from the data provider
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertSame($string, $output);
}
// Calculate how much cached items exist in the cache after the test requests
$cacheEndCounter = count($cache->getCacheInfo());
$newPagesCached = $cacheEndCounter - $cacheStartCounter;
// Clear cache after the test
command('cache:clear');
// Check that amount of new items created in the cache matching expected value from the data provider
$this->assertSame($expectedPagesInCache, $newPagesCached);
// Remove stream filters
2023-02-21 16:40:08 +09:00
CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}
2023-08-03 18:13:11 +08:00
public static function providePageCacheWithCacheQueryString(): iterable
{
$testingUrls = [
2023-01-31 17:43:32 +09:00
// URL #1
'test',
// URL #2
'test?important_parameter=1',
// URL #3
'test?important_parameter=2',
// URL #4
'test?important_parameter=1&not_important_parameter=2',
// URL #5
'test?important_parameter=1&not_important_parameter=2&another_not_important_parameter=3',
];
return [
2023-01-31 17:43:32 +09:00
// We expect only 1 page in the cache, because when cacheQueryString
// is set to false, all GET parameter should be ignored, and page URI
// will be absolutely same "/test" string for all 5 requests
'$cacheQueryString=false' => [false, 1, $testingUrls],
// We expect all 5 pages in the cache, because when cacheQueryString
// is set to true, all GET parameter should be processed as unique requests
'$cacheQueryString=true' => [true, 5, $testingUrls],
// We expect only 3 pages in the cache, because when cacheQueryString
// is set to array with important parameters, we should ignore all
// parameters thats not in the array. Only URL #1, URL #2 and URL #3
// should be cached. URL #4 and URL #5 is duplication of URL #2
// (with value ?important_parameter=1), so they should not be processed
// as new unique requests and application should return already cached
// page for URL #2
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
];
}
/**
* See https://github.com/codeigniter4/CodeIgniter4/issues/7205
*/
public function testRunControllerNotFoundBeforeFilter(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/cannotFound';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
2024-11-01 17:00:17 +03:00
$routes = service('routes');
$routes->setAutoRoute(true);
// Inject the before filter.
$filterConfig = config('Filters');
$filterConfig->aliases['redirectFilter'] = RedirectFilter::class;
$filterConfig->globals['before'] = ['redirectFilter'];
2024-11-01 17:00:17 +03:00
service('filters', $filterConfig);
$this->expectException(PageNotFoundException::class);
$this->codeigniter->run($routes);
}
public function testStartControllerPermitsInvoke(): void
{
$this->setPrivateProperty($this->codeigniter, 'benchmark', new Timer());
$this->setPrivateProperty($this->codeigniter, 'controller', '\\' . Home::class);
$startController = $this->getPrivateMethodInvoker($this->codeigniter, 'startController');
$this->setPrivateProperty($this->codeigniter, 'method', '__invoke');
$startController();
// No PageNotFoundException
$this->assertTrue(true);
}
}