【发布时间】:2015-04-11 07:09:53
【问题描述】:
我正在尝试对 ZF2 应用程序进行单元测试。我的表格会根据学生或员工的不同而略有变化,并且我有两个独立的工厂来生产它们。
无论如何,我尝试在以下测试中测试学生表格:
public function testStudentFormAcceptsValidValues()
{
$this->getEmMock();
$this->mockLogin('admin');
$form = $this->serviceManager->get('student_form');
$this->getApplicationServiceLocator()->setService('student_form', $form);
$url = '/user/account/add/student';
$this->dispatch($url);
$csrf = $form->get('csrf')->getValue();
$postData = $this->userStubData[1];
$postData['csrf'] = $csrf;
$user = new UserEntity();
$this->getApplicationServiceLocator()->setService('user_entity', $user);
$this->dispatch($url, 'POST', $postData);
$this->assertRedirectTo('/user#students');
$this->assertEntityPropertiesSet($user, $postData);
}
当我运行这个测试时,它通过了。但是,我正在对员工表单进行类似的测试,但由于 CSRF 令牌错误,它会失败。所以只是为了好玩,我决定连续两次运行上述测试,看看会发生什么。它再次失败,原因完全相同。
我最终决定检查这两个测试的 CSRF 令牌值,我发现它们都是相同的。出于某种原因,表单重新使用了之前测试中的 CSRF 值,而不是创建新值,因此表单拒绝了它。
我必须做什么才能清除旧的 CSRF 值并获得表单可以接受的新值?
【问题讨论】:
标签: php forms unit-testing zend-framework2 csrf-protection