Add test for Icinga\User

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-22 09:43:53 +02:00
parent e10193f570
commit be410a685b
2 changed files with 86 additions and 55 deletions

View File

@ -29,18 +29,15 @@
namespace Icinga;
use \DateTimeZone;
use \Exception;
use \InvalidArgumentException;
use DateTimeZone;
use InvalidArgumentException;
use Icinga\User\Preferences;
use Icinga\User\Message;
use Icinga\Application\Config;
/**
* This class represents an authorized user
*
* You can retrieve authorization information (@TODO: Not implemented yet) or
* to retrieve user information
* You can retrieve authorization information (@TODO: Not implemented yet) or user information
*/
class User
{
@ -49,77 +46,77 @@ class User
*
* @var string
*/
private $username;
protected $username;
/**
* Firstname
*
* @var string
*/
private $firstname;
protected $firstname;
/**
* Lastname
*
* @var string
*/
private $lastname;
protected $lastname;
/**
* Users email address
*
* @var string
*/
private $email;
protected $email;
/**
* Domain
*
* @var string
*/
private $domain;
protected $domain;
/**
* More information about user
* More information about this user
*
* @var array
*/
private $additionalInformation = array();
protected $additionalInformation = array();
/**
* Set of permissions
*
* @var array
*/
private $permissions = array();
protected $permissions = array();
/**
* Set of restrictions
*
* @var array
*/
private $restrictions = array();
protected $restrictions = array();
/**
* Groups for this user
*
* @var array
*/
private $groups = array();
protected $groups = array();
/**
* Preferences object
*
* @var Preferences
*/
private $preferences;
protected $preferences;
/**
* Queued notifications for this user.
*
* @var array()
*/
private $messages;
protected $messages;
/**
* Creates a user object given the provided information
@ -166,6 +163,7 @@ class User
if ($this->preferences === null) {
$this->preferences = new Preferences();
}
return $this->preferences;
}
@ -181,6 +179,8 @@ class User
/**
* Set the groups this user belongs to
*
* @param array $groups
*/
public function setGroups(array $groups)
{
@ -191,6 +191,7 @@ class User
* Return true if the user is a member of this group
*
* @param string $group
*
* @return boolean
*/
public function isMemberOf($group)
@ -201,7 +202,7 @@ class User
/**
* Return permission information for this user
*
* @return Array
* @return array
*/
public function getPermissions()
{
@ -222,6 +223,7 @@ class User
* Return restriction information for this user
*
* @param string $name
*
* @return array
*/
public function getRestrictions($name)
@ -229,6 +231,7 @@ class User
if (array_key_exists($name, $this->restrictions)) {
return $this->restrictions[$name];
}
return array();
}
@ -316,6 +319,7 @@ class User
* Setter for mail
*
* @param string $mail
*
* @throws InvalidArgumentException When an invalid mail is provided
*/
public function setEmail($mail)
@ -370,6 +374,7 @@ class User
if (isset($this->additionalInformation[$key])) {
return $this->additionalInformation[$key];
}
return null;
}
@ -386,6 +391,7 @@ class User
if ($tz === null) {
$tz = date_default_timezone_get();
}
return new DateTimeZone($tz);
}
@ -404,7 +410,7 @@ class User
/**
* Get all currently pending messages
*
* @return array the messages
* @return array The messages
*/
public function getMessages()
{

View File

@ -4,9 +4,9 @@
namespace Tests\Icinga;
use \DateTimeZone;
use Mockery;
use DateTimeZone;
use Icinga\User;
use Icinga\User\Preferences;
use Icinga\Test\BaseTestCase;
class UserTest extends BaseTestCase
@ -14,9 +14,13 @@ class UserTest extends BaseTestCase
public function testGetDefaultTimezoneIfTimezoneNotSet()
{
$user = new User('unittest');
$prefs = new Preferences(array());
$prefs = Mockery::mock('Icinga\User\Preferences');
$prefs->shouldReceive('get')->with('timezone')->andReturnNull();
$user->setPreferences($prefs);
$this->assertEquals($user->getTimeZone(), new DateTimeZone(date_default_timezone_get()),
$this->assertEquals(
new DateTimeZone(date_default_timezone_get()),
$user->getTimeZone(),
'User\'s timezone does not match the default timezone'
);
}
@ -25,14 +29,35 @@ class UserTest extends BaseTestCase
{
$explicitTz = 'Europe/Berlin';
$user = new User('unittest');
$prefs = new Preferences(array(
'timezone' => $explicitTz
));
$prefs = Mockery::mock('Icinga\User\Preferences');
$prefs->shouldReceive('get')->with('timezone')->andReturn($explicitTz);
$user->setPreferences($prefs);
$this->assertEquals($user->getTimeZone(), new DateTimeZone($explicitTz),
$this->assertEquals(
new DateTimeZone($explicitTz),
$user->getTimeZone(),
'User\'s timezone does not match the timezone set by himself'
);
}
public function testWhetherValidEmailsCanBeSet()
{
$user = new User('unittest');
$user->setEmail('mySampleEmail@someDomain.org');
$this->assertEquals(
$user->getEmail(),
'mySampleEmail@someDomain.org',
'Valid emails set with setEmail are not returned by getEmail'
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testWhetherInvalidEmailsCannotBeSet()
{
$user = new User('unittest');
$user->setEmail('mySampleEmail at someDomain dot org');
}
}