unit-testing - CakePHP2 测试登录()

标签 unit-testing cakephp phpunit cakephp-2.0 cakephp-2.1

我想在我的 UsersController.php 中测试 login() 操作

<?php
class UsersController extends AppController {
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler');

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout', 'login');
        // $this->Auth->allow('logout');
        $this->Security->csrfExpires = '+1 hour';
    }

    public function login() {
        if($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'),'info');
            }
        }
    }

AppController.php

<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {

    public $components  = array(
        'Session',
        'Security',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'dashboards', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'dashboards', 'action' => 'welcome'),
            'authorize' => array('Controller')
        )
    );

    public function isAuthorized($user) {
        //Admin can access every action
        if (isset($user['role']) && $user['role'] === 'admin') {
            return true;
        }

        //Default deny
        $this->Session->setFlash('You are not allowed to access the requested page');
        return false;
    }

}

UsersControllerTest.php

<?php

class UsersControllerTest extends ControllerTestCase {
    public $autoRender = false;
    public $fixtures = array('app.user','app.account');

    public function setUp() {
        parent::setUp();
        $this->User = ClassRegistry::init('User');
    }

...snip...
    public function testLogin() {

        $this->Users = $this->generate('Users', array(
            'components' => array(
                //'Session',
                'Security' => array('_validatePost'),
            )
        ));
        $this->Users->Security->expects($this->any())
             ->method('_validatePost')
             ->will($this->returnValue(true));

        $user = array();
        $user['User']['username'] = 'admin';
        //$user['User']['password'] = Security::hash('test', null, true);
        $user['User']['password'] = 'test';

        $result = $this->testAction('/users/login',
            array('data' => $user, 'method' => 'post', 'return' => 'contents')
        );

        debug( $this->contents); 
        //OUTPUTS: I get "Invalid username or password, try again"
        //EXPECTED: A successful login message since I provided the correct credentials

    }

那么,当 $this->testAction('/users/login', array('data' => $user, 'method' => 'post', 'return' => 'contents')); 什么都不返回?

输出:我收到“无效的用户名或密码,请重试”

预期:由于我提供了正确的凭据而出现的成功登录消息

如有任何回复,我们将不胜感激。谢谢!

最佳答案

感谢@jeremyharris,我能够测试我的 login()

UsersControllerTest.php

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

    $data = array();
    $data['User']['username'] = 'admin';
    $data['User']['password'] = 'test';

    $this->Users->Auth->logout();
    $this->testAction('/users/login',
        array('data' => $data, 'method' => 'post', 'return' => 'contents')
    );

    $result = $this->testAction('/',
        array('method' => 'get', 'return' => 'contents')
    );

    // debug($result); 
    $this->assertContains('You are logged in as: <span class="label">admin</span>',$result);
}

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

    $data = array();
    $data['User']['username'] = 'admin';
    $data['User']['password'] = 'BLAH!';

    $this->Users->Auth->logout();
    $this->testAction('/users/login',
        array('data' => $data, 'method' => 'post', 'return' => 'contents')
    );

    $result = $this->testAction('/users/login',
        array('method' => 'get', 'return' => 'contents')
    );

    // debug($result); 
    $this->assertNotContains('You are logged in as',$result);
    $this->assertContains('id="UserLoginForm" method="post"',$result);
}

UserFixture.php,我使用了 init() 方法 - 正如@jeremyharris 所说的关于散列密码。

<?php
App::uses('AuthComponent', 'Controller/Component');

class UserFixture extends CakeTestFixture {
    /* Optional. Set this property to load fixtures to a different test datasource */
    public $useDbConfig = 'test';

    public $fields = array(
        'id' => array('type' => 'integer', 'key' => 'primary'),
        'account_id' => array('type' => 'integer'),
        'username' => array('type' => 'string', 'length' => 255, 'null' => false),
        'email' => array('type' => 'string', 'length' => 255, 'null' => false),
        'password' => array('type' => 'string', 'length' => 255, 'null' => false),
        'password_token' => array('type' => 'string', 'length' => 255, 'null' => false),
        'password_token_expiry' => array('type' => 'string', 'length' => 255, 'null' => false),
        'role' => array('type' => 'string', 'length' => 25, 'null' => false),
        'created' => 'datetime',
        'modified' => 'datetime'
    );

/*    public $records = array(
        array('id'=>1, 'account_id' => 1, 'username' => 'admin', 'email' => 'admin@test.com', 'password' => 'f57f702f8d557ae5318fa49455cbe9838c1d1712', 'role' => 'admin', 'password_token'=>'', 'password_token_expiry'=>'','created' => '2012-03-18 10:39:23', 'modified' => '2012-03-18 10:41:31'),
        array('id'=>2, 'account_id' => 1, 'username' => 'user', 'email' => 'user@test.com', 'password' => 'f57f702f8d557ae5318fa49455cbe9838c1d1712', 'role' => 'user', 'password_token'=>'', 'password_token_expiry'=>'', 'created' => '2012-03-18 10:39:23', 'modified' => '2012-03-18 10:41:31')
    );
*/
     public function init() {
        $this->records = array(
            array('id'=>1, 'account_id' => 1, 'username' => 'admin', 'email' => 'admin@test.com', 'password' => AuthComponent::password('test'), 'role' => 'admin', 'password_token'=>'', 'password_token_expiry'=>'','created' => '2012-03-18 10:39:23', 'modified' => '2012-03-18 10:41:31'),
            array('id'=>2, 'account_id' => 1, 'username' => 'user', 'email' => 'user@test.com', 'password' => AuthComponent::password('test'), 'role' => 'user', 'password_token'=>'', 'password_token_expiry'=>'','created' => '2012-03-18 10:39:23', 'modified' => '2012-03-18 10:41:31'),
        );
        parent::init();
    }
}

第一个 testAction() 是一个 POST,然后第二个获取“下一个”页面 - 从那里我做断言。

关于unit-testing - CakePHP2 测试登录(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076100/

相关文章:

java - 如何将模拟对象注入(inject)到构造函数中?

JavaScript - Jest/mock 中的模拟控制台 "was not called"

CakePHP 插件模型关联不起作用

php - CakePHP 性能问题 - 30-50 秒的模式加载

netbeans - 尝试将 PHPUnit 与 NetBeans 和 Xampp 结合使用时获取 "Could not open input file"

php - Laravel 在测试具有许多依赖项的模型之前准备数据库,有没有更好的方法?

PHPUnit:syntaxCheck 配置参数究竟代表什么

unit-testing - 在 go 中测试连接的正确方法

python - 是否可以用现有的 Singleton 实例修补类实例?

php - 除非在 CakePHP 中启用调试,否则为什么我的索引页面不会出现?