Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timezone validator #1102

Merged
merged 1 commit into from
Nov 15, 2024
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 application/libraries/Ilch/Translations/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions application/libraries/Ilch/Translations/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 33 additions & 0 deletions application/libraries/Ilch/Validation/Validators/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @copyright Ilch 2
*/

namespace Ilch\Validation\Validators;

/**
* Timezone validation class.
*/
class Timezone extends Base
{
/**
* Default error key for this validator.
*
* @var string
*/
protected $errorKey = 'validation.errors.timezone.notAValidTimezone';

/**
* Runs the validation.
*
* @return self
*/
public function run(): self
{
$includeOutdated = $this->getParameter(1) === 'backwardsCompatible';

$this->setIsValid($this->getValue() === '' || in_array($this->getValue(), \DateTimeZone::listIdentifiers($includeOutdated ? \DateTimeZone::ALL_WITH_BC : \DateTimeZone::ALL)) !== false);

return $this;
}
}
94 changes: 94 additions & 0 deletions tests/libraries/ilch/Validation/Validators/TimezoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* @copyright Ilch 2
* @package ilch_phpunit
*/

namespace Ilch\Validation\Validators;

use PHPUnit\Ilch\TestCase;
use stdClass;

/**
* Tests for the timezone validator
*/
class TimezoneTest extends TestCase
{
/**
* @dataProvider dpForTestValidator
*
* @param stdClass $data
* @param bool $expectedIsValid
* @param string $expectedErrorKey
* @param array $expectedErrorParameters
*/
public function testValidator(stdClass $data, bool $expectedIsValid, string $expectedErrorKey = '', array $expectedErrorParameters = [])
{
$validator = new Timezone($data);
$validator->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;
}
}