View source
<?php
namespace Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule;
class IntegerFieldValidationTest extends FieldValidationRuleBase {
protected $ruleSet;
protected $entity;
const FIELD_NAME = 'field_integer_text';
protected $min;
protected $max;
protected function setUp() {
parent::setUp();
$this->min = -255;
$this->max = 255;
$this
->setupTestArticle(self::FIELD_NAME);
$this->ruleSet = $this->ruleSetStorage
->create([
'name' => 'integer_test_text',
'entity_type' => 'node',
'bundle' => 'article',
]);
$this->ruleSet
->addFieldValidationRule([
'id' => 'integer_field_validation_rule',
'title' => 'validation rule integer text',
'weight' => 1,
'field_name' => self::FIELD_NAME,
'column' => 'value',
'error_message' => 'Integer is not valid text field',
'data' => [
'min' => $this->min,
'max' => $this->max,
],
]);
$this->ruleSet
->save();
$this->entity = $this->nodeStorage
->create([
'type' => 'article',
'title' => 'test',
]);
$this->entity
->get(self::FIELD_NAME)
->getFieldDefinition()
->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $this->ruleSet
->getName(),
]);
}
public function testInteger() {
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, abs(($this->max + 1) * 10), $this->ruleSet);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, ($this->min - 1) * 10, $this->ruleSet);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 'TestS', $this->ruleSet);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, 150);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, -150);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 1.2345, $this->ruleSet);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 1230.0, $this->ruleSet);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 7.0E-18, $this->ruleSet);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, $this->min);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, $this->max);
}
}