public function testAddRoleWithSameName() { $user = new RoleAggregate(); $role1 = new Role('One'); $role2 = new Role('One'); $user->addRole($role1); $user->addRole($role2); $this->assertEquals(1, count($user->getRoles())); $this->assertSame($role1, $user->getRole('One')); }
public function testEdgeConditionAggregateLastAddedWins() { $acl = new Acl(); $user = new Role('User'); $moderator = new Role('Moderator'); $page = new Resource('Page'); $userGroup = new RoleAggregate(); $userGroup->addRole($moderator); $userGroup->addRole($user); $acl->addRule($user, $page, new Rule('View'), true); $acl->addRule($moderator, $page, new Rule('View'), false); $this->assertTrue($acl->isAllowed($userGroup, 'Page', 'View')); $userGroup->removeRole('User'); $this->assertFalse($acl->isAllowed($userGroup, 'Page', 'View')); $userGroup->addRole($user); $this->assertTrue($acl->isAllowed($userGroup, 'Page', 'View')); $acl = new Acl(); $userGroup = new RoleAggregate(); $userGroup->addRole($moderator); $userGroup->addRole($user); // changing rule orders don't change result $acl->addRule($moderator, $page, new Rule('View'), false); $acl->addRule($user, $page, new Rule('View'), true); $this->assertTrue($acl->isAllowed($userGroup, 'Page', 'View')); $userGroup->removeRole('User'); $this->assertFalse($acl->isAllowed($userGroup, 'Page', 'View')); $userGroup->addRole($user); $this->assertTrue($acl->isAllowed($userGroup, 'Page', 'View')); // test case when priority matter $acl = new Acl(); $userGroup = new RoleAggregate(); $userGroup->addRole($moderator); $userGroup->addRole($user); $contact = new Resource('Contact'); $page->addChild($contact); $acl->addRule($moderator, $contact, new Rule('View'), true); $acl->addRule($user, $page, new Rule('View'), false); // user rule match first but moderator has higher priority $this->assertTrue($acl->isAllowed($userGroup, 'Contact', 'View')); $acl->addRule($user, $contact, new Rule('View'), false); // now priorities are equal $this->assertFalse($acl->isAllowed($userGroup, 'Contact', 'View')); }