View source
<?php
namespace Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule;
class DateRangeFieldValidationRuleTest extends FieldValidationRuleBase {
const FIELD_NAME = 'field_date_range_text';
const RULE_ID = 'date_range_field_validation_rule';
const RULE_TITLE = 'validation rule date range';
protected $entity;
protected $ruleSet;
protected function setUp() {
parent::setUp();
$this
->setupTestArticle(self::FIELD_NAME);
$this->ruleSet = $this->ruleSetStorage
->create([
'name' => 'Date_range_test',
'entity_type' => 'node',
'bundle' => 'article',
]);
$this->ruleSet
->addFieldValidationRule([
'id' => self::RULE_ID,
'title' => self::RULE_TITLE,
'weight' => 1,
'field_name' => self::FIELD_NAME,
'column' => 'value',
'error_message' => 'Dates are out of range!',
'data' => [
'min' => '2012-01-01 08:30:00',
'max' => '2013-01-01 08:30:00',
'cycle' => 'global',
],
]);
$this->ruleSet
->save();
$this->entity = $this->nodeStorage
->create([
'type' => 'article',
'title' => 'test',
self::FIELD_NAME => '',
]);
$this->entity
->get(self::FIELD_NAME)
->getFieldDefinition()
->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $this->ruleSet
->getName(),
]);
}
public function testValidDateRangeRule(string $cycle, string $date_string, string $min_date, string $max_date) {
$this->ruleSet = $this
->updateSettings([
'min' => $min_date,
'max' => $max_date,
'cycle' => $cycle,
], self::RULE_ID, self::RULE_TITLE, $this->ruleSet, self::FIELD_NAME);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, $date_string);
}
public function testInvalidDateRangeRule(string $cycle, string $date_string, string $min_date, string $max_date) {
$this->ruleSet = $this
->updateSettings([
'min' => $min_date,
'max' => $max_date,
'cycle' => $cycle,
], self::RULE_ID, self::RULE_TITLE, $this->ruleSet, self::FIELD_NAME);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, $date_string, $this->ruleSet);
}
public function dateValidProvider() {
return [
'global-upper-border' => [
'global',
'2018-01-01 08:30:00',
'2012-01-01 08:30:00',
'2018-01-01 08:30:00',
],
'global-lower-border' => [
'global',
'2012-01-01 08:30:00',
'2012-01-01 08:30:00',
'2018-01-01 08:30:00',
],
'year-upper-border' => [
'year',
'2011-07-01 08:30:00',
'2012-03-01 08:30:00',
'2018-08-01 08:30:00',
],
'year-lower-border' => [
'year',
'2011-04-01 08:30:00',
'2012-03-01 08:30:00',
'2018-08-01 08:30:00',
],
'month-upper-border' => [
'month',
'2014-07-09 08:30:00',
'2014-03-01 08:30:00',
'2014-09-09 08:30:00',
],
'month-lower-border' => [
'month',
'2014-04-03 08:30:00',
'2014-03-03 08:30:00',
'2018-09-09 08:30:00',
],
'day-upper-border' => [
'day',
'2013-01-06 08:30:00',
'2012-01-07 06:30:00',
'2018-01-07 08:30:00',
],
'day-lower-border' => [
'day',
'2012-01-06 08:30:00',
'2012-01-05 08:30:00',
'2018-01-01 09:30:00',
],
'hour-upper-border' => [
'hour',
'2013-01-01 08:40:00',
'2012-01-01 08:30:00',
'2018-01-01 08:40:00',
],
'hour-lower-border' => [
'hour',
'2012-01-01 08:30:00',
'2012-01-01 08:30:00',
'2018-01-01 08:40:00',
],
'minute-upper-border' => [
'minute',
'2012-01-01 08:30:10',
'2012-01-01 08:00:00',
'2018-01-01 08:00:10',
],
'minute-lower-border' => [
'minute',
'2012-01-01 08:30:05',
'2012-01-01 08:30:05',
'2018-01-01 08:30:06',
],
];
}
public function dateInvalidProvider() {
return [
'global-upper-border' => [
'global',
'2017-01-01 08:30:00',
'2012-01-01 08:30:00',
'2016-01-01 08:30:00',
],
'global-lower-border' => [
'global',
'2011-01-01 08:30:00',
'2012-01-01 08:30:00',
'2018-01-01 08:30:00',
],
'year-upper-border' => [
'year',
'2011-09-01 08:30:00',
'2012-03-01 08:30:00',
'2018-08-01 08:30:00',
],
'year-lower-border' => [
'year',
'2011-02-01 08:30:00',
'2012-03-01 08:30:00',
'2018-08-01 08:30:00',
],
'month-upper-border' => [
'month',
'2014-07-10 08:30:00',
'2014-03-01 08:30:00',
'2014-09-09 08:30:00',
],
'month-lower-border' => [
'month',
'2014-04-02 08:30:00',
'2014-03-03 08:30:00',
'2018-09-09 08:30:00',
],
'day-upper-border' => [
'day',
'2013-01-06 09:30:00',
'2012-01-07 06:30:00',
'2018-01-07 08:30:00',
],
'day-lower-border' => [
'day',
'2012-01-06 07:30:00',
'2012-01-05 08:30:00',
'2018-01-01 09:30:00',
],
'hour-upper-border' => [
'hour',
'2013-01-01 08:45:00',
'2012-01-01 08:30:00',
'2018-01-01 08:40:00',
],
'hour-lower-border' => [
'hour',
'2012-01-01 08:25:00',
'2012-01-01 08:30:00',
'2018-01-01 08:40:00',
],
'minute-upper-border' => [
'minute',
'2012-01-01 08:30:15',
'2012-01-01 08:00:00',
'2018-01-01 08:00:10',
],
'minute-lower-border' => [
'minute',
'2012-01-01 08:30:00',
'2012-01-01 08:30:05',
'2018-01-01 08:30:06',
],
];
}
}