cakephp控制器测试 - 如何测试需要授权的操作?

Elw*_*his 3 php unit-testing authorization simpletest cakephp-1.3

标题基本概括了所有内容.我想测试例如UsersController::admin_index()动作,但是需要授权用户访问此位置,因此当我运行测试时它会将我发送到登录页面,即使我手动登录,也没有进行任何测试.

那么如何在不编辑实际授权代码的情况下强制蛋糕跳过授权?

顺便说一句,如果有帮助,我的testAdminIndex()代码看起来像这样:

function testAdminIndex() {     
    $result = $this->testAction('/admin/users/index');      
    debug($result); 
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*ker 5

这里有一篇文章涵盖了这个主题......

http://mark-story.com/posts/view/testing-cakephp-controllers-the-hard-way

建议在为经过身份验证的用户添加会话值后,完全绕过"testAction"并手动执行请求.示例如下......

function testAdminEdit() {
    $this->Posts->Session->write('Auth.User', array(
        'id' => 1,
        'username' => 'markstory',
    ));
    $this->Posts->data = array(
        'Post' => array(
            'id' => 2,
            'title' => 'Best article Evar!',
            'body' => 'some text',
        ),
        'Tag' => array(
            'Tag' => array(1,2,3),
        )
    );
    $this->Posts->params = Router::parse('/admin/posts/edit/2');
    $this->Posts->beforeFilter();
    $this->Posts->Component->startup($this->Posts);
    $this->Posts->admin_edit();
}
Run Code Online (Sandbox Code Playgroud)