Introduced entity property getters/setters

This commit is contained in:
Marcin Kurczewski 2014-09-13 23:58:13 +02:00
parent 847117a408
commit 0548890d97
26 changed files with 440 additions and 286 deletions

View file

@ -29,10 +29,10 @@ final class UserAvatarController extends AbstractController
{ {
$user = $this->userService->getByName($userName); $user = $this->userService->getByName($userName);
switch ($user->avatarStyle) switch ($user->getAvatarStyle())
{ {
case \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR: case \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR:
$hash = md5(strtolower(trim($user->email ? $user->email : $user->id . $user->name))); $hash = md5(strtolower(trim($user->getEmail() ? $user->getEmail() : $user->getId() . $user->getName())));
$url = 'https://www.gravatar.com/avatar/' . $hash . '?d=retro&s=' . $size; $url = 'https://www.gravatar.com/avatar/' . $hash . '?d=retro&s=' . $size;
$this->serveFromUrl($url); $this->serveFromUrl($url);
break; break;

View file

@ -8,8 +8,8 @@ class TokenViewProxy extends AbstractViewProxy
$result = new \StdClass; $result = new \StdClass;
if ($token) if ($token)
{ {
$result->name = $token->name; $result->name = $token->getName();
$result->purpose = $token->purpose; $result->purpose = $token->getPurpose();
} }
return $result; return $result;
} }

View file

@ -15,26 +15,26 @@ class UserViewProxy extends AbstractViewProxy
$result = new \StdClass; $result = new \StdClass;
if ($user) if ($user)
{ {
$result->id = $user->id; $result->id = $user->getId();
$result->name = $user->name; $result->name = $user->getName();
$result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->accessRank); $result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->getAccessRank());
$result->registrationTime = $user->registrationTime; $result->registrationTime = $user->getRegistrationTime();
$result->lastLoginTime = $user->lastLoginTime; $result->lastLoginTime = $user->getLastLoginTime();
$result->avatarStyle = $user->avatarStyle; $result->avatarStyle = $user->getAvatarStyle();
if ($this->privilegeService->isLoggedIn($user)) if ($this->privilegeService->isLoggedIn($user))
{ {
$result->browsingSettings = $user->browsingSettings; $result->browsingSettings = $user->getBrowsingSettings();
} }
if ($this->privilegeService->hasPrivilege(\Szurubooru\Privilege::VIEW_ALL_EMAIL_ADDRESSES) or if ($this->privilegeService->hasPrivilege(\Szurubooru\Privilege::VIEW_ALL_EMAIL_ADDRESSES) or
$this->privilegeService->isLoggedIn($user)) $this->privilegeService->isLoggedIn($user))
{ {
$result->email = $user->email; $result->email = $user->getEmail();
$result->emailUnconfirmed = $user->emailUnconfirmed; $result->emailUnconfirmed = $user->getEmailUnconfirmed();
} }
$result->confirmed = !$user->emailUnconfirmed; $result->confirmed = !$user->getEmailUnconfirmed();
} }
return $result; return $result;
} }

View file

@ -32,11 +32,11 @@ abstract class AbstractDao implements ICrudDao
public function save(&$entity) public function save(&$entity)
{ {
$arrayEntity = $this->entityConverter->toArray($entity); $arrayEntity = $this->entityConverter->toArray($entity);
if ($entity->id) if ($entity->getId())
{ {
$savedId = $arrayEntity['_id']; $savedId = $arrayEntity['_id'];
unset($arrayEntity['_id']); unset($arrayEntity['_id']);
$this->collection->update(['_id' => new \MongoId($entity->id)], $arrayEntity, ['w' => true]); $this->collection->update(['_id' => new \MongoId($entity->getId())], $arrayEntity, ['w' => true]);
$arrayEntity['_id'] = $savedId; $arrayEntity['_id'] = $savedId;
} }
else else
@ -47,7 +47,7 @@ abstract class AbstractDao implements ICrudDao
return $entity; return $entity;
} }
public function getAll() public function findAll()
{ {
$entities = []; $entities = [];
foreach ($this->collection->find() as $key => $arrayEntity) foreach ($this->collection->find() as $key => $arrayEntity)
@ -58,9 +58,9 @@ abstract class AbstractDao implements ICrudDao
return $entities; return $entities;
} }
public function getById($postId) public function findById($entityId)
{ {
$arrayEntity = $this->collection->findOne(['_id' => new \MongoId($postId)]); $arrayEntity = $this->collection->findOne(['_id' => new \MongoId($entityId)]);
return $this->entityConverter->toEntity($arrayEntity); return $this->entityConverter->toEntity($arrayEntity);
} }
@ -69,8 +69,8 @@ abstract class AbstractDao implements ICrudDao
$this->collection->remove(); $this->collection->remove();
} }
public function deleteById($postId) public function deleteById($entityId)
{ {
$this->collection->remove(['_id' => new \MongoId($postId)]); $this->collection->remove(['_id' => new \MongoId($entityId)]);
} }
} }

View file

@ -12,8 +12,15 @@ final class EntityConverter
public function toArray($entity) public function toArray($entity)
{ {
$arrayEntity = (array) $entity; $arrayEntity = [];
if (isset($entity->id)) $reflectionClass = new \ReflectionClass($this->entityName);
$reflectionProperties = $reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED);
foreach ($reflectionProperties as $reflectionProperty)
{
$reflectionProperty->setAccessible(true);
$arrayEntity[$reflectionProperty->getName()] = $reflectionProperty->getValue($entity);
}
if ($entity->getId())
{ {
$arrayEntity['_id'] = $arrayEntity['id']; $arrayEntity['_id'] = $arrayEntity['id'];
unset($arrayEntity['id']); unset($arrayEntity['id']);
@ -23,12 +30,28 @@ final class EntityConverter
public function toEntity($arrayEntity) public function toEntity($arrayEntity)
{ {
$entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName); if ($arrayEntity === null)
if (isset($entity->_id)) return null;
$entity = new $this->entityName;
$reflectionClass = new \ReflectionClass($this->entityName);
$reflectionProperties = $reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED);
foreach ($reflectionProperties as $reflectionProperty)
{ {
$entity->id = (string) $entity->_id; if (isset($arrayEntity[$reflectionProperty->getName()]))
unset($entity->_id); {
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($entity, $arrayEntity[$reflectionProperty->getName()]);
} }
}
$reflectionProperty = $reflectionClass->getProperty('id');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($entity, isset($arrayEntity['_id'])
? (string) $arrayEntity['_id']
: null);
return $entity; return $entity;
} }
} }

View file

