2017-01-06 20:06:06 -06:00
|
|
|
<?php
|
2016-07-12 10:11:17 -07:00
|
|
|
|
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-09-08 11:12:02 +09:00
|
|
|
use CodeIgniter\HTTP\CLIRequest;
|
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;
|
2018-06-07 20:50:03 -07:00
|
|
|
use CodeIgniter\HTTP\URI;
|
|
|
|
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;
|
2022-02-05 13:55:03 +09:00
|
|
|
use CodeIgniter\Test\Mock\MockCodeIgniter;
|
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;
|
|
|
|
use Config\Logger;
|
2021-02-28 06:57:05 +07:00
|
|
|
use Config\Modules;
|
2022-05-31 09:02:12 +09:00
|
|
|
use Config\Services;
|
2021-12-27 11:37:44 +09:00
|
|
|
use Kint;
|
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
|
|
|
/**
|
|
|
|
* @backupGlobals enabled
|
2021-06-25 22:27:37 +08:00
|
|
|
*
|
|
|
|
* @internal
|
2022-05-05 15:37:25 +09:00
|
|
|
*
|
|
|
|
* @group SeparateProcess
|
2017-02-09 19:33:03 +09:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testStringifyAttributes()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testStringifyJsAttributes()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvReturnsDefault()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('baz', env('foo', 'baz'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvGetsFromSERVER()
|
|
|
|
{
|
|
|
|
$_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
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvGetsFromENV()
|
|
|
|
{
|
|
|
|
$_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
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvBooleans()
|
|
|
|
{
|
|
|
|
$_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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRedirectReturnsRedirectResponse()
|
|
|
|
{
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$response = $this->createMock(Response::class);
|
|
|
|
$routes = new RouteCollection(
|
2021-06-16 00:50:52 +08:00
|
|
|
Services::locator(),
|
|
|
|
new Modules()
|
2021-06-04 22:51:52 +08:00
|
|
|
);
|
2022-05-31 09:02:12 +09:00
|
|
|
Services::injectMock('response', $response);
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRedirectDefault()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, redirect());
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:12:02 +09:00
|
|
|
public function testRequestIncomingRequest()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App());
|
|
|
|
|
|
|
|
$request = request();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(IncomingRequest::class, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequestCLIRequest()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
|
|
|
|
$request = request();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(CLIRequest::class, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testResponse()
|
|
|
|
{
|
|
|
|
$response = response();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Response::class, $response);
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
public function testView()
|
|
|
|
{
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewSavedData()
|
|
|
|
{
|
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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewCell()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testEscapeWithDifferentEncodings()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testEscapeBadContext()
|
|
|
|
{
|
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');
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:50:51 +09:00
|
|
|
public function testEscapeBadContextZero()
|
|
|
|
{
|
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
esc('<script>', '0');
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testSessionInstance()
|
|
|
|
{
|
|
|
|
$this->injectSessionMock();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Session::class, session());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testSessionVariable()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testSessionVariableNotThere()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testRouteTo()
|
|
|
|
{
|
|
|
|
// prime the pump
|
|
|
|
$routes = service('routes');
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2022-08-29 11:09:10 +09:00
|
|
|
public function testRouteToInCliWithoutLocaleInRoute()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
$routes = service('routes');
|
|
|
|
$routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2');
|
|
|
|
|
|
|
|
$this->assertSame('/path/string/to/13', route_to('myController::goto', 'string', 13));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRouteToInCliWithLocaleInRoute()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
$routes = service('routes');
|
|
|
|
$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')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRouteToWithUnsupportedLocale()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App(), false);
|
|
|
|
$routes = service('routes');
|
|
|
|
$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')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:52 +08:00
|
|
|
public function testInvisible()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('Javascript', remove_invisible_characters("Java\0script"));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvisibleEncoded()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('Javascript', remove_invisible_characters('Java%0cscript'));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppTimezone()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('America/Chicago', app_timezone());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCSRFToken()
|
|
|
|
{
|
2021-10-17 00:11:01 +09:00
|
|
|
Services::injectMock('security', new MockSecurity(new App()));
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('csrf_test_name', csrf_token());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCSRFHeader()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('X-CSRF-TOKEN', csrf_header());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHash()
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame(32, strlen(csrf_hash()));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCSRFField()
|
|
|
|
{
|
|
|
|
$this->assertStringContainsString('<input type="hidden" ', csrf_field());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCSRFMeta()
|
|
|
|
{
|
|
|
|
$this->assertStringContainsString('<meta name="X-CSRF-TOKEN" ', csrf_meta());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModelNotExists()
|
|
|
|
{
|
|
|
|
$this->assertNull(model(UnexsistenceClass::class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModelExistsBasename()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(JobModel::class, model('JobModel'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModelExistsClassname()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(JobModel::class, model(JobModel::class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModelExistsAbsoluteClassname()
|
|
|
|
{
|
2022-04-29 00:07:19 +07:00
|
|
|
$this->assertInstanceOf(JobModel::class, model(JobModel::class));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testOldInput()
|
|
|
|
{
|
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
|
|
|
$this->routes = new RouteCollection(Services::locator(), new Modules());
|
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
|
|
|
$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-12 14:09:37 +09:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-11-12 14:09:37 +09:00
|
|
|
*/
|
|
|
|
public function testOldInputSerializeData()
|
|
|
|
{
|
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
|
|
|
$this->routes = new RouteCollection(Services::locator(), new Modules());
|
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
|
|
|
$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
|
|
|
|
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
|
2022-09-02 09:32:01 +08:00
|
|
|
*
|
2021-06-04 22:51:52 +08:00
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testOldInputArray()
|
|
|
|
{
|
|
|
|
$this->injectSessionMock();
|
|
|
|
// setup from RedirectResponseTest...
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
|
|
|
|
$this->config = new App();
|
|
|
|
$this->config->baseURL = 'http://example.com/';
|
|
|
|
|
|
|
|
$this->routes = new RouteCollection(Services::locator(), new Modules());
|
|
|
|
Services::injectMock('routes', $this->routes);
|
|
|
|
|
|
|
|
$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testReallyWritable()
|
|
|
|
{
|
|
|
|
// cannot test fully on *nix
|
|
|
|
$this->assertTrue(is_really_writable(WRITEPATH));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSlashItem()
|
|
|
|
{
|
2022-06-01 09:31:23 +08:00
|
|
|
$this->assertSame('/', slash_item('cookiePath')); // /
|
|
|
|
$this->assertSame('', slash_item('cookieDomain')); // ''
|
|
|
|
$this->assertSame('en/', slash_item('defaultLocale')); // en
|
|
|
|
$this->assertSame('7200/', slash_item('sessionExpiration')); // int 7200
|
|
|
|
$this->assertSame('', slash_item('negotiateLocale')); // false
|
|
|
|
$this->assertSame('1/', slash_item('cookieHTTPOnly')); // true
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSlashItemOnInexistentItem()
|
|
|
|
{
|
|
|
|
$this->assertNull(slash_item('foo'));
|
|
|
|
$this->assertNull(slash_item('bar'));
|
|
|
|
$this->assertNull(slash_item('cookieDomains'));
|
|
|
|
$this->assertNull(slash_item('indices'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSlashItemThrowsErrorOnNonStringableItem()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
protected function injectSessionMock()
|
|
|
|
{
|
|
|
|
$defaults = [
|
2022-03-19 22:52:14 +07:00
|
|
|
'sessionDriver' => FileHandler::class,
|
2021-06-04 22:51:52 +08:00
|
|
|
'sessionCookieName' => 'ci_session',
|
|
|
|
'sessionExpiration' => 7200,
|
2022-07-01 13:50:13 +09:00
|
|
|
'sessionSavePath' => '',
|
2021-06-04 22:51:52 +08:00
|
|
|
'sessionMatchIP' => false,
|
|
|
|
'sessionTimeToUpdate' => 300,
|
|
|
|
'sessionRegenerateDestroy' => false,
|
|
|
|
'cookieDomain' => '',
|
|
|
|
'cookiePrefix' => '',
|
|
|
|
'cookiePath' => '/',
|
|
|
|
'cookieSecure' => false,
|
|
|
|
'cookieSameSite' => 'Lax',
|
|
|
|
];
|
|
|
|
|
|
|
|
$appConfig = new App();
|
2021-06-08 01:26:32 +08:00
|
|
|
|
2021-06-07 19:06:26 +08:00
|
|
|
foreach ($defaults as $key => $config) {
|
2021-06-28 22:10:44 +08:00
|
|
|
$appConfig->{$key} = $config;
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$session = new MockSession(new FileHandler($appConfig, '127.0.0.1'), $appConfig);
|
|
|
|
$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
|
|
|
|
public function testRedirectResponseCookies1()
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
->setCookie('login_time', $loginTime, YEAR);
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
$this->assertTrue($answer1->hasCookie('foo', 'onething'));
|
|
|
|
$this->assertTrue($answer1->hasCookie('login_time'));
|
|
|
|
}
|
|
|
|
|
2022-01-12 21:27:11 +09:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
2021-06-04 22:51:52 +08:00
|
|
|
public function testTrace()
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
trace();
|
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertStringContainsString('Debug Backtrace', $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewNotSaveData()
|
|
|
|
{
|
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testForceHttpsNullRequestAndResponse()
|
|
|
|
{
|
|
|
|
$this->assertNull(Services::response()->header('Location'));
|
|
|
|
|
|
|
|
force_https();
|
|
|
|
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame('https://example.com/', Services::response()->header('Location')->getValue());
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dirtyPathsProvider
|
2021-07-24 19:30:51 +08:00
|
|
|
*
|
|
|
|
* @param mixed $input
|
|
|
|
* @param mixed $expected
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function testCleanPathActuallyCleaningThePaths($input, $expected)
|
|
|
|
{
|
2021-06-25 23:35:25 +08:00
|
|
|
$this->assertSame($expected, clean_path($input));
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dirtyPathsProvider()
|
|
|
|
{
|
|
|
|
$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',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsCli()
|
|
|
|
{
|
|
|
|
$this->assertIsBool(is_cli());
|
|
|
|
$this->assertTrue(is_cli());
|
|
|
|
}
|
2021-12-27 11:37:44 +09:00
|
|
|
|
|
|
|
public function testDWithCSP()
|
|
|
|
{
|
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
|
|
|
|
$app = new MockCodeIgniter($config);
|
|
|
|
$app->initialize();
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
2022-10-30 18:52:50 +08:00
|
|
|
* @preserveGlobalState disabled
|
2021-12-27 11:37:44 +09:00
|
|
|
*/
|
|
|
|
public function testTraceWithCSP()
|
|
|
|
{
|
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
|
|
|
|
$app = new MockCodeIgniter($config);
|
|
|
|
$app->initialize();
|
|
|
|
|
2021-12-27 11:37:44 +09:00
|
|
|
Kint::$cli_detection = false;
|
|
|
|
|
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
|
|
|
|
|
|
|
public function testCspStyleNonce()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testCspScriptNonce()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
public function testLangOnCLI()
|
|
|
|
{
|
|
|
|
Services::createRequest(new App(), true);
|
|
|
|
|
|
|
|
$message = lang('CLI.generator.fileCreate', ['TestController.php']);
|
|
|
|
|
|
|
|
$this->assertSame('File created: TestController.php', $message);
|
|
|
|
|
|
|
|
$this->resetServices();
|
|
|
|
}
|
2016-07-12 10:11:17 -07:00
|
|
|
}
|