SecurityComponent黑洞我的控制器测试用例

Thi*_*lem 6 security phpunit unit-testing cakephp

这是我的UsersController测试用例:

<?php
App::uses('UsersController', 'Controller');

class TestUsersController extends UsersController {

    public $autoRender = false;

    public function redirect($url, $status = null, $exit = true) {
        $this->redirectUrl = $url;
    }

    public function render($action = null, $layout = null, $file = null) {
        $this->renderedAction = $action;
    }

    public function _stop($status = 0) {
        $this->stopped = $status;
    }
}

class UsersControllerTestCase extends ControllerTestCase {

    public $fixtures = array('app.user');

    public function setUp() {
        parent::setUp();
        $this->Users = new TestUsersController();
        $this->Users->constructClasses();
    }

    public function tearDown() {
        unset($this->Users);

        parent::tearDown();
    }

    public function testAdminSearchStudents() {
        $data = array('User' => array('search' => 'Ipsum'));
        $result = $this->testAction('/admin', array('return' => 'vars', 'method' => 'post', 'data' => $data));
        $this->assertCount(1, $result['users']);
    }

}
Run Code Online (Sandbox Code Playgroud)

我的UsersController没什么特别之处,但是它使用了SecurityComponent(继承自AppController).

当我运行测试时,我得到了臭名昭着的:

该请求已被黑洞测试案例:UsersControllerTestCase(testAdminSearchStudents)

我想这是因为我在没有CSRF令牌的情况下伪造了一个POST请求并且推荐了吗?

如果不从控制器中删除安全组件,我该怎么做才能完成这项工作?

我不确定这是否会有所帮助,但这是堆栈跟踪的重要部分:

/var/www/source/cakephp/lib/Cake/Controller/Component/SecurityComponent.php : 230
SecurityComponent::startup
/var/www/source/cakephp/lib/Cake/Utility/ObjectCollection.php : 130
ObjectCollection::trigger
/var/www/source/cakephp/lib/Cake/Event/CakeEventManager.php : 246
/var/www/source/cakephp/lib/Cake/Controller/Controller.php : 671
/var/www/source/cakephp/lib/Cake/Routing/Dispatcher.php : 183
/var/www/source/cakephp/lib/Cake/Routing/Dispatcher.php : 161
/var/www/source/cakephp/lib/Cake/TestSuite/ControllerTestCase.php : 271
ControllerTestCase::_testAction
/var/www/source/cakephp/lib/Cake/TestSuite/ControllerTestCase.php : 189
Run Code Online (Sandbox Code Playgroud)

问候

Thi*_*lem 2

我解决了模拟该方法的问题SecurityComponent::_validatePost

$this->Users = $this->generate('Users', array(
    'components' => array(
        'Security' => array('_validatePost'),
    )
));
Run Code Online (Sandbox Code Playgroud)

受到处理 CakePHP 2 测试用例中的安全组件的启发