function RulesTestCase::testComponentInvocations

Same name in other branches
  1. 8.x-3.x d7-tests/rules_test_case.test \RulesTestCase::testComponentInvocations()

Tests invoking components from the action.

File

tests/rules.test, line 562

Class

RulesTestCase
Rules test cases.

Code

public function testComponentInvocations() {
    $set = rules_rule_set(array(
        'node1' => array(
            'type' => 'node',
            'label' => 'node',
        ),
    ));
    $set->rule(rule()->condition('node_is_published', array(
        'node:select' => 'node1',
    ))
        ->action('node_unpublish', array(
        'node:select' => 'node1',
    )));
    $set->integrityCheck()
        ->save('rules_test_set_2');
    // Use different names for the variables to ensure they are properly mapped
    // when taking over the variables to be saved.
    $rule = rule(array(
        'node2' => array(
            'type' => 'node',
            'label' => 'node',
        ),
    ));
    $rule->action('component_rules_test_set_2', array(
        'node1:select' => 'node2',
    ));
    $rule->action('node_make_sticky', array(
        'node:select' => 'node2',
    ));
    $node = $this->drupalCreateNode(array(
        'title' => 'The title.',
        'status' => 1,
        'sticky' => 0,
    ));
    $rule->execute($node);
    $node = node_load($node->nid, NULL, TRUE);
    $this->assertFalse($node->status, 'The component changes have been saved correctly.');
    $this->assertTrue($node->sticky, 'The action changes have been saved correctly.');
    // Check that we have saved the changes only once.
    $text = RulesLog::logger()->render();
    // Make sure both saves are handled in one save operation.
    $this->assertEqual(substr_count($text, 'Saved'), 1, 'Changes have been saved in one save operation.');
    RulesLog::logger()->checkLog();
    // Test recursion prevention on components by invoking the component from
    // itself, what should be prevented.
    $set->action('component_rules_test_set_2', array(
        'node1:select' => 'node1',
    ))
        ->save();
    $rule->execute($node);
    $text1 = RulesLog::logger()->render();
    $text2 = RulesTestCase::t('Not evaluating rule set %rules_test_set_2 to prevent recursion.', array(
        'rules_test_set_2',
    ));
    $this->assertTrue(strpos($text1, $text2) !== FALSE, "Recursion of component invocation prevented.");
    // Test executing the component provided in code via the action. This makes
    // sure the component in code has been properly picked up.
    $node->status = 0;
    node_save($node);
    rules_action('component_rules_test_action_set')->execute($node);
    $this->assertTrue($node->status == 1, 'Component provided in code has been executed.');
}