public function hasRoleWorks()
{
$everybodyRole = new Policy\Role('Neos.Flow:Everybody');
$testRole = new Policy\Role('Acme.Demo:TestRole');
$mockAuthenticationManager = $this->createMock(AuthenticationManagerInterface::class);
$mockAuthenticationManager->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(true));
$mockPolicyService = $this->getAccessibleMock(Policy\PolicyService::class, ['getRole', 'initializeRolesFromPolicy']);
$mockPolicyService->expects($this->atLeastOnce())->method('getRole')->will($this->returnCallback(function ($roleIdentifier) use($everybodyRole) {
switch ($roleIdentifier) {
case 'Neos.Flow:Everybody':
return $everybodyRole;
}
}));
$account = $this->getAccessibleMock(Account::class, ['dummy']);
$account->_set('policyService', $mockPolicyService);
$account->setRoles([$testRole]);
$mockToken = $this->createMock(TokenInterface::class);
$mockToken->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(true));
$mockToken->expects($this->atLeastOnce())->method('getAccount')->will($this->returnValue($account));
$securityContext = $this->getAccessibleMock(Context::class, ['initialize', 'getAccount']);
$securityContext->expects($this->any())->method('getAccount')->will($this->returnValue($account));
$securityContext->_set('activeTokens', [$mockToken]);
$securityContext->_set('policyService', $mockPolicyService);
$securityContext->_set('authenticationManager', $mockAuthenticationManager);
$this->assertTrue($securityContext->hasRole('Acme.Demo:TestRole'));
$this->assertFalse($securityContext->hasRole('Foo.Bar:Baz'));
}