Here's my UsersController test case:
<?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']);
}
}
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:
The request has been black-holed Test case: UsersControllerTestCase(testAdminSearchStudents)
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
Regards
I solved the problem mocking the the SecurityComponent::_validatePost
method:
$this->Users = $this->generate('Users', array(
'components' => array(
'Security' => array('_validatePost'),
)
));
Inspired by Dealing with Security component in a CakePHP 2 test case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With