function RulesIntegrationTestCase::testSystemIntegration

Same name in other branches
  1. 8.x-3.x d7-tests/rules_integration_test_case.test \RulesIntegrationTestCase::testSystemIntegration()

Tests site/system integration.

File

tests/rules.test, line 2013

Class

RulesIntegrationTestCase
Tests provided module integration.

Code

public function testSystemIntegration() {
    // Test using the 'site' variable.
    $condition = rules_condition('data_is', array(
        'data:select' => 'site:current-user:name',
        'value' => $GLOBALS['user']->name,
    ));
    $this->assertTrue($condition->execute(), 'Retrieved the current user\'s name.');
    // Another test using a token replacement.
    $condition = rules_condition('data_is', array(
        'data:select' => 'site:current-user:name',
        'value' => '[site:current-user:name]',
    ));
    $this->assertTrue($condition->execute(), 'Replaced the token for the current user\'s name.');
    // Test breadcrumbs and drupal set message.
    $rule = rules_reaction_rule();
    $rule->event('init')
        ->action('breadcrumb_set', array(
        'titles' => array(
            'foo',
        ),
        'paths' => array(
            'bar',
        ),
    ))
        ->action('drupal_message', array(
        'message' => 'A message.',
    ));
    $rule->save('test');
    $this->drupalGet('node');
    $this->assertLink('foo', 0, 'Breadcrumb has been set.');
    $this->assertText('A message.', 'Drupal message has been shown.');
    // Test the page redirect.
    $node = $this->drupalCreateNode();
    $rule = rules_reaction_rule();
    $rule->event('node_view')
        ->action('redirect', array(
        'url' => 'user',
    ));
    $rule->save('test2');
    $this->drupalGet('node/' . $node->nid);
    $this->assertEqual($this->getUrl(), url('user', array(
        'absolute' => TRUE,
    )), 'Redirect has been issued.');
    // Also test using a URL that includes a fragment.
    $actions = $rule->actions();
    $actions[0]->settings['url'] = 'user#fragment';
    $rule->save();
    $this->drupalGet('node/' . $node->nid);
    $this->assertEqual($this->getUrl(), url('user', array(
        'absolute' => TRUE,
        'fragment' => 'fragment',
    )), 'Redirect has been issued.');
    // Test sending mail.
    $settings = array(
        'to' => 'mail@example.com',
        'subject' => 'subject',
        'message' => 'hello.',
    );
    rules_action('mail', $settings)->execute();
    $this->assertMail('to', 'mail@example.com', 'Mail has been sent.');
    $this->assertMail('from', variable_get('site_mail', ini_get('sendmail_from')), 'Default from address has been used');
    rules_action('mail', $settings + array(
        'from' => 'sender@example.com',
    ))->execute();
    $this->assertMail('from', 'sender@example.com', 'Specified from address has been used');
    // Test sending mail to all users of a role. First clear the mail
    // collector to remove the mail sent in the previous line of code.
    variable_set('drupal_test_email_collector', array());
    // Now make sure there is a custom role and two users with that role.
    $user1 = $this->drupalCreateUser(array(
        'administer nodes',
    ));
    $roles = $user1->roles;
    // Remove the authenticated role so we only use the new role created by
    // drupalCreateUser().
    unset($roles[DRUPAL_AUTHENTICATED_RID]);
    // Now create a second user with the same role.
    $user2 = $this->drupalCreateUser();
    user_save($user2, array(
        'roles' => $roles,
    ));
    // Now create a third user without the same role - this user should NOT
    // receive the role email.
    $user3 = $this->drupalCreateUser(array(
        'administer blocks',
    ));
    $additional_roles = $user3->roles;
    unset($additional_roles[DRUPAL_AUTHENTICATED_RID]);
    // Execute action and check that only two mails were sent.
    rules_action('mail_to_users_of_role', $settings + array(
        'roles' => array_keys($roles),
    ))->execute();
    $mails = $this->drupalGetMails();
    $this->assertEqual(count($mails), 2, '2 e-mails were sent to users of a role.');
    // Check each mail to ensure that only $user1 and $user2 got the mail.
    $mail = array_pop($mails);
    $this->assertTrue($mail['to'] == $user2->mail, 'Mail to user of a role has been sent.');
    $mail = array_pop($mails);
    $this->assertTrue($mail['to'] == $user1->mail, 'Mail to user of a role has been sent.');
    // Execute action again, this time to send mail to both roles.
    // This time check that three mails were sent - one for each user..
    variable_set('drupal_test_email_collector', array());
    rules_action('mail_to_users_of_role', $settings + array(
        'roles' => array_keys($roles + $additional_roles),
    ))->execute();
    $mails = $this->drupalGetMails();
    $this->assertEqual(count($mails), 3, '3 e-mails were sent to users of multiple roles.');
    // Test reacting on new log entries and make sure the log entry is usable.
    $rule = rules_reaction_rule();
    $rule->event('watchdog');
    $rule->action('drupal_message', array(
        'message:select' => 'log_entry:message',
    ));
    $rule->integrityCheck()
        ->save('test_watchdog');
    watchdog('php', 'test %message', array(
        '%message' => 'message',
    ));
    $msg = drupal_get_messages();
    $this->assertEqual(array_pop($msg['status']), t('test %message', array(
        '%message' => 'message',
    )), 'Watchdog event occurred and log entry properties can be used.');
}