diff --git a/src/Controllers/UserAvatarController.php b/src/Controllers/UserAvatarController.php index d2d31435..b1d24fd9 100644 --- a/src/Controllers/UserAvatarController.php +++ b/src/Controllers/UserAvatarController.php @@ -29,10 +29,10 @@ final class UserAvatarController extends AbstractController { $user = $this->userService->getByName($userName); - switch ($user->avatarStyle) + switch ($user->getAvatarStyle()) { 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; $this->serveFromUrl($url); break; diff --git a/src/Controllers/ViewProxies/TokenViewProxy.php b/src/Controllers/ViewProxies/TokenViewProxy.php index 6504b7a7..c2a3e828 100644 --- a/src/Controllers/ViewProxies/TokenViewProxy.php +++ b/src/Controllers/ViewProxies/TokenViewProxy.php @@ -8,8 +8,8 @@ class TokenViewProxy extends AbstractViewProxy $result = new \StdClass; if ($token) { - $result->name = $token->name; - $result->purpose = $token->purpose; + $result->name = $token->getName(); + $result->purpose = $token->getPurpose(); } return $result; } diff --git a/src/Controllers/ViewProxies/UserViewProxy.php b/src/Controllers/ViewProxies/UserViewProxy.php index 7860a40b..f74cd376 100644 --- a/src/Controllers/ViewProxies/UserViewProxy.php +++ b/src/Controllers/ViewProxies/UserViewProxy.php @@ -15,26 +15,26 @@ class UserViewProxy extends AbstractViewProxy $result = new \StdClass; if ($user) { - $result->id = $user->id; - $result->name = $user->name; - $result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->accessRank); - $result->registrationTime = $user->registrationTime; - $result->lastLoginTime = $user->lastLoginTime; - $result->avatarStyle = $user->avatarStyle; + $result->id = $user->getId(); + $result->name = $user->getName(); + $result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->getAccessRank()); + $result->registrationTime = $user->getRegistrationTime(); + $result->lastLoginTime = $user->getLastLoginTime(); + $result->avatarStyle = $user->getAvatarStyle(); if ($this->privilegeService->isLoggedIn($user)) { - $result->browsingSettings = $user->browsingSettings; + $result->browsingSettings = $user->getBrowsingSettings(); } if ($this->privilegeService->hasPrivilege(\Szurubooru\Privilege::VIEW_ALL_EMAIL_ADDRESSES) or $this->privilegeService->isLoggedIn($user)) { - $result->email = $user->email; - $result->emailUnconfirmed = $user->emailUnconfirmed; + $result->email = $user->getEmail(); + $result->emailUnconfirmed = $user->getEmailUnconfirmed(); } - $result->confirmed = !$user->emailUnconfirmed; + $result->confirmed = !$user->getEmailUnconfirmed(); } return $result; } diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index c20798fb..62df4082 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -32,11 +32,11 @@ abstract class AbstractDao implements ICrudDao public function save(&$entity) { $arrayEntity = $this->entityConverter->toArray($entity); - if ($entity->id) + if ($entity->getId()) { $savedId = $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; } else @@ -47,7 +47,7 @@ abstract class AbstractDao implements ICrudDao return $entity; } - public function getAll() + public function findAll() { $entities = []; foreach ($this->collection->find() as $key => $arrayEntity) @@ -58,9 +58,9 @@ abstract class AbstractDao implements ICrudDao 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); } @@ -69,8 +69,8 @@ abstract class AbstractDao implements ICrudDao $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)]); } } diff --git a/src/Dao/EntityConverter.php b/src/Dao/EntityConverter.php index b7fed431..a2d7a356 100644 --- a/src/Dao/EntityConverter.php +++ b/src/Dao/EntityConverter.php @@ -12,8 +12,15 @@ final class EntityConverter public function toArray($entity) { - $arrayEntity = (array) $entity; - if (isset($entity->id)) + $arrayEntity = []; + $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']; unset($arrayEntity['id']); @@ -23,12 +30,28 @@ final class EntityConverter public function toEntity($arrayEntity) { - $entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName); - if (isset($entity->_id)) + if ($arrayEntity === null) + 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; - unset($entity->_id); + if (isset($arrayEntity[$reflectionProperty->getName()])) + { + $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; } } diff --git a/src/Dao/ICrudDao.php b/src/Dao/ICrudDao.php index ae3a0c13..9e38b127 100644 --- a/src/Dao/ICrudDao.php +++ b/src/Dao/ICrudDao.php @@ -3,9 +3,9 @@ namespace Szurubooru\Dao; interface ICrudDao { - public function getAll(); + public function findAll(); - public function getById($objectId); + public function findById($objectId); public function save(&$object); diff --git a/src/Dao/TokenDao.php b/src/Dao/TokenDao.php index dcef9f7e..77ce40dc 100644 --- a/src/Dao/TokenDao.php +++ b/src/Dao/TokenDao.php @@ -8,7 +8,7 @@ class TokenDao extends AbstractDao parent::__construct($databaseConnection, 'tokens', '\Szurubooru\Entities\Token'); } - public function getByName($tokenName) + public function findByName($tokenName) { $arrayEntity = $this->collection->findOne(['name' => $tokenName]); return $this->entityConverter->toEntity($arrayEntity); diff --git a/src/Dao/UserDao.php b/src/Dao/UserDao.php index 5cef016c..d141df80 100644 --- a/src/Dao/UserDao.php +++ b/src/Dao/UserDao.php @@ -9,13 +9,13 @@ class UserDao extends AbstractDao implements ICrudDao parent::__construct($databaseConnection, 'users', '\Szurubooru\Entities\User'); } - public function getByName($userName) + public function findByName($userName) { $arrayEntity = $this->collection->findOne(['name' => $userName]); return $this->entityConverter->toEntity($arrayEntity); } - public function getByEmail($userEmail, $allowUnconfirmed = false) + public function findByEmail($userEmail, $allowUnconfirmed = false) { $arrayEntity = $this->collection->findOne(['email' => $userEmail]); if (!$arrayEntity and $allowUnconfirmed) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 57782749..0bc2dbf5 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -3,5 +3,15 @@ namespace Szurubooru\Entities; abstract class Entity { - public $id = null; + protected $id = null; + + public function __construct($id = null) + { + $this->id = $id; + } + + public function getId() + { + return $this->id; + } } diff --git a/src/Entities/Post.php b/src/Entities/Post.php index 02f2e5a5..47efd227 100644 --- a/src/Entities/Post.php +++ b/src/Entities/Post.php @@ -3,5 +3,15 @@ namespace Szurubooru\Entities; final class Post extends Entity { - public $name; + protected $name; + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } } diff --git a/src/Entities/Tag.php b/src/Entities/Tag.php index 7d7e2bfe..075f6b86 100644 --- a/src/Entities/Tag.php +++ b/src/Entities/Tag.php @@ -3,6 +3,26 @@ namespace Szurubooru\Entities; final class Tag extends Entity { - public $name; - public $usages; + protected $name; + 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; + } } diff --git a/src/Entities/Token.php b/src/Entities/Token.php index cec002fc..af6f960a 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -7,7 +7,37 @@ final class Token extends Entity const PURPOSE_ACTIVATE = 'activate'; const PURPOSE_PASSWORD_RESET = 'passwordReset'; - public $name; - public $purpose; - public $additionalData; + protected $name; + protected $purpose; + 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; + } } diff --git a/src/Entities/User.php b/src/Entities/User.php index fb313d38..da8196d2 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -14,13 +14,103 @@ final class User extends Entity const AVATAR_STYLE_MANUAL = 'manual'; const AVATAR_STYLE_BLANK = 'blank'; - public $name; - public $email; - public $emailUnconfirmed; - public $passwordHash; - public $accessRank; - public $registrationTime; - public $lastLoginTime; - public $avatarStyle; - public $browsingSettings; + protected $name; + protected $email; + protected $emailUnconfirmed; + protected $passwordHash; + protected $accessRank; + protected $registrationTime; + protected $lastLoginTime; + protected $avatarStyle; + 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; + } } diff --git a/src/Helpers/TypeHelper.php b/src/Helpers/TypeHelper.php deleted file mode 100644 index bc4231ee..00000000 --- a/src/Helpers/TypeHelper.php +++ /dev/null @@ -1,20 +0,0 @@ -doFinalChecksOnUser($user); $passwordHash = $this->passwordService->getHash($formData->password); - if ($user->passwordHash !== $passwordHash) + if ($user->getPasswordHash() !== $passwordHash) throw new \InvalidArgumentException('Specified password is invalid.'); $this->loginToken = $this->createAndSaveLoginToken($user); @@ -59,10 +59,10 @@ class AuthService 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.'); - $user = $this->userService->getById($token->additionalData); + $user = $this->userService->getById($token->getAdditionalData()); $this->doFinalChecksOnUser($user); $this->loginToken = $token; @@ -73,8 +73,8 @@ class AuthService public function getAnonymousUser() { $user = new \Szurubooru\Entities\User(); - $user->name = 'Anonymous user'; - $user->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS; + $user->setName('Anonymous user'); + $user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS); return $user; } @@ -95,12 +95,12 @@ class AuthService 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) { - 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.'); } } diff --git a/src/Services/EmailService.php b/src/Services/EmailService.php index 6292f72e..e2bb46a2 100644 --- a/src/Services/EmailService.php +++ b/src/Services/EmailService.php @@ -12,25 +12,25 @@ class EmailService 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.'); $mailSubject = $this->tokenize($this->config->mail->passwordResetSubject); $mailBody = $this->tokenizeFile( $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) { - if (!$user->emailUnconfirmed) + if (!$user->getEmailUnconfirmed()) { throw new \BadMethodCallException( - $user->email + $user->getEmail() ? 'E-mail for this account is already confirmed.' : 'An e-mail address is needed to activate the account.'); } @@ -39,10 +39,10 @@ class EmailService $mailBody = $this->tokenizeFile( $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) diff --git a/src/Services/PrivilegeService.php b/src/Services/PrivilegeService.php index 6f6111ea..90017fa7 100644 --- a/src/Services/PrivilegeService.php +++ b/src/Services/PrivilegeService.php @@ -29,7 +29,7 @@ class PrivilegeService public function getCurrentPrivileges() { - $currentAccessRank = $this->authService->getLoggedInUser()->accessRank; + $currentAccessRank = $this->authService->getLoggedInUser()->getAccessRank(); $currentAccessRankName = \Szurubooru\Helpers\EnumHelper::accessRankToString($currentAccessRank); if (!isset($this->privilegeMap[$currentAccessRankName])) return []; @@ -58,16 +58,16 @@ class PrivilegeService $loggedInUser = $this->authService->getLoggedInUser(); 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)) { - if ($loggedInUser->email) + if ($loggedInUser->getEmail()) { - if ($loggedInUser->email === $userIdentifier) + if ($loggedInUser->getEmail() === $userIdentifier) return true; } - return $loggedInUser->name === $userIdentifier; + return $loggedInUser->getName() === $userIdentifier; } else { diff --git a/src/Services/TokenService.php b/src/Services/TokenService.php index 6e644a1a..b10254a1 100644 --- a/src/Services/TokenService.php +++ b/src/Services/TokenService.php @@ -12,7 +12,7 @@ class TokenService public function getByName($tokenName) { - $token = $this->tokenDao->getByName($tokenName); + $token = $this->tokenDao->findByName($tokenName); if (!$token) throw new \InvalidArgumentException('Token with identifier "' . $tokenName . '" not found.'); return $token; @@ -31,9 +31,9 @@ class TokenService public function createAndSaveToken($additionalData, $tokenPurpose) { $token = new \Szurubooru\Entities\Token(); - $token->name = sha1(date('r') . uniqid() . microtime(true)); - $token->additionalData = $additionalData; - $token->purpose = $tokenPurpose; + $token->setName(sha1(date('r') . uniqid() . microtime(true))); + $token->setAdditionalData($additionalData); + $token->setPurpose($tokenPurpose); $this->invalidateByAdditionalData($additionalData); $this->tokenDao->save($token); return $token; diff --git a/src/Services/UserService.php b/src/Services/UserService.php index e57a0e2a..ef5c0b85 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -40,11 +40,11 @@ class UserService public function getByNameOrEmail($userNameOrEmail, $allowUnconfirmed = false) { - $user = $this->userDao->getByName($userNameOrEmail); + $user = $this->userDao->findByName($userNameOrEmail); if ($user) return $user; - $user = $this->userDao->getByEmail($userNameOrEmail, $allowUnconfirmed); + $user = $this->userDao->findByEmail($userNameOrEmail, $allowUnconfirmed); if ($user) return $user; @@ -53,7 +53,7 @@ class UserService public function getByName($userName) { - $user = $this->userDao->getByName($userName); + $user = $this->userDao->findByName($userName); if (!$user) throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.'); return $user; @@ -61,7 +61,7 @@ class UserService public function getById($userId) { - $user = $this->userDao->getById($userId); + $user = $this->userDao->findById($userId); if (!$user) throw new \InvalidArgumentException('User with id "' . $userId . '" was not found.'); return $user; @@ -79,11 +79,11 @@ class UserService $formData->validate($this->validator); $user = new \Szurubooru\Entities\User(); - $user->registrationTime = $this->timeService->getCurrentTime(); - $user->lastLoginTime = null; - $user->accessRank = $this->userDao->hasAnyUsers() + $user->setRegistrationTime($this->timeService->getCurrentTime()); + $user->setLastLoginTime(null); + $user->setAccessRank($this->userDao->hasAnyUsers() ? \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->updateUserPassword($user, $formData->password); @@ -122,7 +122,7 @@ class UserService public function updateUserAvatarStyle(\Szurubooru\Entities\User $user, $newAvatarStyle) { - $user->avatarStyle = $newAvatarStyle; + $user->setAvatarStyle($newAvatarStyle); } public function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContentInBase64) @@ -135,47 +135,47 @@ class UserService public function updateUserName(\Szurubooru\Entities\User $user, $newName) { $this->assertNoUserWithThisName($user, $newName); - $user->name = $newName; + $user->setName($newName); } 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) { - if ($user->email === $newEmail) + if ($user->getEmail() === $newEmail) { - $user->emailUnconfirmed = null; + $user->setEmailUnconfirmed(null); } else { $this->assertNoUserWithThisEmail($user, $newEmail); - $user->emailUnconfirmed = $newEmail; + $user->setEmailUnconfirmed($newEmail); $user = $this->sendActivationEmailIfNeeded($user); } } public function updateUserAccessRank(\Szurubooru\Entities\User $user, $newAccessRank) { - $user->accessRank = $newAccessRank; + $user->setAccessRank($newAccessRank); } public function updateUserBrowsingSettings(\Szurubooru\Entities\User $user, $newBrowsingSettings) { - $user->browsingSettings = $newBrowsingSettings; + $user->setBrowsingSettings($newBrowsingSettings); } public function updateUserLastLoginTime(\Szurubooru\Entities\User $user) { - $user->lastLoginTime = $this->timeService->getCurrentTime(); + $user->setLastLoginTime($this->timeService->getCurrentTime()); $this->userDao->save($user); } public function deleteUser(\Szurubooru\Entities\User $user) { - $this->userDao->deleteById($user->id); + $this->userDao->deleteById($user->getId()); $avatarSource = $this->getCustomAvatarSourcePath($user); $this->fileService->delete($avatarSource); @@ -184,43 +184,43 @@ class UserService 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); } 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.'); - $user = $this->getByName($token->additionalData); + $user = $this->getByName($token->getAdditionalData()); $newPassword = $this->passwordService->getRandomPassword(); - $user->passwordHash = $this->passwordService->getHash($newPassword); + $user->setPasswordHash($this->passwordService->getHash($newPassword)); $this->userDao->save($user); - $this->tokenService->invalidateByName($token->name); + $this->tokenService->invalidateByName($token->getName()); return $newPassword; } 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); } 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.'); - $user = $this->getByName($token->additionalData); + $user = $this->getByName($token->getAdditionalData()); $user = $this->confirmUserEmail($user); $this->userDao->save($user); - $this->tokenService->invalidateByName($token->name); + $this->tokenService->invalidateByName($token->getName()); } public function getCustomAvatarSourcePath(\Szurubooru\Entities\User $user) { - return 'avatars' . DIRECTORY_SEPARATOR . $user->id; + return 'avatars' . DIRECTORY_SEPARATOR . $user->getId(); } public function getBlankAvatarSourcePath() @@ -230,7 +230,7 @@ class UserService 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); } @@ -251,27 +251,27 @@ class UserService //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: //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->emailUnconfirmed = null; + $user->setEmail($user->getEmailUnconfirmed()); + $user->setEmailUnconfirmed(null); } return $user; } private function assertNoUserWithThisName(\Szurubooru\Entities\User $owner, $nameToCheck) { - $userWithThisName = $this->userDao->getByName($nameToCheck); - if ($userWithThisName and $userWithThisName->id !== $owner->id) + $userWithThisName = $this->userDao->findByName($nameToCheck); + if ($userWithThisName and $userWithThisName->getId() !== $owner->getId()) throw new \DomainException('User with this name already exists.'); } private function assertNoUserWithThisEmail(\Szurubooru\Entities\User $owner, $emailToCheck) { - $userWithThisEmail = $this->userDao->getByEmail($emailToCheck); - if ($userWithThisEmail and $userWithThisEmail->id !== $owner->id) + $userWithThisEmail = $this->userDao->findByEmail($emailToCheck); + if ($userWithThisEmail and $userWithThisEmail->getId() !== $owner->getId()) throw new \DomainException('User with this e-mail already exists.'); } } diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index bf8fdc86..8bf65a94 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -8,23 +8,24 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post = new \Szurubooru\Entities\Post(); - $post->name = 'test'; + $post->setName('test'); $savedPost = $postDao->save($post); - $this->assertEquals('test', $post->name); - $this->assertNotNull($savedPost->id); + $this->assertEquals('test', $post->getName()); + $this->assertNotNull($savedPost->getId()); } public function testUpdating() { $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post = new \Szurubooru\Entities\Post(); - $post->name = 'test'; + $post->setName('test'); $post = $postDao->save($post); - $id = $post->id; - $post->name .= '2'; + $this->assertEquals('test', $post->getName()); + $id = $post->getId(); + $post->setName($post->getName() . '2'); $post = $postDao->save($post); - $this->assertEquals('test2', $post->name); - $this->assertEquals($id, $post->id); + $this->assertEquals('test2', $post->getName()); + $this->assertEquals($id, $post->getId()); } public function testGettingAll() @@ -32,17 +33,17 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post1 = new \Szurubooru\Entities\Post(); - $post1->name = 'test2'; + $post1->setName('test2'); $post2 = new \Szurubooru\Entities\Post(); - $post2->name = 'test2'; + $post2->setName('test2'); $postDao->save($post1); $postDao->save($post2); - $actual = $postDao->getAll(); + $actual = $postDao->findAll(); $expected = [ - $post1->id => $post1, - $post2->id => $post2, + $post1->getId() => $post1, + $post2->getId() => $post2, ]; $this->assertEquals($expected, $actual); @@ -53,15 +54,15 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post1 = new \Szurubooru\Entities\Post(); - $post1->name = 'test2'; + $post1->setName('test2'); $post2 = new \Szurubooru\Entities\Post(); - $post2->name = 'test2'; + $post2->setName('test2'); $postDao->save($post1); $postDao->save($post2); - $actualPost1 = $postDao->getById($post1->id); - $actualPost2 = $postDao->getById($post2->id); + $actualPost1 = $postDao->findById($post1->getId()); + $actualPost2 = $postDao->findById($post2->getId()); $this->assertEquals($post1, $actualPost1); $this->assertEquals($post2, $actualPost2); } @@ -71,20 +72,20 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post1 = new \Szurubooru\Entities\Post(); - $post1->name = 'test2'; + $post1->setName('test2'); $post2 = new \Szurubooru\Entities\Post(); - $post2->name = 'test2'; + $post2->setName('test2'); $postDao->save($post1); $postDao->save($post2); $postDao->deleteAll(); - $actualPost1 = $postDao->getById($post1->id); - $actualPost2 = $postDao->getById($post2->id); + $actualPost1 = $postDao->findById($post1->getId()); + $actualPost2 = $postDao->findById($post2->getId()); $this->assertEquals(null, $actualPost1); $this->assertEquals(null, $actualPost2); - $this->assertEquals(0, count($postDao->getAll())); + $this->assertEquals(0, count($postDao->findAll())); } public function testDeletingById() @@ -92,19 +93,19 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $post1 = new \Szurubooru\Entities\Post(); - $post1->name = 'test2'; + $post1->setName('test2'); $post2 = new \Szurubooru\Entities\Post(); - $post2->name = 'test2'; + $post2->setName('test2'); $postDao->save($post1); $postDao->save($post2); - $postDao->deleteById($post1->id); + $postDao->deleteById($post1->getId()); - $actualPost1 = $postDao->getById($post1->id); - $actualPost2 = $postDao->getById($post2->id); + $actualPost1 = $postDao->findById($post1->getId()); + $actualPost2 = $postDao->findById($post2->getId()); $this->assertEquals(null, $actualPost1); $this->assertEquals($actualPost2, $actualPost2); - $this->assertEquals(1, count($postDao->getAll())); + $this->assertEquals(1, count($postDao->findAll())); } } diff --git a/tests/Dao/Services/UserSearchServiceTest.php b/tests/Dao/Services/UserSearchServiceTest.php index d634a67b..57d4aec6 100644 --- a/tests/Dao/Services/UserSearchServiceTest.php +++ b/tests/Dao/Services/UserSearchServiceTest.php @@ -24,11 +24,11 @@ class UserSearchServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase public function testSorting() { $user1 = new \Szurubooru\Entities\User(); - $user1->name = 'reginald'; - $user1->registrationTime = date('c', mktime(3, 2, 1)); + $user1->setName('reginald'); + $user1->setRegistrationTime(date('c', mktime(3, 2, 1))); $user2 = new \Szurubooru\Entities\User(); - $user2->name = 'beartato'; - $user2->registrationTime = date('c', mktime(1, 2, 3)); + $user2->setName('beartato'); + $user2->setRegistrationTime(date('c', mktime(1, 2, 3))); $this->userDao->save($user1); $this->userDao->save($user2); diff --git a/tests/Dao/TokenDaoTest.php b/tests/Dao/TokenDaoTest.php index a61a16b6..3f2d8b84 100644 --- a/tests/Dao/TokenDaoTest.php +++ b/tests/Dao/TokenDaoTest.php @@ -8,11 +8,11 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); $token = new \Szurubooru\Entities\Token(); - $token->name = 'test'; + $token->setName('test'); $tokenDao->save($token); $expected = $token; - $actual = $tokenDao->getByName($token->name); + $actual = $tokenDao->findByName($token->getName()); $this->assertEquals($actual, $expected); } @@ -21,7 +21,7 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); - $actual = $tokenDao->getByName('rubbish'); + $actual = $tokenDao->findByName('rubbish'); $this->assertNull($actual); } diff --git a/tests/Dao/UserDaoTest.php b/tests/Dao/UserDaoTest.php index aeb6033b..86a085f6 100644 --- a/tests/Dao/UserDaoTest.php +++ b/tests/Dao/UserDaoTest.php @@ -8,11 +8,11 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $userDao = $this->getUserDao(); $user = new \Szurubooru\Entities\User(); - $user->name = 'test'; + $user->setName('test'); $userDao->save($user); $expected = $user; - $actual = $userDao->getByName($user->name); + $actual = $userDao->findByName($user->getName()); $this->assertEquals($actual, $expected); } @@ -21,7 +21,7 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $userDao = $this->getUserDao(); - $actual = $userDao->getByName('rubbish'); + $actual = $userDao->findByName('rubbish'); $this->assertNull($actual); } @@ -33,7 +33,7 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertFalse($userDao->hasAnyUsers()); $user = new \Szurubooru\Entities\User(); - $user->name = 'test'; + $user->setName('test'); $userDao->save($user); $this->assertTrue($userDao->hasAnyUsers()); diff --git a/tests/Services/AuthServiceTest.php b/tests/Services/AuthServiceTest.php index b0b605bf..1e825124 100644 --- a/tests/Services/AuthServiceTest.php +++ b/tests/Services/AuthServiceTest.php @@ -22,11 +22,11 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testInvalidPassword() { $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->name = 'dummy'; - $testUser->passwordHash = 'hash'; + $testUser->setName('dummy'); + $testUser->setPasswordHash('hash'); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $this->setExpectedException(\Exception::class, 'Specified password is invalid'); @@ -40,18 +40,17 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testValidCredentials() { $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->id = 'an unusual database identifier'; - $testUser->name = 'dummy'; - $testUser->passwordHash = 'hash'; + $testUser = new \Szurubooru\Entities\User('an unusual database identifier'); + $testUser->setName('dummy'); + $testUser->setPasswordHash('hash'); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $testToken = new \Szurubooru\Entities\Token(); - $testToken->name = 'mummy'; + $testToken->setName('mummy'); $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->with( - $testUser->id, + $testUser->getId(), \Szurubooru\Entities\Token::PURPOSE_LOGIN)->willReturn($testToken); $authService = $this->getAuthService(); @@ -63,17 +62,17 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase $this->assertTrue($authService->isLoggedIn()); $this->assertEquals($testUser, $authService->getLoggedInUser()); $this->assertNotNull($authService->getLoginToken()); - $this->assertEquals('mummy', $authService->getLoginToken()->name); + $this->assertEquals('mummy', $authService->getLoginToken()->getName()); } public function testValidCredentialsUnconfirmedEmail() { $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->name = 'dummy'; - $testUser->passwordHash = 'hash'; + $testUser->setName('dummy'); + $testUser->setPasswordHash('hash'); $this->userServiceMock->expects($this->once())->method('getByNameOrEmail')->willReturn($testUser); $this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet'); @@ -101,15 +100,14 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testValidToken() { $this->configMock->set('security/needEmailActivationToRegister', false); - $testUser = new \Szurubooru\Entities\User(); - $testUser->id = 5; - $testUser->name = 'dummy'; + $testUser = new \Szurubooru\Entities\User(5); + $testUser->setName('dummy'); $this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser); $testToken = new \Szurubooru\Entities\Token(); - $testToken->name = 'dummy_token'; - $testToken->additionalData = $testUser->id; - $testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN; + $testToken->setName('dummy_token'); + $testToken->setAdditionalData($testUser->getId()); + $testToken->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN); $authService = $this->getAuthService(); $authService->loginFromToken($testToken); @@ -117,16 +115,16 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase $this->assertTrue($authService->isLoggedIn()); $this->assertEquals($testUser, $authService->getLoggedInUser()); $this->assertNotNull($authService->getLoginToken()); - $this->assertEquals('dummy_token', $authService->getLoginToken()->name); + $this->assertEquals('dummy_token', $authService->getLoginToken()->getName()); } public function testValidTokenInvalidPurpose() { $this->configMock->set('security/needEmailActivationToRegister', false); $testToken = new \Szurubooru\Entities\Token(); - $testToken->name = 'dummy_token'; - $testToken->additionalData = 'whatever'; - $testToken->purpose = null; + $testToken->setName('dummy_token'); + $testToken->setAdditionalData('whatever'); + $testToken->setPurpose(null); $this->setExpectedException(\Exception::class, 'This token is not a login token'); $authService = $this->getAuthService(); @@ -140,15 +138,14 @@ class AuthServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testValidTokenUnconfirmedEmail() { $this->configMock->set('security/needEmailActivationToRegister', true); - $testUser = new \Szurubooru\Entities\User(); - $testUser->id = 5; - $testUser->name = 'dummy'; + $testUser = new \Szurubooru\Entities\User(5); + $testUser->setName('dummy'); $this->userServiceMock->expects($this->once())->method('getById')->willReturn($testUser); $testToken = new \Szurubooru\Entities\Token(); - $testToken->name = 'dummy_token'; - $testToken->additionalData = $testUser->id; - $testToken->purpose = \Szurubooru\Entities\Token::PURPOSE_LOGIN; + $testToken->setName('dummy_token'); + $testToken->setAdditionalData($testUser->getId()); + $testToken->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN); $this->setExpectedException(\Exception::class, 'User didn\'t confirm mail yet'); $authService = $this->getAuthService(); diff --git a/tests/Services/PrivilegeServiceTest.php b/tests/Services/PrivilegeServiceTest.php index 5f6d3fd5..8b0429da 100644 --- a/tests/Services/PrivilegeServiceTest.php +++ b/tests/Services/PrivilegeServiceTest.php @@ -16,9 +16,9 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testReadingConfig() { $testUser = new \Szurubooru\Entities\User(); - $testUser->name = 'dummy'; - $testUser->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser); + $testUser->setName('dummy'); + $testUser->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_POWER_USER); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser); $privilege = \Szurubooru\Privilege::LIST_USERS; $this->configMock->set('security/privileges/' . $privilege, 'powerUser'); @@ -31,38 +31,36 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testIsLoggedInByNameString() { $testUser1 = new \Szurubooru\Entities\User(); - $testUser1->name = 'dummy'; + $testUser1->setName('dummy'); $testUser2 = new \Szurubooru\Entities\User(); - $testUser2->name = 'godzilla'; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); + $testUser2->setName('godzilla'); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService(); - $this->assertTrue($privilegeService->isLoggedIn($testUser1->name)); - $this->assertFalse($privilegeService->isLoggedIn($testUser2->name)); + $this->assertTrue($privilegeService->isLoggedIn($testUser1->getName())); + $this->assertFalse($privilegeService->isLoggedIn($testUser2->getName())); } public function testIsLoggedInByEmailString() { $testUser1 = new \Szurubooru\Entities\User(); - $testUser1->name = 'user1'; - $testUser1->email = 'dummy'; + $testUser1->setName('user1'); + $testUser1->setEmail('dummy'); $testUser2 = new \Szurubooru\Entities\User(); - $testUser2->name = 'user2'; - $testUser2->email = 'godzilla'; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); + $testUser2->setName('user2'); + $testUser2->setEmail('godzilla'); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService(); - $this->assertTrue($privilegeService->isLoggedIn($testUser1->email)); - $this->assertFalse($privilegeService->isLoggedIn($testUser2->email)); + $this->assertTrue($privilegeService->isLoggedIn($testUser1->getEmail())); + $this->assertFalse($privilegeService->isLoggedIn($testUser2->getEmail())); } public function testIsLoggedInByUserId() { - $testUser1 = new \Szurubooru\Entities\User(); - $testUser1->id = 'dummy'; - $testUser2 = new \Szurubooru\Entities\User(); - $testUser2->id = 'godzilla'; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); + $testUser1 = new \Szurubooru\Entities\User('dummy'); + $testUser2 = new \Szurubooru\Entities\User('godzilla'); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService(); $this->assertTrue($privilegeService->isLoggedIn($testUser1)); @@ -72,10 +70,10 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testIsLoggedInByUserName() { $testUser1 = new \Szurubooru\Entities\User(); - $testUser1->name = 'dummy'; + $testUser1->setName('dummy'); $testUser2 = new \Szurubooru\Entities\User(); - $testUser2->name = 'godzilla'; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser1); + $testUser2->setName('godzilla'); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService(); $this->assertFalse($privilegeService->isLoggedIn($testUser1)); @@ -85,8 +83,8 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testIsLoggedInByInvalidObject() { $testUser = new \Szurubooru\Entities\User(); - $testUser->name = 'dummy'; - $this->authServiceMock->method('getLoggedInUser')->willReturn($testUser); + $testUser->setName('dummy'); + $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser); $rubbish = new \StdClass; $privilegeService = $this->getPrivilegeService(); diff --git a/tests/Services/UserServiceTest.php b/tests/Services/UserServiceTest.php index ae9ffd5b..d44b0180 100644 --- a/tests/Services/UserServiceTest.php +++ b/tests/Services/UserServiceTest.php @@ -32,8 +32,8 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testGettingByName() { $testUser = new \Szurubooru\Entities\User; - $testUser->name = 'godzilla'; - $this->userDaoMock->expects($this->once())->method('getByName')->willReturn($testUser); + $testUser->setName('godzilla'); + $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($testUser); $userService = $this->getUserService(); $expected = $testUser; $actual = $userService->getByName('godzilla'); @@ -50,8 +50,8 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testGettingById() { $testUser = new \Szurubooru\Entities\User; - $testUser->name = 'godzilla'; - $this->userDaoMock->expects($this->once())->method('getById')->willReturn($testUser); + $testUser->setName('godzilla'); + $this->userDaoMock->expects($this->once())->method('findById')->willReturn($testUser); $userService = $this->getUserService(); $expected = $testUser; $actual = $userService->getById('godzilla'); @@ -68,7 +68,7 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testGettingFilteredUsers() { $mockUser = new \Szurubooru\Entities\User; - $mockUser->name = 'user'; + $mockUser->setName('user'); $expected = [$mockUser]; $this->userSearchService->method('getFiltered')->willReturn($expected); @@ -91,21 +91,21 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData->email = 'human@people.gov'; $this->configMock->set('security/needEmailActivationToRegister', false); - $this->passwordServiceMock->method('getHash')->willReturn('hash'); - $this->timeServiceMock->method('getCurrentTime')->willReturn('now'); - $this->userDaoMock->method('hasAnyUsers')->willReturn(true); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('hash'); + $this->timeServiceMock->expects($this->once())->method('getCurrentTime')->willReturn('now'); + $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true); + $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->emailServiceMock->expects($this->never())->method('sendActivationEmail'); $userService = $this->getUserService(); $savedUser = $userService->createUser($formData); - $this->assertEquals('user', $savedUser->name); - $this->assertEquals('human@people.gov', $savedUser->email); - $this->assertNull($savedUser->emailUnconfirmed); - $this->assertEquals('hash', $savedUser->passwordHash); - $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->accessRank); - $this->assertEquals('now', $savedUser->registrationTime); + $this->assertEquals('user', $savedUser->getName()); + $this->assertEquals('human@people.gov', $savedUser->getEmail()); + $this->assertNull($savedUser->getEmailUnconfirmed()); + $this->assertEquals('hash', $savedUser->getPasswordHash()); + $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->getAccessRank()); + $this->assertEquals('now', $savedUser->getRegistrationTime()); } public function testValidRegistrationWithMailActivation() @@ -116,10 +116,10 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData->email = 'human@people.gov'; $this->configMock->set('security/needEmailActivationToRegister', true); - $this->passwordServiceMock->method('getHash')->willReturn('hash'); - $this->timeServiceMock->method('getCurrentTime')->willReturn('now'); - $this->userDaoMock->method('hasAnyUsers')->willReturn(true); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $this->passwordServiceMock->expects($this->once())->method('getHash')->willReturn('hash'); + $this->timeServiceMock->expects($this->once())->method('getCurrentTime')->willReturn('now'); + $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true); + $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $testToken = new \Szurubooru\Entities\Token; $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn($testToken); @@ -130,12 +130,12 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $userService = $this->getUserService(); $savedUser = $userService->createUser($formData); - $this->assertEquals('user', $savedUser->name); - $this->assertNull($savedUser->email); - $this->assertEquals('human@people.gov', $savedUser->emailUnconfirmed); - $this->assertEquals('hash', $savedUser->passwordHash); - $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->accessRank); - $this->assertEquals('now', $savedUser->registrationTime); + $this->assertEquals('user', $savedUser->getName()); + $this->assertNull($savedUser->getEmail()); + $this->assertEquals('human@people.gov', $savedUser->getEmailUnconfirmed()); + $this->assertEquals('hash', $savedUser->getPasswordHash()); + $this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->getAccessRank()); + $this->assertEquals('now', $savedUser->getRegistrationTime()); } public function testAccessRankOfFirstUser() @@ -146,13 +146,13 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData->email = 'email'; $this->configMock->set('security/needEmailActivationToRegister', false); - $this->userDaoMock->method('hasAnyUsers')->willReturn(false); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(false); + $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $userService = $this->getUserService(); $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() @@ -162,12 +162,11 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData->password = 'password'; $formData->email = 'email'; - $otherUser = new \Szurubooru\Entities\User; - $otherUser->id = 'yes, i exist in database'; + $otherUser = new \Szurubooru\Entities\User('yes, i exist in database'); - $this->userDaoMock->method('hasAnyUsers')->willReturn(true); - $this->userDaoMock->method('getByName')->willReturn($otherUser); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(true); + $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($otherUser); + $this->userDaoMock->expects($this->never())->method('save'); $userService = $this->getUserService(); @@ -178,30 +177,29 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testUpdatingName() { $testUser = new \Szurubooru\Entities\User; - $testUser->name = 'wojtek'; + $testUser->setName('wojtek'); $formData = new \Szurubooru\FormData\UserEditFormData; $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(); $savedUser = $userService->updateUser($testUser, $formData); - $this->assertEquals('sebastian', $savedUser->name); + $this->assertEquals('sebastian', $savedUser->getName()); } public function testUpdatingNameToExisting() { $testUser = new \Szurubooru\Entities\User; - $testUser->name = 'wojtek'; + $testUser->setName('wojtek'); $formData = new \Szurubooru\FormData\UserEditFormData; $formData->userName = 'sebastian'; - $otherUser = new \Szurubooru\Entities\User; - $otherUser->id = 'yes, i exist in database'; - $this->userDaoMock->method('getByName')->willReturn($otherUser); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $otherUser = new \Szurubooru\Entities\User('yes, i exist in database'); + $this->userDaoMock->expects($this->once())->method('findByName')->willReturn($otherUser); + $this->userDaoMock->expects($this->never())->method('save'); $this->setExpectedException(\Exception::class, 'User with this name already exists'); $userService = $this->getUserService(); @@ -216,12 +214,12 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData = new \Szurubooru\FormData\UserEditFormData; $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(); $savedUser = $userService->updateUser($testUser, $formData); - $this->assertEquals('hikari@geofront.gov', $savedUser->email); - $this->assertNull($savedUser->emailUnconfirmed); + $this->assertEquals('hikari@geofront.gov', $savedUser->getEmail()); + $this->assertNull($savedUser->getEmailUnconfirmed()); } public function testUpdatingEmailWithConfirmation() @@ -232,13 +230,13 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData = new \Szurubooru\FormData\UserEditFormData; $formData->email = 'hikari@geofront.gov'; - $this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); + $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $userService = $this->getUserService(); $savedUser = $userService->updateUser($testUser, $formData); - $this->assertNull($savedUser->email); - $this->assertEquals('hikari@geofront.gov', $savedUser->emailUnconfirmed); + $this->assertNull($savedUser->getEmail()); + $this->assertEquals('hikari@geofront.gov', $savedUser->getEmailUnconfirmed()); } public function testUpdatingEmailWithConfirmationToExisting() @@ -249,11 +247,10 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase $formData = new \Szurubooru\FormData\UserEditFormData; $formData->email = 'hikari@geofront.gov'; - $otherUser = new \Szurubooru\Entities\User; - $otherUser->id = 'yes, i exist in database'; - $this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); - $this->userDaoMock->method('getByEmail')->willReturn($otherUser); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $otherUser = new \Szurubooru\Entities\User('yes, i exist in database'); + $this->tokenServiceMock->expects($this->never())->method('createAndSaveToken'); + $this->userDaoMock->expects($this->once())->method('findByEmail')->willReturn($otherUser); + $this->userDaoMock->expects($this->never())->method('save'); $this->setExpectedException(\Exception::class, 'User with this e-mail already exists'); $userService = $this->getUserService(); @@ -262,24 +259,22 @@ final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testUpdatingEmailToAlreadyConfirmed() { - $testUser = new \Szurubooru\Entities\User; - $testUser->email = 'hikari@geofront.gov'; - $testUser->emailUnconfirmed = 'coolcat32@sakura.ne.jp'; - $testUser->id = 5; + $testUser = new \Szurubooru\Entities\User('yep, still me'); + $testUser->setEmail('hikari@geofront.gov'); + $testUser->setEmailUnconfirmed('coolcat32@sakura.ne.jp'); $formData = new \Szurubooru\FormData\UserEditFormData; $formData->email = 'hikari@geofront.gov'; - $otherUser = new \Szurubooru\Entities\User; - $otherUser->id = 5; - $this->tokenServiceMock->method('createAndSaveToken')->willReturn(new \Szurubooru\Entities\Token()); - $this->userDaoMock->method('getByEmail')->willReturn($otherUser); - $this->userDaoMock->method('save')->will($this->returnArgument(0)); + $otherUser = new \Szurubooru\Entities\User('yep, still me'); + $this->tokenServiceMock->expects($this->never())->method('createAndSaveToken'); + $this->userDaoMock->expects($this->never())->method('findByEmail'); + $this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $userService = $this->getUserService(); $savedUser = $userService->updateUser($testUser, $formData); - $this->assertEquals('hikari@geofront.gov', $savedUser->email); - $this->assertNull($savedUser->emailUnconfirmed); + $this->assertEquals('hikari@geofront.gov', $savedUser->getEmail()); + $this->assertNull($savedUser->getEmailUnconfirmed()); } private function getUserService()