Skip to content
Open
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Next release
# UNRELEASED
* 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.
* Sort the invite roles overview by application display name #343

# 4.1.4
* Update saml2 library.
Expand Down
105 changes: 105 additions & 0 deletions src/OpenConext/Profile/Tests/Value/InviteRoleListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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'),
]);

$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, []);
}
}
30 changes: 30 additions & 0 deletions src/OpenConext/Profile/Value/InviteRoleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consideration: This currently changes the value of the value object.
I think it would be nicer if the initializeWith cals this function. That has two advantages: The value object state does not need to be changed anymore (one could argue that if the state needs to be changed it is not valid in the first place). And 2, the sortByApplicationDisplayName can become a pure function.

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();
}
}
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->sortByApplicationDisplayName($request->getLocale());

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