Skip to content

Commit

Permalink
Add timezone validator (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 authored Nov 15, 2024
1 parent cb47e21 commit 1ea49ae
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
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;
}
}

0 comments on commit 1ea49ae

Please sign in to comment.