You are here

public function RulesEngineTest::testRuleCreation in Rules 8.3

Tests creating a rule and iterating over the rule elements.

File

tests/src/Kernel/RulesEngineTest.php, line 35

Class

RulesEngineTest
Test using the Rules API to create and evaluate rules.

Namespace

Drupal\Tests\rules\Kernel

Code

public function testRuleCreation() {

  // Create an 'and' condition container and add conditions to it.
  $and = $this->expressionManager
    ->createAnd()
    ->addCondition('rules_test_false')
    ->addCondition('rules_test_true', ContextConfig::create()
    ->negateResult())
    ->negate();

  // Test that the 'and' condition container evaluates to TRUE.
  $this
    ->assertTrue($and
    ->execute());

  // Create an 'or' condition container and add conditions to it, including
  // the previously created 'and' condition container.
  $or = $this->expressionManager
    ->createOr()
    ->addCondition('rules_test_true', ContextConfig::create()
    ->negateResult())
    ->addCondition('rules_test_false')
    ->addExpressionObject($and);

  // Test that the 'or' condition container evaluates to TRUE.
  $this
    ->assertTrue($or
    ->execute());

  // Create a rule and add conditions to it, including the previously created
  // 'or' condition container.
  $rule = $this->expressionManager
    ->createRule();
  $rule
    ->addCondition('rules_test_true')
    ->addCondition('rules_test_true')
    ->addExpressionObject($or);

  // Test that the rule's condition container evaluates to TRUE.
  $this
    ->assertTrue($rule
    ->getConditions()
    ->execute());

  // Add an action to it and execute the rule.
  $rule
    ->addAction('rules_test_debug_log');
  $rule
    ->execute();

  // Test that the action logged something.
  $this
    ->assertRulesDebugLogEntryExists('action called');
}