How to test page if enabled FormProtection

I have enabled:

$this->loadComponent('FormProtection');

If run test, then get error:

Possibly related to `Cake\Http\Exception\BadRequestException`: "`_Token` was not found in request data."

Test

    public function testCsrfAppliedOk()
    {
        $this->enableCsrfToken();
        $this->post('/', ['hello' => 'world']);

        $this->assertThat(403, $this->logicalNot(new StatusCode($this->_response)));
        $this->assertResponseNotContains('CSRF');
    }

How to write a test in such a case if I have active FormProtection?

Thank You

https://book.cakephp.org/4/en/development/testing.html#testing-actions-protected-by-csrfcomponent-or-securitycomponent

1 Like

I put this $this->enableSecurityToken(); in and it works, thanks!