gpt4 book ai didi

SecurityComponent 黑洞我的 Controller 测试用例

转载 作者:行者123 更新时间:2023-12-02 19:43:40 25 4
gpt4 key购买 nike

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

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

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

我认为这是因为我在没有 CSRF token 和引用的情况下伪造了 POST 请求?

我应该怎样做才能在不从 Controller 中删除安全组件的情况下完成这项工作?

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

/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'),
)
));

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

关于SecurityComponent 黑洞我的 Controller 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12117598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com