2014-05-06 13:07:24 +02:00
|
|
|
<?php
|
|
|
|
class AccessTest extends AbstractTest
|
|
|
|
{
|
|
|
|
public function testDefaultPrivilege()
|
|
|
|
{
|
|
|
|
//by default, all privileges are set to false
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessRanks1()
|
|
|
|
{
|
2014-05-13 21:08:07 +02:00
|
|
|
$user = $this->userMocker->mockSingle();
|
2014-05-06 13:07:24 +02:00
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Admin));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Moderator));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::PowerUser));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Registered));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Nobody));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessRanks2()
|
|
|
|
{
|
|
|
|
getConfig()->privileges->listPosts = 'power-user';
|
|
|
|
Access::init();
|
|
|
|
|
2014-05-13 21:08:07 +02:00
|
|
|
$user = $this->userMocker->mockSingle();
|
2014-05-06 13:07:24 +02:00
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Admin));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Moderator));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::PowerUser));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Registered));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Nobody));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
public function testSubPrivilegesOnlySub()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
2014-05-13 00:02:25 +02:00
|
|
|
getConfig()->privileges->{'listPosts.own'} = 'power-user';
|
2014-05-06 13:07:24 +02:00
|
|
|
Access::init();
|
|
|
|
|
2014-05-13 21:08:07 +02:00
|
|
|
$user = $this->userMocker->mockSingle();
|
2014-05-06 13:07:24 +02:00
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::PowerUser));
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));
|
2014-05-06 13:07:24 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
public function testSubPrivilegesSubAndGeneralNormalOrder()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
2014-05-13 00:02:25 +02:00
|
|
|
getConfig()->privileges->{'listPosts.own'} = 'power-user';
|
2014-05-06 13:07:24 +02:00
|
|
|
getConfig()->privileges->{'listPosts'} = 'admin';
|
|
|
|
Access::init();
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->testSubPrivilegesSubAndGeneral();
|
2014-05-06 13:07:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
public function testSubPrivilegesSubAndGeneralReverseOrder()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
|
|
|
getConfig()->privileges->{'listPosts'} = 'admin';
|
2014-05-13 00:02:25 +02:00
|
|
|
getConfig()->privileges->{'listPosts.own'} = 'power-user';
|
2014-05-06 13:07:24 +02:00
|
|
|
Access::init();
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->testSubPrivilegesSubAndGeneral();
|
2014-05-06 13:07:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
protected function testSubPrivilegesSubAndGeneral()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
2014-05-13 21:08:07 +02:00
|
|
|
$user = $this->userMocker->mockSingle();
|
2014-05-06 13:07:24 +02:00
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::PowerUser));
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user));
|
2014-05-06 13:07:24 +02:00
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Admin));
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));
|
2014-05-06 13:07:24 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
public function testSubPrivilegesMultipleSubAndGeneralNormalOrder()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
2014-05-13 00:02:25 +02:00
|
|
|
getConfig()->privileges->{'listPosts.own'} = 'power-user';
|
|
|
|
getConfig()->privileges->{'listPosts.all'} = 'admin';
|
2014-05-06 13:07:24 +02:00
|
|
|
getConfig()->privileges->{'listPosts'} = 'nobody';
|
|
|
|
Access::init();
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->testSubPrivilegesMultipleSubAndGeneral();
|
2014-05-06 13:07:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
public function testSubPrivilegesMultipleSubAndGeneralReverseOrder()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
|
|
|
getConfig()->privileges->{'listPosts'} = 'nobody';
|
2014-05-13 00:02:25 +02:00
|
|
|
getConfig()->privileges->{'listPosts.own'} = 'power-user';
|
|
|
|
getConfig()->privileges->{'listPosts.all'} = 'admin';
|
2014-05-06 13:07:24 +02:00
|
|
|
Access::init();
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->testSubPrivilegesMultipleSubAndGeneral();
|
2014-05-06 13:07:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:02:25 +02:00
|
|
|
protected function testSubPrivilegesMultipleSubAndGeneral()
|
2014-05-06 13:07:24 +02:00
|
|
|
{
|
2014-05-13 21:08:07 +02:00
|
|
|
$user = $this->userMocker->mockSingle();
|
2014-05-06 13:07:24 +02:00
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::PowerUser));
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user));
|
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));
|
2014-05-06 13:07:24 +02:00
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
|
|
|
|
$user->setAccessRank(new AccessRank(AccessRank::Admin));
|
2014-05-13 00:02:25 +02:00
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user));
|
|
|
|
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));
|
2014-05-06 13:07:24 +02:00
|
|
|
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
|
|
|
|
}
|
|
|
|
}
|