本文介绍了如何为Cakephp 3 PHPunit测试创建CSRF令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在CakePHP 3应用程序中启用CSRF令牌和SSL之后,我试图使单元测试再次正常工作。
I am trying to get my unit tests working again after enabling CSRF tokens and SSL in my CakePHP 3 app.
如何为a创建或生成令牌?测试如下?还是出于测试目的而禁用它?
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' => '[email protected]',
'password' => 'secret'
];
$this->post('/login', $data);
$this->assertResponseSuccess();
$this->assertRedirect(['controller' => 'Users', 'action' => 'dashboard']);
}
推荐答案
官方文档。
您只需调用 $ this-> enableCsrfToken();
和/或 $ this-> enableSecurityToken();
,以便您能够成功使用令牌成功执行请求。
You only have to call $this->enableCsrfToken();
and/or $this->enableSecurityToken();
before your post to be able to perform the request successfully with token.
如官方示例所示:
public function testAdd()
{
$this->enableCsrfToken();
$this->enableSecurityToken();
$this->post('/posts/add', ['title' => 'Exciting news!']);
}
这篇关于如何为Cakephp 3 PHPunit测试创建CSRF令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!