Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
121 changes: 121 additions & 0 deletions src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\Profile\Tests\Value;

use OpenConext\Profile\Value\InviteRole;
use OpenConext\Profile\Value\InviteRoleList;
use PHPUnit\Framework\TestCase;

class InviteRoleListTest extends TestCase
{
public function test_it_can_order_by_application_display_name(): void
{
$list = new InviteRoleList([
$this->buildInviteRole('Role C', 'C-application'),
$this->buildInviteRole('Role A', 'A-application'),
$this->buildInviteRole('Role B', 'B-application'),
]);

$sortedList = $list->sortByApplicationDisplayName('en');

$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([
$this->buildInviteRole('Role C', 'C-application'),
$this->buildInviteRole('Role A', 'a-application'),
$this->buildInviteRole('Role B', 'B-application'),
]);

$sortedList = $list->sortByApplicationDisplayName('en');

$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_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'),
]);

$sortedList = $list->sortByApplicationDisplayName('en');

$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'));
$this->assertEquals('Role A', $sorted[2]->getName());
}

public function test_it_can_order_nothing(): void
{
$list = new InviteRoleList([]);
$sortedList = $list->sortByApplicationDisplayName('en');
$this->assertCount(0, $sortedList);
}

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, []);
}
}
35 changes: 35 additions & 0 deletions src/OpenConext/Profile/Value/InviteRoleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,39 @@ public function count(): int
{
return count($this->roles);
}

/**
* 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(
Comment thread
johanib marked this conversation as resolved.
Resolved
string $locale,
): self {
$sortedRoles = $this->roles;

usort(
$sortedRoles,
fn(InviteRole $a, InviteRole $b): int => strcasecmp(
$this->getSortNameFor($a, $locale),
$this->getSortNameFor($b, $locale),
),
);

return new self($sortedRoles);
}

private function getSortNameFor(
InviteRole $inviteRole,
string $locale,
): string {
if ($inviteRole->hasApplications()) {
$applicationName = $inviteRole->getApplications()[0]->getName($locale);
if ($applicationName !== '') {
return $applicationName;
}
}

return $inviteRole->getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,15 +45,18 @@ public function __construct(
methods: ['GET'],
schemes: ['https'],
)]
public function __invoke(): Response
{
public function __invoke(
Request $request,
): Response {
if (!$this->enabled) {
throw $this->createAccessDeniedException();
}
$this->logger->info('Showing the OpenConext-Invite roles page');

$user = $this->userProvider->getCurrentUser();
$inviteRoles = $this->inviteRepository->findAllFor($user->getUserIdentifier());
$inviteRoles = $inviteRoles->sortByApplicationDisplayName($request->getLocale());

return $this->render('@OpenConextProfile/InviteRoles/overview.html.twig', ['inviteRoles' => $inviteRoles]);
}
}
Loading