Skip to content

Commit 349ca1f

Browse files
authored
Sort invite roles list by application display name (#357)
* 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 * Add changelog entry for invite roles sorting * 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.
1 parent f5971d2 commit 349ca1f

4 files changed

Lines changed: 163 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Upgrade the runtime and build toolchain to PHP 8.5 and Node.js 24.
55
* Refresh the CI Docker base image plus Composer and npm dependencies for the PHP 8.5 build.
66
* Preserve the originally requested deeplink after SAML login instead of redirecting to the homepage #325
7+
* Sort the invite roles overview by application display name #343
78

89
# 4.1.4
910
* Update saml2 library.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/**
6+
* Copyright 2024 SURFnet B.V.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace OpenConext\Profile\Tests\Value;
22+
23+
use OpenConext\Profile\Value\InviteRole;
24+
use OpenConext\Profile\Value\InviteRoleList;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class InviteRoleListTest extends TestCase
28+
{
29+
public function test_it_can_order_by_application_display_name(): void
30+
{
31+
$list = new InviteRoleList([
32+
$this->buildInviteRole('Role C', 'C-application'),
33+
$this->buildInviteRole('Role A', 'A-application'),
34+
$this->buildInviteRole('Role B', 'B-application'),
35+
]);
36+
37+
$sortedList = $list->sortByApplicationDisplayName('en');
38+
39+
$sorted = array_values(iterator_to_array($sortedList->getIterator()));
40+
$this->assertCount(3, $sorted);
41+
$this->assertEquals('A-application', $sorted[0]->getApplications()[0]->getName('en'));
42+
$this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en'));
43+
$this->assertEquals('C-application', $sorted[2]->getApplications()[0]->getName('en'));
44+
}
45+
46+
public function test_sorting_does_not_mutate_the_original_list(): void
47+
{
48+
$list = new InviteRoleList([
49+
$this->buildInviteRole('Role C', 'C-application'),
50+
$this->buildInviteRole('Role A', 'A-application'),
51+
$this->buildInviteRole('Role B', 'B-application'),
52+
]);
53+
54+
$list->sortByApplicationDisplayName('en');
55+
56+
$original = array_values(iterator_to_array($list->getIterator()));
57+
$this->assertEquals('C-application', $original[0]->getApplications()[0]->getName('en'));
58+
$this->assertEquals('A-application', $original[1]->getApplications()[0]->getName('en'));
59+
$this->assertEquals('B-application', $original[2]->getApplications()[0]->getName('en'));
60+
}
61+
62+
public function test_it_can_order_by_application_display_name_case_insensitively(): void
63+
{
64+
$list = new InviteRoleList([
65+
$this->buildInviteRole('Role C', 'C-application'),
66+
$this->buildInviteRole('Role A', 'a-application'),
67+
$this->buildInviteRole('Role B', 'B-application'),
68+
]);
69+
70+
$sortedList = $list->sortByApplicationDisplayName('en');
71+
72+
$sorted = array_values(iterator_to_array($sortedList->getIterator()));
73+
$this->assertCount(3, $sorted);
74+
$this->assertEquals('a-application', $sorted[0]->getApplications()[0]->getName('en'));
75+
$this->assertEquals('B-application', $sorted[1]->getApplications()[0]->getName('en'));
76+
$this->assertEquals('C-application', $sorted[2]->getApplications()[0]->getName('en'));
77+
}
78+
79+
public function test_roles_without_applications_are_sorted_by_role_name(): void
80+
{
81+
$list = new InviteRoleList([
82+
$this->buildInviteRole('Role C', 'C-application'),
83+
$this->buildInviteRoleWithoutApplication('Role A'),
84+
$this->buildInviteRole('Role B', 'B-application'),
85+
]);
86+
87+
$sortedList = $list->sortByApplicationDisplayName('en');
88+
89+
$sorted = array_values(iterator_to_array($sortedList->getIterator()));
90+
$this->assertCount(3, $sorted);
91+
$this->assertEquals('B-application', $sorted[0]->getApplications()[0]->getName('en'));
92+
$this->assertEquals('C-application', $sorted[1]->getApplications()[0]->getName('en'));
93+
$this->assertEquals('Role A', $sorted[2]->getName());
94+
}
95+
96+
public function test_it_can_order_nothing(): void
97+
{
98+
$list = new InviteRoleList([]);
99+
$sortedList = $list->sortByApplicationDisplayName('en');
100+
$this->assertCount(0, $sortedList);
101+
}
102+
103+
private function buildInviteRole(string $roleName, string $applicationNameEn): InviteRole
104+
{
105+
return new InviteRole($roleName, $roleName, [
106+
[
107+
'landingPage' => '',
108+
'nameEn' => $applicationNameEn,
109+
'nameNl' => $applicationNameEn,
110+
'organisationEn' => '',
111+
'organisationNl' => '',
112+
'logo' => '',
113+
],
114+
]);
115+
}
116+
117+
private function buildInviteRoleWithoutApplication(string $roleName): InviteRole
118+
{
119+
return new InviteRole($roleName, $roleName, []);
120+
}
121+
}

‎src/OpenConext/Profile/Value/InviteRoleList.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,39 @@ public function count(): int
6161
{
6262
return count($this->roles);
6363
}
64+
65+
/**
66+
* Returns a new list with the roles sorted alphabetically (case-insensitively) by the
67+
* display name of their first application, falling back to the role name for roles
68+
* without applications. This instance is left unchanged.
69+
*/
70+
public function sortByApplicationDisplayName(
71+
string $locale,
72+
): self {
73+
$sortedRoles = $this->roles;
74+
75+
usort(
76+
$sortedRoles,
77+
fn(InviteRole $a, InviteRole $b): int => strcasecmp(
78+
$this->getSortNameFor($a, $locale),
79+
$this->getSortNameFor($b, $locale),
80+
),
81+
);
82+
83+
return new self($sortedRoles);
84+
}
85+
86+
private function getSortNameFor(
87+
InviteRole $inviteRole,
88+
string $locale,
89+
): string {
90+
if ($inviteRole->hasApplications()) {
91+
$applicationName = $inviteRole->getApplications()[0]->getName($locale);
92+
if ($applicationName !== '') {
93+
return $applicationName;
94+
}
95+
}
96+
97+
return $inviteRole->getName();
98+
}
6499
}

‎src/OpenConext/ProfileBundle/Controller/InviteRolesController.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OpenConext\Profile\Repository\InviteRepositoryInterface;
2525
use Psr\Log\LoggerInterface;
2626
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
27+
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
2829
use Symfony\Component\Routing\Attribute\Route;
2930
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -44,15 +45,18 @@ public function __construct(
4445
methods: ['GET'],
4546
schemes: ['https'],
4647
)]
47-
public function __invoke(): Response
48-
{
48+
public function __invoke(
49+
Request $request,
50+
): Response {
4951
if (!$this->enabled) {
5052
throw $this->createAccessDeniedException();
5153
}
5254
$this->logger->info('Showing the OpenConext-Invite roles page');
5355

5456
$user = $this->userProvider->getCurrentUser();
5557
$inviteRoles = $this->inviteRepository->findAllFor($user->getUserIdentifier());
58+
$inviteRoles = $inviteRoles->sortByApplicationDisplayName($request->getLocale());
59+
5660
return $this->render('@OpenConextProfile/InviteRoles/overview.html.twig', ['inviteRoles' => $inviteRoles]);
5761
}
5862
}

0 commit comments

Comments
 (0)