* * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ namespace CodeIgniter; use CodeIgniter\Config\BaseService; use CodeIgniter\Config\Factories; use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Exceptions\RedirectException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Router\RouteCollection; use CodeIgniter\Session\Handlers\FileHandler; use CodeIgniter\Session\Session; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockIncomingRequest; use CodeIgniter\Test\Mock\MockSecurity; use CodeIgniter\Test\Mock\MockSession; use CodeIgniter\Test\TestLogger; use Config\App; use Config\Cookie; use Config\DocTypes; use Config\Logger; use Config\Modules; use Config\Routing; use Config\Security as SecurityConfig; use Config\Services; use Config\Session as SessionConfig; use Exception; use Kint; 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; use PHPUnit\Framework\Attributes\WithoutErrorHandler; use stdClass; use Tests\Support\Models\JobModel; /** * @internal */ #[BackupGlobals(true)] #[Group('SeparateProcess')] final class CommonFunctionsTest extends CIUnitTestCase { private ?App $config = null; private IncomingRequest $request; protected function setUp(): void { unset($_ENV['foo'], $_SERVER['foo']); $this->resetServices(); parent::setUp(); } public function testStringifyAttributes(): void { $this->assertSame(' class="foo" id="bar"', stringify_attributes(['class' => 'foo', 'id' => 'bar'])); $atts = new stdClass(); $atts->class = 'foo'; $atts->id = 'bar'; $this->assertSame(' class="foo" id="bar"', stringify_attributes($atts)); $atts = new stdClass(); $this->assertSame('', stringify_attributes($atts)); $this->assertSame(' class="foo" id="bar"', stringify_attributes('class="foo" id="bar"')); $this->assertSame('', stringify_attributes([])); } public function testStringifyJsAttributes(): void { $this->assertSame('width=800,height=600', stringify_attributes(['width' => '800', 'height' => '600'], true)); $atts = new stdClass(); $atts->width = 800; $atts->height = 600; $this->assertSame('width=800,height=600', stringify_attributes($atts, true)); } public function testEnvReturnsDefault(): void { $this->assertSame('baz', env('foo', 'baz')); } public function testEnvGetsFromSERVER(): void { $_SERVER['foo'] = 'bar'; $this->assertSame('bar', env('foo', 'baz')); } public function testEnvGetsFromENV(): void { $_ENV['foo'] = 'bar'; $this->assertSame('bar', env('foo', 'baz')); } public function testEnvBooleans(): void { $_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')); } private function createRouteCollection(): RouteCollection { return new RouteCollection(Services::locator(), new Modules(), new Routing()); } public function testRedirectReturnsRedirectResponse(): void { $_SERVER['REQUEST_METHOD'] = 'GET'; $response = $this->createMock(Response::class); Services::injectMock('response', $response); $routes = $this->createRouteCollection(); Services::injectMock('routes', $routes); $routes->add('home/base', 'Controller::index', ['as' => 'base']); $response->method('redirect')->willReturnArgument(0); $this->assertInstanceOf(RedirectResponse::class, redirect('base')); } public function testRedirectDefault(): void { $this->assertInstanceOf(RedirectResponse::class, redirect()); } public function testRequestIncomingRequest(): void { Services::createRequest(new App()); $request = request(); $this->assertInstanceOf(IncomingRequest::class, $request); } public function testRequestCLIRequest(): void { Services::createRequest(new App(), true); $request = request(); $this->assertInstanceOf(CLIRequest::class, $request); } public function testResponse(): void { $response = response(); $this->assertInstanceOf(Response::class, $response); } public function testSolidusElement(): void { $this->assertSame('', _solidus()); } public function testSolidusElementXHTML(): void { $this->disableHtml5(); $this->assertSame(' /', _solidus()); $this->enableHtml5(); } private function disableHtml5(): void { $doctypes = new DocTypes(); $doctypes->html5 = false; _solidus($doctypes); } private function enableHtml5(): void { $doctypes = new DocTypes(); _solidus($doctypes); } public function testView(): void { $data = [ 'testString' => 'bar', 'bar' => 'baz', ]; $expected = '