2014-09-06 10:00:26 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
|
|
|
|
|
|
|
class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|
|
|
{
|
|
|
|
private $configMock;
|
|
|
|
private $authServiceMock;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-09-10 17:42:28 +02:00
|
|
|
parent::setUp();
|
2014-09-06 10:00:26 +02:00
|
|
|
$this->configMock = $this->mockConfig();
|
|
|
|
$this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadingConfig()
|
|
|
|
{
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('dummy');
|
|
|
|
$testUser->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_POWER_USER);
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser);
|
2014-09-06 10:00:26 +02:00
|
|
|
|
2014-09-06 17:54:02 +02:00
|
|
|
$privilege = \Szurubooru\Privilege::LIST_USERS;
|
2014-09-06 10:00:26 +02:00
|
|
|
$this->configMock->set('security/privileges/' . $privilege, 'powerUser');
|
|
|
|
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
|
|
|
$this->assertEquals([$privilege], $privilegeService->getCurrentPrivileges());
|
|
|
|
$this->assertTrue($privilegeService->hasPrivilege($privilege));
|
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function testIsLoggedInByNameString()
|
2014-09-06 10:00:26 +02:00
|
|
|
{
|
|
|
|
$testUser1 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser1->setName('dummy');
|
2014-09-06 10:00:26 +02:00
|
|
|
$testUser2 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser2->setName('godzilla');
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
|
2014-09-06 10:00:26 +02:00
|
|
|
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertTrue($privilegeService->isLoggedIn($testUser1->getName()));
|
|
|
|
$this->assertFalse($privilegeService->isLoggedIn($testUser2->getName()));
|
2014-09-06 10:00:26 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function testIsLoggedInByEmailString()
|
|
|
|
{
|
|
|
|
$testUser1 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser1->setName('user1');
|
|
|
|
$testUser1->setEmail('dummy');
|
2014-09-08 13:06:32 +02:00
|
|
|
$testUser2 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser2->setName('user2');
|
|
|
|
$testUser2->setEmail('godzilla');
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
|
2014-09-08 13:06:32 +02:00
|
|
|
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertTrue($privilegeService->isLoggedIn($testUser1->getEmail()));
|
|
|
|
$this->assertFalse($privilegeService->isLoggedIn($testUser2->getEmail()));
|
2014-09-08 13:06:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 19:19:30 +02:00
|
|
|
public function testIsLoggedInByUserId()
|
|
|
|
{
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser1 = new \Szurubooru\Entities\User('dummy');
|
|
|
|
$testUser2 = new \Szurubooru\Entities\User('godzilla');
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
|
2014-09-10 19:19:30 +02:00
|
|
|
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
|
|
|
$this->assertTrue($privilegeService->isLoggedIn($testUser1));
|
|
|
|
$this->assertFalse($privilegeService->isLoggedIn($testUser2));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsLoggedInByUserName()
|
2014-09-06 10:00:26 +02:00
|
|
|
{
|
|
|
|
$testUser1 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser1->setName('dummy');
|
2014-09-06 10:00:26 +02:00
|
|
|
$testUser2 = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser2->setName('godzilla');
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
|
2014-09-06 10:00:26 +02:00
|
|
|
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
2014-09-10 19:19:30 +02:00
|
|
|
$this->assertFalse($privilegeService->isLoggedIn($testUser1));
|
2014-09-06 10:00:26 +02:00
|
|
|
$this->assertFalse($privilegeService->isLoggedIn($testUser2));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsLoggedInByInvalidObject()
|
|
|
|
{
|
|
|
|
$testUser = new \Szurubooru\Entities\User();
|
2014-09-13 23:58:13 +02:00
|
|
|
$testUser->setName('dummy');
|
|
|
|
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser);
|
2014-09-06 10:00:26 +02:00
|
|
|
|
|
|
|
$rubbish = new \StdClass;
|
|
|
|
$privilegeService = $this->getPrivilegeService();
|
|
|
|
$this->setExpectedException(\InvalidArgumentException::class);
|
|
|
|
$this->assertTrue($privilegeService->isLoggedIn($rubbish));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPrivilegeService()
|
|
|
|
{
|
|
|
|
return new \Szurubooru\Services\PrivilegeService(
|
|
|
|
$this->configMock,
|
|
|
|
$this->authServiceMock);
|
|
|
|
}
|
|
|
|
}
|