You are here

public function RulesTestCase::testRuleSets in Rules 7.2

Same name and namespace in other branches
  1. 8.3 d7-tests/rules_test_case.test \RulesTestCase::testRuleSets()

Tests making use of rule sets.

File

tests/rules.test, line 525
Rules tests.

Class

RulesTestCase
Rules test cases.

Code

public function testRuleSets() {
  $set = rules_rule_set(array(
    'node' => array(
      'type' => 'node',
      'label' => 'node',
    ),
  ));
  $set
    ->rule(rule()
    ->action('drupal_message', array(
    'message:select' => 'node:title',
  )))
    ->rule(rule()
    ->condition('rules_condition_content_is_published')
    ->action('drupal_message', array(
    'message' => 'Node is published.',
  )));
  $set
    ->integrityCheck()
    ->save('rules_test_set_1');
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'The title.',
    'status' => 1,
  ));

  // Execute.
  rules_invoke_component('rules_test_set_1', $node);
  $msg = drupal_get_messages();
  $this
    ->assertEqual($msg['status'][0], 'The title.', 'First rule evaluated.');
  $this
    ->assertEqual($msg['status'][1], 'Node is published.', 'Second rule evaluated.');

  // Test a condition set.
  $set = rules_or(array(
    'node' => array(
      'type' => 'node',
      'label' => 'node',
    ),
  ));
  $set
    ->condition('data_is', array(
    'data:select' => 'node:author:name',
    'value' => 'notthename',
  ))
    ->condition('data_is', array(
    'data:select' => 'node:nid',
    'value' => $node->nid,
  ))
    ->integrityCheck()
    ->save('test', 'rules_test');

  // Load and execute condition set.
  $set = rules_config_load('test');
  $this
    ->assertTrue($set
    ->execute($node), 'Set has been correctly evaluated.');
  RulesLog::logger()
    ->checkLog();
}