From 1ea49ae2c18545f27e974c379405c84be0c7f3c8 Mon Sep 17 00:00:00 2001 From: blackcoder87 Date: Fri, 15 Nov 2024 13:54:47 +0100 Subject: [PATCH] Add timezone validator (#1102) --- .../libraries/Ilch/Translations/de.php | 1 + .../libraries/Ilch/Translations/en.php | 1 + .../Ilch/Validation/Validators/Timezone.php | 33 +++++++ .../Validation/Validators/TimezoneTest.php | 94 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 application/libraries/Ilch/Validation/Validators/Timezone.php create mode 100644 tests/libraries/ilch/Validation/Validators/TimezoneTest.php diff --git a/application/libraries/Ilch/Translations/de.php b/application/libraries/Ilch/Translations/de.php index 651eea0bd..50d9cb526 100644 --- a/application/libraries/Ilch/Translations/de.php +++ b/application/libraries/Ilch/Translations/de.php @@ -32,6 +32,7 @@ 'validation.errors.max.numeric' => '%s darf höchstens %s betragen.', 'validation.errors.max.string' => '%s darf höchstens %s Zeichen lang sein.', 'validation.errors.max.array' => '%s darf höchstens %s Einträge haben.', + 'validation.errors.timezone.notAValidTimezone' => '%s ist keine gültige Zeitzone.', // general 'saveSuccess' => 'Erfolgreich gespeichert', diff --git a/application/libraries/Ilch/Translations/en.php b/application/libraries/Ilch/Translations/en.php index fcf4d458b..ef71bcd31 100644 --- a/application/libraries/Ilch/Translations/en.php +++ b/application/libraries/Ilch/Translations/en.php @@ -32,6 +32,7 @@ 'validation.errors.max.numeric' => '%s may not be greater than %s.', 'validation.errors.max.string' => '%s may not be greater than %s characters.', 'validation.errors.max.array' => '%s may not have more than %s items.', + 'validation.errors.timezone.notAValidTimezone' => '%s is not a valid timezone.', // general 'saveSuccess' => 'Saved successful', diff --git a/application/libraries/Ilch/Validation/Validators/Timezone.php b/application/libraries/Ilch/Validation/Validators/Timezone.php new file mode 100644 index 000000000..b81556840 --- /dev/null +++ b/application/libraries/Ilch/Validation/Validators/Timezone.php @@ -0,0 +1,33 @@ +getParameter(1) === 'backwardsCompatible'; + + $this->setIsValid($this->getValue() === '' || in_array($this->getValue(), \DateTimeZone::listIdentifiers($includeOutdated ? \DateTimeZone::ALL_WITH_BC : \DateTimeZone::ALL)) !== false); + + return $this; + } +} diff --git a/tests/libraries/ilch/Validation/Validators/TimezoneTest.php b/tests/libraries/ilch/Validation/Validators/TimezoneTest.php new file mode 100644 index 000000000..323dd77a9 --- /dev/null +++ b/tests/libraries/ilch/Validation/Validators/TimezoneTest.php @@ -0,0 +1,94 @@ +run(); + self::assertSame($expectedIsValid, $validator->isValid()); + if (!empty($expectedErrorKey)) { + self::assertSame($expectedErrorKey, $validator->getErrorKey()); + self::assertSame($expectedErrorParameters, $validator->getErrorParameters()); + } + } + + /** + * @return array + */ + public function dpForTestValidator(): array + { + return [ + // timezone validations + 'timezone valid' => [ + 'data' => $this->createData('Europe/Kyiv'), + 'expectedIsValid' => true + ], + 'timezone valid including outdated' => [ + 'data' => $this->createData('America/Shiprock', true), + 'expectedIsValid' => true + ], + 'empty timezone valid' => [ + 'data' => $this->createData(''), + 'expectedIsValid' => true + ], + 'timezone invalid' => [ + 'data' => $this->createData('test'), + 'expectedIsValid' => false, + 'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', + 'expectedErrorParameters' => [] + ], + 'timezone invalid completely removed' => [ + 'data' => $this->createData('Canada/East-Saskatchewan'), + 'expectedIsValid' => false, + 'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', + 'expectedErrorParameters' => [] + ], + 'timezone invalid outdated' => [ + 'data' => $this->createData('America/Shiprock'), + 'expectedIsValid' => false, + 'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', + 'expectedErrorParameters' => [] + ], + ]; + } + + /** + * Helper function for creating data object + * + * @param string $value + * @return stdClass + */ + private function createData(string $value, bool $includeOutdated = false): stdClass + { + $data = new stdClass(); + $data->field = 'fieldName'; + $data->parameters = ['']; + $data->input = ['fieldName' => $value]; + if ($includeOutdated) { + $data->parameters[] = 'backwardsCompatible'; + } + return $data; + } +}