You are here

protected function LengthFieldValidationRuleTest::setUp in Field Validation 8

Overrides FieldValidationRuleBase::setUp

File

tests/src/Kernel/Plugin/FieldValidationRule/LengthFieldValidationRuleTest.php, line 57

Class

LengthFieldValidationRuleTest
Tests LengthFieldValidationRule.

Namespace

Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule

Code

protected function setUp() {
  parent::setUp();
  $this->min = 10;
  $this->max = 32;
  $this
    ->setupTestArticle(self::FIELD_NAME);
  $this->ruleSet = $this->ruleSetStorage
    ->create([
    'name' => 'length_rule_test',
    'entity_type' => 'node',
    'bundle' => 'article',
  ]);
  $this->ruleSet
    ->addFieldValidationRule([
    'id' => 'length_field_validation_rule',
    'title' => 'validation rule length',
    'weight' => 1,
    'field_name' => self::FIELD_NAME,
    'column' => 'value',
    'error_message' => 'Text is too long',
    'data' => [
      'min' => $this->min,
      'max' => $this->max,
    ],
  ]);
  $this->ruleSet
    ->save();
  $this->entity = $this->nodeStorage
    ->create([
    'type' => 'article',
    'title' => 'test',
    self::FIELD_NAME => $this->randomString,
  ]);
  $this->entity
    ->get(self::FIELD_NAME)
    ->getFieldDefinition()
    ->addConstraint('FieldValidationConstraint', [
    'ruleset_name' => $this->ruleSet
      ->getName(),
  ]);
}