Skip to content

Commit 18d0d49

Browse files
committed
tests: add test for skip html/js injection in development mode
1 parent 6c41a51 commit 18d0d49

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

tests/system/Debug/ToolbarTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Debug;
15+
16+
use CodeIgniter\CodeIgniter;
17+
use CodeIgniter\Config\Factories;
18+
use CodeIgniter\Config\Services;
19+
use CodeIgniter\HTTP\IncomingRequest;
20+
use CodeIgniter\HTTP\ResponseInterface;
21+
use CodeIgniter\Test\CIUnitTestCase;
22+
use Config\Toolbar as ToolbarConfig;
23+
use PHPUnit\Framework\Attributes\BackupGlobals;
24+
use PHPUnit\Framework\Attributes\Group;
25+
26+
/**
27+
* @internal
28+
*/
29+
#[BackupGlobals(true)]
30+
#[Group('Others')]
31+
final class ToolbarTest extends CIUnitTestCase
32+
{
33+
private ToolbarConfig $config;
34+
private ?IncomingRequest $request = null;
35+
private ?ResponseInterface $response = null;
36+
37+
protected function setUp(): void
38+
{
39+
parent::setUp();
40+
Services::reset();
41+
42+
$this->config = new ToolbarConfig();
43+
44+
// Mock CodeIgniter core service to provide performance stats
45+
$app = $this->createMock(CodeIgniter::class);
46+
$app->method('getPerformanceStats')->willReturn([
47+
'startTime' => microtime(true),
48+
'totalTime' => 0.05,
49+
]);
50+
Services::injectMock('codeigniter', $app);
51+
}
52+
53+
public function testPrepareRespectsDisableOnHeaders(): void
54+
{
55+
// Set up the new configuration property
56+
$this->config->disableOnHeaders = ['HX-Request'];
57+
Factories::injectMock('config', 'Toolbar', $this->config);
58+
59+
// Initialize Request with the custom header
60+
$this->request = service('incomingrequest', null, false);
61+
$this->request->setHeader('HX-Request', 'true');
62+
63+
// Initialize Response
64+
$this->response = service('response', null, false);
65+
$this->response->setBody('<html><body>Content</body></html>');
66+
$this->response->setHeader('Content-Type', 'text/html');
67+
68+
$toolbar = new Toolbar($this->config);
69+
$toolbar->prepare($this->request, $this->response);
70+
71+
// Assertions
72+
$this->assertTrue($this->response->hasHeader('Debugbar-Time'));
73+
$this->assertStringNotContainsString('id="debugbar_loader"', (string) $this->response->getBody());
74+
}
75+
76+
public function testPrepareInjectsNormallyWithoutIgnoredHeader(): void
77+
{
78+
$this->config->disableOnHeaders = ['HX-Request'];
79+
Factories::injectMock('config', 'Toolbar', $this->config);
80+
81+
$this->request = service('incomingrequest', null, false);
82+
$this->response = service('response', null, false);
83+
$this->response->setBody('<html><body>Content</body></html>');
84+
$this->response->setHeader('Content-Type', 'text/html');
85+
86+
$toolbar = new Toolbar($this->config);
87+
$toolbar->prepare($this->request, $this->response);
88+
89+
// Assertions
90+
$this->assertStringContainsString('id="debugbar_loader"', (string) $this->response->getBody());
91+
}
92+
}
93+
94+
/**
95+
* Mock is_cli() to return false within this namespace.
96+
*/
97+
function is_cli(): bool
98+
{
99+
return false;
100+
}

0 commit comments

Comments
 (0)