You are here

function RulesTriggerTestCase::testRecursionOnDifferentArguments in Rules 8.3

Same name and namespace in other branches
  1. 7.2 tests/rules.test \RulesTriggerTestCase::testRecursionOnDifferentArguments()

Ensure the recursion prevention still allows to let the rule trigger again during evaluation of the same event set, if the event isn't caused by the rule itself - thus we won't run in an infinite loop.

File

d7-tests/rules_test_trigger_case.test, line 160
Rules 7.x tests.

Class

RulesTriggerTestCase
Test triggering rules.

Code

function testRecursionOnDifferentArguments() {

  // Create rule1 - which might recurse.
  $rule = $this
    ->createTestRule(FALSE, 'node_update');
  $rule
    ->action('rules_node_make_sticky_action');
  $rule->label = 'rule 1';
  $rule
    ->integrityCheck()
    ->save();

  // Create rule2 - which triggers rule1 on another node.
  $node2 = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'sticky' => 0,
    'status' => 0,
  ));
  $rule2 = $this
    ->createTestRule(FALSE, 'node_update');
  $rule2
    ->action('rules_action_load_node', array(
    'nid' => $node2->nid,
  ))
    ->action('rules_node_make_sticky_action', array(
    'node:select' => 'node_loaded',
  ));
  $rule2->label = 'rule 2';
  $rule2
    ->save();

  // Now trigger both rules by generating the event.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'sticky' => 0,
    'status' => 0,
  ));
  node_save($node);

  //debug(RulesLog::logger()->render());
  $text = RulesLog::logger()
    ->render();
  $pos = strpos($text, RulesTestCase::t('Evaluating conditions of rule %rule 1', array(
    'rule 1',
  )));
  $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Evaluating conditions of rule %rule 2', array(
    'rule 2',
  )), $pos) : FALSE;
  $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Saved %node_loaded of type %node.', array(
    'node_loaded',
    'node',
  )), $pos) : FALSE;
  $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Evaluating conditions of rule %rule 1', array(
    'rule 1',
  )), $pos) : FALSE;
  $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Not evaluating reaction rule %rule 2 to prevent recursion', array(
    'rule 2',
  )), $pos) : FALSE;
  $this
    ->assertTrue($pos !== FALSE, 'Rule1 was triggered on the event caused by Rule2.');
}