20
20
use CodeIgniter \Test \CIUnitTestCase ;
21
21
use CodeIgniter \Test \Mock \MockAppConfig ;
22
22
use CodeIgniter \Test \Mock \MockSecurity ;
23
- use Config \Cookie as CookieConfig ;
24
23
use Config \Security as SecurityConfig ;
24
+ use Config \Services ;
25
25
26
26
/**
27
27
* @backupGlobals enabled
@@ -41,11 +41,7 @@ protected function setUp(): void
41
41
42
42
public function testBasicConfigIsSaved ()
43
43
{
44
- $ config = new MockAppConfig ();
45
- $ security = $ this ->getMockBuilder (Security::class)
46
- ->setConstructorArgs ([$ config ])
47
- ->onlyMethods (['doSendCookie ' ])
48
- ->getMock ();
44
+ $ security = new MockSecurity (new MockAppConfig ());
49
45
50
46
$ hash = $ security ->getHash ();
51
47
@@ -57,11 +53,7 @@ public function testHashIsReadFromCookie()
57
53
{
58
54
$ _COOKIE ['csrf_cookie_name ' ] = '8b9218a55906f9dcc1dc263dce7f005a ' ;
59
55
60
- $ config = new MockAppConfig ();
61
- $ security = $ this ->getMockBuilder (Security::class)
62
- ->setConstructorArgs ([$ config ])
63
- ->onlyMethods (['doSendCookie ' ])
64
- ->getMock ();
56
+ $ security = new MockSecurity (new MockAppConfig ());
65
57
66
58
$ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ security ->getHash ());
67
59
}
@@ -74,7 +66,8 @@ public function testGetHashSetsCookieWhenGETWithoutCSRFCookie()
74
66
75
67
$ security ->verify (new Request (new MockAppConfig ()));
76
68
77
- $ this ->assertSame ($ _COOKIE ['csrf_cookie_name ' ], $ security ->getHash ());
69
+ $ cookie = Services::response ()->getCookie ('csrf_cookie_name ' );
70
+ $ this ->assertSame ($ security ->getHash (), $ cookie ->getValue ());
78
71
}
79
72
80
73
public function testGetHashReturnsCSRFCookieWhenGETWithCSRFCookie ()
@@ -238,45 +231,4 @@ public function testGetters(): void
238
231
$ this ->assertIsString ($ security ->getCookieName ());
239
232
$ this ->assertIsBool ($ security ->shouldRedirect ());
240
233
}
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
- }
282
234
}
0 commit comments