You are here

protected function BlackListFieldValidationRuleTest::setUp in Field Validation 8

Overrides FieldValidationRuleBase::setUp

File

tests/src/Kernel/Plugin/FieldValidationRule/BlackListFieldValidationRuleTest.php, line 58

Class

BlackListFieldValidationRuleTest
Tests BlacklistFieldValidationRule.

Namespace

Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule

Code

protected function setUp() {
  parent::setUp();
  $this
    ->setupTestArticle(self::FIELD_NAME);
  $this->ruleSet = $this->ruleSetStorage
    ->create([
    'name' => 'Blacklist_test',
    'entity_type' => 'node',
    'bundle' => 'article',
  ]);
  $this->ruleSet
    ->addFieldValidationRule([
    'id' => 'blacklist_field_validation_rule',
    'title' => 'validation rule blacklist',
    'weight' => 1,
    'field_name' => self::FIELD_NAME,
    'column' => 'value',
    'error_message' => 'Blacklisted words are in field',
    'data' => [
      'setting' => implode(',', $this->blacklisted),
    ],
  ]);
  $this->ruleSet
    ->save();
  $this->entity = $this->nodeStorage
    ->create([
    'type' => 'article',
    'title' => 'test',
    self::FIELD_NAME => $this->blacklisted[array_rand($this->blacklisted)],
  ]);
  $this->entity
    ->get(self::FIELD_NAME)
    ->getFieldDefinition()
    ->addConstraint('FieldValidationConstraint', [
    'ruleset_name' => $this->ruleSet
      ->getName(),
  ]);
}