@ -3,9 +3,9 @@ namespace Szurubooru\Dao;
interface ICrudDao interface ICrudDao
{ {
public function getAll(); public function findAll();
public function getById($objectId); public function findById($objectId);
public function save(&$object); public function save(&$object);

View file

@ -8,7 +8,7 @@ class TokenDao extends AbstractDao
parent::__construct($databaseConnection, 'tokens', '\Szurubooru\Entities\Token'); parent::__construct($databaseConnection, 'tokens', '\Szurubooru\Entities\Token');
} }
public function getByName($tokenName) public function findByName($tokenName)
{ {
$arrayEntity = $this->collection->findOne(['name' => $tokenName]); $arrayEntity = $this->collection->findOne(['name' => $tokenName]);
return $this->entityConverter->toEntity($arrayEntity); return $this->entityConverter->toEntity($arrayEntity);

View file

@ -9,13 +9,13 @@ class UserDao extends AbstractDao implements ICrudDao
parent::__construct($databaseConnection, 'users', '\Szurubooru\Entities\User'); parent::__construct($databaseConnection, 'users', '\Szurubooru\Entities\User');
} }
public function getByName($userName) public function findByName($userName)
{ {
$arrayEntity = $this->collection->findOne(['name' => $userName]); $arrayEntity = $this->collection->findOne(['name' => $userName]);
return $this->entityConverter->toEntity($arrayEntity); return $this->entityConverter->toEntity($arrayEntity);
} }
public function getByEmail($userEmail, $allowUnconfirmed = false) public function findByEmail($userEmail, $allowUnconfirmed = false)
{ {
$arrayEntity = $this->collection->findOne(['email' => $userEmail]); $arrayEntity = $this->collection->findOne(['email' => $userEmail]);
if (!$arrayEntity and $allowUnconfirmed) if (!$arrayEntity and $allowUnconfirmed)

View file

@ -3,5 +3,15 @@ namespace Szurubooru\Entities;
abstract class Entity abstract class Entity
{ {
public $id = null; protected $id = null;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
} }

View file

@ -3,5 +3,15 @@ namespace Szurubooru\Entities;
final class Post extends Entity final class Post extends Entity
{ {
public $name; protected $name;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
} }

View file

@ -3,6 +3,26 @@ namespace Szurubooru\Entities;
final class Tag extends Entity final class Tag extends Entity
{ {
public $name; protected $name;
public $usages; protected $usages;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getUsages()
{
return $this->usages;
}
public function setUsages($usages)
{
$this->usages = $usages;
}
} }

View file

@ -7,7 +7,37 @@ final class Token extends Entity
const PURPOSE_ACTIVATE = 'activate'; const PURPOSE_ACTIVATE = 'activate';
const PURPOSE_PASSWORD_RESET = 'passwordReset'; const PURPOSE_PASSWORD_RESET = 'passwordReset';
public $name; protected $name;
public $purpose; protected $purpose;
public $additionalData; protected $additionalData;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getPurpose()
{
return $this->purpose;
}
public function setPurpose($purpose)
{
$this->purpose = $purpose;
}
public function getAdditionalData()
{
return $this->additionalData;
}
public function setAdditionalData($additionalData)
{
$this->additionalData = $additionalData;
}
} }

View file

@ -14,13 +14,103 @@ final class User extends Entity
const AVATAR_STYLE_MANUAL = 'manual'; const AVATAR_STYLE_MANUAL = 'manual';
const AVATAR_STYLE_BLANK = 'blank'; const AVATAR_STYLE_BLANK = 'blank';
public $name; protected $name;
public $email; protected $email;
public $emailUnconfirmed; protected $emailUnconfirmed;
public $passwordHash; protected $passwordHash;
public $accessRank; protected $accessRank;
public $registrationTime; protected $registrationTime;
public $lastLoginTime; protected $lastLoginTime;
public $avatarStyle; protected $avatarStyle;
public $browsingSettings; protected $browsingSettings;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getEmailUnconfirmed()
{
return $this->emailUnconfirmed;
}
public function setEmailUnconfirmed($emailUnconfirmed)
{
$this->emailUnconfirmed = $emailUnconfirmed;
}
public function getPasswordHash()
{
return $this->passwordHash;
}
public function setPasswordHash($passwordHash)
{
$this->passwordHash = $passwordHash;
}
public function getAccessRank()
{
return $this->accessRank;
}
public function setAccessRank($accessRank)
{
$this->accessRank = $accessRank;
}
public function getRegistrationTime()
{
return $this->registrationTime;
}
public function setRegistrationTime($registrationTime)
{
$this->registrationTime = $registrationTime;
}
public function getLastLoginTime()
{
return $this->lastLoginTime;
}
public function setLastLoginTime($lastLoginTime)
{
$this->lastLoginTime = $lastLoginTime;
}
public function getAvatarStyle()
{
return $this->avatarStyle;
}
public function setAvatarStyle($avatarStyle)
{
$this->avatarStyle = $avatarStyle;
}
public function getBrowsingSettings()
{
return $this->browsingSettings;
}
public function setBrowsingSettings($browsingSettings)
{
$this->browsingSettings = $browsingSettings;
}
} }

View file

@ -1,20 +0,0 @@
<?php
namespace Szurubooru\Helpers;
final class TypeHelper
{
public static function arrayToClass($array, $className)
{
if (!$array)
return null;
if (!class_exists($className, true))
return null;
return unserialize(
preg_replace(
'/^O:[0-9]+:"[^"]+":/i',
'O:' . strlen($className) . ':"' . $className . '":',
serialize((object) $array)));
}
}

View file

@ -49,7 +49,7 @@ class AuthService
$this->doFinalChecksOnUser($user); $this->doFinalChecksOnUser($user);
$passwordHash = $this->passwordService->getHash($formData->password); $passwordHash = $this->passwordService->getHash($formData->password);
if ($user->passwordHash !== $passwordHash) if ($user->getPasswordHash() !== $passwordHash)
throw new \InvalidArgumentException('Specified password is invalid.'); throw new \InvalidArgumentException('Specified password is invalid.');
$this->loginToken = $this->createAndSaveLoginToken($user); $this->loginToken = $this->createAndSaveLoginToken($user);
@ -59,10 +59,10 @@ class AuthService
public function loginFromToken(\Szurubooru\Entities\Token $token) public function loginFromToken(\Szurubooru\Entities\Token $token)
{ {
if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_LOGIN) if ($token->getPurpose() !== \Szurubooru\Entities\Token::PURPOSE_LOGIN)
throw new \Exception('This token is not a login token.'); throw new \Exception('This token is not a login token.');
$user = $this->userService->getById($token->additionalData); $user = $this->userService->getById($token->getAdditionalData());
$this->doFinalChecksOnUser($user); $this->doFinalChecksOnUser($user);
$this->loginToken = $token; $this->loginToken = $token;
@ -73,8 +73,8 @@ class AuthService
public function getAnonymousUser() public function getAnonymousUser()
{ {
$user = new \Szurubooru\Entities\User(); $user = new \Szurubooru\Entities\User();
$user->name = 'Anonymous user'; $user->setName('Anonymous user');
$user->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS; $user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS);
return $user; return $user;
} }
@ -95,12 +95,12 @@ class AuthService
private function createAndSaveLoginToken(\Szurubooru\Entities\User $user) private function createAndSaveLoginToken(\Szurubooru\Entities\User $user)
{ {
return $this->tokenService->createAndSaveToken($user->id, \Szurubooru\Entities\Token::PURPOSE_LOGIN); return $this->tokenService->createAndSaveToken($user->getId(), \Szurubooru\Entities\Token::PURPOSE_LOGIN);
} }
private function doFinalChecksOnUser($user) private function doFinalChecksOnUser($user)
{ {
if (!$user->email and $this->config->security->needEmailActivationToRegister) if (!$user->getEmail() and $this->config->security->needEmailActivationToRegister)
throw new \DomainException('User didn\'t confirm mail yet.'); throw new \DomainException('User didn\'t confirm mail yet.');
} }
} }

