SecurityComponent黑洞我的控制器测试用例 [英] SecurityComponent black-holing my controller test case

查看:175
本文介绍了SecurityComponent黑洞我的控制器测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 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)。

There's nothing special about my UsersController, but it uses the SecurityComponent (inherited from AppController).

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

And when I run the tests, i get the infamous:


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

The request has been black-holed Test case: UsersControllerTestCase(testAdminSearchStudents)

我认为这是因为我伪造一个POST请求没有CSRF令牌和引用?

I think it's because i'm forging a POST request without the CSRF token and referal?

我应该做什么,使这项工作

What should I do to make this work without removing the Security component from my Controller?

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

I'm not sure if this will help, but here's the important part of the stack trace:

/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

回答

推荐答案

我解决了嘲笑 SecurityComponent :: _ validatePost 方法:

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

灵感来自在CakePHP 2测试用例中处理Security组件

这篇关于SecurityComponent黑洞我的控制器测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