我想给测试控制器方法(添加,编辑,...)使用安全组件。
ContactsController
public function initialize() {
$this->loadComponent('Security');
}
public function add() {
$contact = $this->Contacts->newEntity();
if ($this->request->is('post')) {
$contact = $this->Contacts->patchEntity($contact, $this->request->data);
if ($this->Contacts->save($contact)) {
$this->Flash->success(__d('contact_manager', 'The contact has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__d('contact_manager', 'The contact could not be saved. Please, try again.'));
}
}
$this->set(compact('contact'));
$this->set('_serialize', ['contact']);
}
ContactsControllerTest
public function testAdd() {
$data = $this->_getData();
$this->post('/contacts/add', $data);
$this->assertResponseSuccess();
$query = $this->Contacts->find()->where([
'Profiles.lastname' => $data['profile']['lastname'],
'Profiles.firstname' => $data['profile']['firstname']
]);
$this->assertEquals(1, $query->count());
}
protected function _getData() {
$data = [
'id' => '',
'organization_id' => 2,
'profile_id' => '',
'profile' => [
'lastname' => 'Demo',
'firstname' => 'Demo',
'gender' => 'f',
'birthday' => '1990-05-20',
'email' => 'demo@demo.com',
'phone' => '0102030405',
'phone_mobile' => '0607080900'
]
];
return $data;
}
testAdd()
总是失败,因为请求是黑色带孔(与“验证”指标),但add()
在浏览器中运行良好。