Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

qwales1's avatar

CSRF middleware not running in test

Hello,

I am testing a form submission and the test is passing without the presence of the CSRF token. If I go to the browser and submit the form, I get the TokenMismatchException that I was expecting. I checked and the VerifyCsrfToken middleware isn't running at all during the test and I am not using the withoutMiddleware trait. Any ideas why that would be?

0 likes
5 replies
qwales1's avatar

@SaeedPrez ahhh thanks! I am sure there is a good reason for that, but am curious how I can test to make sure the form is able to be submitted without the tokenMatches check running.

SaeedPrez's avatar

@qwales1

Open /app/Http/Middleware/VerifyCsrfToken.php and add you modifications there. You can for example copy the handle() method and remove the $this->runningUnitTests() || line..

1 like

Please or to participate in this conversation.