@@ -47,11 +47,14 @@ class CSRFTokenValidatorTest extends TestCase
4747
4848 public const EXISTING_ACTION_NAME = 'foo ' ;
4949
50+ private const CSRF_TOKEN_FOR_SHOP_ONE = '__csrf_token-1 ' ;
51+
5052 /**
5153 * @before
5254 */
5355 public function enableCsrfInFrontend (): void
5456 {
57+ $ this ->getContainer ()->get ('session ' )->offsetUnset (self ::CSRF_TOKEN_FOR_SHOP_ONE );
5558 Utils::hijackProperty ($ this ->getContainer ()->get (CSRFTokenValidator::class), 'isEnabledFrontend ' , true );
5659 }
5760
@@ -60,6 +63,7 @@ public function enableCsrfInFrontend(): void
6063 */
6164 public function disableCsrfInFrontend (): void
6265 {
66+ $ this ->getContainer ()->get ('session ' )->offsetUnset (self ::CSRF_TOKEN_FOR_SHOP_ONE );
6367 Utils::hijackProperty ($ this ->getContainer ()->get (CSRFTokenValidator::class), 'isEnabledFrontend ' , false );
6468 }
6569
@@ -85,7 +89,7 @@ public function testFrontendTokenIsValid(): void
8589
8690 $ tokenValidator ->checkFrontendTokenValidation ($ enlightEventArgs );
8791
88- static ::assertNotNull ($ this ->getContainer ()->get ('session ' )->get (' __csrf_token-1 ' ));
92+ static ::assertIsString ($ this ->getContainer ()->get ('session ' )->get (self :: CSRF_TOKEN_FOR_SHOP_ONE ));
8993 static ::assertTrue ($ incomingRequest ->getAttribute ('isValidated ' ));
9094 }
9195
@@ -115,8 +119,8 @@ public function testFrontendTokenValidationThrowsError(): void
115119 static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
116120 }
117121
118- static ::assertNotNull ($ this ->getContainer ()->get ('session ' )->get (' __csrf_token-1 ' ));
119- static ::assertNotEquals ($ token , $ this ->getContainer ()->get ('session ' )->get (' __csrf_token-1 ' ));
122+ static ::assertIsString ($ this ->getContainer ()->get ('session ' )->get (self :: CSRF_TOKEN_FOR_SHOP_ONE ));
123+ static ::assertNotEquals ($ token , $ this ->getContainer ()->get ('session ' )->get (self :: CSRF_TOKEN_FOR_SHOP_ONE ));
120124 }
121125
122126 public function testCsrfExceptionIsThrownWhenNoSession (): void
@@ -141,7 +145,7 @@ public function testCsrfExceptionIsThrownWhenNoSession(): void
141145 static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
142146 }
143147
144- static ::assertNotNull ($ this ->getContainer ()->get ('session ' )->get (' __csrf_token-1 ' ));
148+ static ::assertIsString ($ this ->getContainer ()->get ('session ' )->get (self :: CSRF_TOKEN_FOR_SHOP_ONE ));
145149 }
146150
147151 public function testCsrfExceptionIsThrownWhenNoRequestCsrfIsSet (): void
@@ -167,7 +171,51 @@ public function testCsrfExceptionIsThrownWhenNoRequestCsrfIsSet(): void
167171 static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
168172 }
169173
170- static ::assertNotNull ($ this ->getContainer ()->get ('session ' )->get ('__csrf_token-1 ' ));
174+ static ::assertIsString ($ this ->getContainer ()->get ('session ' )->get (self ::CSRF_TOKEN_FOR_SHOP_ONE ));
175+ }
176+
177+ public function testCsrfTokenIsUpdatedIfItIsNotAvailableInTheSessionAndIsGetRequest (): void
178+ {
179+ $ tokenValidator = $ this ->getContainer ()->get (CSRFTokenValidator::class);
180+ $ this ->getContainer ()->get (ContextServiceInterface::class)->createShopContext (1 );
181+
182+ static ::assertNull ($ this ->getContainer ()->get ('session ' )->get (self ::CSRF_TOKEN_FOR_SHOP_ONE ));
183+
184+ $ controller = new NotProtectionAwareController ();
185+ $ incomingRequest = new Enlight_Controller_Request_RequestTestCase ();
186+ $ incomingRequest ->setMethod ('GET ' );
187+ $ createResponse = new Enlight_Controller_Response_ResponseTestCase ();
188+ $ controller ->setRequest ($ incomingRequest );
189+ $ controller ->setResponse ($ createResponse );
190+ $ enlightEventArgs = new Enlight_Event_EventArgs ([
191+ 'subject ' => $ controller ,
192+ ]);
193+
194+ $ tokenValidator ->checkFrontendTokenValidation ($ enlightEventArgs );
195+
196+ static ::assertIsString ($ this ->getContainer ()->get ('session ' )->get (self ::CSRF_TOKEN_FOR_SHOP_ONE ));
197+ }
198+
199+ public function testCsrfTokenIsNotUpdatedIfItIsNotAvailableInTheSession (): void
200+ {
201+ $ tokenValidator = $ this ->getContainer ()->get (CSRFTokenValidator::class);
202+ $ this ->getContainer ()->get (ContextServiceInterface::class)->createShopContext (1 );
203+
204+ static ::assertNull ($ this ->getContainer ()->get ('session ' )->get (self ::CSRF_TOKEN_FOR_SHOP_ONE ));
205+
206+ $ controller = new MockController ();
207+ $ incomingRequest = new Enlight_Controller_Request_RequestTestCase ();
208+ $ incomingRequest ->setMethod ('GET ' );
209+ $ incomingRequest ->setActionName (self ::EXISTING_ACTION_NAME );
210+ $ createResponse = new Enlight_Controller_Response_ResponseTestCase ();
211+ $ controller ->setRequest ($ incomingRequest );
212+ $ controller ->setResponse ($ createResponse );
213+ $ enlightEventArgs = new Enlight_Event_EventArgs ([
214+ 'subject ' => $ controller ,
215+ ]);
216+
217+ $ this ->expectException (CSRFTokenValidationException::class);
218+ $ tokenValidator ->checkFrontendTokenValidation ($ enlightEventArgs );
171219 }
172220}
173221
@@ -180,3 +228,7 @@ public function getCSRFProtectedActions()
180228 ];
181229 }
182230}
231+
232+ class NotProtectionAwareController extends Enlight_Controller_Action
233+ {
234+ }
0 commit comments