/** * Gets an Assertion event seeded with Mock objects. * * @param bool $isOneEmployeeAllowed if true, one employee in the employees collection gets job create permissions. * * @return AssertionEvent */ protected function getTestEvent($isOneEmployeeAllowed = true) { $employees = new ArrayCollection(); for ($i = 0; $i < 3; $i++) { $empUser = new User(); $empUser->setId('1234-' . $i); $perm = new EmployeePermissions(EmployeePermissionsInterface::JOBS_VIEW); if (2 == $i && $isOneEmployeeAllowed) { $perm->grant(EmployeePermissionsInterface::JOBS_CREATE); } $emp = new Employee($empUser, $perm); $employees->add($emp); } $org = $this->getMockBuilder('\\Organizations\\Entity\\OrganizationReference')->disableOriginalConstructor()->getMock(); $org->expects($this->once())->method('hasAssociation')->willReturn(true); $org->expects($this->once())->method('isOwner')->willReturn(false); $org->expects($this->once())->method('getEmployees')->willReturn($employees); $role = new User(); $role->setId('1234-2'); $role->setOrganization($org); $e = new AssertionEvent(); $e->setRole($role); return $e; }
/** * Does the managing of permissions work? * * That means, calls to grant, revoke, grantAll and revokeAll. * */ public function testManagingPermissions() { $target = new EmployeePermissions(); $target->grant(EmployeePermissions::JOBS_CREATE); $this->assertTrue($target->isAllowed(EmployeePermissions::JOBS_CREATE)); $target->revoke(EmployeePermissions::JOBS_CREATE); $this->assertFalse($target->isAllowed(EmployeePermissions::JOBS_CREATE)); // Test setting multiple permissions at once $target->setPermissions(18); // reset $target->grant(EmployeePermissions::JOBS_CHANGE, EmployeePermissions::APPLICATIONS_CHANGE); $this->assertTrue($target->isAllowed(EmployeePermissions::JOBS_CHANGE)); $this->assertTrue($target->isAllowed(EmployeePermissions::APPLICATIONS_CHANGE)); $target->revoke(EmployeePermissions::JOBS_CHANGE, EmployeePermissions::APPLICATIONS_CHANGE); $this->assertFalse($target->isAllowed(EmployeePermissions::JOBS_CHANGE)); $this->assertFalse($target->isAllowed(EmployeePermissions::APPLICATIONS_CHANGE)); // test grantAll / revokeAll $target->grantAll(); $this->assertEquals($target->getPermissions(), EmployeePermissions::ALL); $target->revokeAll(); $this->assertEquals($target->getPermissions(), EmployeePermissions::NONE); }