2012-06-18 100 views
3

我想在我的UsersController.php測試登錄()動作CakePHP2測試登錄()

<?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'));什麼都不返回時測試我的login()方法?

輸出:我得到「無效的用戶名或密碼,請重試」

預期:成功的登錄信息,因爲我提供正確的憑據

任何答覆將不勝感激。謝謝!

回答

4

感謝@jeremyharris我能夠測試我的登錄()

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' => '[email protected]', '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' => '[email protected]', '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' => '[email protected]', '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' => '[email protected]', '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,那麼第二個獲得「下一個」頁面 - 從那裏我做的斷言。

0

問題是你在嘲笑整個Session組件。這意味着所有會話方法都將返回null。從$this->generate()刪除'會話'鍵,你應該很好。

+0

感謝您的回覆jeremyharris。但我仍然得到錯誤的結果。請參閱http://pastie.org/4110786 – wenbert

+1

在將密碼傳遞給登錄操作之前,您不應該對密碼進行哈希處理。稍後由認證過程完成。另外,你沒有設置任何東西,所以'$ this-> vars'將一直是空的。當它通過時,頭部不會。如果您調試會話,您應該看到「Invalid username ...」消息。 – jeremyharris

+0

謝謝@jeremyharris。這就說得通了。但是,我遇到了一個新問題,現在我得到「無效的用戶名/密碼」而不是「傳遞」登錄名。請參閱上面的更新代碼。 – wenbert