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; namespace Icinga;
use \DateTimeZone; use DateTimeZone;
use \Exception; use InvalidArgumentException;
use \InvalidArgumentException;
use Icinga\User\Preferences; use Icinga\User\Preferences;
use Icinga\User\Message; use Icinga\User\Message;
use Icinga\Application\Config;
/** /**
* This class represents an authorized user * This class represents an authorized user
* *
* You can retrieve authorization information (@TODO: Not implemented yet) or * You can retrieve authorization information (@TODO: Not implemented yet) or user information
* to retrieve user information
*/ */
class User class User
{ {
@ -49,85 +46,85 @@ class User
* *
* @var string * @var string
*/ */
private $username; protected $username;
/** /**
* Firstname * Firstname
* *
* @var string * @var string
*/ */
private $firstname; protected $firstname;
/** /**
* Lastname * Lastname
* *
* @var string * @var string
*/ */
private $lastname; protected $lastname;
/** /**
* Users email address * Users email address
* *
* @var string * @var string
*/ */
private $email; protected $email;
/** /**
* Domain * Domain
* *
* @var string * @var string
*/ */
private $domain; protected $domain;
/** /**
* More information about user * More information about this user
* *
* @var array * @var array
*/ */
private $additionalInformation = array(); protected $additionalInformation = array();
/** /**
* Set of permissions * Set of permissions
* *
* @var array * @var array
*/ */
private $permissions = array(); protected $permissions = array();
/** /**
* Set of restrictions * Set of restrictions
* *
* @var array * @var array
*/ */
private $restrictions = array(); protected $restrictions = array();
/** /**
* Groups for this user * Groups for this user
* *
* @var array * @var array
*/ */
private $groups = array(); protected $groups = array();
/** /**
* Preferences object * Preferences object
* *
* @var Preferences * @var Preferences
*/ */
private $preferences; protected $preferences;
/** /**
* Queued notifications for this user. * Queued notifications for this user.
* *
* @var array() * @var array()
*/ */
private $messages; protected $messages;
/** /**
* Creates a user object given the provided information * Creates a user object given the provided information
* *
* @param string $username * @param string $username
* @param string $firstname * @param string $firstname
* @param string $lastname * @param string $lastname
* @param string $email * @param string $email
*/ */
public function __construct($username, $firstname = null, $lastname = null, $email = null) public function __construct($username, $firstname = null, $lastname = null, $email = null)
{ {
@ -149,7 +146,7 @@ class User
/** /**
* Setter for preferences * Setter for preferences
* *
* @param Preferences $preferences * @param Preferences $preferences
*/ */
public function setPreferences(Preferences $preferences) public function setPreferences(Preferences $preferences)
{ {
@ -159,20 +156,21 @@ class User
/** /**
* Getter for preferences * Getter for preferences
* *
* @return Preferences * @return Preferences
*/ */
public function getPreferences() public function getPreferences()
{ {
if ($this->preferences === null) { if ($this->preferences === null) {
$this->preferences = new Preferences(); $this->preferences = new Preferences();
} }
return $this->preferences; return $this->preferences;
} }
/** /**
* Return all groups this user belongs to * Return all groups this user belongs to
* *
* @return array * @return array
*/ */
public function getGroups() public function getGroups()
{ {
@ -181,6 +179,8 @@ class User
/** /**
* Set the groups this user belongs to * Set the groups this user belongs to
*
* @param array $groups
*/ */
public function setGroups(array $groups) public function setGroups(array $groups)
{ {
@ -190,8 +190,9 @@ class User
/** /**
* Return true if the user is a member of this group * Return true if the user is a member of this group
* *
* @param string $group * @param string $group
* @return boolean *
* @return boolean
*/ */
public function isMemberOf($group) public function isMemberOf($group)
{ {
@ -201,7 +202,7 @@ class User
/** /**
* Return permission information for this user * Return permission information for this user
* *
* @return Array * @return array
*/ */
public function getPermissions() public function getPermissions()
{ {
@ -211,7 +212,7 @@ class User
/** /**
* Setter for permissions * Setter for permissions
* *
* @param array $permissions * @param array $permissions
*/ */
public function setPermissions(array $permissions) public function setPermissions(array $permissions)
{ {
@ -222,6 +223,7 @@ class User
* Return restriction information for this user * Return restriction information for this user
* *
* @param string $name * @param string $name
*
* @return array * @return array
*/ */
public function getRestrictions($name) public function getRestrictions($name)
@ -229,13 +231,14 @@ class User
if (array_key_exists($name, $this->restrictions)) { if (array_key_exists($name, $this->restrictions)) {
return $this->restrictions[$name]; return $this->restrictions[$name];
} }
return array(); return array();
} }
/** /**
* Settter for restrictions * Settter for restrictions
* *
* @param array $restrictions * @param array $restrictions
*/ */
public function setRestrictions(array $restrictions) public function setRestrictions(array $restrictions)
{ {
@ -245,7 +248,7 @@ class User
/** /**
* Getter for username * Getter for username
* *
* @return string * @return string
*/ */
public function getUsername() public function getUsername()
{ {
@ -255,7 +258,7 @@ class User
/** /**
* Setter for username * Setter for username
* *
* @param string $name * @param string $name
*/ */
public function setUsername($name) public function setUsername($name)
{ {
@ -265,7 +268,7 @@ class User
/** /**
* Getter for firstname * Getter for firstname
* *
* @return string * @return string
*/ */
public function getFirstname() public function getFirstname()
{ {
@ -275,7 +278,7 @@ class User
/** /**
* Setter for firstname * Setter for firstname
* *
* @param string $name * @param string $name
*/ */
public function setFirstname($name) public function setFirstname($name)
{ {
@ -285,7 +288,7 @@ class User
/** /**
* Getter for lastname * Getter for lastname
* *
* @return string * @return string
*/ */
public function getLastname() public function getLastname()
{ {
@ -295,7 +298,7 @@ class User
/** /**
* Setter for lastname * Setter for lastname
* *
* @param string $name * @param string $name
*/ */
public function setLastname($name) public function setLastname($name)
{ {
@ -305,7 +308,7 @@ class User
/** /**
* Getter for email * Getter for email
* *
* @return string * @return string
*/ */
public function getEmail() public function getEmail()
{ {
@ -315,8 +318,9 @@ class User
/** /**
* Setter for mail * Setter for mail
* *
* @param string $mail * @param string $mail
* @throws InvalidArgumentException When an invalid mail is provided *
* @throws InvalidArgumentException When an invalid mail is provided
*/ */
public function setEmail($mail) public function setEmail($mail)
{ {
@ -330,7 +334,7 @@ class User
/** /**
* Setter for domain * Setter for domain
* *
* @param string $domain * @param string $domain
*/ */
public function setDomain($domain) public function setDomain($domain)
{ {
@ -340,7 +344,7 @@ class User
/** /**
* Getter for domain * Getter for domain
* *
* @return string * @return string
*/ */
public function getDomain() public function getDomain()
{ {
@ -351,8 +355,8 @@ class User
/** /**
* Set additional information about user * Set additional information about user
* *
* @param string $key * @param string $key
* @param string $value * @param string $value
*/ */
public function setAdditional($key, $value) public function setAdditional($key, $value)
{ {
@ -362,14 +366,15 @@ class User
/** /**
* Getter for additional information * Getter for additional information
* *
* @param string $key * @param string $key
* @return mixed|null * @return mixed|null
*/ */
public function getAdditional($key) public function getAdditional($key)
{ {
if (isset($this->additionalInformation[$key])) { if (isset($this->additionalInformation[$key])) {
return $this->additionalInformation[$key]; return $this->additionalInformation[$key];
} }
return null; return null;
} }
@ -386,6 +391,7 @@ class User
if ($tz === null) { if ($tz === null) {
$tz = date_default_timezone_get(); $tz = date_default_timezone_get();
} }
return new DateTimeZone($tz); return new DateTimeZone($tz);
} }
@ -394,7 +400,7 @@ class User
* *
* This function does NOT automatically write to the session, messages will not be persisted until you do. * This function does NOT automatically write to the session, messages will not be persisted until you do.
* *
* @param Message $msg The message * @param Message $msg The message
*/ */
public function addMessage(Message $msg) public function addMessage(Message $msg)
{ {
@ -404,7 +410,7 @@ class User
/** /**
* Get all currently pending messages * Get all currently pending messages
* *
* @return array the messages * @return array The messages
*/ */
public function getMessages() public function getMessages()
{ {

View File

@ -4,9 +4,9 @@
namespace Tests\Icinga; namespace Tests\Icinga;
use \DateTimeZone; use Mockery;
use DateTimeZone;
use Icinga\User; use Icinga\User;
use Icinga\User\Preferences;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
class UserTest extends BaseTestCase class UserTest extends BaseTestCase
@ -14,9 +14,13 @@ class UserTest extends BaseTestCase
public function testGetDefaultTimezoneIfTimezoneNotSet() public function testGetDefaultTimezoneIfTimezoneNotSet()
{ {
$user = new User('unittest'); $user = new User('unittest');
$prefs = new Preferences(array()); $prefs = Mockery::mock('Icinga\User\Preferences');
$prefs->shouldReceive('get')->with('timezone')->andReturnNull();
$user->setPreferences($prefs); $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' 'User\'s timezone does not match the default timezone'
); );
} }
@ -25,14 +29,35 @@ class UserTest extends BaseTestCase
{ {
$explicitTz = 'Europe/Berlin'; $explicitTz = 'Europe/Berlin';
$user = new User('unittest'); $user = new User('unittest');
$prefs = new Preferences(array( $prefs = Mockery::mock('Icinga\User\Preferences');
'timezone' => $explicitTz $prefs->shouldReceive('get')->with('timezone')->andReturn($explicitTz);
));
$user->setPreferences($prefs); $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' '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');
}
} }