]> BookStack Code Mirror - bookstack/blob - tests/Settings/CustomHeadContentTest.php
Make building of search results work for multi-byte encoded characters
[bookstack] / tests / Settings / CustomHeadContentTest.php
1 <?php
2
3 namespace Tests\Settings;
4
5 use BookStack\Util\CspService;
6 use Tests\TestCase;
7
8 class CustomHeadContentTest extends TestCase
9 {
10     public function test_configured_content_shows_on_pages()
11     {
12         $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']);
13         $resp = $this->get('/login');
14         $resp->assertSee('console.log("cat")', false);
15     }
16
17     public function test_configured_content_does_not_show_on_settings_page()
18     {
19         $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']);
20         $resp = $this->asAdmin()->get('/settings');
21         $resp->assertDontSee('console.log("cat")', false);
22     }
23
24     public function test_divs_in_js_preserved_in_configured_content()
25     {
26         $this->setSettings(['app-custom-head' => '<script><div id="hello">cat</div></script>']);
27         $resp = $this->get('/login');
28         $resp->assertSee('<div id="hello">cat</div>', false);
29     }
30
31     public function test_nonce_application_handles_edge_cases()
32     {
33         $mockCSP = $this->mock(CspService::class);
34         $mockCSP->shouldReceive('getNonce')->andReturn('abc123');
35
36         $content = trim('
37 <script>console.log("cat");</script>
38 <script type="text/html"><\script>const a = `<div></div>`<\/\script></script>
39 <script >const a = `<div></div>`;</script>
40 <script type="<script text>test">const c = `<div></div>`;</script>
41 <script
42     type="text/html"
43 >
44 const a = `<\script><\/script>`;
45 const b = `<script`;
46 </script>
47 <SCRIPT>const b = `↗️£`;</SCRIPT>
48         ');
49
50         $expectedOutput = trim('
51 <script nonce="abc123">console.log("cat");</script>
52 <script type="text/html" nonce="abc123"><\script>const a = `<div></div>`<\/\script></script>
53 <script nonce="abc123">const a = `<div></div>`;</script>
54 <script type="&lt;script text&gt;test" nonce="abc123">const c = `<div></div>`;</script>
55 <script type="text/html" nonce="abc123">
56 const a = `<\script><\/script>`;
57 const b = `<script`;
58 </script>
59 <script nonce="abc123">const b = `↗️£`;</script>
60         ');
61
62         $this->setSettings(['app-custom-head' => $content]);
63         $resp = $this->get('/login');
64         $resp->assertSee($expectedOutput, false);
65     }
66 }