View source
<?php
namespace Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule;
class WordsFieldValidationRuleTest extends FieldValidationRuleBase {
protected $entity;
const FIELD_NAME = 'field_word_text';
const RULE_ID = 'words_field_validation_rule';
const RULE_TITLE = 'validation rule word text';
protected $ruleSet;
protected function setUp() {
parent::setUp();
$this
->setupTestArticle(self::FIELD_NAME);
$this->ruleSet = $this->ruleSetStorage
->create([
'name' => 'words_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' => 'Words error!',
'data' => [
'min' => 5,
'max' => 10,
],
]);
$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 testCountWords() {
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 'one', $this->ruleSet);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 'one two three four five six seven eight nine ten eleven', $this->ruleSet);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, 'one two three four five six');
$this
->updateSettings([
'min' => '1',
'max' => '2',
], self::RULE_ID, self::RULE_TITLE, $this->ruleSet, self::FIELD_NAME);
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 'one two three four five six', $this->ruleSet);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, 'one');
}
}