2020use CodeIgniter \Test \CIUnitTestCase ;
2121use CodeIgniter \Test \Mock \MockAppConfig ;
2222use CodeIgniter \Test \Mock \MockSecurity ;
23- use Config \Cookie as CookieConfig ;
2423use Config \Security as SecurityConfig ;
24+ use Config \Services ;
2525
2626/**
2727 * @backupGlobals enabled
@@ -41,11 +41,7 @@ protected function setUp(): void
4141
4242 public function testBasicConfigIsSaved ()
4343 {
44- $ config = new MockAppConfig ();
45- $ security = $ this ->getMockBuilder (Security::class)
46- ->setConstructorArgs ([$ config ])
47- ->onlyMethods (['doSendCookie ' ])
48- ->getMock ();
44+ $ security = new MockSecurity (new MockAppConfig ());
4945
5046 $ hash = $ security ->getHash ();
5147
@@ -57,11 +53,7 @@ public function testHashIsReadFromCookie()
5753 {
5854 $ _COOKIE ['csrf_cookie_name ' ] = '8b9218a55906f9dcc1dc263dce7f005a ' ;
5955
60- $ config = new MockAppConfig ();
61- $ security = $ this ->getMockBuilder (Security::class)
62- ->setConstructorArgs ([$ config ])
63- ->onlyMethods (['doSendCookie ' ])
64- ->getMock ();
56+ $ security = new MockSecurity (new MockAppConfig ());
6557
6658 $ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ security ->getHash ());
6759 }
@@ -74,7 +66,8 @@ public function testGetHashSetsCookieWhenGETWithoutCSRFCookie()
7466
7567 $ security ->verify (new Request (new MockAppConfig ()));
7668
77- $ this ->assertSame ($ _COOKIE ['csrf_cookie_name ' ], $ security ->getHash ());
69+ $ cookie = Services::response ()->getCookie ('csrf_cookie_name ' );
70+ $ this ->assertSame ($ security ->getHash (), $ cookie ->getValue ());
7871 }
7972
8073 public function testGetHashReturnsCSRFCookieWhenGETWithCSRFCookie ()
@@ -238,45 +231,4 @@ public function testGetters(): void
238231 $ this ->assertIsString ($ security ->getCookieName ());
239232 $ this ->assertIsBool ($ security ->shouldRedirect ());
240233 }
241-
242- public function testSendingCookiesFalse (): void
243- {
244- $ request = $ this ->createMock (IncomingRequest::class);
245- $ request ->method ('isSecure ' )->willReturn (false );
246-
247- $ config = new CookieConfig ();
248-
249- $ config ->secure = true ;
250- Factories::injectMock ('config ' , CookieConfig::class, $ config );
251-
252- $ security = $ this ->getMockBuilder (Security::class)
253- ->setConstructorArgs ([new MockAppConfig ()])
254- ->onlyMethods (['doSendCookie ' ])
255- ->getMock ();
256-
257- $ sendCookie = $ this ->getPrivateMethodInvoker ($ security , 'sendCookie ' );
258-
259- $ security ->expects ($ this ->never ())->method ('doSendCookie ' );
260- $ this ->assertFalse ($ sendCookie ($ request ));
261- }
262-
263- public function testSendingGoodCookies (): void
264- {
265- $ request = $ this ->createMock (IncomingRequest::class);
266- $ request ->method ('isSecure ' )->willReturn (true );
267-
268- $ config = new MockAppConfig ();
269-
270- $ config ->cookieSecure = true ;
271-
272- $ security = $ this ->getMockBuilder (Security::class)
273- ->setConstructorArgs ([$ config ])
274- ->onlyMethods (['doSendCookie ' ])
275- ->getMock ();
276-
277- $ sendCookie = $ this ->getPrivateMethodInvoker ($ security , 'sendCookie ' );
278-
279- $ security ->expects ($ this ->once ())->method ('doSendCookie ' );
280- $ this ->assertInstanceOf (Security::class, $ sendCookie ($ request ));
281- }
282234}
0 commit comments