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,77 +46,77 @@ 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
@ -166,6 +163,7 @@ class User
if ($this->preferences === null) { if ($this->preferences === null) {
$this->preferences = new Preferences(); $this->preferences = new Preferences();
} }
return $this->preferences; return $this->preferences;
} }
@ -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)
{ {
@ -191,6 +191,7 @@ 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()
{ {
@ -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,6 +231,7 @@ 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();
} }
@ -316,6 +319,7 @@ 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)
@ -370,6 +374,7 @@ class User
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);
} }
@ -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');
}
} }