Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security CakePHP-控制器测试因安全组件而失败_Security_Unit Testing_Cakephp_Cakephp 3.0 - Fatal编程技术网

Security CakePHP-控制器测试因安全组件而失败

Security CakePHP-控制器测试因安全组件而失败,security,unit-testing,cakephp,cakephp-3.0,Security,Unit Testing,Cakephp,Cakephp 3.0,我正在尝试测试使用安全组件的控制器方法(添加、编辑等) 联系人控制器 public function initialize() { $this->loadComponent('Security'); } public function add() { $contact = $this->Contacts->newEntity(); if ($this->request->is('post')) { $contact = $t

我正在尝试测试使用安全组件的控制器方法(添加、编辑等)

联系人控制器

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']);
}
联系人控制器测试

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()
总是失败,因为请求是黑色的(带有“Auth”指示器),但是
add()
在浏览器中运行良好

这是意料之中的,因为您没有发送必要的安全令牌,而这正是安全组件所需要的

只需查看您生成的表单,它将包含
\u令牌
字段的隐藏输入,以及子键
字段
解锁
,其中
字段
将包含散列,可能还包含锁定字段的名称,
解锁
保存未锁定字段的名称

只需将令牌数据添加到您的请求中,一切都会很好。这里有一个例子

$data = [
    'id' => '',
    'organization_id' => 2,
    'profile_id' => '',
    'profile' => [
        'lastname' => 'Demo',
        // ...
    ],
    '_Token' => [
        'fields' => 'e87e3ad9579abcd289ccec2a7a42065b338cacd0%3Aid'
        'unlocked' => ''
    ]
];
请注意,
unlocked
键必须存在,即使它不包含任何数据

您应该能够简单地从生成的表单复制令牌值,如果您对令牌的生成和验证方式感兴趣,请查看


另请参见预期的

,因为您没有发送必要的安全令牌,而这正是安全组件所需要的

只需查看您生成的表单,它将包含
\u令牌
字段的隐藏输入,以及子键
字段
解锁
,其中
字段
将包含散列,可能还包含锁定字段的名称,
解锁
保存未锁定字段的名称

只需将令牌数据添加到您的请求中,一切都会很好。这里有一个例子

$data = [
    'id' => '',
    'organization_id' => 2,
    'profile_id' => '',
    'profile' => [
        'lastname' => 'Demo',
        // ...
    ],
    '_Token' => [
        'fields' => 'e87e3ad9579abcd289ccec2a7a42065b338cacd0%3Aid'
        'unlocked' => ''
    ]
];
请注意,
unlocked
键必须存在,即使它不包含任何数据

您应该能够简单地从生成的表单复制令牌值,如果您对令牌的生成和验证方式感兴趣,请查看


另请参见《蛋糕3.1.2》,因为最好的方法是添加

$this->enableCsrfToken();
$this->enableSecurityToken();
到您的TestFunction


Doku:

因为Cake 3.1.2,最好的方法是添加

$this->enableCsrfToken();
$this->enableSecurityToken();
到您的TestFunction


Doku:

我在
$data
中添加了带有子键的
\u Token
字段,因此
$data
包含与生成表单完全相同的键。但是
testAdd()
再次失败(使用“auth”blackhole)。@Lacos然后我建议您进行一些调试,侵入安全组件,检查它的确切释放位置,检索什么值,生成的比较哈希与您传递的哈希相比是什么样的,等等。。。从这里我真的无能为力。非常感谢。由于用于生成令牌的当前请求URL,我的测试总是失败。如果我在控制台(/contacts/add)中启动测试,如果我在浏览器(/myapp/contacts/add)中使用表单,则此url不相同。我已添加了带有子键的
$data
字段,因此
$data
包含与生成表单完全相同的键。但是
testAdd()
再次失败(使用“auth”blackhole)。@Lacos然后我建议您进行一些调试,侵入安全组件,检查它的确切释放位置,检索什么值,生成的比较哈希与您传递的哈希相比是什么样的,等等。。。从这里我真的无能为力。非常感谢。由于用于生成令牌的当前请求URL,我的测试总是失败。如果我在控制台(/contacts/add)中启动我的测试,并且如果我在浏览器(/myapp/contacts/add)中使用表单,则此url不相同。我得到的是在请求数据中找不到令牌,这将修复它。非常感谢。我得到的是_在请求数据中找不到令牌,这修复了它。非常感谢。