mercredi 9 août 2017

CakePHP 3 - Testing controller actions with SecurityComponent unlockedFields

I'm trying to run some unit tests on a controller action that uses some unlockedFields for the SecurityComponent and keep getting an Invalid security debug token Exception.

Is there any easy way to test an action protected by the SecurityComponent that use some unlockedFields ?

Controller:

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);

    $action = $this->request->getParam('action');
    switch ($action) {
        case 'add':
            $this->Security->config('unlockedFields', ['people']);
            break;
    }
}

Test:

$this->enableCsrfToken();
$this->enableSecurityToken();

$data = [
    'title'  => 'My title',
    'people' => ['', 1]
];

$this->post('/deposits/add/', $data);

Please note that if I remove 'people' from the data the test runs fine.

Aucun commentaire:

Enregistrer un commentaire