Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:19:25+00:00 2026-06-05T22:19:25+00:00

I want to test the login() action in my UsersController.php <?php class UsersController extends

  • 0

I want to test the login() action in my UsersController.php

<?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');
            }
        }
    }

The 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;
    }

}

The 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

    }

So, how would I test my login() method when the $this->testAction('/users/login', array('data' => $user, 'method' => 'post', 'return' => 'contents')); returns nothing?

OUTPUTS: I get “Invalid username or password, try again”

EXPECTED: A successful login message since I provided the correct credentials

Any reply would be greatly appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:19:26+00:00Added an answer on June 5, 2026 at 10:19 pm

    Thanks to @jeremyharris I was able to test my 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, I used the init() method – as @jeremyharris said regarding the hashed passwords.

    <?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();
        }
    }
    

    The first testAction() is a POST, then the second one gets the “next” page – from there I do the asserts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am using Django's form for my login page. I want to set focus to
If a logged in user goes to the login action, I want to redirect
I've created a login submit form in HTML but for some reason user/password autocompletion
When I create a controller in CodeIgniter, for example login.php. I create a public
I want to test login function if it works propperly and only lets valid
I want to test a given IP and login detail to see whether the
I am working on sencha2.0. I have one login form and I want to
I want to test my logic at Desktop (to eliminate pain of waiting until
I have a piece of logic I want to test and it uses dependency
Windows 7 Firefox 10.0.1 I have simple setTimeout logic that I want to test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.