View file

@ -12,25 +12,25 @@ class EmailService
public function sendPasswordResetEmail(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Token $token) public function sendPasswordResetEmail(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Token $token)
{ {
if (!$user->email) if (!$user->getEmail())
throw new \BadMethodCall('An activated e-mail addreses is needed to reset the password.'); throw new \BadMethodCall('An activated e-mail addreses is needed to reset the password.');
$mailSubject = $this->tokenize($this->config->mail->passwordResetSubject); $mailSubject = $this->tokenize($this->config->mail->passwordResetSubject);
$mailBody = $this->tokenizeFile( $mailBody = $this->tokenizeFile(
$this->config->mail->passwordResetBodyPath, $this->config->mail->passwordResetBodyPath,
[ [
'link' => $this->config->basic->serviceBaseUrl . '#/password-reset/' . $token->name, 'link' => $this->config->basic->serviceBaseUrl . '#/password-reset/' . $token->getName(),
]); ]);
$this->sendEmail($user->email, $mailSubject, $mailBody); $this->sendEmail($user->getEmail(), $mailSubject, $mailBody);
} }
public function sendActivationEmail(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Token $token) public function sendActivationEmail(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Token $token)
{ {
if (!$user->emailUnconfirmed) if (!$user->getEmailUnconfirmed())
{ {
throw new \BadMethodCallException( throw new \BadMethodCallException(
$user->email $user->getEmail()
? 'E-mail for this account is already confirmed.' ? 'E-mail for this account is already confirmed.'
: 'An e-mail address is needed to activate the account.'); : 'An e-mail address is needed to activate the account.');
} }
@ -39,10 +39,10 @@ class EmailService
$mailBody = $this->tokenizeFile( $mailBody = $this->tokenizeFile(
$this->config->mail->activationBodyPath, $this->config->mail->activationBodyPath,
[ [
'link' => $this->config->basic->serviceBaseUrl . '#/activate/' . $token->name, 'link' => $this->config->basic->serviceBaseUrl . '#/activate/' . $token->getName(),
]); ]);
$this->sendEmail($user->emailUnconfirmed, $mailSubject, $mailBody); $this->sendEmail($user->getEmailUnconfirmed(), $mailSubject, $mailBody);
} }
private function sendEmail($recipientEmail, $subject, $body) private function sendEmail($recipientEmail, $subject, $body)

View file

