diff --git a/tests/integration/REST/GetUsersWithPermissionInfoToContentItemTest.php b/tests/integration/REST/GetUsersWithPermissionInfoToContentItemTest.php new file mode 100644 index 0000000000..d5af5c7b3c --- /dev/null +++ b/tests/integration/REST/GetUsersWithPermissionInfoToContentItemTest.php @@ -0,0 +1,203 @@ + 'application/json', + 'X-Siteaccess' => 'admin', + ]; + private const EDITOR_USER_GROUP_REMOTE_ID = '3c160cca19fb135f83bd02d911f04db2'; + private const USER_REGISTRATION_REMOTE_ID = '5f7f0bdb3381d6a461d8c29ff53d908f'; + private const MEDIA_CONTENT_ITEM_ID = 41; + private const MEDIA_LOCATION_ID = 43; + private const MODULE_CONTENT = 'content'; + private const FUNCTION_EDIT = 'edit'; + private const FUNCTION_READ = 'read'; + + private UserService $userService; + + private RoleService $roleService; + + protected function setUp(): void + { + parent::setUp(); + + $ibexaTestCore = $this->getIbexaTestCore(); + $this->userService = $ibexaTestCore->getUserService(); + $this->roleService = $ibexaTestCore->getRoleService(); + + $this->createUsers(); + } + + /** + * @dataProvider provideDataForTestGetUsersWithPermissionsEndpoint + * + * @param array $queryParameters + */ + public function testGetUsersWithPermissionsEndpoint( + int $contentId, + string $module, + string $function, + array $queryParameters, + string $expectedResponse + ): void { + $uri = $this->getUri($contentId, $module, $function, $queryParameters); + $this->client->request('GET', $uri, [], [], self::HEADERS); + + $response = $this->client->getResponse(); + + self::assertSame(200, $response->getStatusCode()); + self::assertSame($expectedResponse, $response->getContent()); + } + + /** + * @return iterable, + * string, + * }> + */ + public function provideDataForTestGetUsersWithPermissionsEndpoint(): iterable + { + yield 'Check content-read for content item 41' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_READ, + [], + '{"access":[{"name":"Administrator User","email":"admin@link.invalid"},{"name":"John Doe","email":"john@link.invalid"},{"name":"Josh Bar","email":"joshua@link.invalid"}],"no_access":[]}', + ]; + + yield 'Check content-read for content item 41 and location 51' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_READ, + ['locationId' => 51], + '{"access":[{"name":"Administrator User","email":"admin@link.invalid"},{"name":"John Doe","email":"john@link.invalid"},{"name":"Josh Bar","email":"joshua@link.invalid"}],"no_access":[]}', + ]; + + yield 'Check content-read for content item 41 and phrase=adm' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_READ, + ['phrase' => 'Adm*'], + '{"access":[{"name":"Administrator User","email":"admin@link.invalid"}],"no_access":[]}', + ]; + + yield 'Check content-read for phrase=undef*' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_READ, + ['phrase' => 'undef*'], + '{"access":[],"no_access":[]}', + ]; + + yield 'Check content-edit for content item 2 and phrase=jo' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_EDIT, + [ + 'phrase' => 'jo*', + ], + '{"access":[{"name":"John Doe","email":"john@link.invalid"},{"name":"Josh Bar","email":"joshua@link.invalid"}],"no_access":[]}', + ]; + + yield 'Check content-edit for content item 41 and phrase=bar*' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_EDIT, + [ + 'phrase' => 'bar*', + ], + '{"access":[{"name":"Josh Bar","email":"joshua@link.invalid"}],"no_access":[]}', + ]; + + yield 'Check content-edit for content item 41 and location 43 and phrase=joshua@link.invalid' => [ + self::MEDIA_CONTENT_ITEM_ID, + self::MODULE_CONTENT, + self::FUNCTION_EDIT, + [ + 'phrase' => 'joshua*', + 'locationId' => self::MEDIA_LOCATION_ID, + ], + '{"access":[{"name":"Josh Bar","email":"joshua@link.invalid"}],"no_access":[]}', + ]; + } + + private function createUsers(): void + { + $this->getIbexaTestCore()->setAdministratorUser(); + + $role = $this->roleService->loadRoleByIdentifier('Editor'); + $editorGroup = $this->userService->loadUserGroupByRemoteId(self::EDITOR_USER_GROUP_REMOTE_ID); + + $user1CreateStruct = $this->createUserCreateStruct('john', 'John', 'Doe', 'john@link.invalid'); + + $user1 = $this->userService->createUser($user1CreateStruct, [$editorGroup]); + $this->roleService->assignRoleToUser($role, $user1); + + $user2CreateStruct = $this->createUserCreateStruct('josh', 'Josh', 'Bar', 'joshua@link.invalid'); + $user2 = $this->userService->createUser($user2CreateStruct, [$editorGroup]); + $this->roleService->assignRoleToUser($role, $user2); + + // Guest user should not be visible on the list + $guestCreateStruct = $this->createUserCreateStruct('guest', 'Guest', 'Guest', 'guest@link.invalid'); + $groupGuest = $this->userService->loadUserGroupByRemoteId(self::USER_REGISTRATION_REMOTE_ID); + $guest = $this->userService->createUser($guestCreateStruct, [$groupGuest]); + + $roleMember = $this->roleService->loadRoleByIdentifier('Member'); + $this->roleService->assignRoleToUser($roleMember, $guest); + } + + private function createUserCreateStruct( + string $login, + string $firstName, + string $lastName, + string $email + ): UserCreateStruct { + $userCreateStruct = $this->userService->newUserCreateStruct( + $login, + $email, + $login, + 'eng-GB' + ); + + $userCreateStruct->setField('first_name', $firstName); + $userCreateStruct->setField('last_name', $lastName); + + return $userCreateStruct; + } + + /** + * @param array $queryParameters + */ + private static function getUri( + int $contentId, + string $module, + string $function, + array $queryParameters = [] + ): string { + return sprintf( + self::ENDPOINT_URL, + $contentId, + $module, + $function, + http_build_query($queryParameters), + ); + } +} diff --git a/tests/integration/Resources/routing.yaml b/tests/integration/Resources/routing.yaml index 83a0143bc3..7c425026d6 100644 --- a/tests/integration/Resources/routing.yaml +++ b/tests/integration/Resources/routing.yaml @@ -1,3 +1,6 @@ +ibexa.admin_ui: + resource: '@IbexaAdminUiBundle/Resources/config/routing.yaml' + ibexa.admin_ui.rest: resource: '@IbexaAdminUiBundle/Resources/config/routing_rest.yaml' prefix: '%ibexa.rest.path_prefix%'