【问题标题】:SecurityComponent black-holing my controller test caseSecurityComponent 黑洞我的控制器测试用例
【发布时间】:2012-08-20 12:02:38
【问题描述】:

这是我的 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']);
    }

}

我的 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

问候

【问题讨论】:

  • 您好,我遇到了同样的问题,但我认为问题与发布请求无关,而是与返回类型有关。当我将返回类型从默认的“result”更改为“vars”或其他时,我在任何 get-request 上都会遇到相同的错误,即使安全组件处于活动状态,post 请求也可以正常工作。 =/

标签: security unit-testing cakephp phpunit


【解决方案1】:

我通过SecurityComponent::_validatePost 方法模拟解决了问题:

$this->Users = $this->generate('Users', array(
    'components' => array(
        'Security' => array('_validatePost'),
    )
));

灵感来自Dealing with Security component in a CakePHP 2 test case

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多