View source
<?php
namespace Drupal\Tests\rules\Kernel\Engine;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\RulesComponent;
use Drupal\Tests\rules\Kernel\RulesKernelTestBase;
class MetadataAssertionTest extends RulesKernelTestBase {
protected static $modules = [
'rules',
'typed_data',
'system',
'node',
'field',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('user');
$this->container
->get('module_handler')
->loadInclude('user', 'install');
user_install();
$this
->installEntitySchema('node');
$this
->installConfig([
'field',
]);
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type_manager
->getStorage('node_type')
->create([
'type' => 'page',
])
->save();
FieldStorageConfig::create([
'field_name' => 'field_text',
'type' => 'string',
'entity_type' => 'node',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'field_name' => 'field_text',
'entity_type' => 'node',
'bundle' => 'page',
])
->save();
}
public function testAssertingEntityBundle() {
$rule = $this->expressionManager
->createRule();
$rule
->addAction('rules_system_message', ContextConfig::create()
->map('message', 'node.field_text.value')
->setValue('type', 'status'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(1, iterator_count($violation_list));
$this
->assertEquals('Data selector %selector for context %context_name is invalid. @message', $violation_list
->get(0)
->getMessage()
->getUntranslatedString());
$rule
->addCondition('rules_entity_is_of_bundle', ContextConfig::create()
->map('entity', 'node')
->setValue('type', 'node')
->setValue('bundle', 'page'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(0, iterator_count($violation_list));
}
public function testAssertingWithLogicalOperations() {
$rule = $this->expressionManager
->createRule();
$and = $this->expressionManager
->createAnd();
$and
->addCondition('rules_entity_is_of_bundle', ContextConfig::create()
->map('entity', 'node')
->setValue('type', 'node')
->setValue('bundle', 'page'));
$rule
->addExpressionObject($and);
$rule
->addAction('rules_system_message', ContextConfig::create()
->map('message', 'node.field_text.value')
->setValue('type', 'status'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(0, iterator_count($violation_list));
$rule = $this->expressionManager
->createRule();
$or = $this->expressionManager
->createOr();
$or
->addCondition('rules_entity_is_of_bundle', ContextConfig::create()
->map('entity', 'node')
->setValue('type', 'node')
->setValue('bundle', 'page'));
$rule
->addExpressionObject($or);
$rule
->addAction('rules_system_message', ContextConfig::create()
->map('message', 'node.field_text.value')
->setValue('type', 'status'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(1, iterator_count($violation_list));
}
public function testAssertingOfNegatedConditions() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_entity_is_of_bundle', ContextConfig::create()
->map('entity', 'node')
->setValue('type', 'node')
->setValue('bundle', 'page'))
->negate(TRUE);
$rule
->addAction('rules_system_message', ContextConfig::create()
->map('message', 'node.field_text.value')
->setValue('type', 'status'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(1, iterator_count($violation_list));
$rule = $this->expressionManager
->createRule();
$and = $this->expressionManager
->createAnd();
$and
->addCondition('rules_entity_is_of_bundle', ContextConfig::create()
->map('entity', 'node')
->setValue('type', 'node')
->setValue('bundle', 'page'));
$and
->negate(TRUE);
$rule
->addExpressionObject($and);
$rule
->addAction('rules_system_message', ContextConfig::create()
->map('message', 'node.field_text.value')
->setValue('type', 'status'));
$violation_list = RulesComponent::create($rule)
->addContextDefinition('node', ContextDefinition::create('entity:node'))
->checkIntegrity();
$this
->assertEquals(1, iterator_count($violation_list));
}
}