2014-09-01 19:43:49 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
|
|
|
|
|
|
|
final class UserServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
private $configMock;
|
2014-09-02 09:09:07 +02:00
|
|
|
private $validatorMock;
|
2014-09-01 19:43:49 +02:00
|
|
|
private $userDaoMock;
|
2014-09-03 19:07:53 +02:00
|
|
|
private $userSearchServiceMock;
|
2014-09-01 19:43:49 +02:00
|
|
|
private $passwordServiceMock;
|
|
|
|
private $emailServiceMock;
|
|
|
|
private $timeServiceMock;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->configMock = $this->mock(\Szurubooru\Config::class);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validatorMock = $this->mock(\Szurubooru\Validator::class);
|
2014-09-01 19:43:49 +02:00
|
|
|
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->userSearchService = $this->mock(\Szurubooru\Dao\Services\UserSearchService::class);
|
2014-09-01 19:43:49 +02:00
|
|
|
$this->passwordServiceMock = $this->mock(\Szurubooru\Services\PasswordService::class);
|
|
|
|
$this->emailServiceMock = $this->mock(\Szurubooru\Services\EmailService::class);
|
|
|
|
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
public function testGettingFilteredUsers()
|
|
|
|
{
|
|
|
|
$mockUser = new \Szurubooru\Entities\User();
|
|
|
|
$mockUser->name = 'user';
|
|
|
|
$expected = [$mockUser];
|
|
|
|
$this->userSearchService->method('getFiltered')->willReturn($expected);
|
|
|
|
|
|
|
|
$this->configMock->users = new \StdClass;
|
|
|
|
$this->configMock->users->usersPerPage = 1;
|
|
|
|
$searchFormData = new \Szurubooru\FormData\SearchFormData;
|
|
|
|
$searchFormData->query = '';
|
|
|
|
$searchFormData->order = 'joined';
|
|
|
|
$searchFormData->page = 2;
|
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$actual = $userService->getFiltered($searchFormData);
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2014-09-01 19:43:49 +02:00
|
|
|
public function testValidRegistration()
|
|
|
|
{
|
|
|
|
$formData = new \Szurubooru\FormData\RegistrationFormData;
|
|
|
|
$formData->name = 'user';
|
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'email';
|
|
|
|
|
|
|
|
$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));
|
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->register($formData);
|
|
|
|
|
|
|
|
$this->assertEquals('user', $savedUser->name);
|
|
|
|
$this->assertEquals('email', $savedUser->email);
|
|
|
|
$this->assertEquals('hash', $savedUser->passwordHash);
|
|
|
|
$this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, $savedUser->accessRank);
|
|
|
|
$this->assertEquals('now', $savedUser->registrationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessRankOfFirstUser()
|
|
|
|
{
|
|
|
|
$formData = new \Szurubooru\FormData\RegistrationFormData;
|
|
|
|
$formData->name = 'user';
|
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'email';
|
|
|
|
|
|
|
|
$this->userDaoMock->method('hasAnyUsers')->willReturn(false);
|
|
|
|
$this->userDaoMock->method('save')->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
$savedUser = $userService->register($formData);
|
|
|
|
|
|
|
|
$this->assertEquals(\Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, $savedUser->accessRank);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegistrationWhenUserExists()
|
|
|
|
{
|
|
|
|
$formData = new \Szurubooru\FormData\RegistrationFormData;
|
|
|
|
$formData->name = 'user';
|
|
|
|
$formData->password = 'password';
|
|
|
|
$formData->email = 'email';
|
|
|
|
|
|
|
|
$this->userDaoMock->method('hasAnyUsers')->willReturn(true);
|
|
|
|
$this->userDaoMock->method('getByName')->willReturn(new \Szurubooru\Entities\User());
|
|
|
|
$this->userDaoMock->method('save')->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$userService = $this->getUserService();
|
|
|
|
|
|
|
|
$this->setExpectedException(\Exception::class, 'User with this name already exists');
|
|
|
|
$savedUser = $userService->register($formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserService()
|
|
|
|
{
|
|
|
|
return new \Szurubooru\Services\UserService(
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->configMock,
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validatorMock,
|
2014-09-01 19:43:49 +02:00
|
|
|
$this->userDaoMock,
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->userSearchService,
|
2014-09-01 19:43:49 +02:00
|
|
|
$this->passwordServiceMock,
|
|
|
|
$this->emailServiceMock,
|
|
|
|
$this->timeServiceMock);
|
|
|
|
}
|
|
|
|
}
|