initializeTestDoubles(); $this->initializeServiceLocator(); $this->initializeSut(); parent::setUp(); } public function initializeTestDoubles(): void { $this->rootMock = $this->createMock(core_kernel_classes_Class::class); $this->categoryServiceMock = $this->createMock(CategoryService::class); $this->resourceLookupMock = $this->createMock(ResourceLookup::class); $this->resourceServiceMock = $this->createMock(ResourceService::class); $this->sessionServiceMock = $this->createMock(SessionService::class); } public function initializeTestDoubleExpectancies(): void { $this->resourceLookupMock ->expects(static::once()) ->method('getResources') ->with($this->rootMock, [], [], 0, 30) ->willReturnCallback([$this, 'getResources']); $this->categoryServiceMock ->method('getItemCategories') ->willReturn([]); $sessionMock = new common_session_AnonymousSession(); $this->sessionServiceMock ->method('getCurrentSession') ->willReturn($sessionMock); $self = $this; $this->resourceServiceMock ->method('getResourcesPermissions') ->willReturnCallback(static function($user, $resources) use ($self) { $data = []; foreach ($self->permissions as $uri) { $data[$uri] = ['GRANT']; } return [ 'supportedRights' => ['GRANT', 'WRITE', 'READ'], 'data' => $data, ]; }); } public function initializeServiceLocator(): void { $this->serviceLocatorMock = $this->getServiceLocatorMock( [ CategoryService::SERVICE_ID => $this->categoryServiceMock, TreeResourceLookup::SERVICE_ID => $this->resourceLookupMock, ResourceService::SERVICE_ID => $this->resourceServiceMock, SessionService::SERVICE_ID => $this->sessionServiceMock, ] ); } public function initializeSut(): void { $this->sut = new TreeItemLookup(); $this->sut->setServiceLocator($this->serviceLocatorMock); } public function getResources(): array { return $this->resources; } public function dataProvider(): array { return [ 'Empty' => [ 'expected' => [], ], 'No restrictions' => [ 'expected' => [ [ 'uri' => 'http://root', 'type' => 'class', 'accessMode' => 'allowed', 'children' => [ [ 'uri' => 'http://child#1', 'type' => 'instance', 'categories' => [], 'accessMode' => 'allowed', ], ], ], ], 'resources' => [ [ 'uri' => 'http://root', 'type' => 'class', 'children' => [ [ 'uri' => 'http://child#1', 'type' => 'instance', ], ], ], ], 'readableResourceMap' => [ 'http://child#1', 'http://root' ], ], 'Restrictions' => [ 'expected' => [ [ 'uri' => 'http://root', 'type' => 'class', 'accessMode' => 'denied', 'children' => [ [ 'uri' => 'http://child#1', 'type' => 'instance', 'accessMode' => 'denied', 'categories' => [], ], [ 'uri' => 'http://child#2', 'type' => 'instance', 'categories' => [], 'accessMode' => 'allowed' ], [ 'uri' => 'http://child#3', 'type' => 'class', 'accessMode' => 'denied', ], ], ], ], 'resources' => [ [ 'uri' => 'http://root', 'type' => 'class', 'children' => [ [ 'uri' => 'http://child#1', 'type' => 'instance', ], [ 'uri' => 'http://child#2', 'type' => 'instance', ], [ 'uri' => 'http://child#3', 'type' => 'class', ], ], ], ], 'readableResourceMap' => [ 'http://child#2', ], ], ]; } /** * @noinspection PhpDocMissingThrowsInspection * * @dataProvider dataProvider * * @param array $expected * @param array $resources * @param array $readableResourceMap */ public function testGetItems(array $expected, array $resources = [], array $readableResourceMap = []): void { $this->initializeTestDoubleExpectancies(); $this->resources = $resources; $this->permissions = $readableResourceMap; /** @noinspection PhpUnhandledExceptionInspection */ $result = $this->sut->getItems($this->rootMock); if (!empty($result[0]['children'])) { $result[0]['children'] = array_values($result[0]['children']); } self::assertEquals($expected, $result); } public function testGetItemsNoResources(): void { $this->initializeTestDoubleExpectancies(); $this->permissions = []; $data = [ [ 'type' => 'class', 'children' => [ [ 'type' => 'class' ] ] ] ]; $this->resourceLookupMock ->expects(static::once()) ->method('getResources') ->willReturn($data); /** @noinspection PhpUnhandledExceptionInspection */ $this->sut->getItems($this->rootMock); } }