From bece91b32afbc3e7cfff74a8a76bf7de3d42bff5 Mon Sep 17 00:00:00 2001 From: Kay Joosten Date: Tue, 29 Sep 2026 09:21:56 +0200 Subject: [PATCH 1/3] Sort invite roles list by application display name # If applied, this commit will... Sort the list of roles shown on the invite-roles page by application display name. # Why is this change needed? Prior to this change, the list of invite roles shown under /invite-roles was not sorted. Every request could return the roles in a different order, making the menu feel unpredictable and hard to scan. # How does it address the issue? This change adds a sortByApplicationDisplayName method to InviteRoleList that orders roles alphabetically and case insensitively by their application display name, falling back to the role name when a role has no applications. The InviteRolesController now sorts the list using the current locale before rendering it, matching the approach already used for the my-services overview. # Provide links to any relevant tickets, articles or other resources Closes #343 --- .../Tests/Value/InviteRoleListTest.php | 105 ++++++++++++++++++ .../Profile/Value/InviteRoleList.php | 30 +++++ .../Controller/InviteRolesController.php | 8 +- 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php diff --git a/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php b/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php new file mode 100644 index 000000000..9194815d2 --- /dev/null +++ b/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php @@ -0,0 +1,105 @@ +buildInviteRole('Role C', 'C-application'), + $this->buildInviteRole('Role A', 'A-application'), + $this->buildInviteRole('Role B', 'B-application'), + ]); + + $list->sortByApplicationDisplayName('en'); + + $sorted = array_values(iterator_to_array($list->getIterator())); + $this->assertCount(3, $sorted); + $this->assertEquals('A-application', $sorted[0]->getApplications()[0]->getName('en')); + $this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en')); + $this->assertEquals('C-application', $sorted[2]->getApplications()[0]->getName('en')); + } + + public function test_it_can_order_by_application_display_name_case_insensitively(): void + { + $list = new InviteRoleList([ + $this->buildInviteRole('Role C', 'C-application'), + $this->buildInviteRole('Role A', 'a-application'), + $this->buildInviteRole('Role B', 'B-application'), + ]); + + $list->sortByApplicationDisplayName('en'); + + $sorted = array_values(iterator_to_array($list->getIterator())); + $this->assertCount(3, $sorted); + $this->assertEquals('a-application', $sorted[0]->getApplications()[0]->getName('en')); + $this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en')); + $this->assertEquals('C-application', $sorted[2]->getApplications()[0]->getName('en')); + } + + public function test_roles_without_applications_are_sorted_by_role_name(): void + { + $list = new InviteRoleList([ + $this->buildInviteRole('Role C', 'C-application'), + $this->buildInviteRoleWithoutApplication('Role A'), + $this->buildInviteRole('Role B', 'B-application'), + ]); + + $list->sortByApplicationDisplayName('en'); + + $sorted = array_values(iterator_to_array($list->getIterator())); + $this->assertCount(3, $sorted); + $this->assertEquals('B-application', $sorted[0]->getApplications()[0]->getName('en')); + $this->assertEquals('C-application', $sorted[1]->getApplications()[0]->getName('en')); + $this->assertEquals('Role A', $sorted[2]->getName()); + } + + public function test_it_can_order_nothing(): void + { + $list = new InviteRoleList([]); + $list->sortByApplicationDisplayName('en'); + $this->assertCount(0, $list); + } + + private function buildInviteRole(string $roleName, string $applicationNameEn): InviteRole + { + return new InviteRole($roleName, $roleName, [ + [ + 'landingPage' => '', + 'nameEn' => $applicationNameEn, + 'nameNl' => $applicationNameEn, + 'organisationEn' => '', + 'organisationNl' => '', + 'logo' => '', + ], + ]); + } + + private function buildInviteRoleWithoutApplication(string $roleName): InviteRole + { + return new InviteRole($roleName, $roleName, []); + } +} diff --git a/src/OpenConext/Profile/Value/InviteRoleList.php b/src/OpenConext/Profile/Value/InviteRoleList.php index 5d5704dab..702443a19 100644 --- a/src/OpenConext/Profile/Value/InviteRoleList.php +++ b/src/OpenConext/Profile/Value/InviteRoleList.php @@ -61,4 +61,34 @@ public function count(): int { return count($this->roles); } + + /** + * Sorts the roles alphabetically (case-insensitively) by the display name of their + * first application, falling back to the role name for roles without applications. + */ + public function sortByApplicationDisplayName( + string $locale, + ): void { + usort( + $this->roles, + fn(InviteRole $a, InviteRole $b): int => strcasecmp( + $this->getSortNameFor($a, $locale), + $this->getSortNameFor($b, $locale), + ), + ); + } + + private function getSortNameFor( + InviteRole $inviteRole, + string $locale, + ): string { + if ($inviteRole->hasApplications()) { + $applicationName = $inviteRole->getApplications()[0]->getName($locale); + if ($applicationName !== '') { + return $applicationName; + } + } + + return $inviteRole->getName(); + } } diff --git a/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php b/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php index 3c1781a33..32d268e03 100644 --- a/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php +++ b/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php @@ -24,6 +24,7 @@ use OpenConext\Profile\Repository\InviteRepositoryInterface; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Security\Core\User\UserProviderInterface; @@ -44,8 +45,9 @@ public function __construct( methods: ['GET'], schemes: ['https'], )] - public function __invoke(): Response - { + public function __invoke( + Request $request, + ): Response { if (!$this->enabled) { throw $this->createAccessDeniedException(); } @@ -53,6 +55,8 @@ public function __invoke(): Response $user = $this->userProvider->getCurrentUser(); $inviteRoles = $this->inviteRepository->findAllFor($user->getUserIdentifier()); + $inviteRoles->sortByApplicationDisplayName($request->getLocale()); + return $this->render('@OpenConextProfile/InviteRoles/overview.html.twig', ['inviteRoles' => $inviteRoles]); } } From 2eeffbda6622d43a8b10fe38161a7ac37ba6505b Mon Sep 17 00:00:00 2001 From: Kay Joosten Date: Tue, 29 Sep 2026 09:32:07 +0200 Subject: [PATCH 2/3] Add changelog entry for invite roles sorting --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a7dafd5..9f5be4dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Upgrade the runtime and build toolchain to PHP 8.5 and Node.js 24. * Refresh the CI Docker base image plus Composer and npm dependencies for the PHP 8.5 build. * Preserve the originally requested deeplink after SAML login instead of redirecting to the homepage #325 +* Sort the invite roles overview by application display name #343 # 4.1.4 * Update saml2 library. From 91e281b75bb068ffacf1c29b94ade511f9efd291 Mon Sep 17 00:00:00 2001 From: Kay Joosten Date: Mon, 19 Oct 2026 09:46:03 +0200 Subject: [PATCH 3/3] Make InviteRoleList sorting a pure, non-mutating operation If applied, this commit will make sortByApplicationDisplayName return a new sorted InviteRoleList instead of mutating the existing one. Why is this change needed? Prior to this change, sortByApplicationDisplayName mutated the InviteRoleList value object in place, which is unexpected for a value object and made the method harder to reason about and test. How does it address the issue? This change makes sortByApplicationDisplayName a pure function: it copies the roles, sorts the copy, and returns a new InviteRoleList built through the constructor, leaving the original instance untouched. The controller and tests are updated to use the returned list. Provide links to any relevant tickets, articles or other resources Addresses review feedback on PR #357. --- .../Tests/Value/InviteRoleListTest.php | 32 ++++++++++++++----- .../Profile/Value/InviteRoleList.php | 13 +++++--- .../Controller/InviteRolesController.php | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php b/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php index 9194815d2..d5f63ac68 100644 --- a/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php +++ b/src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php @@ -34,15 +34,31 @@ public function test_it_can_order_by_application_display_name(): void $this->buildInviteRole('Role B', 'B-application'), ]); - $list->sortByApplicationDisplayName('en'); + $sortedList = $list->sortByApplicationDisplayName('en'); - $sorted = array_values(iterator_to_array($list->getIterator())); + $sorted = array_values(iterator_to_array($sortedList->getIterator())); $this->assertCount(3, $sorted); $this->assertEquals('A-application', $sorted[0]->getApplications()[0]->getName('en')); $this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en')); $this->assertEquals('C-application', $sorted[2]->getApplications()[0]->getName('en')); } + public function test_sorting_does_not_mutate_the_original_list(): void + { + $list = new InviteRoleList([ + $this->buildInviteRole('Role C', 'C-application'), + $this->buildInviteRole('Role A', 'A-application'), + $this->buildInviteRole('Role B', 'B-application'), + ]); + + $list->sortByApplicationDisplayName('en'); + + $original = array_values(iterator_to_array($list->getIterator())); + $this->assertEquals('C-application', $original[0]->getApplications()[0]->getName('en')); + $this->assertEquals('A-application', $original[1]->getApplications()[0]->getName('en')); + $this->assertEquals('B-application', $original[2]->getApplications()[0]->getName('en')); + } + public function test_it_can_order_by_application_display_name_case_insensitively(): void { $list = new InviteRoleList([ @@ -51,9 +67,9 @@ public function test_it_can_order_by_application_display_name_case_insensitively $this->buildInviteRole('Role B', 'B-application'), ]); - $list->sortByApplicationDisplayName('en'); + $sortedList = $list->sortByApplicationDisplayName('en'); - $sorted = array_values(iterator_to_array($list->getIterator())); + $sorted = array_values(iterator_to_array($sortedList->getIterator())); $this->assertCount(3, $sorted); $this->assertEquals('a-application', $sorted[0]->getApplications()[0]->getName('en')); $this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en')); @@ -68,9 +84,9 @@ public function test_roles_without_applications_are_sorted_by_role_name(): void $this->buildInviteRole('Role B', 'B-application'), ]); - $list->sortByApplicationDisplayName('en'); + $sortedList = $list->sortByApplicationDisplayName('en'); - $sorted = array_values(iterator_to_array($list->getIterator())); + $sorted = array_values(iterator_to_array($sortedList->getIterator())); $this->assertCount(3, $sorted); $this->assertEquals('B-application', $sorted[0]->getApplications()[0]->getName('en')); $this->assertEquals('C-application', $sorted[1]->getApplications()[0]->getName('en')); @@ -80,8 +96,8 @@ public function test_roles_without_applications_are_sorted_by_role_name(): void public function test_it_can_order_nothing(): void { $list = new InviteRoleList([]); - $list->sortByApplicationDisplayName('en'); - $this->assertCount(0, $list); + $sortedList = $list->sortByApplicationDisplayName('en'); + $this->assertCount(0, $sortedList); } private function buildInviteRole(string $roleName, string $applicationNameEn): InviteRole diff --git a/src/OpenConext/Profile/Value/InviteRoleList.php b/src/OpenConext/Profile/Value/InviteRoleList.php index 702443a19..bd6d9229b 100644 --- a/src/OpenConext/Profile/Value/InviteRoleList.php +++ b/src/OpenConext/Profile/Value/InviteRoleList.php @@ -63,19 +63,24 @@ public function count(): int } /** - * Sorts the roles alphabetically (case-insensitively) by the display name of their - * first application, falling back to the role name for roles without applications. + * Returns a new list with the roles sorted alphabetically (case-insensitively) by the + * display name of their first application, falling back to the role name for roles + * without applications. This instance is left unchanged. */ public function sortByApplicationDisplayName( string $locale, - ): void { + ): self { + $sortedRoles = $this->roles; + usort( - $this->roles, + $sortedRoles, fn(InviteRole $a, InviteRole $b): int => strcasecmp( $this->getSortNameFor($a, $locale), $this->getSortNameFor($b, $locale), ), ); + + return new self($sortedRoles); } private function getSortNameFor( diff --git a/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php b/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php index 32d268e03..d71b4f37b 100644 --- a/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php +++ b/src/OpenConext/ProfileBundle/Controller/InviteRolesController.php @@ -55,7 +55,7 @@ public function __invoke( $user = $this->userProvider->getCurrentUser(); $inviteRoles = $this->inviteRepository->findAllFor($user->getUserIdentifier()); - $inviteRoles->sortByApplicationDisplayName($request->getLocale()); + $inviteRoles = $inviteRoles->sortByApplicationDisplayName($request->getLocale()); return $this->render('@OpenConextProfile/InviteRoles/overview.html.twig', ['inviteRoles' => $inviteRoles]); }