From 35032231fc9a84b3f63cb480c37f4e632ab758ea Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 29 Jul 2024 21:02:37 +0200 Subject: [PATCH 1/3] Add tests for the Crud methods that configure date/time/dateTime --- src/Config/Crud.php | 2 +- tests/Config/CrudTest.php | 115 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/Config/Crud.php b/src/Config/Crud.php index 1f57d42388..1a93d7e758 100644 --- a/src/Config/Crud.php +++ b/src/Config/Crud.php @@ -201,7 +201,7 @@ public function setDateTimeFormat(string $dateFormatOrPattern, string $timeForma } if (!$isDatePattern && !\in_array($timeFormat, DateTimeField::VALID_DATE_FORMATS, true)) { - throw new \InvalidArgumentException(sprintf('The value of the time format can only be one of the following: %s (but "%s" was given).', implode(', ', DateTimeField::VALID_DATE_FORMATS), $timeFormat)); + throw new \InvalidArgumentException(sprintf('When using a predefined format for the date, the time format must also be a predefined format (one of the following: %s) but "%s" was given.', implode(', ', DateTimeField::VALID_DATE_FORMATS), $timeFormat)); } $this->dto->setDateTimePattern($dateFormatOrPattern, $timeFormat); diff --git a/tests/Config/CrudTest.php b/tests/Config/CrudTest.php index 6ecd012d8a..af096f914e 100644 --- a/tests/Config/CrudTest.php +++ b/tests/Config/CrudTest.php @@ -58,4 +58,119 @@ public function testSetDecimalSeparator(string $separator) $this->assertSame($separator, $crudConfig->getAsDto()->getDecimalSeparator()); } + + /** + * @testWith ["short", "M/d/yy"] + * ["medium", "MMM d, y"] + * ["long", "MMMM d, y"] + * ["full", "EEEE, MMMM d, y"] + * ["EEEE", "EEEE"] + * ["MMMM/d, EEEE", "MMMM/d, EEEE"] + * // the following invalid format is used on purpose to test that the method accept any custom format + * ["this is wrong", "this is wrong"] + * + */ + public function testSetDateFormat(string $dateFormat, string $parsedFormat) + { + $crudConfig = Crud::new(); + $crudConfig->setDateFormat($dateFormat); + + $this->assertSame($parsedFormat, $crudConfig->getAsDto()->getDatePattern()); + } + + /** + * @testWith ["none", ""] + */ + public function testSetDateFormatWithInvalidFormat(string $dateFormat) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The first argument of the "EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateFormat()" method cannot be "none" or an empty string. Use either the special date formats (short, medium, long, full) or a datetime Intl pattern.'); + + $crudConfig = Crud::new(); + $crudConfig->setDateFormat($dateFormat); + } + + /** + * @testWith ["short", "h:mm a"] + * ["medium", "h:mm:ss a"] + * ["long", "h:mm:ss a z"] + * ["full", "h:mm:ss a zzzz"] + * ["zzzz", "zzzz"] + * ["ss, a, mm", "ss, a, mm"] + * // the following invalid format is used on purpose to test that the method accept any custom format + * ["this is wrong", "this is wrong"] + * + */ + public function testSetTimeFormat(string $timeFormat, string $parsedFormat) + { + $crudConfig = Crud::new(); + $crudConfig->setTimeFormat($timeFormat); + + $this->assertSame($parsedFormat, $crudConfig->getAsDto()->getTimePattern()); + } + + /** + * @testWith ["none", ""] + */ + public function testSetTimeFormatWithInvalidFormat(string $timeFormat) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The first argument of the "EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setTimeFormat()" method cannot be "none" or an empty string. Use either the special time formats (short, medium, long, full) or a datetime Intl pattern.'); + + $crudConfig = Crud::new(); + $crudConfig->setTimeFormat($timeFormat); + } + + /** + * @testWith ["none", "short"] + * ["none", "medium"] + * ["none", "long"] + * ["none", "full"] + * ["short", "none"] + * ["short", "short"] + * ["short", "medium"] + * ["short", "long"] + * ["short", "full"] + * ["medium", "none"] + * ["medium", "short"] + * ["medium", "medium"] + * ["medium", "long"] + * ["medium", "full"] + * ["long", "none"] + * ["long", "short"] + * ["long", "medium"] + * ["long", "long"] + * ["long", "full"] + * ["full", "none"] + * ["full", "short"] + * ["full", "medium"] + * ["full", "long"] + * ["full", "full"] + * ["MMMM/d, EEEE ss, a, mm", "none"] + * // the following invalid formats are used on purpose to test that the method accept any custom formats + * ["this is wrong", "this is wrong"] + */ + public function testSetDateTimeFormat(string $dateFormat, string $timeFormat) + { + $crudConfig = Crud::new(); + $crudConfig->setDateTimeFormat($dateFormat, $timeFormat); + + $this->assertSame([$dateFormat, $timeFormat], $crudConfig->getAsDto()->getDateTimePattern()); + } + + /** + * @testWith ["", "short", "The first argument of the \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" method cannot be an empty string. Use either a date format (none, short, medium, long, full) or a datetime Intl pattern."] + * ["none", "", "The values of the arguments of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" cannot be \"none\" or an empty string at the same time. Change any of them (or both)."] + * ["none", "none", "The values of the arguments of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" cannot be \"none\" or an empty string at the same time. Change any of them (or both)."] + * ["EEEE", "zzzz", "When the first argument of \"EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Crud::setDateTimeFormat()\" is a datetime pattern, you cannot set the time format in the second argument (define the time format inside the datetime pattern)."] + * ["long", "zzz", "When using a predefined format for the date, the time format must also be a predefined format (one of the following: none, short, medium, long, full) but \"zzz\" was given."] + */ + public function testSetDateTimeExceptions(string $dateFormat, string $timeFormat, string $exceptionMessage) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage($exceptionMessage); + + $crudConfig = Crud::new(); + $crudConfig->setDateTimeFormat($dateFormat, $timeFormat); + } } From 615470c8fab618606c67b3a368e2cec714b68946 Mon Sep 17 00:00:00 2001 From: Stephan Hoffmann Date: Tue, 30 Jul 2024 09:08:03 +0200 Subject: [PATCH 2/3] Locale fix --- src/Factory/AdminContextFactory.php | 4 ++-- src/Router/AdminUrlGenerator.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Factory/AdminContextFactory.php b/src/Factory/AdminContextFactory.php index 493cd7f7c4..fd0c24879a 100644 --- a/src/Factory/AdminContextFactory.php +++ b/src/Factory/AdminContextFactory.php @@ -78,8 +78,8 @@ private function getDashboardDto(Request $request, DashboardControllerInterface foreach ($dashboardControllerRoutes as $routeName => $controller) { if ($controller === $dashboardController) { // if present, remove the suffix of i18n route names (it's a two-letter locale at the end - // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.es' -> remove '.es') - $dashboardRouteName = preg_replace('~\.\w{2}$~', '', $routeName); + // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') + $dashboardRouteName = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $routeName); break; } diff --git a/src/Router/AdminUrlGenerator.php b/src/Router/AdminUrlGenerator.php index ae6afbbd13..88176e5951 100644 --- a/src/Router/AdminUrlGenerator.php +++ b/src/Router/AdminUrlGenerator.php @@ -295,8 +295,8 @@ public function generateUrl(): string } // if present, remove the suffix of i18n route names (it's a two-letter locale at the end - // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.es' -> remove '.es') - $this->dashboardRoute = preg_replace('~\.\w{2}$~', '', $this->dashboardRoute); + // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') + $this->dashboardRoute = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $this->dashboardRoute); // this removes any parameter with a NULL value $routeParameters = array_filter( From e4f952153c0f639e0eef3045511214cd9942081e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 30 Jul 2024 21:31:16 +0200 Subject: [PATCH 3/3] Update some code comments --- src/Factory/AdminContextFactory.php | 4 ++-- src/Router/AdminUrlGenerator.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Factory/AdminContextFactory.php b/src/Factory/AdminContextFactory.php index fd0c24879a..d2f53b4c75 100644 --- a/src/Factory/AdminContextFactory.php +++ b/src/Factory/AdminContextFactory.php @@ -77,8 +77,8 @@ private function getDashboardDto(Request $request, DashboardControllerInterface foreach ($dashboardControllerRoutes as $routeName => $controller) { if ($controller === $dashboardController) { - // if present, remove the suffix of i18n route names (it's a two-letter locale at the end - // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') + // if present, remove the suffix of i18n route names (it's the content after the last dot + // in the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') $dashboardRouteName = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $routeName); break; diff --git a/src/Router/AdminUrlGenerator.php b/src/Router/AdminUrlGenerator.php index 88176e5951..6d4a677c9d 100644 --- a/src/Router/AdminUrlGenerator.php +++ b/src/Router/AdminUrlGenerator.php @@ -294,8 +294,8 @@ public function generateUrl(): string $this->dashboardRoute = $this->dashboardControllerRegistry->getFirstDashboardRoute(); } - // if present, remove the suffix of i18n route names (it's a two-letter locale at the end - // of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') + // if present, remove the suffix of i18n route names (it's the content after the last dot + // in the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.en_US' -> remove '.en_US') $this->dashboardRoute = preg_replace('~\.[a-z]{2}(_[A-Z]{2})?$~', '', $this->dashboardRoute); // this removes any parameter with a NULL value