You are here

public function RulesIntegrationTestCase::testRulesCoreIntegration in Rules 7.2

Same name and namespace in other branches
  1. 8.3 d7-tests/rules_integration_test_case.test \RulesIntegrationTestCase::testRulesCoreIntegration()

Tests the "rules_core" integration.

File

tests/rules.test, line 1941
Rules tests.

Class

RulesIntegrationTestCase
Tests provided module integration.

Code

public function testRulesCoreIntegration() {

  // Make sure the date input evaluator evaluates properly using strtotime().
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'foo',
  ));
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
    ),
  ));
  $rule
    ->action('data_set', array(
    'data:select' => 'node:created',
    'value' => '+1 day',
  ));
  $rule
    ->execute($node);
  RulesLog::logger()
    ->checkLog();
  $node = node_load($node->nid, NULL, TRUE);
  $now = RulesDateInputEvaluator::gmstrtotime('now');

  // Tolerate a difference of a second.
  $this
    ->assertTrue(abs($node->created - $now - 86400) <= 1, 'Date input has been evaluated.');

  // Test using a numeric offset.
  $rule = rule(array(
    'number' => array(
      'type' => 'decimal',
    ),
  ), array(
    'number',
  ));
  $rule
    ->action('data_set', array(
    'data:select' => 'number',
    'value:select' => 'number',
    'value:process' => array(
      'num_offset' => array(
        'value' => 1,
      ),
    ),
  ));
  $rule
    ->integrityCheck();
  list($result) = $rule
    ->execute(10);
  $this
    ->assertTrue($result == 11, 'Numeric offset has been applied');

  // Test using a date offset.
  $set = rules_action_set(array(
    'date' => array(
      'type' => 'date',
    ),
  ), array(
    'date',
  ));
  $set
    ->action('data_set', array(
    'data:select' => 'date',
    'value:select' => 'date',
    'value:process' => array(
      'date_offset' => array(
        'value' => 1000,
      ),
    ),
  ));
  $date = date_create("14 Mar 1984 10:19:23 +01:00")
    ->format('U');
  list($result) = $set
    ->execute($date);
  $this
    ->assertEqual($result, $date + 1000, 'Date offset in seconds has been added.');

  // Test using a negative offset of 2 months.
  $set = rules_action_set(array(
    'date' => array(
      'type' => 'date',
    ),
  ), array(
    'date',
  ));
  $set
    ->action('data_set', array(
    'data:select' => 'date',
    'value:select' => 'date',
    'value:process' => array(
      'date_offset' => array(
        'value' => -86400 * 30 * 2,
      ),
    ),
  ));
  $date = date_create("14 Mar 1984 10:19:23 +01:00")
    ->format('U');
  list($result) = $set
    ->execute($date);
  $this
    ->assertEqual($result, date_create("14 Jan 1984 10:19:23 +01:00")
    ->format('U'), 'Date offset of -2 months has been added.');

  // Test using a positive offset of 1 year 6 months and 30 minutes.
  $set = rules_action_set(array(
    'date' => array(
      'type' => 'date',
    ),
  ), array(
    'date',
  ));
  $set
    ->action('data_set', array(
    'data:select' => 'date',
    'value:select' => 'date',
    'value:process' => array(
      'date_offset' => array(
        'value' => 86400 * 30 * 18 + 30 * 60,
      ),
    ),
  ));
  $date = date_create("14 Mar 1984 10:19:23 +01:00")
    ->format('U');
  list($result) = $set
    ->execute($date);
  $this
    ->assertEqual($result, date_create("14 Sep 1985 10:49:23 +01:00")
    ->format('U'), 'Date offset of 1 year 6 months and 30 minutes has been added.');
  RulesLog::logger()
    ->checkLog();
}