1111
1212namespace CodeIgniter \Honeypot ;
1313
14+ use CodeIgniter \Config \Factories ;
1415use CodeIgniter \Config \Services ;
1516use CodeIgniter \Filters \Filters ;
1617use CodeIgniter \Honeypot \Exceptions \HoneypotException ;
1718use CodeIgniter \HTTP \CLIRequest ;
1819use CodeIgniter \HTTP \IncomingRequest ;
1920use CodeIgniter \HTTP \Response ;
2021use CodeIgniter \Test \CIUnitTestCase ;
22+ use Config \App ;
2123use Config \Honeypot as HoneypotConfig ;
2224
2325/**
@@ -42,14 +44,16 @@ final class HoneypotTest extends CIUnitTestCase
4244 protected function setUp (): void
4345 {
4446 parent ::setUp ();
47+
4548 $ this ->config = new HoneypotConfig ();
4649 $ this ->honeypot = new Honeypot ($ this ->config );
4750
4851 unset($ _POST [$ this ->config ->name ]);
4952 $ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
5053 $ _POST [$ this ->config ->name ] = 'hey ' ;
51- $ this ->request = Services::request (null , false );
52- $ this ->response = Services::response ();
54+
55+ $ this ->request = Services::request (null , false );
56+ $ this ->response = Services::response ();
5357 }
5458
5559 public function testAttachHoneypot ()
@@ -77,6 +81,25 @@ public function testAttachHoneypotAndContainer()
7781 $ this ->assertSame ($ expected , $ this ->response ->getBody ());
7882 }
7983
84+ public function testAttachHoneypotAndContainerWithCSP ()
85+ {
86+ $ this ->resetServices ();
87+
88+ $ config = new App ();
89+ $ config ->CSPEnabled = true ;
90+ Factories::injectMock ('config ' , 'App ' , $ config );
91+ $ this ->response = Services::response ($ config , false );
92+
93+ $ this ->config = new HoneypotConfig ();
94+ $ this ->honeypot = new Honeypot ($ this ->config );
95+
96+ $ this ->response ->setBody ('<head></head><body><form></form></body> ' );
97+ $ this ->honeypot ->attachHoneypot ($ this ->response );
98+
99+ $ regex = '!<head><style nonce="[0-9a-f]+">#hpc { display:none }</style></head><body><form><div style="display:none" id="hpc"><label>Fill This Field</label><input type="text" name="honeypot" value=""></div></form></body>!u ' ;
100+ $ this ->assertMatchesRegularExpression ($ regex , $ this ->response ->getBody ());
101+ }
102+
80103 public function testHasntContent ()
81104 {
82105 unset($ _POST [$ this ->config ->name ]);
0 commit comments