@@ -47,11 +47,14 @@ class CSRFTokenValidatorTest extends TestCase
47
47
48
48
public const EXISTING_ACTION_NAME = 'foo ' ;
49
49
50
+ private const CSRF_TOKEN_FOR_SHOP_ONE = '__csrf_token-1 ' ;
51
+
50
52
/**
51
53
* @before
52
54
*/
53
55
public function enableCsrfInFrontend (): void
54
56
{
57
+ $ this ->getContainer ()->get ('session ' )->offsetUnset (self ::CSRF_TOKEN_FOR_SHOP_ONE );
55
58
Utils::hijackProperty ($ this ->getContainer ()->get (CSRFTokenValidator::class), 'isEnabledFrontend ' , true );
56
59
}
57
60
@@ -60,6 +63,7 @@ public function enableCsrfInFrontend(): void
60
63
*/
61
64
public function disableCsrfInFrontend (): void
62
65
{
66
+ $ this ->getContainer ()->get ('session ' )->offsetUnset (self ::CSRF_TOKEN_FOR_SHOP_ONE );
63
67
Utils::hijackProperty ($ this ->getContainer ()->get (CSRFTokenValidator::class), 'isEnabledFrontend ' , false );
64
68
}
65
69
@@ -85,7 +89,7 @@ public function testFrontendTokenIsValid(): void
85
89
86
90
$ tokenValidator ->checkFrontendTokenValidation ($ enlightEventArgs );
87
91
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 ));
89
93
static ::assertTrue ($ incomingRequest ->getAttribute ('isValidated ' ));
90
94
}
91
95
@@ -115,8 +119,8 @@ public function testFrontendTokenValidationThrowsError(): void
115
119
static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
116
120
}
117
121
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 ));
120
124
}
121
125
122
126
public function testCsrfExceptionIsThrownWhenNoSession (): void
@@ -141,7 +145,7 @@ public function testCsrfExceptionIsThrownWhenNoSession(): void
141
145
static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
142
146
}
143
147
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 ));
145
149
}
146
150
147
151
public function testCsrfExceptionIsThrownWhenNoRequestCsrfIsSet (): void
@@ -167,7 +171,51 @@ public function testCsrfExceptionIsThrownWhenNoRequestCsrfIsSet(): void
167
171
static ::assertInstanceOf (CSRFTokenValidationException::class, $ e );
168
172
}
169
173
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 );
171
219
}
172
220
}
173
221
@@ -180,3 +228,7 @@ public function getCSRFProtectedActions()
180
228
];
181
229
}
182
230
}
231
+
232
+ class NotProtectionAwareController extends Enlight_Controller_Action
233
+ {
234
+ }
0 commit comments