Fix user() return value for nested data

This commit is contained in:
Ceeram 2012-05-31 14:40:23 +02:00
parent 119377422e
commit 6c9b2a1fec
2 changed files with 35 additions and 2 deletions

View file

@ -569,8 +569,8 @@ class AuthComponent extends Component {
if ($key === null) {
return $user;
}
if (isset($user[$key])) {
return $user[$key];
if ($value = Hash::get($user, $key)) {
return $value;
}
return null;
}

View file

@ -1254,4 +1254,37 @@ class AuthComponentTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* testUser method
*
* @return void
*/
public function testUser() {
$data = array(
'User' => array(
'id' => '2',
'username' => 'mark',
'group_id' => 1,
'Group' => array(
'id' => '1',
'name' => 'Members'
),
));
$this->Auth->Session->write('Auth', $data);
$result = $this->Auth->user();
$this->assertEquals($data['User'], $result);
$result = $this->Auth->user('username');
$this->assertEquals($data['User']['username'], $result);
$result = $this->Auth->user('Group.name');
$this->assertEquals($data['User']['Group']['name'], $result);
$result = $this->Auth->user('invalid');
$this->assertEquals(null, $result);
$result = $this->Auth->user('Company.invalid');
$this->assertEquals(null, $result);
}
}