@ -29,7 +29,7 @@ class PrivilegeService
public function getCurrentPrivileges() public function getCurrentPrivileges()
{ {
$currentAccessRank = $this->authService->getLoggedInUser()->accessRank; $currentAccessRank = $this->authService->getLoggedInUser()->getAccessRank();
$currentAccessRankName = \Szurubooru\Helpers\EnumHelper::accessRankToString($currentAccessRank); $currentAccessRankName = \Szurubooru\Helpers\EnumHelper::accessRankToString($currentAccessRank);
if (!isset($this->privilegeMap[$currentAccessRankName])) if (!isset($this->privilegeMap[$currentAccessRankName]))
return []; return [];
@ -58,16 +58,16 @@ class PrivilegeService
$loggedInUser = $this->authService->getLoggedInUser(); $loggedInUser = $this->authService->getLoggedInUser();
if ($userIdentifier instanceof \Szurubooru\Entities\User) if ($userIdentifier instanceof \Szurubooru\Entities\User)
{ {
return $loggedInUser->id and ($loggedInUser->id === $userIdentifier->id); return $loggedInUser->getId() and ($loggedInUser->getId() === $userIdentifier->getId());
} }
elseif (is_string($userIdentifier)) elseif (is_string($userIdentifier))
{ {
if ($loggedInUser->email) if ($loggedInUser->getEmail())
{ {
if ($loggedInUser->email === $userIdentifier) if ($loggedInUser->getEmail() === $userIdentifier)
return true; return true;
} }
return $loggedInUser->name === $userIdentifier; return $loggedInUser->getName() === $userIdentifier;
} }
else else
{ {

View file

@ -12,7 +12,7 @@ class TokenService
public function getByName($tokenName) public function getByName($tokenName)
{ {
$token = $this->tokenDao->getByName($tokenName); $token = $this->tokenDao->findByName($tokenName);
if (!$token) if (!$token)
throw new \InvalidArgumentException('Token with identifier "' . $tokenName . '" not found.'); throw new \InvalidArgumentException('Token with identifier "' . $tokenName . '" not found.');
return $token; return $token;
@ -31,9 +31,9 @@ class TokenService
public function createAndSaveToken($additionalData, $tokenPurpose) public function createAndSaveToken($additionalData, $tokenPurpose)
{ {
$token = new \Szurubooru\Entities\Token(); $token = new \Szurubooru\Entities\Token();
$token->name = sha1(date('r') . uniqid() . microtime(true)); $token->setName(sha1(date('r') . uniqid() . microtime(true)));
$token->additionalData = $additionalData; $token->setAdditionalData($additionalData);
$token->purpose = $tokenPurpose; $token->setPurpose($tokenPurpose);
$this->invalidateByAdditionalData($additionalData); $this->invalidateByAdditionalData($additionalData);
$this->tokenDao->save($token); $this->tokenDao->save($token);
return $token; return $token;

View file

@ -40,11 +40,11 @@ class UserService
public function getByNameOrEmail($userNameOrEmail, $allowUnconfirmed = false) public function getByNameOrEmail($userNameOrEmail, $allowUnconfirmed = false)
{ {
$user = $this->userDao->getByName($userNameOrEmail); $user = $this->userDao->findByName($userNameOrEmail);
if ($user) if ($user)
return $user; return $user;
$user = $this->userDao->getByEmail($userNameOrEmail, $allowUnconfirmed); $user = $this->userDao->findByEmail($userNameOrEmail, $allowUnconfirmed);
if ($user) if ($user)
return $user; return $user;
@ -53,7 +53,7 @@ class UserService
public function getByName($userName) public function getByName($userName)
{ {
$user = $this->userDao->getByName($userName); $user = $this->userDao->findByName($userName);
if (!$user) if (!$user)
throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.'); throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.');
return $user; return $user;
@ -61,7 +61,7 @@ class UserService
public function getById($userId) public function getById($userId)
{ {
$user = $this->userDao->getById($userId); $user = $this->userDao->findById($userId);
if (!$user) if (!$user)
throw new \InvalidArgumentException('User with id "' . $userId . '" was not found.'); throw new \InvalidArgumentException('User with id "' . $userId . '" was not found.');
return $user; return $user;
@ -79,11 +79,11 @@ class UserService
$formData->validate($this->validator); $formData->validate($this->validator);
$user = new \Szurubooru\Entities\User(); $user = new \Szurubooru\Entities\User();
$user->registrationTime = $this->timeService->getCurrentTime(); $user->setRegistrationTime($this->timeService->getCurrentTime());
$user->lastLoginTime = null; $user->setLastLoginTime(null);
$user->accessRank = $this->userDao->hasAnyUsers() $user->setAccessRank($this->userDao->hasAnyUsers()
? \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER ? \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER
: \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR; : \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR);
$this->updateUserName($user, $formData->userName); $this->updateUserName($user, $formData->userName);
$this->updateUserPassword($user, $formData->password); $this->updateUserPassword($user, $formData->password);
@ -122,7 +122,7 @@ class UserService
public function updateUserAvatarStyle(\Szurubooru\Entities\User $user, $newAvatarStyle) public function updateUserAvatarStyle(\Szurubooru\Entities\User $user, $newAvatarStyle)
{ {
$user->avatarStyle = $newAvatarStyle; $user->setAvatarStyle($newAvatarStyle);
} }
public function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContentInBase64) public function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContentInBase64)
@ -135,47 +135,47 @@ class UserService
public function updateUserName(\Szurubooru\Entities\User $user, $newName) public function updateUserName(\Szurubooru\Entities\User $user, $newName)
{ {
$this->assertNoUserWithThisName($user, $newName); $this->assertNoUserWithThisName($user, $newName);
$user->name = $newName; $user->setName($newName);
} }
public function updateUserPassword(\Szurubooru\Entities\User $user, $newPassword) public function updateUserPassword(\Szurubooru\Entities\User $user, $newPassword)
{ {
$user->passwordHash = $this->passwordService->getHash($newPassword); $user->setPasswordHash($this->passwordService->getHash($newPassword));
} }
public function updateUserEmail(\Szurubooru\Entities\User $user, $newEmail) public function updateUserEmail(\Szurubooru\Entities\User $user, $newEmail)
{ {
if ($user->email === $newEmail) if ($user->getEmail() === $newEmail)
{ {
$user->emailUnconfirmed = null; $user->setEmailUnconfirmed(null);
} }
else else
{ {
$this->assertNoUserWithThisEmail($user, $newEmail); $this->assertNoUserWithThisEmail($user, $newEmail);
$user->emailUnconfirmed = $newEmail; $user->setEmailUnconfirmed($newEmail);
$user = $this->sendActivationEmailIfNeeded($user); $user = $this->sendActivationEmailIfNeeded($user);
} }
} }
public function updateUserAccessRank(\Szurubooru\Entities\User $user, $newAccessRank) public function updateUserAccessRank(\Szurubooru\Entities\User $user, $newAccessRank)
{ {
$user->accessRank = $newAccessRank; $user->setAccessRank($newAccessRank);
} }
public function updateUserBrowsingSettings(\Szurubooru\Entities\User $user, $newBrowsingSettings) public function updateUserBrowsingSettings(\Szurubooru\Entities\User $user, $newBrowsingSettings)
{ {
$user->browsingSettings = $newBrowsingSettings; $user->setBrowsingSettings($newBrowsingSettings);
} }
public function updateUserLastLoginTime(\Szurubooru\Entities\User $user) public function updateUserLastLoginTime(\Szurubooru\Entities\User $user)
{ {
$user->lastLoginTime = $this->timeService->getCurrentTime(); $user->setLastLoginTime($this->timeService->getCurrentTime());
$this->userDao->save($user); $this->userDao->save($user);
} }
public function deleteUser(\Szurubooru\Entities\User $user) public function deleteUser(\Szurubooru\Entities\User $user)
{ {
$this->userDao->deleteById($user->id); $this->userDao->deleteById($user->getId());
$avatarSource = $this->getCustomAvatarSourcePath($user); $avatarSource = $this->getCustomAvatarSourcePath($user);
$this->fileService->delete($avatarSource); $this->fileService->delete($avatarSource);
@ -184,43 +184,43 @@ class UserService
public function sendPasswordResetEmail(\Szurubooru\Entities\User $user) public function sendPasswordResetEmail(\Szurubooru\Entities\User $user)
{ {
$token = $this->tokenService->createAndSaveToken($user->name, \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET); $token = $this->tokenService->createAndSaveToken($user->getName(), \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET);
$this->emailService->sendPasswordResetEmail($user, $token); $this->emailService->sendPasswordResetEmail($user, $token);
} }
public function finishPasswordReset(\Szurubooru\Entities\Token $token) public function finishPasswordReset(\Szurubooru\Entities\Token $token)
{ {
if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET) if ($token->getPurpose() !== \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET)
throw new \Exception('This token is not a password reset token.'); throw new \Exception('This token is not a password reset token.');
$user = $this->getByName($token->additionalData); $user = $this->getByName($token->getAdditionalData());
$newPassword = $this->passwordService->getRandomPassword(); $newPassword = $this->passwordService->getRandomPassword();
$user->passwordHash = $this->passwordService->getHash($newPassword); $user->setPasswordHash($this->passwordService->getHash($newPassword));
$this->userDao->save($user); $this->userDao->save($user);
$this->tokenService->invalidateByName($token->name); $this->tokenService->invalidateByName($token->getName());
return $newPassword; return $newPassword;
} }
public function sendActivationEmail(\Szurubooru\Entities\User $user) public function sendActivationEmail(\Szurubooru\Entities\User $user)
{ {
$token = $this->tokenService->createAndSaveToken($user->name, \Szurubooru\Entities\Token::PURPOSE_ACTIVATE); $token = $this->tokenService->createAndSaveToken($user->getName(), \Szurubooru\Entities\Token::PURPOSE_ACTIVATE);
$this->emailService->sendActivationEmail($user, $token); $this->emailService->sendActivationEmail($user, $token);
} }
public function finishActivation(\Szurubooru\Entities\Token $token) public function finishActivation(\Szurubooru\Entities\Token $token)
{ {
if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_ACTIVATE) if ($token->getPurpose() !== \Szurubooru\Entities\Token::PURPOSE_ACTIVATE)
throw new \Exception('This token is not an activation token.'); throw new \Exception('This token is not an activation token.');
$user = $this->getByName($token->additionalData); $user = $this->getByName($token->getAdditionalData());
$user = $this->confirmUserEmail($user); $user = $this->confirmUserEmail($user);
$this->userDao->save($user); $this->userDao->save($user);
$this->tokenService->invalidateByName($token->name); $this->tokenService->invalidateByName($token->getName());
} }
public function getCustomAvatarSourcePath(\Szurubooru\Entities\User $user) public function getCustomAvatarSourcePath(\Szurubooru\Entities\User $user)
{ {
return 'avatars' . DIRECTORY_SEPARATOR . $user->id; return 'avatars' . DIRECTORY_SEPARATOR . $user->getId();
} }
public function getBlankAvatarSourcePath() public function getBlankAvatarSourcePath()
@ -230,7 +230,7 @@ class UserService
private function sendActivationEmailIfNeeded(\Szurubooru\Entities\User $user) private function sendActivationEmailIfNeeded(\Szurubooru\Entities\User $user)
{ {
if ($user->accessRank === \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR or !$this->config->security->needEmailActivationToRegister) if ($user->getAccessRank() === \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR or !$this->config->security->needEmailActivationToRegister)
{ {
$user = $this->confirmUserEmail($user); $user = $this->confirmUserEmail($user);
} }
@ -251,27 +251,27 @@ class UserService
//5. two users share the same mail --> problem. //5. two users share the same mail --> problem.
//by checking here again for users with such mail, this problem is solved with first-come first-serve approach: //by checking here again for users with such mail, this problem is solved with first-come first-serve approach:
//whoever confirms e-mail first, wins. //whoever confirms e-mail first, wins.
$this->assertNoUserWithThisEmail($user, $user->emailUnconfirmed); $this->assertNoUserWithThisEmail($user, $user->getEmailUnconfirmed());
if ($user->emailUnconfirmed) if ($user->getEmailUnconfirmed())
{ {
$user->email = $user->emailUnconfirmed; $user->setEmail($user->getEmailUnconfirmed());
$user->emailUnconfirmed = null; $user->setEmailUnconfirmed(null);
} }
return $user; return $user;
} }
private function assertNoUserWithThisName(\Szurubooru\Entities\User $owner, $nameToCheck) private function assertNoUserWithThisName(\Szurubooru\Entities\User $owner, $nameToCheck)
{ {
$userWithThisName = $this->userDao->getByName($nameToCheck); $userWithThisName = $this->userDao->findByName($nameToCheck);
if ($userWithThisName and $userWithThisName->id !== $owner->id) if ($userWithThisName and $userWithThisName->getId() !== $owner->getId())
throw new \DomainException('User with this name already exists.'); throw new \DomainException('User with this name already exists.');
} }
private function assertNoUserWithThisEmail(\Szurubooru\Entities\User $owner, $emailToCheck) private function assertNoUserWithThisEmail(\Szurubooru\Entities\User $owner, $emailToCheck)
{ {
$userWithThisEmail = $this->userDao->getByEmail($emailToCheck); $userWithThisEmail = $this->userDao->findByEmail($emailToCheck);
if ($userWithThisEmail and $userWithThisEmail->id !== $owner->id) if ($userWithThisEmail and $userWithThisEmail->getId() !== $owner->getId())
throw new \DomainException('User with this e-mail already exists.'); throw new \DomainException('User with this e-mail already exists.');
} }
} }

View file

@ -8,23 +8,24 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post = new \Szurubooru\Entities\Post(); $post = new \Szurubooru\Entities\Post();
$post->name = 'test'; $post->setName('test');
$savedPost = $postDao->save($post); $savedPost = $postDao->save($post);
$this->assertEquals('test', $post->name); $this->assertEquals('test', $post->getName());
$this->assertNotNull($savedPost->id); $this->assertNotNull($savedPost->getId());
} }
public function testUpdating() public function testUpdating()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post = new \Szurubooru\Entities\Post(); $post = new \Szurubooru\Entities\Post();
$post->name = 'test'; $post->setName('test');
$post = $postDao->save($post); $post = $postDao->save($post);
$id = $post->id; $this->assertEquals('test', $post->getName());
$post->name .= '2'; $id = $post->getId();
$post->setName($post->getName() . '2');
$post = $postDao->save($post); $post = $postDao->save($post);
$this->assertEquals('test2', $post->name); $this->assertEquals('test2', $post->getName());
$this->assertEquals($id, $post->id); $this->assertEquals($id, $post->getId());
} }
public function testGettingAll() public function testGettingAll()
@ -32,17 +33,17 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->setName('test2');
$post2 = new \Szurubooru\Entities\Post(); $post2 = new \Szurubooru\Entities\Post();
$post2->name = 'test2'; $post2->setName('test2');
$postDao->save($post1); $postDao->save($post1);
$postDao->save($post2); $postDao->save($post2);
$actual = $postDao->getAll(); $actual = $postDao->findAll();
$expected = [ $expected = [
$post1->id => $post1, $post1->getId() => $post1,
$post2->id => $post2, $post2->getId() => $post2,
]; ];
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
@ -53,15 +54,15 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->setName('test2');
$post2 = new \Szurubooru\Entities\Post(); $post2 = new \Szurubooru\Entities\Post();
$post2->name = 'test2'; $post2->setName('test2');
$postDao->save($post1); $postDao->save($post1);
$postDao->save($post2); $postDao->save($post2);
$actualPost1 = $postDao->getById($post1->id); $actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->getById($post2->id); $actualPost2 = $postDao->findById($post2->getId());
$this->assertEquals($post1, $actualPost1); $this->assertEquals($post1, $actualPost1);
$this->assertEquals($post2, $actualPost2); $this->assertEquals($post2, $actualPost2);
} }
@ -71,20 +72,20 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->setName('test2');
$post2 = new \Szurubooru\Entities\Post(); $post2 = new \Szurubooru\Entities\Post();
$post2->name = 'test2'; $post2->setName('test2');
$postDao->save($post1); $postDao->save($post1);
$postDao->save($post2); $postDao->save($post2);
$postDao->deleteAll(); $postDao->deleteAll();
$actualPost1 = $postDao->getById($post1->id); $actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->getById($post2->id); $actualPost2 = $postDao->findById($post2->getId());
$this->assertEquals(null, $actualPost1); $this->assertEquals(null, $actualPost1);
$this->assertEquals(null, $actualPost2); $this->assertEquals(null, $actualPost2);
$this->assertEquals(0, count($postDao->getAll())); $this->assertEquals(0, count($postDao->findAll()));
} }
public function testDeletingById() public function testDeletingById()
@ -92,19 +93,19 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->setName('test2');
$post2 = new \Szurubooru\Entities\Post(); $post2 = new \Szurubooru\Entities\Post();
$post2->name = 'test2'; $post2->setName('test2');
$postDao->save($post1); $postDao->save($post1);
$postDao->save($post2); $postDao->save($post2);
$postDao->deleteById($post1->id); $postDao->deleteById($post1->getId());
$actualPost1 = $postDao->getById($post1->id); $actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->getById($post2->id); $actualPost2 = $postDao->findById($post2->getId());
$this->assertEquals(null, $actualPost1); $this->assertEquals(null, $actualPost1);
$this->assertEquals($actualPost2, $actualPost2); $this->assertEquals($actualPost2, $actualPost2);
$this->assertEquals(1, count($postDao->getAll())); $this->assertEquals(1, count($postDao->findAll()));
} }
} }

