Skip to content

Commit

Permalink
Converting AuthTest to phpunit. Adding setter and getter method logge…
Browse files Browse the repository at this point in the history
…dIn()
  • Loading branch information
lorenzo committed Jun 10, 2010
1 parent 837e257 commit fa8df12
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 48 deletions.
14 changes: 14 additions & 0 deletions cake/libs/controller/components/auth.php
Expand Up @@ -940,4 +940,18 @@ public function shutdown(&$controller) {
$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;
}
}
92 changes: 44 additions & 48 deletions cake/tests/cases/libs/controller/components/auth.test.php
Expand Up @@ -21,8 +21,6 @@
App::import('Model', 'DbAcl');
App::import('Core', 'Xml');

Mock::generate('AclComponent', 'AuthTestMockAclComponent');

/**
* TestAuthComponent class
*
Expand Down Expand Up @@ -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();
Expand All @@ -509,11 +509,12 @@ function startTest() {
* @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');
Expand Down Expand Up @@ -559,7 +560,7 @@ function testIsErrorOrTests() {
* @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');
Expand Down Expand Up @@ -592,7 +593,7 @@ function testLogin() {
$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 --';
Expand All @@ -601,7 +602,7 @@ function testLogin() {
$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';
Expand All @@ -610,7 +611,7 @@ function testLogin() {
$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';
Expand Down Expand Up @@ -656,7 +657,7 @@ function testLoginActionNotSettingAuthRedirect() {
* @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';
Expand All @@ -682,7 +683,7 @@ function testAuthorizeFalse() {
* @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';
Expand All @@ -706,7 +707,7 @@ function testAuthorizeController() {
* @return void
*/
function testAuthorizeModel() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);

Expand All @@ -732,7 +733,7 @@ function testAuthorizeModel() {
* @return void
*/
function testAuthorizeCrud() {
$this->AuthUser =& new AuthUser();
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);

Expand All @@ -744,31 +745,31 @@ function testAuthorizeCrud() {
$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;

$this->Controller->Acl->Aro->create(array(
'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');
Expand All @@ -793,24 +794,22 @@ function testAuthorizeCrud() {
* @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);

$this->Controller->Auth->userModel = 'AuthUser';
$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());
Expand Down Expand Up @@ -1139,7 +1138,7 @@ function testNoRedirectOn404() {
* @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');
Expand Down Expand Up @@ -1170,7 +1169,7 @@ function testEmptyUsernameOrPassword() {
* @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'));

Expand Down Expand Up @@ -1243,19 +1242,16 @@ function testHashPasswords() {
$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);
}

/**
Expand All @@ -1279,7 +1275,7 @@ function testCustomRoute() {
'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'
Expand Down Expand Up @@ -1325,7 +1321,7 @@ function testCustomRoute() {
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'
Expand Down Expand Up @@ -1404,7 +1400,7 @@ function testPluginModel() {
), 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');
Expand Down Expand Up @@ -1453,7 +1449,7 @@ function testAjaxLogin() {
}

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);
Expand Down Expand Up @@ -1505,9 +1501,9 @@ function testLoginActionRedirect() {
*/
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'));
}

/**
Expand Down Expand Up @@ -1537,7 +1533,7 @@ function testInitializeAndRoutingPrefixes() {
* @return void
*/
function testComponentSettings() {
$this->Controller =& new AuthTestController();
$this->Controller = new AuthTestController();
$this->Controller->components = array(
'Auth' => array(
'fields' => array('username' => 'email', 'password' => 'password'),
Expand All @@ -1551,7 +1547,7 @@ function testComponentSettings() {
$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'
Expand Down

0 comments on commit fa8df12

Please sign in to comment.