2017-01-06 20:06:06 -06:00
|
|
|
<?php
|
2016-07-12 10:11:17 -07:00
|
|
|
|
2022-08-29 18:02:14 +09:00
|
|
|
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-02-28 06:47:59 +07:00
|
|
|
namespace CodeIgniter;
|
|
|
|
|
|
|
|
use CodeIgniter\Config\BaseService;
|
2022-12-23 15:01:10 +09:00
|
|
|
use CodeIgniter\Config\Factories;
|
2022-09-08 11:12:02 +09:00
|
|
|
use CodeIgniter\HTTP\CLIRequest;
|
2023-06-24 06:54:14 +08:00
|
|
|
use CodeIgniter\HTTP\Exceptions\RedirectException;
|
2022-06-28 07:10:48 +09:00
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
2018-06-07 20:50:03 -07:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2020-04-23 01:27:54 +07:00
|
|
|
use CodeIgniter\HTTP\Response;
|
2023-11-12 10:47:36 +09:00
|
|
|
use CodeIgniter\HTTP\SiteURI;
|
2018-06-07 20:50:03 -07:00
|
|
|
use CodeIgniter\HTTP\UserAgent;
|
2020-04-23 01:27:54 +07:00
|
|
|
use CodeIgniter\Router\RouteCollection;
|
|
|
|
use CodeIgniter\Session\Handlers\FileHandler;
|
2021-02-28 08:28:54 +07:00
|
|
|
use CodeIgniter\Session\Session;
|
2020-11-05 21:10:03 +08:00
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
2020-02-19 10:39:45 -05:00
|
|
|
use CodeIgniter\Test\Mock\MockIncomingRequest;
|
2021-10-17 00:11:01 +09:00
|
|
|
use CodeIgniter\Test\Mock\MockSecurity;
|
2020-02-19 10:39:45 -05:00
|
|
|
use CodeIgniter\Test\Mock\MockSession;
|
2020-04-23 01:27:54 +07:00
|
|
|
use CodeIgniter\Test\TestLogger;
|
|
|
|
use Config\App;
|
2022-12-23 15:01:10 +09:00
|
|
|
use Config\Cookie;
|
2023-08-29 15:31:25 +09:00
|
|
|
use Config\DocTypes;
|
2020-04-23 01:27:54 +07:00
|
|
|
use Config\Logger;
|
2021-02-28 06:57:05 +07:00
|
|
|
use Config\Modules;
|
2023-04-07 09:33:56 +09:00
|
|
|
use Config\Routing;
|
2023-06-27 15:53:17 +09:00
|
|
|
use Config\Security as SecurityConfig;
|
2022-05-31 09:02:12 +09:00
|
|
|
use Config\Services;
|
2023-02-14 20:04:09 +09:00
|
|
|
use Config\Session as SessionConfig;
|
2023-06-24 06:54:14 +08:00
|
|
|
use Exception;
|
2021-12-27 11:37:44 +09:00
|
|
|
use Kint;
|
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\PreserveGlobalState;
|
|
|
|
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
|
2022-06-01 09:31:23 +08:00
|
|
|
use RuntimeException;
|
2021-02-28 08:28:54 +07:00
|
|
|
use stdClass;
|
2020-04-17 17:27:16 +07:00
|
|
|
use Tests\Support\Models\JobModel;
|
2018-06-07 02:56:46 -07:00
|
|
|
|
2017-02-09 19:33:03 +09:00
|
|
|
/**
|
2021-06-25 22:27:37 +08:00
|
|
|
* @internal
|
2017-02-09 19:33:03 +09:00
|
|
|
*/
|
2024-04-23 01:29:05 +08:00
|
|
|
#[BackupGlobals(true)]
|
|
|
|
#[Group('SeparateProcess')]
|
2021-06-25 22:27:37 +08:00
|
|
|
final class CommonFunctionsTest extends CIUnitTestCase
|
2016-07-12 10:11:17 -07:00
|
|
|
{
|
2022-06-28 07:10:48 +09:00
|
|
|
private ?App $config = null;
|
|
|
|
private IncomingRequest $request;
|
2022-06-27 18:24:58 +09:00
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
unset($_ENV['foo'], $_SERVER['foo']);
|
2022-06-29 11:43:41 +09:00
|
|
|
$this->resetServices();
|
2021-12-02 16:06:58 +09:00
|
|
|
|
|
|
|
parent::setUp();
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testStringifyAttributes(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame(' class="foo" id="bar"', stringify_attributes(['class' => 'foo', 'id' => 'bar']));
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-18 00:57:03 +08:00
|
|
|
$atts = new stdClass();
|
2021-06-04 22:51:52 +08:00
|
|
|
$atts->class = 'foo';
|
|
|
|
$atts->id = 'bar';
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame(' class="foo" id="bar"', stringify_attributes($atts));
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-18 00:57:03 +08:00
|
|
|
$atts = new stdClass();
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('', stringify_attributes($atts));
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame(' class="foo" id="bar"', stringify_attributes('class="foo" id="bar"'));
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('', stringify_attributes([]));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testStringifyJsAttributes(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('width=800,height=600', stringify_attributes(['width' => '800', 'height' => '600'], true));
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-18 00:57:03 +08:00
|
|
|
$atts = new stdClass();
|
2021-06-04 22:51:52 +08:00
|
|
|
$atts->width = 800;
|
|
|
|
$atts->height = 600;
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('width=800,height=600', stringify_attributes($atts, true));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEnvReturnsDefault(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('baz', env('foo', 'baz'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEnvGetsFromSERVER(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$_SERVER['foo'] = 'bar';
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('bar', env('foo', 'baz'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEnvGetsFromENV(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$_ENV['foo'] = 'bar';
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('bar', env('foo', 'baz'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEnvBooleans(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$_ENV['p1'] = 'true';
|
|
|
|
$_ENV['p2'] = 'false';
|
|
|
|
$_ENV['p3'] = 'empty';
|
|
|
|
$_ENV['p4'] = 'null';
|
|
|
|
|
|
|
|
$this->assertTrue(env('p1'));
|
|
|
|
$this->assertFalse(env('p2'));
|
|
|
|
$this->assertEmpty(env('p3'));
|
|
|
|
$this->assertNull(env('p4'));
|
|
|
|
}
|
|
|
|
|
2023-04-07 09:51:42 +09:00
|
|
|
private function createRouteCollection(): RouteCollection
|
|
|
|
{
|
|
|
|
return new RouteCollection(Services::locator(), new Modules(), new Routing());
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRedirectReturnsRedirectResponse(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$response = $this->createMock(Response::class);
|
2022-05-31 09:02:12 +09:00
|
|
|
Services::injectMock('response', $response);
|
2023-04-07 09:51:42 +09:00
|
|
|
|
|
|
|
$routes = $this->createRouteCollection();
|
2022-05-31 09:02:12 +09:00
|
|
|
Services::injectMock('routes', $routes);
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
$routes->add('home/base', 'Controller::index', ['as' => 'base']);
|
2021-06-24 23:29:53 +08:00
|
|
|
$response->method('redirect')->willReturnArgument(0);
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, redirect('base'));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRedirectDefault(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, redirect());
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRequestIncomingRequest(): void
|
2022-09-08 11:12:02 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App());
|
|
|
|
|
|
|
|
$request = request();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(IncomingRequest::class, $request);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRequestCLIRequest(): void
|
2022-09-08 11:12:02 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
|
|
|
|
$request = request();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(CLIRequest::class, $request);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testResponse(): void
|
2022-09-08 11:12:02 +09:00
|
|
|
{
|
|
|
|
$response = response();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Response::class, $response);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSolidusElement(): void
|
2022-10-30 20:42:46 +07:00
|
|
|
{
|
|
|
|
$this->assertSame('', _solidus());
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSolidusElementXHTML(): void
|
2022-10-30 20:42:46 +07:00
|
|
|
{
|
2023-08-29 15:31:25 +09:00
|
|
|
$this->disableHtml5();
|
2022-10-30 20:42:46 +07:00
|
|
|
|
|
|
|
$this->assertSame(' /', _solidus());
|
|
|
|
|
2023-08-29 15:31:25 +09:00
|
|
|
$this->enableHtml5();
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:28:07 +09:00
|
|
|
private function disableHtml5(): void
|
2023-08-29 15:31:25 +09:00
|
|
|
{
|
|
|
|
$doctypes = new DocTypes();
|
|
|
|
$doctypes->html5 = false;
|
|
|
|
_solidus($doctypes);
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:28:07 +09:00
|
|
|
private function enableHtml5(): void
|
2023-08-29 15:31:25 +09:00
|
|
|
{
|
|
|
|
$doctypes = new DocTypes();
|
|
|
|
_solidus($doctypes);
|
2022-10-30 20:42:46 +07:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testView(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-08 11:17:07 +08:00
|
|
|
$data = [
|
2021-06-04 22:51:52 +08:00
|
|
|
'testString' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
];
|
|
|
|
$expected = '<h1>bar</h1>';
|
|
|
|
$this->assertStringContainsString($expected, view('\Tests\Support\View\Views\simple', $data));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testViewSavedData(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-08 11:17:07 +08:00
|
|
|
$data = [
|
2021-06-04 22:51:52 +08:00
|
|
|
'testString' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
];
|
|
|
|
$expected = '<h1>bar</h1>';
|
|
|
|
$this->assertStringContainsString($expected, view('\Tests\Support\View\Views\simple', $data, ['saveData' => true]));
|
|
|
|
$this->assertStringContainsString($expected, view('\Tests\Support\View\Views\simple'));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testViewCell(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$expected = 'Hello';
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame($expected, view_cell('\Tests\Support\View\SampleClass::hello'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEscapeWithDifferentEncodings(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('<x', esc('<x', 'html', 'utf-8'));
|
|
|
|
$this->assertSame('<x', esc('<x', 'html', 'iso-8859-1'));
|
|
|
|
$this->assertSame('<x', esc('<x', 'html', 'windows-1251'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEscapeBadContext(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2022-03-21 01:39:09 +07:00
|
|
|
$this->expectException('InvalidArgumentException');
|
2021-06-04 22:51:52 +08:00
|
|
|
esc(['width' => '800', 'height' => '600'], 'bogus');
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testEscapeBadContextZero(): void
|
2022-10-20 10:50:51 +09:00
|
|
|
{
|
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
esc('<script>', '0');
|
|
|
|
}
|
|
|
|
|
2024-03-18 11:26:02 +02:00
|
|
|
public function testEscapeArray(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'a' => [
|
|
|
|
'b' => 'c&',
|
|
|
|
],
|
|
|
|
'd' => 'e>',
|
|
|
|
];
|
|
|
|
$expected = $data;
|
|
|
|
$expected['a']['b'] = 'c&';
|
|
|
|
$expected['d'] = 'e>';
|
|
|
|
$this->assertSame($expected, esc($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEscapeRecursiveArrayRaw(): void
|
|
|
|
{
|
|
|
|
$data = ['a' => 'b', 'c' => 'd'];
|
|
|
|
$data['e'] = &$data;
|
|
|
|
$this->assertSame($data, esc($data, 'raw'));
|
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSessionInstance(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->injectSessionMock();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Session::class, session());
|
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSessionVariable(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->injectSessionMock();
|
|
|
|
|
|
|
|
$_SESSION['notbogus'] = 'Hi there';
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('Hi there', session('notbogus'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSessionVariableNotThere(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->injectSessionMock();
|
|
|
|
|
|
|
|
$_SESSION['bogus'] = 'Hi there';
|
2021-06-25 22:34:50 +08:00
|
|
|
$this->assertNull(session('notbogus'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRouteTo(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
// prime the pump
|
|
|
|
$routes = service('routes');
|
2023-01-11 21:58:32 +09:00
|
|
|
// @TODO Do not put any placeholder after (:any).
|
|
|
|
// Because the number of parameters passed to the controller method may change.
|
2021-06-04 22:51:52 +08:00
|
|
|
$routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2');
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('/path/string/to/13', route_to('myController::goto', 'string', 13));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRouteToInCliWithoutLocaleInRoute(): void
|
2022-08-29 11:09:10 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
$routes = service('routes');
|
2023-01-11 21:58:32 +09:00
|
|
|
// @TODO Do not put any placeholder after (:any).
|
|
|
|
// Because the number of parameters passed to the controller method may change.
|
2022-08-29 11:09:10 +09:00
|
|
|
$routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2');
|
|
|
|
|
|
|
|
$this->assertSame('/path/string/to/13', route_to('myController::goto', 'string', 13));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRouteToInCliWithLocaleInRoute(): void
|
2022-08-29 11:09:10 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
$routes = service('routes');
|
2023-01-11 21:58:32 +09:00
|
|
|
// @TODO Do not put any placeholder after (:any).
|
|
|
|
// Because the number of parameters passed to the controller method may change.
|
2022-08-29 11:09:10 +09:00
|
|
|
$routes->add('{locale}/path/(:any)/to/(:num)', 'myController::goto/$1/$2', ['as' => 'path-to']);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'/en/path/string/to/13',
|
|
|
|
route_to('path-to', 'string', 13, 'en')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRouteToWithUnsupportedLocale(): void
|
2022-08-29 11:09:10 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App(), false);
|
|
|
|
$routes = service('routes');
|
2023-01-11 21:58:32 +09:00
|
|
|
// @TODO Do not put any placeholder after (:any).
|
|
|
|
// Because the number of parameters passed to the controller method may change.
|
2022-08-29 11:09:10 +09:00
|
|
|
$routes->add('{locale}/path/(:any)/to/(:num)', 'myController::goto/$1/$2', ['as' => 'path-to']);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'/en/path/string/to/13',
|
|
|
|
route_to('path-to', 'string', 13, 'invalid')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testInvisible(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('Javascript', remove_invisible_characters("Java\0script"));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testInvisibleEncoded(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('Javascript', remove_invisible_characters('Java%0cscript'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testAppTimezone(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2022-11-07 09:05:51 +09:00
|
|
|
$this->assertSame('UTC', app_timezone());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCSRFToken(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2023-06-27 15:53:17 +09:00
|
|
|
Services::injectMock('security', new MockSecurity(new SecurityConfig()));
|
2021-10-17 00:11:01 +09:00
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('csrf_test_name', csrf_token());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCSRFHeader(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('X-CSRF-TOKEN', csrf_header());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testHash(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame(32, strlen(csrf_hash()));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCSRFField(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertStringContainsString('<input type="hidden" ', csrf_field());
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCSRFMeta(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertStringContainsString('<meta name="X-CSRF-TOKEN" ', csrf_meta());
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testModelNotExists(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertNull(model(UnexsistenceClass::class));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testModelExistsBasename(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertInstanceOf(JobModel::class, model('JobModel'));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testModelExistsClassname(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertInstanceOf(JobModel::class, model(JobModel::class));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testModelExistsAbsoluteClassname(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2022-04-29 00:07:19 +07:00
|
|
|
$this->assertInstanceOf(JobModel::class, model(JobModel::class));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testOldInput(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
2023-04-07 09:51:42 +09:00
|
|
|
$this->routes = $this->createRouteCollection();
|
2021-06-04 22:51:52 +08:00
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
2023-11-12 10:47:36 +09:00
|
|
|
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
|
2021-06-04 22:51:52 +08:00
|
|
|
Services::injectMock('request', $this->request);
|
|
|
|
|
|
|
|
// setup & ask for a redirect...
|
|
|
|
$_SESSION = [];
|
|
|
|
$_GET = ['foo' => 'bar'];
|
|
|
|
$_POST = [
|
|
|
|
'bar' => 'baz',
|
2021-11-11 17:10:10 +09:00
|
|
|
'zibble' => 'fritz',
|
2021-06-04 22:51:52 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$response = new RedirectResponse(new App());
|
2021-06-10 23:58:59 +08:00
|
|
|
$response->withInput();
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('bar', old('foo')); // regular parameter
|
|
|
|
$this->assertSame('doo', old('yabba dabba', 'doo')); // non-existing parameter
|
2021-11-12 14:08:04 +09:00
|
|
|
$this->assertSame('fritz', old('zibble'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testOldInputSerializeData(): void
|
2021-11-12 14:09:37 +09:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-11-12 14:09:37 +09:00
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
2023-04-07 09:51:42 +09:00
|
|
|
$this->routes = $this->createRouteCollection();
|
2021-11-12 14:09:37 +09:00
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
2023-11-12 10:47:36 +09:00
|
|
|
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
|
2021-11-12 14:09:37 +09:00
|
|
|
Services::injectMock('request', $this->request);
|
|
|
|
|
|
|
|
// setup & ask for a redirect...
|
|
|
|
$_SESSION = [];
|
|
|
|
$_GET = [];
|
|
|
|
$_POST = [
|
|
|
|
'zibble' => serialize('fritz'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = new RedirectResponse(new App());
|
|
|
|
$response->withInput();
|
|
|
|
|
|
|
|
// serialized parameters are only HTML-escaped.
|
|
|
|
$this->assertSame('s:5:"fritz";', old('zibble'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 23:58:59 +08:00
|
|
|
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1492
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testOldInputArray(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
2023-04-07 09:51:42 +09:00
|
|
|
$this->routes = $this->createRouteCollection();
|
2021-06-04 22:51:52 +08:00
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
2023-11-12 10:47:36 +09:00
|
|
|
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
|
2021-06-04 22:51:52 +08:00
|
|
|
Services::injectMock('request', $this->request);
|
|
|
|
|
|
|
|
$locations = [
|
|
|
|
'AB' => 'Alberta',
|
|
|
|
'BC' => 'British Columbia',
|
|
|
|
'SK' => 'Saskatchewan',
|
|
|
|
];
|
|
|
|
|
|
|
|
// setup & ask for a redirect...
|
|
|
|
$_SESSION = [];
|
|
|
|
$_GET = [];
|
|
|
|
$_POST = ['location' => $locations];
|
|
|
|
|
|
|
|
$response = new RedirectResponse(new App());
|
2021-06-24 15:43:23 +07:00
|
|
|
$response->withInput();
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame($locations, old('location'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testReallyWritable(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
// cannot test fully on *nix
|
|
|
|
$this->assertTrue(is_really_writable(WRITEPATH));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSlashItem(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2022-06-01 09:31:23 +08:00
|
|
|
$this->assertSame('en/', slash_item('defaultLocale')); // en
|
|
|
|
$this->assertSame('', slash_item('negotiateLocale')); // false
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSlashItemOnInexistentItem(): void
|
2022-06-01 09:31:23 +08:00
|
|
|
{
|
|
|
|
$this->assertNull(slash_item('foo'));
|
|
|
|
$this->assertNull(slash_item('bar'));
|
|
|
|
$this->assertNull(slash_item('cookieDomains'));
|
|
|
|
$this->assertNull(slash_item('indices'));
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testSlashItemThrowsErrorOnNonStringableItem(): void
|
2022-06-01 09:31:23 +08:00
|
|
|
{
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
|
|
$this->expectExceptionMessage('Cannot convert "Config\\App::$supportedLocales" of type "array" to type "string".');
|
|
|
|
|
|
|
|
slash_item('supportedLocales');
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
protected function injectSessionMock(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2023-02-14 20:04:09 +09:00
|
|
|
$sessionConfig = new SessionConfig();
|
2022-12-23 15:01:10 +09:00
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$defaults = [
|
2023-02-14 20:04:09 +09:00
|
|
|
'driver' => FileHandler::class,
|
|
|
|
'cookieName' => 'ci_session',
|
|
|
|
'expiration' => 7200,
|
|
|
|
'savePath' => '',
|
|
|
|
'matchIP' => false,
|
|
|
|
'timeToUpdate' => 300,
|
|
|
|
'regenerateDestroy' => false,
|
2021-06-04 22:51:52 +08:00
|
|
|
];
|
|
|
|
|
2021-06-07 19:06:26 +08:00
|
|
|
foreach ($defaults as $key => $config) {
|
2023-02-14 20:04:09 +09:00
|
|
|
$sessionConfig->{$key} = $config;
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:01:10 +09:00
|
|
|
$cookie = new Cookie();
|
|
|
|
|
|
|
|
foreach ([
|
|
|
|
'prefix' => '',
|
|
|
|
'domain' => '',
|
|
|
|
'path' => '/',
|
|
|
|
'secure' => false,
|
|
|
|
'samesite' => 'Lax',
|
|
|
|
] as $key => $value) {
|
|
|
|
$cookie->{$key} = $value;
|
|
|
|
}
|
|
|
|
Factories::injectMock('config', 'Cookie', $cookie);
|
|
|
|
|
2023-02-14 20:04:09 +09:00
|
|
|
$session = new MockSession(new FileHandler($sessionConfig, '127.0.0.1'), $sessionConfig);
|
2021-06-04 22:51:52 +08:00
|
|
|
$session->setLogger(new TestLogger(new Logger()));
|
|
|
|
BaseService::injectMock('session', $session);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure cookies are set by RedirectResponse this way
|
|
|
|
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testRedirectResponseCookies1(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$loginTime = time();
|
|
|
|
|
|
|
|
$routes = service('routes');
|
|
|
|
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);
|
|
|
|
|
|
|
|
$answer1 = redirect()->route('login')
|
2021-06-17 00:23:39 +08:00
|
|
|
->setCookie('foo', 'onething', YEAR)
|
2023-10-25 12:04:59 +09:00
|
|
|
->setCookie('login_time', (string) $loginTime, YEAR);
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
$this->assertTrue($answer1->hasCookie('foo', 'onething'));
|
|
|
|
$this->assertTrue($answer1->hasCookie('login_time'));
|
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testTrace(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
ob_start();
|
|
|
|
trace();
|
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertStringContainsString('Debug Backtrace', $content);
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testViewNotSaveData(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'testString' => 'bar',
|
|
|
|
'bar' => 'baz',
|
|
|
|
];
|
|
|
|
$this->assertStringContainsString('<h1>bar</h1>', view('\Tests\Support\View\Views\simples', $data, ['saveData' => false]));
|
|
|
|
$this->assertStringContainsString('<h1>is_not</h1>', view('\Tests\Support\View\Views\simples'));
|
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testForceHttpsNullRequestAndResponse(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->assertNull(Services::response()->header('Location'));
|
2023-11-11 21:37:59 +09:00
|
|
|
|
2023-06-24 06:54:14 +08:00
|
|
|
Services::response()->setCookie('force', 'cookie');
|
|
|
|
Services::response()->setHeader('Force', 'header');
|
|
|
|
Services::response()->setBody('default body');
|
|
|
|
|
|
|
|
try {
|
|
|
|
force_https();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertInstanceOf(RedirectException::class, $e);
|
2023-02-26 17:03:50 +09:00
|
|
|
$this->assertSame(
|
|
|
|
'https://example.com/index.php/',
|
|
|
|
$e->getResponse()->header('Location')->getValue()
|
|
|
|
);
|
2023-06-24 06:54:14 +08:00
|
|
|
$this->assertFalse($e->getResponse()->hasCookie('force'));
|
|
|
|
$this->assertSame('header', $e->getResponse()->getHeaderLine('Force'));
|
|
|
|
$this->assertSame('', $e->getResponse()->getBody());
|
|
|
|
$this->assertSame(307, $e->getResponse()->getStatusCode());
|
|
|
|
}
|
2021-06-04 22:51:52 +08:00
|
|
|
|
2023-06-24 06:54:14 +08:00
|
|
|
$this->expectException(RedirectException::class);
|
2021-06-04 22:51:52 +08:00
|
|
|
force_https();
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:37:59 +09:00
|
|
|
public function testForceHttpsWithBaseUrlSubFolder(): void
|
|
|
|
{
|
|
|
|
$config = config(App::class);
|
|
|
|
$config->baseURL = 'https://example.jp/codeIgniter/';
|
|
|
|
$uri = new SiteURI($config, 'en/home?foo=bar');
|
|
|
|
$request = new IncomingRequest($config, $uri, '', new UserAgent());
|
|
|
|
Services::injectMock('request', $request);
|
|
|
|
|
|
|
|
try {
|
|
|
|
force_https();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertInstanceOf(RedirectException::class, $e);
|
|
|
|
$this->assertSame(
|
|
|
|
'https://example.jp/codeIgniter/index.php/en/home?foo=bar',
|
|
|
|
$e->getResponse()->header('Location')->getValue()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
/**
|
2021-07-24 19:30:51 +08:00
|
|
|
* @param mixed $input
|
|
|
|
* @param mixed $expected
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
2024-04-23 01:29:05 +08:00
|
|
|
#[DataProvider('provideCleanPathActuallyCleaningThePaths')]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCleanPathActuallyCleaningThePaths($input, $expected): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame($expected, clean_path($input));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 18:13:11 +08:00
|
|
|
public static function provideCleanPathActuallyCleaningThePaths(): iterable
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$ds = DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
ROOTPATH . 'spark',
|
|
|
|
'ROOTPATH' . $ds . 'spark',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
APPPATH . 'Config' . $ds . 'App.php',
|
|
|
|
'APPPATH' . $ds . 'Config' . $ds . 'App.php',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
SYSTEMPATH . 'CodeIgniter.php',
|
|
|
|
'SYSTEMPATH' . $ds . 'CodeIgniter.php',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
VENDORPATH . 'autoload.php',
|
|
|
|
'VENDORPATH' . $ds . 'autoload.php',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
FCPATH . 'index.php',
|
|
|
|
'FCPATH' . $ds . 'index.php',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testIsCli(): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
$this->assertIsBool(is_cli());
|
|
|
|
$this->assertTrue(is_cli());
|
|
|
|
}
|
2021-12-27 11:37:44 +09:00
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testDWithCSP(): void
|
2021-12-27 11:37:44 +09:00
|
|
|
{
|
2021-12-31 14:54:38 +09:00
|
|
|
$this->resetServices();
|
|
|
|
|
2021-12-27 11:37:44 +09:00
|
|
|
/** @var App $config */
|
2022-02-05 13:55:03 +09:00
|
|
|
$config = config('App');
|
|
|
|
$config->CSPEnabled = true;
|
2021-12-27 11:37:44 +09:00
|
|
|
|
2022-02-05 13:55:03 +09:00
|
|
|
// Initialize Kint
|
2024-02-18 18:54:13 +09:00
|
|
|
Services::autoloader()->initializeKint(CI_DEBUG);
|
2022-02-05 13:55:03 +09:00
|
|
|
|
|
|
|
$cliDetection = Kint::$cli_detection;
|
2021-12-27 11:37:44 +09:00
|
|
|
Kint::$cli_detection = false;
|
|
|
|
|
2022-02-05 10:01:59 +09:00
|
|
|
$this->expectOutputRegex('/<script class="kint-rich-script" nonce="[0-9a-z]{24}">/u');
|
2021-12-27 11:37:44 +09:00
|
|
|
d('string');
|
|
|
|
|
|
|
|
// Restore settings
|
|
|
|
Kint::$cli_detection = $cliDetection;
|
|
|
|
}
|
|
|
|
|
2024-04-23 01:29:05 +08:00
|
|
|
#[PreserveGlobalState(false)]
|
2024-05-08 00:05:51 +08:00
|
|
|
#[RunInSeparateProcess]
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testTraceWithCSP(): void
|
2021-12-27 11:37:44 +09:00
|
|
|
{
|
2024-03-05 17:31:31 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
restore_error_handler();
|
|
|
|
|
2021-12-31 14:54:38 +09:00
|
|
|
$this->resetServices();
|
|
|
|
|
2021-12-27 11:37:44 +09:00
|
|
|
/** @var App $config */
|
2022-02-05 13:55:03 +09:00
|
|
|
$config = config('App');
|
|
|
|
$config->CSPEnabled = true;
|
|
|
|
|
|
|
|
// Initialize Kint
|
2024-02-18 18:54:13 +09:00
|
|
|
Services::autoloader()->initializeKint(CI_DEBUG);
|
2022-02-05 13:55:03 +09:00
|
|
|
|
2021-12-27 11:37:44 +09:00
|
|
|
Kint::$cli_detection = false;
|
|
|
|
|
2024-03-05 19:10:26 +09:00
|
|
|
// Workaround for errors on PHPUnit 10 and PHP 8.3.
|
|
|
|
// See https://github.com/sebastianbergmann/phpunit/issues/5403#issuecomment-1906810619
|
|
|
|
// `$app->initialize()` sets error handler.
|
|
|
|
restore_error_handler();
|
|
|
|
|
2022-02-05 10:01:59 +09:00
|
|
|
$this->expectOutputRegex('/<style class="kint-rich-style" nonce="[0-9a-z]{24}">/u');
|
2021-12-27 11:37:44 +09:00
|
|
|
trace();
|
|
|
|
}
|
2021-12-31 13:49:53 +09:00
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCspStyleNonce(): void
|
2021-12-31 13:49:53 +09:00
|
|
|
{
|
2021-12-31 14:54:38 +09:00
|
|
|
$config = config('App');
|
|
|
|
$config->CSPEnabled = true;
|
|
|
|
|
2022-01-06 12:58:22 +09:00
|
|
|
$this->assertStringStartsWith('nonce="', csp_style_nonce());
|
2021-12-31 13:49:53 +09:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testCspScriptNonce(): void
|
2021-12-31 13:49:53 +09:00
|
|
|
{
|
2021-12-31 14:54:38 +09:00
|
|
|
$config = config('App');
|
|
|
|
$config->CSPEnabled = true;
|
|
|
|
|
2022-01-06 12:58:22 +09:00
|
|
|
$this->assertStringStartsWith('nonce="', csp_script_nonce());
|
2021-12-31 13:49:53 +09:00
|
|
|
}
|
2022-06-30 17:39:33 +09:00
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testLangOnCLI(): void
|
2022-06-30 17:39:33 +09:00
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
|
|
|
|
$message = lang('CLI.generator.fileCreate', ['TestController.php']);
|
|
|
|
|
|
|
|
$this->assertSame('File created: TestController.php', $message);
|
|
|
|
|
|
|
|
$this->resetServices();
|
|
|
|
}
|
2022-11-19 10:50:27 +08:00
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testIsWindows(): void
|
2022-11-19 10:50:27 +08:00
|
|
|
{
|
2024-02-25 17:30:08 +09:00
|
|
|
$this->assertSame(str_contains(php_uname(), 'Windows'), is_windows());
|
2022-11-19 10:50:27 +08:00
|
|
|
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows());
|
|
|
|
}
|
2022-11-24 10:50:40 +08:00
|
|
|
|
2023-07-29 22:59:04 +08:00
|
|
|
public function testIsWindowsUsingMock(): void
|
2022-11-24 10:50:40 +08:00
|
|
|
{
|
|
|
|
is_windows(true);
|
|
|
|
$this->assertTrue(is_windows());
|
|
|
|
$this->assertNotFalse(is_windows());
|
|
|
|
|
|
|
|
is_windows(false);
|
|
|
|
$this->assertFalse(is_windows());
|
|
|
|
$this->assertNotTrue(is_windows());
|
|
|
|
|
|
|
|
is_windows(null);
|
2024-02-25 17:30:08 +09:00
|
|
|
$this->assertSame(str_contains(php_uname(), 'Windows'), is_windows());
|
2022-11-24 10:50:40 +08:00
|
|
|
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows());
|
|
|
|
}
|
2016-07-12 10:11:17 -07:00
|
|
|
}
|