phpunit-testingcakephp-2.3

Cakephp 2.3 unit test params['requested']


I'm trying to test a controller action that is only supposed to be called from an element like this:

$notes = $this->requestAction(array(
        'controller' => 'notes'
    ) , array(
        'pass' => array(
            'location' => $requestUrl
        )
    ));

On the action itself, there is a check to make sure the action is 'requested':

public function index() {
    if (!empty($this->params['requested'])) {
        ...
        return $notes;
    } else {
        throw new ForbiddenException();
    }
}

How can I test the above code? The following:

$this->testAction('/notes', array(

                'passed' => array('location'=>'location1'),

                'return' => 'vars'

            ));

triggers a ForbiddenException. I tried using $this->generate but I'm not sure how should I generate the $this->controller->params object.


Solution

  • Thank you @nanoman for your response. However, I found a simpler solution (don't know why I thought about this before). Writing it here for future reference.

    Just call requestAction from the test!

    function testMytest() {
        $this->controller = $this->generate('Notes', array(
               'components' => array(
                    'Auth'
                )
            ));
        $notes = $this->controller->requestAction(array(
            'controller' => 'notes'
        ) , array(
            'pass' => array(
                'location' => 'location1'
            )
        ));
    }