createMock(User::class); $user->method('getIdentifier')->willReturn('userId'); $sessionService = $this->createMock(SessionService::class); $sessionService->method('getCurrentUser')->willReturn($user); $this->service = $this->createMock(SecureResourceService::class); $this->cache = $this->createMock(common_cache_Cache::class); $sl = $this->getServiceLocatorMock( [ SessionService::SERVICE_ID => $sessionService, common_cache_Cache::SERVICE_ID => $this->cache, ] ); $this->cachedService = new SecureResourceCachedService( $this->service, new ValidatePermissionsCacheKeyFactory(), new GetAllChildrenCacheKeyFactory(), common_cache_Cache::SERVICE_ID, 60 ); $this->cachedService->setServiceLocator($sl); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testValidatePermissionNoDataInCache(): void { $this->cache->expects($this->once())->method('has'); $this->cache->expects($this->once())->method('put'); $this->cachedService->validatePermission('resource', ['READ']); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testValidatePermissionValidResourceInCache(): void { $this->cache->expects($this->once())->method('has')->willReturn(true); $this->cache->expects($this->once())->method('get')->willReturn(true); $this->cache->expects($this->never())->method('put'); $this->service->expects($this->never())->method('validatePermission'); $this->cachedService->validatePermission('resource', ['READ']); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testValidatePermissionNotValidResourceInCache(): void { $this->expectException(ResourceAccessDeniedException::class); $this->cache->expects($this->once())->method('has')->willReturn(true); $this->cache->expects($this->once())->method('get')->willReturn(false); $this->cache->expects($this->never())->method('put'); $this->service->expects($this->never())->method('validatePermission'); $this->cachedService->validatePermission('resource', ['READ']); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testValidatePermissions(): void { $this->cache->method('has')->willReturn(false); $this->service->expects($this->exactly(3))->method('validatePermission'); $this->cachedService->validatePermissions(['1','2', '3'], ['READ']); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testGetAllChildrenDataInCache(): void { $resultCollection = $this->createMock(SecureResourceServiceAllChildrenCacheCollection::class); $this->cache->expects($this->once())->method('has')->willReturn(true); $this->cache->expects($this->once())->method('get')->willReturn($resultCollection); $this->cache->expects($this->never())->method('put'); $this->service->expects($this->never())->method('getAllChildren'); $class = $this->createMock(core_kernel_classes_Class::class); $class->method('getUri')->willReturn('userId'); $this->cachedService->getAllChildren($class); } /** * @throws common_cache_NotFoundException * @throws common_exception_Error */ public function testGetAllChildrenDataNotInCache(): void { $this->cache->expects($this->once())->method('has')->willReturn(false); $this->cache->expects($this->never())->method('get'); $this->cache->expects($this->once())->method('put'); $this->service->expects($this->once())->method('getAllChildren'); $class = $this->createMock(core_kernel_classes_Class::class); $class->method('getUri')->willReturn('userId'); $this->cachedService->getAllChildren($class); } }