2014-09-01 19:43:49 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\UserDao;
|
|
|
|
use Szurubooru\Entities\Token;
|
|
|
|
use Szurubooru\Entities\User;
|
|
|
|
use Szurubooru\FormData\RegistrationFormData;
|
|
|
|
use Szurubooru\FormData\UserEditFormData;
|
|
|
|
use Szurubooru\Services\EmailService;
|
|
|
|
use Szurubooru\Services\PasswordService;
|
|
|
|
use Szurubooru\Services\TimeService;
|
|
|
|
use Szurubooru\Services\TokenService;
|
|
|
|
use Szurubooru\Services\UserService;
|
|
|
|
use Szurubooru\Tests\AbstractTestCase;
|
|
|
|
use Szurubooru\Validator;
|
|
|
|
|
|
|
|
final class UserServiceTest extends AbstractTestCase
|
2014-09-01 19:43:49 +02:00
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
private $configMock;
|
2014-09-02 09:09:07 +02:00
|
|
|
private $validatorMock;
|
2014-09-14 18:41:14 +02:00
|
|
|
private $transactionManagerMock;
|
2014-09-01 19:43:49 +02:00
|
|
|
private $userDaoMock;
|
|
|
|
private $passwordServiceMock;
|
|
|
|
private $emailServiceMock;
|
|
|
|
private $timeServiceMock;
|
2014-09-08 13:06:32 +02:00
|
|
|
private $tokenServiceMock;
|
2014-09-01 19:43:49 +02:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-09-10 17:42:28 +02:00
|
|
|
parent::setUp();
|
2014-09-05 20:08:54 +02:00
|
|
|
$this->configMock = $this->mockConfig();
|
2014-09-14 18:41:14 +02:00
|
|
|
$this->transactionManagerMock = $this->mockTransactionManager();
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->validatorMock = $this->mock(Validator::class);
|
|
|
|
$this->userDaoMock = $this->mock(UserDao::class);
|
|
|
|
$this->passwordServiceMock = $this->mock(PasswordService::class);
|
|
|
|
$this->emailServiceMock = $this->mock(EmailService::class);
|
|
|
|
$this->timeServiceMock = $this->mock(TimeService::class);
|
|
|
|
$this->tokenServiceMock = $this->mock(TokenService::class);
|
2014-09-01 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 08:20:31 +02:00
|
|
|
public function testGettingByName()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('godzilla');
|
|
|
|
$this->userDaoMock->expects($this->once())->method('findByName')->willReturn($testUser);
|
2014-09-08 08:20:31 +02:00
|
|
|
$userService = $this->getUserService();
|
|
|
|
$expected = $testUser;
|
|
|
|
$actual = $userService->getByName('godzilla');
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingByNameNonExistentUsers()
|
|
|
|
{
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with name "godzilla" was not found.');
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$userService->getByName('godzilla');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingById()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('godzilla');
|
|
|
|
$this->userDaoMock->expects($this->once())->method('findById')->willReturn($testUser);
|
2014-09-08 08:20:31 +02:00
|
|
|
$userService = $this->getUserService();
|
|
|
|
$expected = $testUser;
|
|
|
|
$actual = $userService->getById('godzilla');
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingByIdNonExistentUsers()
|
|
|
|
{
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with id "godzilla" was not found.');
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$userService->getById('godzilla');
|
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function testValidRegistrationWithoutMailActivation()
|
2014-09-01 19:43:49 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new RegistrationFormData;
|
2014-09-07 00:33:46 +02:00
|
|
|
$formData->userName = 'user';
|
2014-09-01 19:43:49 +02:00
|
|
|
$formData->password = 'password';
|
2014-09-08 13:06:32 +02:00
|
|
|
$formData->email = 'human@people.gov';
|
2014-09-01 19:43:49 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-10-12 11:44:41 +02:00
|
|
|
$this->configMock->set('security/defaultAccessRank', 'regularUser');
|
2014-10-05 22:26:56 +02:00
|
|
|
$this->passwordServiceMock->expects($this->once())->method('getRandomPassword')->willReturn('salt');
|
|
|
|
$this->passwordServiceMock->expects($this->once())->method('getHash')->with('password', 'salt')->willReturn('hash');
|
2014-09-13 23:58:13 +02:00
|
|
|
$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));
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->emailServiceMock->expects($this->never())->method('sendActivationEmail');
|
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->createUser($formData);
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('user', $savedUser->getName());
|
|
|
|
$this->assertEquals('human@people.gov', $savedUser->getEmail());
|
|
|
|
$this->assertNull($savedUser->getEmailUnconfirmed());
|
|
|
|
$this->assertEquals('hash', $savedUser->getPasswordHash());
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(User::ACCESS_RANK_REGULAR_USER, $savedUser->getAccessRank());
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('now', $savedUser->getRegistrationTime());
|
2014-09-14 16:44:57 +02:00
|
|
|
$this->assertTrue($savedUser->isAccountConfirmed());
|
2014-09-08 13:06:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidRegistrationWithMailActivation()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new RegistrationFormData;
|
2014-09-08 13:06:32 +02:00
|
|
|
$formData->userName = 'user';
|
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'human@people.gov';
|
|
|
|
|
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', true);
|
2014-10-12 11:44:41 +02:00
|
|
|
$this->configMock->set('security/defaultAccessRank', 'powerUser');
|
2014-10-05 22:26:56 +02:00
|
|
|
$this->passwordServiceMock->expects($this->once())->method('getRandomPassword')->willReturn('salt');
|
|
|
|
$this->passwordServiceMock->expects($this->once())->method('getHash')->with('password', 'salt')->willReturn('hash');
|
2014-09-13 23:58:13 +02:00
|
|
|
$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));
|
2014-09-08 13:06:32 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$testToken = new Token;
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn($testToken);
|
|
|
|
$this->emailServiceMock->expects($this->once())->method('sendActivationEmail')->with(
|
|
|
|
$this->anything(),
|
|
|
|
$testToken);
|
2014-09-01 19:43:49 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
2014-09-07 00:33:46 +02:00
|
|
|
$savedUser = $userService->createUser($formData);
|
2014-09-01 19:43:49 +02:00
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('user', $savedUser->getName());
|
|
|
|
$this->assertNull($savedUser->getEmail());
|
|
|
|
$this->assertEquals('human@people.gov', $savedUser->getEmailUnconfirmed());
|
|
|
|
$this->assertEquals('hash', $savedUser->getPasswordHash());
|
2014-10-12 11:44:41 +02:00
|
|
|
$this->assertEquals(User::ACCESS_RANK_POWER_USER, $savedUser->getAccessRank());
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('now', $savedUser->getRegistrationTime());
|
2014-09-14 16:44:57 +02:00
|
|
|
$this->assertFalse($savedUser->isAccountConfirmed());
|
2014-09-01 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessRankOfFirstUser()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new RegistrationFormData;
|
2014-09-07 00:33:46 +02:00
|
|
|
$formData->userName = 'user';
|
2014-09-01 19:43:49 +02:00
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'email';
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->userDaoMock->expects($this->once())->method('hasAnyUsers')->willReturn(false);
|
|
|
|
$this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-09-01 19:43:49 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
2014-09-07 00:33:46 +02:00
|
|
|
$savedUser = $userService->createUser($formData);
|
2014-09-01 19:43:49 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(User::ACCESS_RANK_ADMINISTRATOR, $savedUser->getAccessRank());
|
2014-09-01 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegistrationWhenUserExists()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new RegistrationFormData;
|
2014-09-07 00:33:46 +02:00
|
|
|
$formData->userName = 'user';
|
2014-09-01 19:43:49 +02:00
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'email';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$otherUser = new User('yes, i exist in database');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
2014-10-12 11:44:41 +02:00
|
|
|
$this->configMock->set('security/defaultAccessRank', 'restrictedUser');
|
2014-09-13 23:58:13 +02:00
|
|
|
$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');
|
2014-09-01 19:43:49 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with this name already exists');
|
2014-09-07 00:33:46 +02:00
|
|
|
$savedUser = $userService->createUser($formData);
|
2014-09-01 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 18:22:15 +02:00
|
|
|
public function testUpdatingName()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('wojtek');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->userName = 'sebastian';
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->updateUser($testUser, $formData);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('sebastian', $savedUser->getName());
|
2014-09-10 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatingNameToExisting()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('wojtek');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->userName = 'sebastian';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$otherUser = new User('yes, i exist in database');
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->userDaoMock->expects($this->once())->method('findByName')->willReturn($otherUser);
|
|
|
|
$this->userDaoMock->expects($this->never())->method('save');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with this name already exists');
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->updateUser($testUser, $formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatingEmailWithoutConfirmation()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-10 18:22:15 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', false);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->email = 'hikari@geofront.gov';
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->updateUser($testUser, $formData);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('hikari@geofront.gov', $savedUser->getEmail());
|
|
|
|
$this->assertNull($savedUser->getEmailUnconfirmed());
|
2014-09-14 16:44:57 +02:00
|
|
|
$this->assertTrue($savedUser->isAccountConfirmed());
|
2014-09-10 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatingEmailWithConfirmation()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-10 18:22:15 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', true);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->email = 'hikari@geofront.gov';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->tokenServiceMock->expects($this->once())->method('createAndSaveToken')->willReturn(new Token());
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->userDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->updateUser($testUser, $formData);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertNull($savedUser->getEmail());
|
|
|
|
$this->assertEquals('hikari@geofront.gov', $savedUser->getEmailUnconfirmed());
|
2014-09-14 16:44:57 +02:00
|
|
|
$this->assertFalse($savedUser->isAccountConfirmed());
|
2014-09-10 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatingEmailWithConfirmationToExisting()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User;
|
2014-09-10 18:22:15 +02:00
|
|
|
$this->configMock->set('security/needEmailActivationToRegister', true);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->email = 'hikari@geofront.gov';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$otherUser = new User('yes, i exist in database');
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->tokenServiceMock->expects($this->never())->method('createAndSaveToken');
|
|
|
|
$this->userDaoMock->expects($this->once())->method('findByEmail')->willReturn($otherUser);
|
|
|
|
$this->userDaoMock->expects($this->never())->method('save');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with this e-mail already exists');
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$userService->updateUser($testUser, $formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatingEmailToAlreadyConfirmed()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$testUser = new User('yep, still me');
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setEmail('hikari@geofront.gov');
|
2014-09-14 16:44:57 +02:00
|
|
|
$testUser->setAccountConfirmed(true);
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setEmailUnconfirmed('coolcat32@sakura.ne.jp');
|
2014-09-10 18:22:15 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData;
|
2014-09-10 18:22:15 +02:00
|
|
|
$formData->email = 'hikari@geofront.gov';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$otherUser = new User('yep, still me');
|
2014-09-13 23:58:13 +02:00
|
|
|
$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));
|
2014-09-10 18:22:15 +02:00
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->updateUser($testUser, $formData);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('hikari@geofront.gov', $savedUser->getEmail());
|
|
|
|
$this->assertNull($savedUser->getEmailUnconfirmed());
|
2014-09-14 16:44:57 +02:00
|
|
|
$this->assertTrue($savedUser->isAccountConfirmed());
|
2014-09-10 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 19:43:49 +02:00
|
|
|
private function getUserService()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new UserService(
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->configMock,
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validatorMock,
|
2014-09-14 18:41:14 +02:00
|
|
|
$this->transactionManagerMock,
|
2014-09-01 19:43:49 +02:00
|
|
|
$this->userDaoMock,
|
|
|
|
$this->passwordServiceMock,
|
|
|
|
$this->emailServiceMock,
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->timeServiceMock,
|
|
|
|
$this->tokenServiceMock);
|
2014-09-01 19:43:49 +02:00
|
|
|
}
|
|
|
|
}
|