CodeIgniter4/tests/system/CommonFunctionsTest.php

444 lines
12 KiB
PHP
Raw Normal View History

<?php
2016-07-12 10:11:17 -07:00
use CodeIgniter\Session\Handlers\FileHandler;
2018-11-15 09:00:27 -08:00
use CodeIgniter\HTTP\Response;
use Config\App;
use CodeIgniter\Config\Services;
2018-06-07 02:56:46 -07:00
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use Config\Logger;
use CodeIgniter\Test\Mock\MockIncomingRequest;
2020-02-19 10:48:45 -05:00
use CodeIgniter\Test\TestLogger;
use CodeIgniter\Test\Mock\MockSession;
2020-04-17 17:27:16 +07:00
use Tests\Support\Models\JobModel;
2018-06-07 02:56:46 -07:00
/**
* @backupGlobals enabled
*/
class CommonFunctionsTest extends \CodeIgniter\Test\CIUnitTestCase
2016-07-12 10:11:17 -07:00
{
//--------------------------------------------------------------------
protected function setUp(): void
2016-07-12 10:11:17 -07:00
{
2018-05-21 22:47:16 -05:00
parent::setUp();
2020-04-21 16:34:52 +08:00
$renderer = Services::renderer();
$renderer->resetData();
2018-06-07 02:56:46 -07:00
unset($_ENV['foo'], $_SERVER['foo']);
2016-07-12 10:11:17 -07:00
}
//--------------------------------------------------------------------
public function testStringifyAttributes()
{
$this->assertEquals(' class="foo" id="bar"', stringify_attributes(['class' => 'foo', 'id' => 'bar']));
2016-07-12 10:11:17 -07:00
2018-11-10 02:57:39 -02:00
$atts = new stdClass;
2016-07-12 10:11:17 -07:00
$atts->class = 'foo';
2018-11-10 02:57:39 -02:00
$atts->id = 'bar';
2016-07-12 10:11:17 -07:00
$this->assertEquals(' class="foo" id="bar"', stringify_attributes($atts));
$atts = new stdClass;
$this->assertEquals('', stringify_attributes($atts));
$this->assertEquals(' class="foo" id="bar"', stringify_attributes('class="foo" id="bar"'));
$this->assertEquals('', stringify_attributes([]));
2016-07-12 10:11:17 -07:00
}
// ------------------------------------------------------------------------
public function testStringifyJsAttributes()
{
2018-11-10 02:57:39 -02:00
$this->assertEquals('width=800,height=600', stringify_attributes(['width' => '800', 'height' => '600'], true));
2016-07-12 10:11:17 -07:00
2018-11-10 02:57:39 -02:00
$atts = new stdClass;
$atts->width = 800;
2016-07-12 10:11:17 -07:00
$atts->height = 600;
2018-11-10 02:57:39 -02:00
$this->assertEquals('width=800,height=600', stringify_attributes($atts, true));
2016-07-12 10:11:17 -07:00
}
// ------------------------------------------------------------------------
2018-06-07 02:56:46 -07:00
public function testEnvReturnsDefault()
{
$this->assertEquals('baz', env('foo', 'baz'));
}
public function testEnvGetsFromSERVER()
{
$_SERVER['foo'] = 'bar';
$this->assertEquals('bar', env('foo', 'baz'));
}
2017-01-16 23:33:24 -06:00
2018-06-07 02:56:46 -07:00
public function testEnvGetsFromENV()
{
$_ENV['foo'] = 'bar';
2017-01-16 23:33:24 -06:00
2018-06-07 02:56:46 -07:00
$this->assertEquals('bar', env('foo', 'baz'));
}
2017-01-16 23:33:24 -06:00
2018-06-07 02:56:46 -07: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'));
}
2017-01-16 23:33:24 -06:00
2018-06-07 02:56:46 -07:00
// ------------------------------------------------------------------------
2017-04-19 22:05:40 -05:00
public function testRedirectReturnsRedirectResponse()
2017-04-19 22:05:40 -05:00
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
2018-11-30 18:20:13 -02:00
$routes = new \CodeIgniter\Router\RouteCollection(
Services::locator(), new \Config\Modules()
);
2017-04-19 22:05:40 -05:00
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);
$routes->add('home/base', 'Controller::index', ['as' => 'base']);
$response->method('redirect')
2018-06-07 02:56:46 -07:00
->will($this->returnArgument(0));
2017-04-19 22:05:40 -05:00
$this->assertInstanceOf(\CodeIgniter\HTTP\RedirectResponse::class, redirect('base'));
2018-06-07 02:56:46 -07:00
}
public function testRedirectDefault()
{
$this->assertInstanceOf(\CodeIgniter\HTTP\RedirectResponse::class, redirect());
}
2018-06-07 02:56:46 -07:00
// ------------------------------------------------------------------------
public function testView()
{
2018-11-10 02:57:39 -02:00
$data = [
2018-06-07 02:56:46 -07:00
'testString' => 'bar',
2018-11-10 02:57:39 -02:00
'bar' => 'baz',
2018-06-07 02:56:46 -07:00
];
$expected = '<h1>bar</h1>';
$this->assertStringContainsString($expected, view('\Tests\Support\View\Views\simple', $data, []));
2018-06-07 02:56:46 -07:00
}
public function testViewSavedData()
{
2018-11-10 02:57:39 -02:00
$data = [
2018-06-07 02:56:46 -07:00
'testString' => 'bar',
2018-11-10 02:57:39 -02:00
'bar' => 'baz',
2018-06-07 02:56:46 -07:00
];
$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'));
2018-06-07 02:56:46 -07:00
}
// ------------------------------------------------------------------------
public function testViewCell()
{
$expected = 'Hello';
2018-08-08 22:48:35 -05:00
$this->assertEquals($expected, view_cell('\Tests\Support\View\SampleClass::hello'));
2018-06-07 02:56:46 -07:00
}
// ------------------------------------------------------------------------
public function testEscapeWithDifferentEncodings()
{
$this->assertEquals('&lt;x', esc('<x', 'html', 'utf-8'));
$this->assertEquals('&lt;x', esc('<x', 'html', 'iso-8859-1'));
$this->assertEquals('&lt;x', esc('<x', 'html', 'windows-1251'));
}
// ------------------------------------------------------------------------
2018-06-07 02:56:46 -07:00
public function testEscapeBadContext()
{
$this->expectException(InvalidArgumentException::class);
esc(['width' => '800', 'height' => '600'], 'bogus');
}
// ------------------------------------------------------------------------
/**
* @runInSeparateProcess
2018-11-10 02:57:39 -02:00
* @preserveGlobalState disabled
*/
2018-06-07 02:56:46 -07:00
public function testSessionInstance()
{
$this->injectSessionMock();
2018-06-07 02:56:46 -07:00
$this->assertInstanceOf(CodeIgniter\Session\Session::class, session());
}
/**
* @runInSeparateProcess
2018-11-10 02:57:39 -02:00
* @preserveGlobalState disabled
*/
2018-06-07 02:56:46 -07:00
public function testSessionVariable()
{
$this->injectSessionMock();
2018-06-07 02:56:46 -07:00
$_SESSION['notbogus'] = 'Hi there';
2018-06-07 02:56:46 -07:00
$this->assertEquals('Hi there', session('notbogus'));
}
/**
* @runInSeparateProcess
2018-11-10 02:57:39 -02:00
* @preserveGlobalState disabled
*/
2018-06-07 02:56:46 -07:00
public function testSessionVariableNotThere()
{
$this->injectSessionMock();
2018-06-07 02:56:46 -07:00
$_SESSION['bogus'] = 'Hi there';
$this->assertEquals(null, session('notbogus'));
}
// ------------------------------------------------------------------------
public function testSingleService()
{
$timer1 = single_service('timer');
$timer2 = single_service('timer');
$this->assertFalse($timer1 === $timer2);
}
// ------------------------------------------------------------------------
public function testRouteTo()
{
// prime the pump
$routes = service('routes');
$routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2');
$this->assertEquals('/path/string/to/13', route_to('myController::goto', 'string', 13));
}
// ------------------------------------------------------------------------
public function testInvisible()
{
$this->assertEquals('Javascript', remove_invisible_characters("Java\0script"));
}
public function testInvisibleEncoded()
{
2018-11-10 02:57:39 -02:00
$this->assertEquals('Javascript', remove_invisible_characters('Java%0cscript', true));
2018-06-07 02:56:46 -07:00
}
// ------------------------------------------------------------------------
public function testAppTimezone()
{
$this->assertEquals('America/Chicago', app_timezone());
}
// ------------------------------------------------------------------------
public function testCSRFToken()
{
$this->assertEquals('csrf_test_name', csrf_token());
}
public function testCSRFHeader()
{
$this->assertEquals('X-CSRF-TOKEN', csrf_header());
}
2018-06-07 02:56:46 -07:00
public function testHash()
{
$this->assertEquals(32, strlen(csrf_hash()));
}
public function testCSRFField()
{
$this->assertStringContainsString('<input type="hidden" ', csrf_field());
2018-06-07 02:56:46 -07:00
}
2017-04-19 22:05:40 -05:00
public function testCSRFMeta()
{
$this->assertStringContainsString('<meta name="X-CSRF-TOKEN" ', csrf_meta());
}
// ------------------------------------------------------------------------
2020-04-17 17:27:16 +07:00
public function testModelNotExists()
{
$this->assertNull(model(UnexsistenceClass::class));
}
public function testModelExists()
{
$this->assertInstanceOf(JobModel::class, model(JobModel::class));
}
// ------------------------------------------------------------------------
/**
* @runInSeparateProcess
2018-11-10 02:57:39 -02:00
* @preserveGlobalState disabled
*/
public function testOldInput()
{
$this->injectSessionMock();
// setup from RedirectResponseTest...
$_SERVER['REQUEST_METHOD'] = 'GET';
2018-11-10 02:57:39 -02:00
$this->config = new App();
$this->config->baseURL = 'http://example.com';
2018-11-30 18:20:13 -02:00
$this->routes = new RouteCollection(Services::locator(), new \Config\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 = [];
2018-11-10 02:57:39 -02:00
$_GET = ['foo' => 'bar'];
$_POST = [
'bar' => 'baz',
'zibble' => serialize('fritz'),
];
$response = new RedirectResponse(new App());
$returned = $response->withInput();
$this->assertEquals('bar', old('foo')); // regular parameter
$this->assertEquals('doo', old('yabba dabba', 'doo')); // non-existing parameter
$this->assertEquals('fritz', old('zibble')); // serialized parameter
}
2018-11-17 09:43:32 -08:00
// Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testOldInputArray()
{
$this->injectSessionMock();
// setup from RedirectResponseTest...
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->config = new App();
$this->config->baseURL = 'http://example.com';
2018-11-30 18:20:13 -02:00
$this->routes = new RouteCollection(Services::locator(), new \Config\Modules());
2018-11-17 09:43:32 -08:00
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());
$returned = $response->withInput();
$this->assertEquals($locations, old('location'));
}
// ------------------------------------------------------------------------
public function testReallyWritable()
{
// cannot test fully on *nix
$this->assertTrue(is_really_writable(WRITEPATH));
}
// ------------------------------------------------------------------------
public function testSlashItem()
{
$this->assertEquals('/', slash_item('cookiePath')); // slash already there
$this->assertEquals('', slash_item('cookieDomain')); // empty, so untouched
$this->assertEquals('en/', slash_item('defaultLocale')); // slash appended
}
protected function injectSessionMock()
{
$defaults = [
2018-11-10 02:57:39 -02:00
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler',
'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200,
'sessionSavePath' => null,
'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false,
2018-11-10 02:57:39 -02:00
'cookieDomain' => '',
'cookiePrefix' => '',
'cookiePath' => '/',
'cookieSecure' => false,
];
2018-11-15 09:00:27 -08:00
$config = (object) $defaults;
2018-10-30 01:17:35 +07:00
$session = new MockSession(new FileHandler($config, '127.0.0.1'), $config);
$session->setLogger(new TestLogger(new Logger()));
\CodeIgniter\Config\BaseService::injectMock('session', $session);
}
2018-11-15 09:00:27 -08:00
//--------------------------------------------------------------------
// Make sure cookies are set by RedirectResponse this way
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
2018-11-15 10:13:27 -08:00
public function testRedirectResponseCookies1()
2018-11-15 09:00:27 -08:00
{
$login_time = time();
$response = new Response(new App());
2018-11-15 10:13:27 -08:00
$routes = service('routes');
2018-11-15 09:00:27 -08:00
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);
2018-11-15 10:13:27 -08:00
$answer1 = redirect()->route('login')
->setCookie('foo', 'onething', YEAR)
2018-11-15 09:00:27 -08:00
->setCookie('login_time', $login_time, YEAR);
2018-11-15 10:13:27 -08:00
$this->assertTrue($answer1->hasCookie('foo', 'onething'));
$this->assertTrue($answer1->hasCookie('login_time'));
}
2020-04-17 17:27:16 +07:00
//--------------------------------------------------------------------
public function testTrace()
{
ob_start();
trace();
$content = ob_get_clean();
$this->assertStringContainsString('Debug Backtrace', $content);
}
2020-04-21 16:34:52 +08:00
public function testViewNotSaveData()
{
2020-04-22 09:07:04 +08:00
$data = [
2020-04-21 16:34:52 +08:00
'testString' => 'bar',
'bar' => 'baz',
];
2020-04-22 09:07:04 +08:00
$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'));
2020-04-21 16:34:52 +08:00
}
2016-07-12 10:11:17 -07:00
}