View file

@ -24,11 +24,11 @@ class UserSearchServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
public function testSorting() public function testSorting()
{ {
$user1 = new \Szurubooru\Entities\User(); $user1 = new \Szurubooru\Entities\User();
$user1->name = 'reginald'; $user1->setName('reginald');
$user1->registrationTime = date('c', mktime(3, 2, 1)); $user1->setRegistrationTime(date('c', mktime(3, 2, 1)));
$user2 = new \Szurubooru\Entities\User(); $user2 = new \Szurubooru\Entities\User();
$user2->name = 'beartato'; $user2->setName('beartato');
$user2->registrationTime = date('c', mktime(1, 2, 3)); $user2->setRegistrationTime(date('c', mktime(1, 2, 3)));
$this->userDao->save($user1); $this->userDao->save($user1);
$this->userDao->save($user2); $this->userDao->save($user2);

View file

@ -8,11 +8,11 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection);
$token = new \Szurubooru\Entities\Token(); $token = new \Szurubooru\Entities\Token();
$token->name = 'test'; $token->setName('test');
$tokenDao->save($token); $tokenDao->save($token);
$expected = $token; $expected = $token;
$actual = $tokenDao->getByName($token->name); $actual = $tokenDao->findByName($token->getName());
$this->assertEquals($actual, $expected); $this->assertEquals($actual, $expected);
} }
@ -21,7 +21,7 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
$tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection);
$actual = $tokenDao->getByName('rubbish'); $actual = $tokenDao->findByName('rubbish');
$this->assertNull($actual); $this->assertNull($actual);
} }

View file

