2014-09-01 20:51:59 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Dao;
|
|
|
|
|
|
|
|
final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|
|
|
{
|
2014-09-20 12:45:56 +02:00
|
|
|
private $fileServiceMock;
|
|
|
|
private $thumbnailServiceMock;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
|
|
|
|
$this->thumbnailServiceMock = $this->mock(\Szurubooru\Services\ThumbnailService::class);
|
|
|
|
}
|
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
public function testRetrievingByValidName()
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$userDao = $this->getUserDao();
|
2014-09-01 20:51:59 +02:00
|
|
|
|
2014-09-14 16:16:15 +02:00
|
|
|
$user = $this->getTestUser();
|
2014-09-01 20:51:59 +02:00
|
|
|
$userDao->save($user);
|
2014-09-14 16:16:15 +02:00
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
$expected = $user;
|
2014-09-13 23:58:13 +02:00
|
|
|
$actual = $userDao->findByName($user->getName());
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->assertEntitiesEqual($actual, $expected);
|
2014-09-01 20:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrievingByInvalidName()
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$userDao = $this->getUserDao();
|
2014-09-01 20:51:59 +02:00
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$actual = $userDao->findByName('rubbish');
|
2014-09-01 20:51:59 +02:00
|
|
|
|
|
|
|
$this->assertNull($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckingUserPresence()
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$userDao = $this->getUserDao();
|
2014-09-01 20:51:59 +02:00
|
|
|
$this->assertFalse($userDao->hasAnyUsers());
|
|
|
|
|
2014-09-14 16:16:15 +02:00
|
|
|
$user = $this->getTestUser();
|
2014-09-01 20:51:59 +02:00
|
|
|
$userDao->save($user);
|
|
|
|
$this->assertTrue($userDao->hasAnyUsers());
|
|
|
|
}
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-09-20 12:45:56 +02:00
|
|
|
public function testNotLoadingAvatarContentForNewUsers()
|
|
|
|
{
|
|
|
|
$userDao = $this->getUserDao();
|
|
|
|
$user = $this->getTestUser();
|
|
|
|
$user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_MANUAL);
|
|
|
|
$userDao->save($user);
|
|
|
|
|
|
|
|
$this->assertNull($user->getCustomAvatarSourceContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoadingContentUsersForExistingUsers()
|
|
|
|
{
|
|
|
|
$userDao = $this->getUserDao();
|
|
|
|
$user = $this->getTestUser();
|
|
|
|
$user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_MANUAL);
|
|
|
|
$userDao->save($user);
|
|
|
|
|
|
|
|
$user = $userDao->findById($user->getId());
|
|
|
|
|
|
|
|
$this->fileServiceMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('load')
|
|
|
|
->with($user->getCustomAvatarSourceContentPath())->willReturn('whatever');
|
|
|
|
|
|
|
|
$this->assertEquals('whatever', $user->getCustomAvatarSourceContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingContent()
|
|
|
|
{
|
|
|
|
$userDao = $this->getUserDao();
|
|
|
|
$user = $this->getTestUser();
|
|
|
|
$user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_MANUAL);
|
|
|
|
$user->setCustomAvatarSourceContent('whatever');
|
|
|
|
|
|
|
|
$this->thumbnailServiceMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deleteUsedThumbnails')
|
|
|
|
->with($this->callback(
|
|
|
|
function($subject) use ($user)
|
|
|
|
{
|
|
|
|
return $subject == $user->getCustomAvatarSourceContentPath();
|
|
|
|
}));
|
|
|
|
|
|
|
|
$this->fileServiceMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('save')
|
|
|
|
->with($this->callback(
|
|
|
|
function($subject) use ($user)
|
|
|
|
{
|
|
|
|
//callback is used because ->save() will create id, which is going to be used by the function below
|
|
|
|
return $subject == $user->getCustomAvatarSourceContentPath();
|
|
|
|
}), 'whatever');
|
|
|
|
|
|
|
|
$userDao->save($user);
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
private function getUserDao()
|
|
|
|
{
|
2014-09-20 12:45:56 +02:00
|
|
|
return new \Szurubooru\Dao\UserDao(
|
|
|
|
$this->databaseConnection,
|
|
|
|
$this->fileServiceMock,
|
|
|
|
$this->thumbnailServiceMock);
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
2014-09-14 16:16:15 +02:00
|
|
|
|
|
|
|
private function getTestUser()
|
|
|
|
{
|
|
|
|
$user = new \Szurubooru\Entities\User();
|
|
|
|
$user->setName('test');
|
|
|
|
$user->setPasswordHash('whatever');
|
2014-09-28 16:26:44 +02:00
|
|
|
$user->setLastLoginTime(date('c', mktime(1, 2, 3)));
|
|
|
|
$user->setRegistrationTime(date('c', mktime(3, 2, 1)));
|
2014-09-16 11:16:48 +02:00
|
|
|
$user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER);
|
2014-09-14 16:16:15 +02:00
|
|
|
return $user;
|
|
|
|
}
|
2014-09-01 20:51:59 +02:00
|
|
|
}
|