function RulesTestCase::testDataSelectors

Same name in other branches
  1. 7.x-2.x tests/rules.test \RulesTestCase::testDataSelectors()

Tests using data selectors.

File

d7-tests/rules_test_case.test, line 409

Class

RulesTestCase

Code

function testDataSelectors() {
    $body[LANGUAGE_NONE][0] = array(
        'value' => '<b>The body & nothing.</b>',
    );
    $node = $this->drupalCreateNode(array(
        'body' => $body,
        'type' => 'page',
        'summary' => '',
    ));
    $rule = rule(array(
        'nid' => array(
            'type' => 'integer',
        ),
    ));
    $rule->action('rules_action_load_node')
        ->action('drupal_message', array(
        'message:select' => 'node_loaded:body:value',
    ))
        ->execute($node->nid);
    RulesLog::logger()->checkLog();
    $msg = drupal_get_messages('status');
    $last_msg = array_pop($msg['status']);
    $wrapper = entity_metadata_wrapper('node', $node);
    $this->assertEqual($last_msg, $wrapper->body->value
        ->value(array(
        'sanitize' => TRUE,
    )), 'Data selector for getting parameter applied.');
    // Get a "reference" on the same object as returned by node_load().
    $node = node_load($node->nid);
    $rule = rule(array(
        'nid' => array(
            'type' => 'integer',
        ),
    ));
    $rule->action('rules_action_load_node')
        ->action('data_set', array(
        'data:select' => 'node_loaded:title',
        'value' => 'Test title',
    ))
        ->action('data_set', array(
        'data:select' => 'node_loaded:title',
        'value' => 'Test title2',
    ))
        ->execute($node->nid);
    $wrapper = entity_metadata_wrapper('node', $node);
    $this->assertEqual('Test title2', $wrapper->title
        ->value(), 'Data has been modified and saved.');
    RulesLog::logger()->checkLog();
    $text = RulesLog::logger()->render();
    $msg = RulesTestCase::t('Saved %node_loaded of type %node.', array(
        'node_loaded',
        'node',
    ));
    if ($pos1 = strpos($text, $msg)) {
        $pos2 = strpos($text, $msg, $pos1 + 1);
    }
    $this->assertTrue($pos1 && $pos2 === FALSE, 'Data has been saved only once.');
    // Test validation.
    try {
        rules_action('data_set', array(
            'data' => 'no-selector',
            'value' => '',
        ))->integrityCheck();
        $this->fail("Validation hasn't created an exception.");
    } catch (RulesIntegrityException $e) {
        $this->pass("Validation error correctly detected: " . $e);
    }
    // Test auto creation of nested data structures, like the node body field.
    // I.e. if $node->body is not set, it is automatically initialized to an
    // empty array, so that the nested value can be set and the wrappers do not
    // complain about missing parent data structures.
    $rule = rule();
    $rule->action('entity_create', array(
        'type' => 'node',
        'param_type' => 'page',
        'param_title' => 'foo',
        'param_author' => $GLOBALS['user'],
    ));
    $rule->action('data_set', array(
        'data:select' => 'entity_created:body:value',
        'value' => 'test content',
    ))
        ->execute();
    try {
        RulesLog::logger()->checkLog();
        $this->pass('Auto creation of nested data structures.');
    } catch (Exception $e) {
        $this->fail('Auto creation of nested data structures.');
    }
    // Make sure variables that are passed wrapped work.
    $result = rules_condition('rules_test_condition_node_wrapped')->execute($node->nid);
    $this->assertTrue($result, 'Condition receiving wrapped parameter.');
    // Make sure wrapped parameters are checked for containing NULL values.
    $rule = rule(array(
        'node' => array(
            'type' => 'node',
            'optional' => TRUE,
        ),
    ));
    $rule->condition('rules_test_condition_node_wrapped', array(
        'node:select' => 'node',
    ));
    $rule->execute(entity_metadata_wrapper('node'));
    $text = RulesLog::logger()->render();
    $msg = RulesTestCase::t('The variable or parameter %node is empty.', array(
        'node',
    ));
    $this->assertTrue(strpos($text, $msg) !== FALSE, 'Evaluation aborted due to an empty argument value.');
}