3 namespace Tests\Settings;
5 use BookStack\Util\CspService;
8 class CustomHeadContentTest extends TestCase
10 public function test_configured_content_shows_on_pages()
12 $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']);
13 $resp = $this->get('/login');
14 $resp->assertSee('console.log("cat")', false);
17 public function test_content_wrapped_in_specific_html_comments()
19 // These comments are used to identify head content for editor injection
20 $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']);
21 $resp = $this->get('/login');
22 $resp->assertSee('<!-- Start: custom user content -->', false);
23 $resp->assertSee('<!-- End: custom user content -->', false);
26 public function test_configured_content_does_not_show_on_settings_page()
28 $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']);
29 $resp = $this->asAdmin()->get('/settings/features');
30 $resp->assertDontSee('console.log("cat")', false);
33 public function test_divs_in_js_preserved_in_configured_content()
35 $this->setSettings(['app-custom-head' => '<script><div id="hello">cat</div></script>']);
36 $resp = $this->get('/login');
37 $resp->assertSee('<div id="hello">cat</div>', false);
40 public function test_nonce_application_handles_edge_cases()
42 $mockCSP = $this->mock(CspService::class);
43 $mockCSP->shouldReceive('getNonce')->andReturn('abc123');
46 <script>console.log("cat");</script>
47 <script type="text/html"><\script>const a = `<div></div>`<\/\script></script>
48 <script >const a = `<div></div>`;</script>
49 <script type="<script text>test">const c = `<div></div>`;</script>
53 const a = `<\script><\/script>`;
56 <SCRIPT>const b = `↗️£`;</SCRIPT>
59 $expectedOutput = trim('
60 <script nonce="abc123">console.log("cat");</script>
61 <script type="text/html" nonce="abc123"><\script>const a = `<div></div>`<\/\script></script>
62 <script nonce="abc123">const a = `<div></div>`;</script>
63 <script type="<script text>test" nonce="abc123">const c = `<div></div>`;</script>
64 <script type="text/html" nonce="abc123">
65 const a = `<\script><\/script>`;
68 <script nonce="abc123">const b = `↗️£`;</script>
71 $this->setSettings(['app-custom-head' => $content]);
72 $resp = $this->get('/login');
73 $resp->assertSee($expectedOutput, false);