PlainTextFieldValidationRuleTest.php in Field Validation 8
File
tests/src/Kernel/Plugin/FieldValidationRule/PlainTextFieldValidationRuleTest.php
View source
<?php
namespace Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule;
class PlainTextFieldValidationRuleTest extends FieldValidationRuleBase {
protected $entity;
const FIELD_NAME = 'field_plain_text';
const RULE_ID = 'plain_text_field_validation_rule';
const RULE_TITLE = 'validation rule plain text';
protected $ruleSet;
protected function setUp() {
parent::setUp();
$this
->setupTestArticle(self::FIELD_NAME);
$this->ruleSet = $this->ruleSetStorage
->create([
'name' => 'plain_text_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' => 'Text is not plain!',
]);
$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 testPlainTextRule() {
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, 'i am a plain text!');
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, '<i>i am not plain text </i>', $this->ruleSet);
}
}