From f1fa6eb676cb899ca3f207b2fc01bfb0035ee825 Mon Sep 17 00:00:00 2001 From: turegjorup Date: Wed, 26 Aug 2026 12:16:05 +0200 Subject: [PATCH] fix: make the Service Agreements page render and state its currency --- CHANGELOG.md | 6 ++ .../Admin/SecurityContractCrudController.php | 33 ++++++++++- .../Admin/SecurityContractCurrencyTest.php | 58 +++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/Controller/Admin/SecurityContractCurrencyTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d2afa19..900ed7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- [#96](https://github.com/itk-dev/devops_itksites/pull/96) + Service Agreements: fix the page and name the currency + - Add `#[AdminRoute]` to `syncAll()`. EasyAdmin 5 requires it on a custom CRUD + action, and without it rendering the "Sync all" button threw, taking the + whole index page with it + - Show the amounts as Danish kroner, `12.500,50 kr.`, on index and detail - [#95](https://github.com/itk-dev/devops_itksites/pull/95) Update `vincentlanglet/twig-cs-fixer` to 4.0. Every other dependency is already at its latest minor; the remaining majors are held back by their diff --git a/src/Controller/Admin/SecurityContractCrudController.php b/src/Controller/Admin/SecurityContractCrudController.php index 758851d..bb54788 100644 --- a/src/Controller/Admin/SecurityContractCrudController.php +++ b/src/Controller/Admin/SecurityContractCrudController.php @@ -72,9 +72,14 @@ public function configureFields(string $pageName): iterable yield TextField::new('clientContactEmail')->hideOnIndex(); yield FormField::addFieldset('Budget'); - yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6); + // The amounts are Danish kroner, which until now the admin never said + // anywhere. 5.5's prepend()/append() addons would be the way to show a + // unit inside an input, but they render on form pages only and this CRUD + // disables NEW and EDIT (see configureActions), so the only pages that + // exist are index and detail. Hence formatting instead of an addon. + yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6)->formatValue(self::formatKroner(...)); yield NumberField::new('quarterlyHours')->setTextAlign('right')->setColumns(6); - yield NumberField::new('cybersecurityPrice')->setTextAlign('right')->hideOnIndex()->setColumns(6); + yield NumberField::new('cybersecurityPrice')->setTextAlign('right')->hideOnIndex()->setColumns(6)->formatValue(self::formatKroner(...)); yield TextareaField::new('cybersecurityNote')->hideOnIndex()->setColumns(12); yield FormField::addFieldset('Infrastructure'); @@ -88,6 +93,30 @@ public function configureFields(string $pageName): iterable yield DateField::new('validTo')->setColumns(6); } + #[AdminRoute] + /** + * An amount as Danish kroner: 12.500,50 kr. + * + * Through Intl rather than by pasting a suffix on, so the grouping and the + * decimal separator are Danish too. The application locale is `en`, which + * would otherwise render 12,500.5 with no currency at all. + */ + public static function formatKroner(?float $value): ?string + { + if (null === $value) { + return null; + } + + return (new \NumberFormatter('da_DK', \NumberFormatter::CURRENCY))->formatCurrency($value, 'DKK') ?: null; + } + + /** + * The attribute is what makes this method reachable as a CRUD action. + * + * Without it EasyAdmin throws while rendering the "Sync all" button, which + * took the whole index page with it — see the "Custom CRUD Actions" section + * of the bundle's UPGRADE.md. + */ #[AdminRoute] public function syncAll(): RedirectResponse { diff --git a/tests/Controller/Admin/SecurityContractCurrencyTest.php b/tests/Controller/Admin/SecurityContractCurrencyTest.php new file mode 100644 index 0000000..de931b4 --- /dev/null +++ b/tests/Controller/Admin/SecurityContractCurrencyTest.php @@ -0,0 +1,58 @@ +get(EntityManagerInterface::class); + $client->loginUser($entityManager->getRepository(User::class)->findOneBy([])); + + $contract = new SecurityContract(); + $contract->setEconomicsId(4711); + $contract->setProjectName('Kroner probe'); + $contract->setMonthlyPrice(12500.5); + $entityManager->persist($contract); + $entityManager->flush(); + + $url = static::getContainer()->get(AdminUrlGenerator::class) + ->setController(SecurityContractCrudController::class) + ->setAction(Crud::PAGE_INDEX) + ->generateUrl(); + + $client->request('GET', $url); + + $this->assertResponseIsSuccessful(); + + $content = (string) $client->getResponse()->getContent(); + + // Danish grouping and separator, not the application locale's 12,500.5. + // Amount and unit are asserted apart because Intl joins them with a + // non-breaking space. + $this->assertStringContainsString('12.500,50', $content); + $this->assertStringContainsString('kr.', $content); + } +}