You are here

public function MetadataAssertionTest::testAssertingEntityBundle in Rules 8.3

Tests asserting metadata using the EntityIfOfBundle condition.

File

tests/src/Kernel/Engine/MetadataAssertionTest.php, line 70

Class

MetadataAssertionTest
Tests asserting metadata works correctly.

Namespace

Drupal\Tests\rules\Kernel\Engine

Code

public function testAssertingEntityBundle() {

  // When trying to use the field_text field without knowledge of the bundle,
  // the field is not available.
  $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());

  // Now add the EntityIsOfBundle condition and try again.
  $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));
}