function RulesIntegrationTestCase::testDataIntegration

Same name and namespace in other branches
  1. 7.x-2.x tests/rules.test \RulesIntegrationTestCase::testDataIntegration()

Test data integration.

File

d7-tests/rules_integration_test_case.test, line 50

Class

RulesIntegrationTestCase
Tests provided module integration.

Code

function testDataIntegration() {
  // Test data_create action.
  $action = rules_action('data_create', array(
    'type' => 'log_entry',
    'param_type' => 'rules_test',
    'param_message' => 'Rules test log message',
    'param_severity' => WATCHDOG_WARNING,
    'param_request_uri' => 'http://example.com',
    'param_link' => '',
  ));
  $action->access();
  $action->execute();
  $text = RulesLog::logger()->render();
  $pos = strpos($text, RulesTestCase::t('Added the provided variable %data_created of type %log_entry', array(
    'data_created',
    'log_entry',
  )));
  $this->assertTrue($pos !== FALSE, 'Data of type log entry has been created.');
  // Test variable_add action.
  $action = rules_action('variable_add', array(
    'type' => 'text_formatted',
    'value' => array(
      'value' => 'test text',
      'format' => 1,
    ),
  ));
  $action->access();
  $action->execute();
  $text = RulesLog::logger()->render();
  $pos = strpos($text, RulesTestCase::t('Added the provided variable %variable_added of type %text_formatted', array(
    'variable_added',
    'text_formatted',
  )));
  $this->assertTrue($pos !== FALSE, 'Data of type text formatted has been created.');
  // Test using the list actions.
  $rule = rule(array(
    'list' => array(
      'type' => 'list<text>',
      'label' => 'A list of text',
    ),
  ));
  $rule->action('list_add', array(
    'list:select' => 'list',
    'item' => 'bar2',
  ));
  $rule->action('list_add', array(
    'list:select' => 'list',
    'item' => 'bar',
    'pos' => 'start',
  ));
  $rule->action('list_add', array(
    'list:select' => 'list',
    'item' => 'bar',
    'unique' => TRUE,
  ));
  $rule->action('list_remove', array(
    'list:select' => 'list',
    'item' => 'bar2',
  ));
  $list = entity_metadata_wrapper('list', array(
    'foo',
    'foo2',
  ));
  $rule->execute($list);
  RulesLog::logger()->checkLog();
  $this->assertEqual($list->value(), array(
    'bar',
    'foo',
    'foo2',
  ), 'List items removed and added.');
  $this->assertFalse(rules_condition('list_contains')->execute($list, 'foo-bar'), 'Condition "List item contains" evaluates to FALSE');
  $this->assertTrue(rules_condition('list_contains')->execute($list, 'foo'), 'Condition "List item contains" evaluates to TRUE');
  //debug(RulesLog::logger()->render());
  // Test data_is condition with IN operation.
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
    ),
  ));
  $rule->condition('data_is', array(
    'data:select' => 'node:title',
    'op' => 'IN',
    'value' => array(
      'foo',
      'bar',
    ),
  ));
  $rule->action('data_set', array(
    'data:select' => 'node:title',
    'value' => 'bar',
  ));
  $rule->integrityCheck();
  $node = $this->drupalCreateNode(array(
    'title' => 'foo',
  ));
  $rule->execute($node);
  $this->assertEqual($node->title, 'bar', "Data comparison using IN operation evaluates to TRUE.");
  // Test Condition: Data is empty.
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
    ),
  ));
  $rule->condition('data_is_empty', array(
    'data:select' => 'node:title',
  ));
  $rule->action('data_set', array(
    'data:select' => 'node:title',
    'value' => 'bar',
  ));
  $rule->integrityCheck();
  // Data is empty condition evaluates to TRUE
  // for node with empty title, action sets title to 'bar'.
  $node = $this->drupalCreateNode(array(
    'title' => '',
    'type' => 'article',
  ));
  $rule->execute($node);
  $this->assertEqual($node->title, 'bar', "Data is empty condition evaluates to TRUE for node with empty title, action sets title to 'bar'.");
  // Data is empty condition evaluates to FALSE
  // for node with title 'foo', action is not executed.
  $node = $this->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
  ));
  $rule->execute($node);
  $this->assertEqual($node->title, 'foo', "Data is empty condition evaluates to FALSE for node with title 'foo', action is not executed.");
  // Data is empty condition evaluates to TRUE for the parent of a
  // not existing term in the tags field of the node.
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
    ),
  ));
  $rule->condition('node_is_of_type', array(
    'type' => array(
      'article',
    ),
  ));
  $rule->condition('data_is_empty', array(
    'data:select' => 'node:field-tags:0:parent',
  ));
  $rule->action('data_set', array(
    'data:select' => 'node:title',
    'value' => 'bar',
  ));
  $rule->integrityCheck();
  $node = $this->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
  ));
  $rule->execute($node);
  $this->assertEqual($node->title, 'bar', "Data is empty condition evaluates to TRUE for not existing data structures");
  // Test Action: Calculate a value.
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
    ),
  ));
  $rule->action('data_calc', array(
    'input_1:select' => 'node:nid',
    'op' => '*',
    'input_2' => 2,
  ));
  $rule->action('data_set', array(
    'data:select' => 'node:title',
    'value:select' => 'result',
  ));
  $rule->integrityCheck();
  $rule->execute($node);
  $this->assertEqual($node->title, $node->nid * 2, "Value has been calculated.");
  // Test moving a date.
  $action_set = rules_action_set(array(
    'date' => array(
      'type' => 'date',
    ),
  ), array(
    'date',
  ));
  $action_set->action('data_calc', array(
    'input_1:select' => 'date',
    'op' => '+',
    'input_2' => 3600,
  ))
    ->action('data_set', array(
    'data:select' => 'date',
    'value:select' => 'result',
  ));
  $action_set->integrityCheck();
  list($result) = $action_set->execute(REQUEST_TIME);
  $this->assertEqual($result, REQUEST_TIME + 3600, 'Used data calculation action to move a date by an hour.');
  // Test data type conversion action.
  $set = rules_action_set(array(
    'result' => array(
      'type' => 'text',
      'parameter' => FALSE,
    ),
  ), array(
    'result',
  ));
  $set->action('data_convert', array(
    'type' => 'text',
    'value:select' => 'site:login-url',
  ));
  $set->action('data_set', array(
    'data:select' => 'result',
    'value:select' => 'conversion_result',
  ));
  list($result) = $set->execute();
  $set->integrityCheck();
  $this->assertEqual($result, url('user', array(
    'absolute' => TRUE,
  )), 'Converted URI to text.');
  $set = rules_action_set(array(
    'result' => array(
      'type' => 'integer',
      'parameter' => FALSE,
    ),
    'source' => array(
      'type' => 'text',
    ),
  ), array(
    'result',
  ));
  $set->action('data_convert', array(
    'type' => 'integer',
    'value:select' => 'source',
  ));
  $set->action('data_set', array(
    'data:select' => 'result',
    'value:select' => 'conversion_result',
  ));
  list($result) = $set->execute('9.4');
  $this->assertEqual($result, 9, 'Converted decimal to integer using rounding.');
  $set = rules_action_set(array(
    'result' => array(
      'type' => 'integer',
      'parameter' => FALSE,
    ),
    'source' => array(
      'type' => 'text',
    ),
  ), array(
    'result',
  ));
  $set->action('data_convert', array(
    'type' => 'integer',
    'value:select' => 'source',
    'rounding_behavior' => 'down',
  ));
  $set->action('data_set', array(
    'data:select' => 'result',
    'value:select' => 'conversion_result',
  ));
  list($result) = $set->execute('9.6');
  $this->assertEqual($result, 9, 'Converted decimal to integer using roundin behavio down.');
  $set = rules_action_set(array(
    'result' => array(
      'type' => 'integer',
      'parameter' => FALSE,
    ),
    'source' => array(
      'type' => 'text',
    ),
  ), array(
    'result',
  ));
  $set->action('data_convert', array(
    'type' => 'integer',
    'value:select' => 'source',
    'rounding_behavior' => 'up',
  ));
  $set->action('data_set', array(
    'data:select' => 'result',
    'value:select' => 'conversion_result',
  ));
  list($result) = $set->execute('9.4');
  $this->assertEqual($result, 10, 'Converted decimal to integer using rounding behavior up.');
  // Test text matching condition.
  $result = rules_condition('text_matches')->execute('my-text', 'text', 'contains');
  $result2 = rules_condition('text_matches')->execute('my-text', 'tex2t', 'contains');
  $this->assertTrue($result && !$result2, 'Text matching condition using operation contain evaluated.');
  $result = rules_condition('text_matches')->execute('my-text', 'my', 'starts');
  $result2 = rules_condition('text_matches')->execute('my-text', 'text', 'starts');
  $this->assertTrue($result && !$result2, 'Text matching condition using operation starts evaluated.');
  $result = rules_condition('text_matches')->execute('my-text', 'text', 'ends');
  $result2 = rules_condition('text_matches')->execute('my-text', 'my', 'ends');
  $this->assertTrue($result && !$result2, 'Text matching condition using operation ends evaluated.');
  $result = rules_condition('text_matches')->execute('my-text', 'me?y-texx?t', 'regex');
  $result2 = rules_condition('text_matches')->execute('my-text', 'me+y-texx?t', 'regex');
  $this->assertTrue($result && !$result2, 'Text matching condition using operation regex evaluated.');
}