| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Settings; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 23:19:17 +08:00
										 |  |  | use BookStack\Util\CspService; | 
					
						
							| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CustomHeadContentTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_configured_content_shows_on_pages() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']); | 
					
						
							|  |  |  |         $resp = $this->get('/login'); | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |         $resp->assertSee('console.log("cat")', false); | 
					
						
							| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 09:01:37 +08:00
										 |  |  |     public function test_content_wrapped_in_specific_html_comments() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // These comments are used to identify head content for editor injection
 | 
					
						
							|  |  |  |         $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']); | 
					
						
							|  |  |  |         $resp = $this->get('/login'); | 
					
						
							|  |  |  |         $resp->assertSee('<!-- Start: custom user content -->', false); | 
					
						
							|  |  |  |         $resp->assertSee('<!-- End: custom user content -->', false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  |     public function test_configured_content_does_not_show_on_settings_page() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-custom-head' => '<script>console.log("cat");</script>']); | 
					
						
							| 
									
										
										
										
											2022-03-31 02:15:24 +08:00
										 |  |  |         $resp = $this->asAdmin()->get('/settings/features'); | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |         $resp->assertDontSee('console.log("cat")', false); | 
					
						
							| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_divs_in_js_preserved_in_configured_content() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-custom-head' => '<script><div id="hello">cat</div></script>']); | 
					
						
							|  |  |  |         $resp = $this->get('/login'); | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |         $resp->assertSee('<div id="hello">cat</div>', false); | 
					
						
							| 
									
										
										
										
											2021-09-06 06:52:39 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-12 23:19:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_nonce_application_handles_edge_cases() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $mockCSP = $this->mock(CspService::class); | 
					
						
							|  |  |  |         $mockCSP->shouldReceive('getNonce')->andReturn('abc123'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $content = trim(' | 
					
						
							|  |  |  | <script>console.log("cat");</script> | 
					
						
							|  |  |  | <script type="text/html"><\script>const a = `<div></div>`<\/\script></script> | 
					
						
							|  |  |  | <script >const a = `<div></div>`;</script> | 
					
						
							|  |  |  | <script type="<script text>test">const c = `<div></div>`;</script> | 
					
						
							|  |  |  | <script | 
					
						
							|  |  |  |     type="text/html" | 
					
						
							|  |  |  | > | 
					
						
							|  |  |  | const a = `<\script><\/script>`; | 
					
						
							|  |  |  | const b = `<script`; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | <SCRIPT>const b = `↗️£`;</SCRIPT> | 
					
						
							|  |  |  |         '); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $expectedOutput = trim(' | 
					
						
							|  |  |  | <script nonce="abc123">console.log("cat");</script> | 
					
						
							|  |  |  | <script type="text/html" nonce="abc123"><\script>const a = `<div></div>`<\/\script></script> | 
					
						
							|  |  |  | <script nonce="abc123">const a = `<div></div>`;</script> | 
					
						
							|  |  |  | <script type="<script text>test" nonce="abc123">const c = `<div></div>`;</script> | 
					
						
							|  |  |  | <script type="text/html" nonce="abc123"> | 
					
						
							|  |  |  | const a = `<\script><\/script>`; | 
					
						
							|  |  |  | const b = `<script`; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | <script nonce="abc123">const b = `↗️£`;</script> | 
					
						
							|  |  |  |         '); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setSettings(['app-custom-head' => $content]); | 
					
						
							|  |  |  |         $resp = $this->get('/login'); | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |         $resp->assertSee($expectedOutput, false); | 
					
						
							| 
									
										
										
										
											2021-09-12 23:19:17 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-07 05:19:06 +08:00
										 |  |  | } |