@ -8,11 +8,11 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$userDao = $this->getUserDao(); $userDao = $this->getUserDao();
$user = new \Szurubooru\Entities\User(); $user = new \Szurubooru\Entities\User();
$user->name = 'test'; $user->setName('test');
$userDao->save($user); $userDao->save($user);
$expected = $user; $expected = $user;
$actual = $userDao->getByName($user->name); $actual = $userDao->findByName($user->getName());
$this->assertEquals($actual, $expected); $this->assertEquals($actual, $expected);
} }
@ -21,7 +21,7 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
$userDao = $this->getUserDao(); $userDao = $this->getUserDao();
$actual = $userDao->getByName('rubbish'); $actual = $userDao->findByName('rubbish');
$this->assertNull($actual); $this->assertNull($actual);
} }
@ -33,7 +33,7 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$this->assertFalse($userDao->hasAnyUsers()); $this->assertFalse($userDao->hasAnyUsers());
$user = new \Szurubooru\Entities\User(); $user = new \Szurubooru\Entities\User();
$user->name = 'test'; $user->setName('test');
$userDao->save($user); $userDao->save($user);
$this->assertTrue($userDao->hasAnyUsers()); $this->assertTrue($userDao->hasAnyUsers());

View file

@ -22,11 +22,11 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testInvalidPassword() public function testInvalidPassword()
{ {
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$this->passwordServiceMock->method('getHash')->willReturn('unmatchingHash'); $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('unmatchingHash');
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->setName('dummy');
$testUser->passwordHash = 'hash'; $testUser->setPasswordHash('hash');
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
$this->setExpectedException(\Exception::class, 'Specified password is invalid'); $this->setExpectedException(\Exception::class, 'Specified password is invalid');
@ -40,18 +40,17 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testValidCredentials() public function testValidCredentials()
{ {
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$this->passwordServiceMock->method('getHash')->willReturn('hash'); $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('hash');
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User('an unusual database identifier');
$testUser->id = 'an unusual database identifier'; $testUser->setName('dummy');
$testUser->name = 'dummy'; $testUser->setPasswordHash('hash');
$testUser->passwordHash = 'hash';
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
$testToken = new \Szurubooru\Entities\Token(); $testToken = new \Szurubooru\Entities\Token();
$testToken->name = 'mummy'; $testToken->setName('mummy');
$this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->with( $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->with(
$testUser->id, $testUser->getId(),
\Szurubooru\Entities\Token::PURPOSE_LOGIN)->willReturn($testToken); \Szurubooru\Entities\Token::PURPOSE_LOGIN)->willReturn($testToken);
$authService = $this->getAuthService(); $authService = $this->getAuthService();
@ -63,17 +62,17 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->assertTrue($authService->isLoggedIn()); $this->assertTrue($authService->isLoggedIn());
$this->assertEquals($testUser, $authService->getLoggedInUser()); $this->assertEquals($testUser, $authService->getLoggedInUser());
$this->assertNotNull($authService->getLoginToken()); $this->assertNotNull($authService->getLoginToken());
$this->assertEquals('mummy', $authService->getLoginToken()->name); $this->assertEquals('mummy', $authService->getLoginToken()->getName());
} }
public function testValidCredentialsUnconfirmedEmail() public function testValidCredentialsUnconfirmedEmail()
{ {
$this->configMock->set('security/needEmailActivationToRegister', true); $this->configMock->set('security/needEmailActivationToRegister', true);
$this->passwordServiceMock->method('getHash')->willReturn('hash'); $this->passwordServiceMock->expects($this->never())->method('getHash')->willReturn('hash');
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->setName('dummy');
$testUser->passwordHash = 'hash'; $testUser->setPasswordHash('hash');
$this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser);
$this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet'); $this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet');
@ -101,15 +100,14 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testValidToken() public function testValidToken()
{ {
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User(5);
$testUser->id = 5; $testUser->setName('dummy');
$testUser->name = 'dummy';
$this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser); $this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser);
$testToken = new \Szurubooru\Entities\Token(); $testToken = new \Szurubooru\Entities\Token();
$testToken->name = 'dummy_token'; $testToken->setName('dummy_token');
$testToken->additionalData = $testUser->id; $testToken->setAdditionalData($testUser->getId());
$testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN; $testToken->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN);
$authService = $this->getAuthService(); $authService = $this->getAuthService();
$authService->loginFromToken($testToken); $authService->loginFromToken($testToken);
@ -117,16 +115,16 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->assertTrue($authService->isLoggedIn()); $this->assertTrue($authService->isLoggedIn());
$this->assertEquals($testUser, $authService->getLoggedInUser()); $this->assertEquals($testUser, $authService->getLoggedInUser());
$this->assertNotNull($authService->getLoginToken()); $this->assertNotNull($authService->getLoginToken());
$this->assertEquals('dummy_token', $authService->getLoginToken()->name); $this->assertEquals('dummy_token', $authService->getLoginToken()->getName());
} }
public function testValidTokenInvalidPurpose() public function testValidTokenInvalidPurpose()
{ {
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$testToken = new \Szurubooru\Entities\Token(); $testToken = new \Szurubooru\Entities\Token();
$testToken->name = 'dummy_token'; $testToken->setName('dummy_token');
$testToken->additionalData = 'whatever'; $testToken->setAdditionalData('whatever');
$testToken->purpose = null; $testToken->setPurpose(null);
$this->setExpectedException(\Exception::class, 'This token is not a login token'); $this->setExpectedException(\Exception::class, 'This token is not a login token');
$authService = $this->getAuthService(); $authService = $this->getAuthService();
@ -140,15 +138,14 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testValidTokenUnconfirmedEmail() public function testValidTokenUnconfirmedEmail()
{ {
$this->configMock->set('security/needEmailActivationToRegister', true); $this->configMock->set('security/needEmailActivationToRegister', true);
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User(5);
$testUser->id = 5; $testUser->setName('dummy');
$testUser->name = 'dummy';
$this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser); $this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser);
$testToken = new \Szurubooru\Entities\Token(); $testToken = new \Szurubooru\Entities\Token();
$testToken->name = 'dummy_token'; $testToken->setName('dummy_token');
$testToken->additionalData = $testUser->id; $testToken->setAdditionalData($testUser->getId());
$testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN; $testToken->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN);
$this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet'); $this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet');
$authService = $this->getAuthService(); $authService = $this->getAuthService();

View file

