1

I am trying to get my unit tests working again after enabling CSRF tokens and SSL in my CakePHP 3 app.

How do I create or generate a token for a test like the following? Or do I just disable it for testing purposes?

public function testLogin() {
    $this->get('/login');
    $this->assertResponseOk();

    $data = [
        'email' => 'info@example.com',
        'password' => 'secret'
    ];
    $this->post('/login', $data);

    $this->assertResponseSuccess();
    $this->assertRedirect(['controller' => 'Users', 'action' => 'dashboard']);
}
lorem monkey
  • 3,942
  • 3
  • 35
  • 49

1 Answers1

4

The official documentation has good approach since version 3.1.2.

You only have to call $this->enableCsrfToken(); and/or $this->enableSecurityToken(); before your post to be able to perform the request successfully with token.

As the official example shows:

public function testAdd()
{
    $this->enableCsrfToken();
    $this->enableSecurityToken();
    $this->post('/posts/add', ['title' => 'Exciting news!']);
}
lorem monkey
  • 3,942
  • 3
  • 35
  • 49
Genar
  • 433
  • 3
  • 22