Search code examples
phpauthenticationcakephp-1.3simpletest

Cakephp testing login


I want to test login function if it works propperly and only lets valid and active users in.

My user fixture contains:

array(
        'password' => '*emptyPasswordHash*', // empty password
        'username' => 'Lorem',
        'balance' => 0,
        'currency' => 'USD',
        'id' => 1,
        'user_group_id' => 3, //Customer
        'active' => 1,
        'hash' => 'LoremHash'
    ),

My test function looks like this:

function testLogin() {
            //test valid login
            $result = $this->testAction('/users/login', array(
                'data' => array(
                    'User' => array(
                        'username' => 'Lorem',
                        'pass' => '',
                        'remember' => true
                )),
                'method' => 'post',
                'return' => 'view'
            ));

            debug($result);

}

The login form has 3 inputs: username, password and remember

I have set $this->Auth->autoRedirect = false; in the UsersController::beforeFilter and I am doing some cookie setting stuff

when I debug($this->data); in UsersController::login() it shows the exact same data when testing and when logging normaly. But while testing the login fails and I get $this->Auth->loginError message instead of a login.

How do I test login action propperly?


Solution

  • if you use cakes Auth component and dont hack it up you dont need to...

    https://github.com/cakephp/cakephp/blob/master/cake/tests/cases/libs/controller/components/auth.test.php#L545

    and if you really want to, look at how the pro's do it :)