Converting AuthTest to phpunit. Adding setter and getter method loggedIn()

This commit is contained in:
José Lorenzo Rodríguez 2010-06-10 00:03:49 -04:30
parent 837e2574a3
commit fa8df12fa0
2 changed files with 58 additions and 48 deletions

View file

@ -940,4 +940,18 @@ class AuthComponent extends Object {
$this->Session->delete('Auth.redirect');
}
}
/**
* Sets or gets whether the user is logged in
*
* @param boolean $logged sets the status of the user, true to logged in, false to logged out
* @return boolean true if the user is logged in, false otherwise
* @access public
*/
public function loggedIn($logged = null) {
if (!is_null($logged)) {
$this->_loggedIn = $logged;
}
return $this->_loggedIn;
}
}

View file

@ -21,8 +21,6 @@ App::import('Component', array('Auth', 'Acl'));
App::import('Model', 'DbAcl');
App::import('Core', 'Xml');
Mock::generate('AclComponent', 'AuthTestMockAclComponent');
/**
* TestAuthComponent class
*
@ -477,18 +475,20 @@ class AuthTest extends CakeTestCase {
* @access public
* @return void
*/
function startTest() {
function setUp() {
$this->_server = $_SERVER;
$this->_env = $_ENV;
$this->_securitySalt = Configure::read('Security.salt');
Configure::write('Security.salt', 'JfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
$this->_securityCipher = Configure::read('Security.cipherSeed');
Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
Configure::write('Security.cipherSeed', 770011223369876);
$this->_acl = Configure::read('Acl');
Configure::write('Acl.database', 'test_suite');
Configure::write('Acl.classname', 'DbAcl');
$this->Controller =& new AuthTestController();
$this->Controller = new AuthTestController();
$this->Controller->Component->init($this->Controller);
$this->Controller->Component->initialize($this->Controller);
$this->Controller->beforeFilter();
@ -509,11 +509,12 @@ class AuthTest extends CakeTestCase {
* @access public
* @return void
*/
function endTest() {
function tearDown() {
$_SERVER = $this->_server;
$_ENV = $this->_env;
Configure::write('Acl', $this->_acl);
Configure::write('Security.salt', $this->_securitySalt);
Configure::write('Security.cipherSeed', $this->_securityCipher);
$this->Controller->Session->delete('Auth');
$this->Controller->Session->delete('Message.auth');
@ -559,7 +560,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testLogin() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user['id'] = 1;
$user['username'] = 'mariano';
$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
@ -592,7 +593,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$this->assertFalse($user);
$this->assertNull($user);
$this->Controller->Session->delete('Auth');
$this->Controller->data['AuthUser']['username'] = 'now() or 1=1 --';
@ -601,7 +602,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$this->assertFalse($user);
$this->assertNull($user);
$this->Controller->Session->delete('Auth');
$this->Controller->data['AuthUser']['username'] = 'now() or 1=1 # something';
@ -610,7 +611,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$this->assertFalse($user);
$this->assertNull($user);
$this->Controller->Session->delete('Auth');
$this->Controller->Auth->userModel = 'UuidUser';
@ -656,7 +657,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testAuthorizeFalse() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->Auth->userModel = 'AuthUser';
@ -682,7 +683,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testAuthorizeController() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->Auth->userModel = 'AuthUser';
@ -706,7 +707,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testAuthorizeModel() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
@ -732,7 +733,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testAuthorizeCrud() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
@ -744,13 +745,13 @@ class AuthTest extends CakeTestCase {
$this->Controller->Acl->Aro->id = null;
$this->Controller->Acl->Aro->create(array('alias' => 'Roles'));
$result = $this->Controller->Acl->Aro->save();
$this->assertTrue($result);
$this->assertFalse(empty($result));
$parent = $this->Controller->Acl->Aro->id;
$this->Controller->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Admin'));
$result = $this->Controller->Acl->Aro->save();
$this->assertTrue($result);
$this->assertFalse(empty($result));
$parent = $this->Controller->Acl->Aro->id;
@ -758,17 +759,17 @@ class AuthTest extends CakeTestCase {
'model' => 'AuthUser', 'parent_id' => $parent, 'foreign_key' => 1, 'alias'=> 'mariano'
));
$result = $this->Controller->Acl->Aro->save();
$this->assertTrue($result);
$this->assertFalse(empty($result));
$this->Controller->Acl->Aco->create(array('alias' => 'Root'));
$result = $this->Controller->Acl->Aco->save();
$this->assertTrue($result);
$this->assertFalse(empty($result));
$parent = $this->Controller->Acl->Aco->id;
$this->Controller->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'AuthTest'));
$result = $this->Controller->Acl->Aco->save();
$this->assertTrue($result);
$this->assertFalse(empty($result));
$this->Controller->Acl->allow('Roles/Admin', 'Root');
$this->Controller->Acl->allow('Roles/Admin', 'Root/AuthTest');
@ -793,14 +794,14 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testAuthorizeActions() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->params['controller'] = 'auth_test';
$this->Controller->params['action'] = 'add';
$this->Controller->Acl =& new AuthTestMockAclComponent();
$this->Controller->Acl->setReturnValue('check', true);
$this->Controller->Acl = $this->getMock('AclComponent');
$this->Controller->Acl->expects($this->atLeastOnce())->method('check')->will($this->returnValue(true));
$this->Controller->Auth->initialize($this->Controller);
@ -808,9 +809,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->authorize = 'actions';
$this->Controller->Auth->actionPath = 'Root/';
$this->Controller->Acl->expectAt(0, 'check', array(
$user, 'Root/AuthTest/add'
));
$this->Controller->Acl->expects($this->at(0))->method('check')->with($user, 'Root/AuthTest/add');
$this->Controller->Auth->startup($this->Controller);
$this->assertTrue($this->Controller->Auth->isAuthorized());
@ -1139,7 +1138,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testEmptyUsernameOrPassword() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user['id'] = 1;
$user['username'] = 'mariano';
$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
@ -1170,7 +1169,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testInjection() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$this->AuthUser->id = 2;
$this->AuthUser->saveField('password', Security::hash(Configure::read('Security.salt') . 'cake'));
@ -1243,19 +1242,16 @@ class AuthTest extends CakeTestCase {
$expected = $data;
$expected['AuthUser']['password'] = Security::hash($expected['AuthUser']['password'], null, true);
$this->assertEqual($return, $expected);
if (PHP5) {
$xml = array(
'User' => array(
'username' => 'batman@batcave.com',
'password' => 'bruceWayne',
)
);
$data =& new Xml($xml);
$return = $this->Controller->Auth->hashPasswords($data);
$expected = $data;
$this->assertEqual($return, $expected);
}
$xml = array(
'User' => array(
'username' => 'batman@batcave.com',
'password' => 'bruceWayne',
)
);
$data = new Xml($xml);
$return = $this->Controller->Auth->hashPasswords($data);
$expected = $data;
$this->assertEqual($return, $expected);
}
/**
@ -1279,7 +1275,7 @@ class AuthTest extends CakeTestCase {
'argSeparator' => ':', 'namedArgs' => array()
)));
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = array(
'id' => 1, 'username' => 'felix',
'password' => Security::hash(Configure::read('Security.salt') . 'cake'
@ -1325,7 +1321,7 @@ class AuthTest extends CakeTestCase {
function testCustomField() {
Router::reload();
$this->AuthUserCustomField =& new AuthUserCustomField();
$this->AuthUserCustomField = new AuthUserCustomField();
$user = array(
'id' => 1, 'email' => 'harking@example.com',
'password' => Security::hash(Configure::read('Security.salt') . 'cake'
@ -1404,7 +1400,7 @@ class AuthTest extends CakeTestCase {
), true);
App::objects('plugin', null, false);
$PluginModel =& ClassRegistry::init('TestPlugin.TestPluginAuthUser');
$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
$user['id'] = 1;
$user['username'] = 'gwoo';
$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
@ -1453,7 +1449,7 @@ class AuthTest extends CakeTestCase {
}
ob_start();
$Dispatcher =& new Dispatcher();
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch('/ajax_auth/add', array('return' => 1));
$result = ob_get_clean();
$this->assertEqual("Ajax!\nthis is the test element", $result);
@ -1505,9 +1501,9 @@ class AuthTest extends CakeTestCase {
*/
function testShutDown() {
$this->Controller->Session->write('Auth.redirect', 'foo');
$this->Controller->Auth->_loggedIn = true;
$this->Controller->Auth->loggedIn(true);
$this->Controller->Auth->shutdown($this->Controller);
$this->assertFalse($this->Controller->Session->read('Auth.redirect'));
$this->assertNull($this->Controller->Session->read('Auth.redirect'));
}
/**
@ -1537,7 +1533,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testComponentSettings() {
$this->Controller =& new AuthTestController();
$this->Controller = new AuthTestController();
$this->Controller->components = array(
'Auth' => array(
'fields' => array('username' => 'email', 'password' => 'password'),
@ -1551,7 +1547,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Component->initialize($this->Controller);
Router::reload();
$this->AuthUserCustomField =& new AuthUserCustomField();
$this->AuthUserCustomField = new AuthUserCustomField();
$user = array(
'id' => 1, 'email' => 'harking@example.com',
'password' => Security::hash(Configure::read('Security.salt') . 'cake'