MustBeEmptyFieldValidationRuleTest.php in Field Validation 8
File
tests/src/Kernel/Plugin/FieldValidationRule/MustBeEmptyFieldValidationRuleTest.php
View source
<?php
namespace Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule;
class MustBeEmptyFieldValidationRuleTest extends FieldValidationRuleBase {
protected $ruleSet;
protected $entity;
protected $randomString;
const FIELD_NAME = 'field_empty_text';
protected function setUp() {
parent::setUp();
$this
->setupTestArticle(self::FIELD_NAME);
$this->ruleSet = $this->ruleSetStorage
->create([
'name' => 'must_be_empty',
'entity_type' => 'node',
'bundle' => 'article',
]);
$this->ruleSet
->addFieldValidationRule([
'id' => 'must_be_empty_field_validation_rule',
'title' => 'validation rule must be empty',
'weight' => 1,
'field_name' => self::FIELD_NAME,
'column' => 'value',
'error_message' => 'Field is not empty',
]);
$this->ruleSet
->save();
$this->entity = $this->nodeStorage
->create([
'type' => 'article',
'title' => 'test',
self::FIELD_NAME => 'I am groot!',
]);
$this->entity
->get(self::FIELD_NAME)
->getFieldDefinition()
->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $this->ruleSet
->getName(),
]);
}
public function testEmptyField() {
$this
->assertConstraintFail($this->entity, self::FIELD_NAME, 'I am groot!', $this->ruleSet);
$this
->assertConstraintPass($this->entity, self::FIELD_NAME, '');
}
}