function RulesTestCase::testLoops

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

Test using loops.

File

d7-tests/rules_test_case.test, line 660

Class

RulesTestCase

Code

function testLoops() {
    // Test passing the list parameter as argument to ensure that is working
    // generally for plugin container too.
    drupal_get_messages(NULL, TRUE);
    $loop = rules_loop();
    $loop->action('drupal_message', array(
        'message' => 'test',
    ));
    $arg_info = $loop->parameterInfo();
    $this->assert($arg_info['list']['type'] == 'list', 'Argument info contains list.');
    $loop->execute(array(
        1,
        2,
    ));
    // Ensure the action has been executed twice, once for each list item.
    $msg = drupal_get_messages();
    $this->assert($msg['status'][0] == 'test' && $msg['status'][1], 'Loop has been properly executed');
    // Now test looping over nodes.
    $node1 = $this->drupalCreateNode(array(
        'type' => 'page',
        'sticky' => 0,
    ));
    $node2 = $this->drupalCreateNode(array(
        'type' => 'page',
        'sticky' => 0,
    ));
    $node3 = $this->drupalCreateNode(array(
        'type' => 'page',
        'sticky' => 0,
    ));
    $rule = rule(array(
        'list' => array(
            'type' => 'list<node>',
            'label' => 'A list of nodes',
        ),
    ));
    $loop = rules_loop(array(
        'list:select' => 'list',
        'item:var' => 'node',
    ));
    $loop->action('data_set', array(
        'data:select' => 'node:sticky',
        'value' => TRUE,
    ));
    $rule->action($loop);
    // Test using a list with data selectors, just output the last nodes type.
    $rule->action('drupal_message', array(
        'message:select' => 'list:2:type',
    ));
    $rule->execute(array(
        $node1->nid,
        $node2->nid,
        $node3->nid,
    ));
    $text = RulesLog::logger()->render();
    $save_msg = RulesTestCase::t('Saved %node of type %node.', array(
        'node',
        'node',
    ));
    $this->assertTrue(substr_count($text, $save_msg) == 3, 'List item variables have been saved.');
    RulesLog::logger()->checkLog();
}