@ -16,9 +16,9 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testReadingConfig() public function testReadingConfig()
{ {
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->setName('dummy');
$testUser->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER; $testUser->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_POWER_USER);
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser);
$privilege = \Szurubooru\Privilege::LIST_USERS; $privilege = \Szurubooru\Privilege::LIST_USERS;
$this->configMock->set('security/privileges/' . $privilege, 'powerUser'); $this->configMock->set('security/privileges/' . $privilege, 'powerUser');
@ -31,38 +31,36 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testIsLoggedInByNameString() public function testIsLoggedInByNameString()
{ {
$testUser1 = new \Szurubooru\Entities\User(); $testUser1 = new \Szurubooru\Entities\User();
$testUser1->name = 'dummy'; $testUser1->setName('dummy');
$testUser2 = new \Szurubooru\Entities\User(); $testUser2 = new \Szurubooru\Entities\User();
$testUser2->name = 'godzilla'; $testUser2->setName('godzilla');
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
$privilegeService = $this->getPrivilegeService(); $privilegeService = $this->getPrivilegeService();
$this->assertTrue($privilegeService->isLoggedIn($testUser1->name)); $this->assertTrue($privilegeService->isLoggedIn($testUser1->getName()));
$this->assertFalse($privilegeService->isLoggedIn($testUser2->name)); $this->assertFalse($privilegeService->isLoggedIn($testUser2->getName()));
} }
public function testIsLoggedInByEmailString() public function testIsLoggedInByEmailString()
{ {
$testUser1 = new \Szurubooru\Entities\User(); $testUser1 = new \Szurubooru\Entities\User();
$testUser1->name = 'user1'; $testUser1->setName('user1');
$testUser1->email = 'dummy'; $testUser1->setEmail('dummy');
$testUser2 = new \Szurubooru\Entities\User(); $testUser2 = new \Szurubooru\Entities\User();
$testUser2->name = 'user2'; $testUser2->setName('user2');
$testUser2->email = 'godzilla'; $testUser2->setEmail('godzilla');
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
$privilegeService = $this->getPrivilegeService(); $privilegeService = $this->getPrivilegeService();
$this->assertTrue($privilegeService->isLoggedIn($testUser1->email)); $this->assertTrue($privilegeService->isLoggedIn($testUser1->getEmail()));
$this->assertFalse($privilegeService->isLoggedIn($testUser2->email)); $this->assertFalse($privilegeService->isLoggedIn($testUser2->getEmail()));
} }
public function testIsLoggedInByUserId() public function testIsLoggedInByUserId()
{ {
$testUser1 = new \Szurubooru\Entities\User(); $testUser1 = new \Szurubooru\Entities\User('dummy');
$testUser1->id = 'dummy'; $testUser2 = new \Szurubooru\Entities\User('godzilla');
$testUser2 = new \Szurubooru\Entities\User(); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
$testUser2->id = 'godzilla';
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1);
$privilegeService = $this->getPrivilegeService(); $privilegeService = $this->getPrivilegeService();
$this->assertTrue($privilegeService->isLoggedIn($testUser1)); $this->assertTrue($privilegeService->isLoggedIn($testUser1));
@ -72,10 +70,10 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testIsLoggedInByUserName() public function testIsLoggedInByUserName()
{ {
$testUser1 = new \Szurubooru\Entities\User(); $testUser1 = new \Szurubooru\Entities\User();
$testUser1->name = 'dummy'; $testUser1->setName('dummy');
$testUser2 = new \Szurubooru\Entities\User(); $testUser2 = new \Szurubooru\Entities\User();
$testUser2->name = 'godzilla'; $testUser2->setName('godzilla');
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
$privilegeService = $this->getPrivilegeService(); $privilegeService = $this->getPrivilegeService();
$this->assertFalse($privilegeService->isLoggedIn($testUser1)); $this->assertFalse($privilegeService->isLoggedIn($testUser1));
@ -85,8 +83,8 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testIsLoggedInByInvalidObject() public function testIsLoggedInByInvalidObject()
{ {
$testUser = new \Szurubooru\Entities\User(); $testUser = new \Szurubooru\Entities\User();
$testUser->name = 'dummy'; $testUser->setName('dummy');
$this->authServiceMock->method('getLoggedInUser')->willReturn($testUser); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser);
$rubbish = new \StdClass; $rubbish = new \StdClass;
$privilegeService = $this->getPrivilegeService(); $privilegeService = $this->getPrivilegeService();

View file

@ -32,8 +32,8 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testGettingByName() public function testGettingByName()
{ {
$testUser = new \Szurubooru\Entities\User; $testUser = new \Szurubooru\Entities\User;
$testUser->name = 'godzilla'; $testUser->setName('godzilla');
$this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($testUser);
$userService = $this->getUserService(); $userService = $this->getUserService();
$expected = $testUser; $expected = $testUser;
$actual = $userService->getByName('godzilla'); $actual = $userService->getByName('godzilla');
@ -50,8 +50,8 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testGettingById() public function testGettingById()
{ {
$testUser = new \Szurubooru\Entities\User; $testUser = new \Szurubooru\Entities\User;
$testUser->name = 'godzilla'; $testUser->setName('godzilla');
$this->userDaoMock->expects($this->once())->method('getById')->willReturn($testUser); $this->userDaoMock->expects($this->once())->method('findById')->willReturn($testUser);
$userService = $this->getUserService(); $userService = $this->getUserService();
$expected = $testUser; $expected = $testUser;
$actual = $userService->getById('godzilla'); $actual = $userService->getById('godzilla');
@ -68,7 +68,7 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testGettingFilteredUsers() public function testGettingFilteredUsers()
{ {
$mockUser = new \Szurubooru\Entities\User; $mockUser = new \Szurubooru\Entities\User;
$mockUser->name = 'user'; $mockUser->setName('user');
$expected = [$mockUser]; $expected = [$mockUser];
$this->userSearchService->method('getFiltered')->willReturn($expected); $this->userSearchService->method('getFiltered')->willReturn($expected);
@ -91,21 +91,21 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData->email = 'human@people.gov'; $formData->email = 'human@people.gov';
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$this->passwordServiceMock->method('getHash')->willReturn('hash'); $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('hash');
$this->timeServiceMock->method('getCurrentTime')->willReturn('now'); $this->timeServiceMock->expects($this->once())->method('getCurrentTime')->willReturn('now');
$this->userDaoMock->method('hasAnyUsers')->willReturn(true); $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true);
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->emailServiceMock->expects($this->never())->method('sendActivationEmail'); $this->emailServiceMock->expects($this->never())->method('sendActivationEmail');
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->createUser($formData); $savedUser = $userService->createUser($formData);
$this->assertEquals('user', $savedUser->name); $this->assertEquals('user', $savedUser->getName());
$this->assertEquals('human@people.gov', $savedUser->email); $this->assertEquals('human@people.gov', $savedUser->getEmail());
$this->assertNull($savedUser->emailUnconfirmed); $this->assertNull($savedUser->getEmailUnconfirmed());
$this->assertEquals('hash', $savedUser->passwordHash); $this->assertEquals('hash', $savedUser->getPasswordHash());
$this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->accessRank); $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->getAccessRank());
$this->assertEquals('now', $savedUser->registrationTime); $this->assertEquals('now', $savedUser->getRegistrationTime());
} }
public function testValidRegistrationWithMailActivation() public function testValidRegistrationWithMailActivation()
@ -116,10 +116,10 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData->email = 'human@people.gov'; $formData->email = 'human@people.gov';
$this->configMock->set('security/needEmailActivationToRegister', true); $this->configMock->set('security/needEmailActivationToRegister', true);
$this->passwordServiceMock->method('getHash')->willReturn('hash'); $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('hash');
$this->timeServiceMock->method('getCurrentTime')->willReturn('now'); $this->timeServiceMock->expects($this->once())->method('getCurrentTime')->willReturn('now');
$this->userDaoMock->method('hasAnyUsers')->willReturn(true); $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true);
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$testToken = new \Szurubooru\Entities\Token; $testToken = new \Szurubooru\Entities\Token;
$this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn($testToken); $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn($testToken);
@ -130,12 +130,12 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->createUser($formData); $savedUser = $userService->createUser($formData);
$this->assertEquals('user', $savedUser->name); $this->assertEquals('user', $savedUser->getName());
$this->assertNull($savedUser->email); $this->assertNull($savedUser->getEmail());
$this->assertEquals('human@people.gov', $savedUser->emailUnconfirmed); $this->assertEquals('human@people.gov', $savedUser->getEmailUnconfirmed());
$this->assertEquals('hash', $savedUser->passwordHash); $this->assertEquals('hash', $savedUser->getPasswordHash());
$this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->accessRank); $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->getAccessRank());
$this->assertEquals('now', $savedUser->registrationTime); $this->assertEquals('now', $savedUser->getRegistrationTime());
} }
public function testAccessRankOfFirstUser() public function testAccessRankOfFirstUser()
@ -146,13 +146,13 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData->email = 'email'; $formData->email = 'email';
$this->configMock->set('security/needEmailActivationToRegister', false); $this->configMock->set('security/needEmailActivationToRegister', false);
$this->userDaoMock->method('hasAnyUsers')->willReturn(false); $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(false);
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->createUser($formData); $savedUser = $userService->createUser($formData);
$this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, $savedUser->accessRank); $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, $savedUser->getAccessRank());
} }
public function testRegistrationWhenUserExists() public function testRegistrationWhenUserExists()
@ -162,12 +162,11 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData->password = 'password'; $formData->password = 'password';
$formData->email = 'email'; $formData->email = 'email';
$otherUser = new \Szurubooru\Entities\User; $otherUser = new \Szurubooru\Entities\User('yes, i exist in database');
$otherUser->id = 'yes, i exist in database';
$this->userDaoMock->method('hasAnyUsers')->willReturn(true); $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true);
$this->userDaoMock->method('getByName')->willReturn($otherUser); $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($otherUser);
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->never())->method('save');
$userService = $this->getUserService(); $userService = $this->getUserService();
@ -178,30 +177,29 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testUpdatingName() public function testUpdatingName()
{ {
$testUser = new \Szurubooru\Entities\User; $testUser = new \Szurubooru\Entities\User;
$testUser->name = 'wojtek'; $testUser->setName('wojtek');
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->userName = 'sebastian'; $formData->userName = 'sebastian';
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->updateUser($testUser, $formData); $savedUser = $userService->updateUser($testUser, $formData);
$this->assertEquals('sebastian', $savedUser->name); $this->assertEquals('sebastian', $savedUser->getName());
} }
public function testUpdatingNameToExisting() public function testUpdatingNameToExisting()
{ {
$testUser = new \Szurubooru\Entities\User; $testUser = new \Szurubooru\Entities\User;
$testUser->name = 'wojtek'; $testUser->setName('wojtek');
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->userName = 'sebastian'; $formData->userName = 'sebastian';
$otherUser = new \Szurubooru\Entities\User; $otherUser = new \Szurubooru\Entities\User('yes, i exist in database');
$otherUser->id = 'yes, i exist in database'; $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($otherUser);
$this->userDaoMock->method('getByName')->willReturn($otherUser); $this->userDaoMock->expects($this->never())->method('save');
$this->userDaoMock->method('save')->will($this->returnArgument(0));
$this->setExpectedException(\Exception::class, 'User with this name already exists'); $this->setExpectedException(\Exception::class, 'User with this name already exists');
$userService = $this->getUserService(); $userService = $this->getUserService();
@ -216,12 +214,12 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->email = 'hikari@geofront.gov'; $formData->email = 'hikari@geofront.gov';
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->updateUser($testUser, $formData); $savedUser = $userService->updateUser($testUser, $formData);
$this->assertEquals('hikari@geofront.gov', $savedUser->email); $this->assertEquals('hikari@geofront.gov', $savedUser->getEmail());
$this->assertNull($savedUser->emailUnconfirmed); $this->assertNull($savedUser->getEmailUnconfirmed());
} }
public function testUpdatingEmailWithConfirmation() public function testUpdatingEmailWithConfirmation()
@ -232,13 +230,13 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->email = 'hikari@geofront.gov'; $formData->email = 'hikari@geofront.gov';
$this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token());
$this->userDaoMock->method('save')->will($this->returnArgument(0)); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->updateUser($testUser, $formData); $savedUser = $userService->updateUser($testUser, $formData);
$this->assertNull($savedUser->email); $this->assertNull($savedUser->getEmail());
$this->assertEquals('hikari@geofront.gov', $savedUser->emailUnconfirmed); $this->assertEquals('hikari@geofront.gov', $savedUser->getEmailUnconfirmed());
} }
public function testUpdatingEmailWithConfirmationToExisting() public function testUpdatingEmailWithConfirmationToExisting()
@ -249,11 +247,10 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->email = 'hikari@geofront.gov'; $formData->email = 'hikari@geofront.gov';
$otherUser = new \Szurubooru\Entities\User; $otherUser = new \Szurubooru\Entities\User('yes, i exist in database');
$otherUser->id = 'yes, i exist in database'; $this->tokenServiceMock->expects($this->never())->method('createAndSaveToken');
$this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); $this->userDaoMock->expects($this->once())->method('findByEmail')->willReturn($otherUser);
$this->userDaoMock->method('getByEmail')->willReturn($otherUser); $this->userDaoMock->expects($this->never())->method('save');
$this->userDaoMock->method('save')->will($this->returnArgument(0));
$this->setExpectedException(\Exception::class, 'User with this e-mail already exists'); $this->setExpectedException(\Exception::class, 'User with this e-mail already exists');
$userService = $this->getUserService(); $userService = $this->getUserService();
@ -262,24 +259,22 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testUpdatingEmailToAlreadyConfirmed() public function testUpdatingEmailToAlreadyConfirmed()
{ {
$testUser = new \Szurubooru\Entities\User; $testUser = new \Szurubooru\Entities\User('yep, still me');
$testUser->email = 'hikari@geofront.gov'; $testUser->setEmail('hikari@geofront.gov');
$testUser->emailUnconfirmed = 'coolcat32@sakura.ne.jp'; $testUser->setEmailUnconfirmed('coolcat32@sakura.ne.jp');
$testUser->id = 5;
$formData = new \Szurubooru\FormData\UserEditFormData; $formData = new \Szurubooru\FormData\UserEditFormData;
$formData->email = 'hikari@geofront.gov'; $formData->email = 'hikari@geofront.gov';
$otherUser = new \Szurubooru\Entities\User; $otherUser = new \Szurubooru\Entities\User('yep, still me');
$otherUser->id = 5; $this->tokenServiceMock->expects($this->never())->method('createAndSaveToken');
$this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); $this->userDaoMock->expects($this->never())->method('findByEmail');
$this->userDaoMock->method('getByEmail')->willReturn($otherUser); $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->userDaoMock->method('save')->will($this->returnArgument(0));
$userService = $this->getUserService(); $userService = $this->getUserService();
$savedUser = $userService->updateUser($testUser, $formData); $savedUser = $userService->updateUser($testUser, $formData);
$this->assertEquals('hikari@geofront.gov', $savedUser->email); $this->assertEquals('hikari@geofront.gov', $savedUser->getEmail());
$this->assertNull($savedUser->emailUnconfirmed); $this->assertNull($savedUser->getEmailUnconfirmed());
} }
private function getUserService() private function getUserService()