private function prepareUserAuthenticationForUnknownRole() { $userEntity = UserEntityProvider::createEntityWithRandomData(); $ref = new \ReflectionClass(UserEntity::class); $prop = $ref->getProperty('allowedRoles'); $prop->setAccessible(true); $prop->setValue($userEntity, ['admin', self::UNKNOWN_USER_ROLE]); $userEntity->setRole(self::UNKNOWN_USER_ROLE); $this->prepareAuthenticateMock(true, $userEntity); }
public function testInvoke_WhenUserIsLogged() { $userEntity = UserEntityProvider::createEntityWithRandomData(); $resource = 'ModuleA\\Sub'; $privilege = 'ControllerA:ActionB'; $this->viewMock->expects($this->once())->method('identity')->will($this->returnValue($userEntity)); $this->aclMock->expects($this->once())->method('isAllowed')->with($userEntity->getRole(), strtolower($resource), strtolower($privilege))->will($this->returnValue(true)); $result = $this->testedObject->__invoke($resource, $privilege); $this->assertTrue($result); }
public function testAuthenticate_WhenVerifyFailed() { $userEntity = UserEntityProvider::createEntityWithRandomData(); $differentPassword = uniqid('password'); $this->testedObject->setIdentity($userEntity->getLogin())->setCredential($differentPassword); $this->userRepositoryMock->expects($this->once())->method('findOneBy')->with(['login' => $userEntity->getLogin()])->will($this->returnValue($userEntity)); $this->cryptMock->expects($this->once())->method('verify')->with($differentPassword, $userEntity->getPassword())->will($this->returnValue(false)); $result = $this->testedObject->authenticate(); $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isValid()); $this->assertSame(null, $result->getIdentity()); }
public function testCheckAccess_WithInvalidRequest_WhenUserIsAdmin() { $userEntity = UserEntityProvider::createEntityWithRandomData(); $event = $this->prepareEvent(); $this->authServiceMock->expects($this->once())->method('getIdentity')->will($this->returnValue($userEntity)); $this->namesResolverMock->expects($this->once())->method('resolve')->with($event)->will($this->returnValue(['module', 'controller', 'action'])); $this->aclMock->expects($this->once())->method('isAllowed')->with($userEntity->getRole(), 'module', 'controller:action')->will($this->returnValue(false)); /** @var Response $result */ $result = $this->testedObject->checkAccess($event); $this->assertInstanceOf(ResponseInterface::class, $result); /** @var Location $location */ $location = $result->getHeaders()->get('Location'); $this->assertSame($location->getUri(), self::URL_NO_ACCESS); }
public function testWriteAndReadAndClear() { $userEntity = UserEntityProvider::createEntityWithRandomData(); $this->testedObject->write($userEntity->getId()); $this->userRepositoryMock->expects($this->once())->method('find')->with($userEntity->getId())->will($this->returnValue($userEntity)); $result = $this->testedObject->read(); $this->assertSame($userEntity, $result); // read again without searching in repository $result = $this->testedObject->read(); $this->assertSame($userEntity, $result); $this->testedObject->clear(); $result = $this->testedObject->read(); $this->assertSame(null, $result); }
/** * Creates and authenticates a user. * * @param array $params * * @return UserEntity */ protected function authenticateUser(array $params = []) { $userEntity = UserEntityProvider::createEntityWithRandomData($params); $this->prepareAuthenticateMock(true, $userEntity); return $userEntity; }