function RulesI18nTestCase::testRulesConfigTranslation

Tests translating rules configs.

File

rules_i18n/rules_i18n.test, line 82

Class

RulesI18nTestCase
Test the i18n integration.

Code

public function testRulesConfigTranslation() {
    // Create a rule and translate it.
    $rule = rule();
    $rule->label = 'label-en';
    $rule->action('drupal_message', array(
        'message' => 'English message for [site:current-user].',
    ));
    $rule->save();
    $actions = $rule->actions();
    $id = $actions[0]->elementId();
    // Add a translation.
    i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:label", 'de', 'label-de');
    i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:{$id}:message", 'de', 'German message für [site:current-user].');
    // Execute the Rule and make sure the translated message has been output.
    // To do so, set the global language to German.
    $languages = language_list();
    $GLOBALS['language'] = $languages['de'];
    // Clear messages and execute the rule.
    i18n_string_textgroup('rules')->cache_reset();
    drupal_get_messages();
    $rule->execute();
    $messages = drupal_get_messages();
    $this->assertEqual($messages['status'][0], 'German message für ' . $GLOBALS['user']->name . '.', 'Translated message has been output.');
    // Test re-naming the rule.
    $rule->name = 'rules_i18n_name_2';
    $rule->save();
    $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
    $this->assertEqual($translation, 'label-de', 'Translation survives a name change.');
    // Test updating and make sure the translation stays.
    $rule->label = 'Label new';
    $rule->save();
    $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
    $this->assertEqual($translation, 'label-de', 'Translation survives an update.');
    // Test deleting the action and make sure the string is deleted too.
    $actions[0]->delete();
    $rule->save();
    $translation = entity_i18n_string("rules_config:{$rule->name}:{$id}:message", 'English message for [site:current-user].', 'de');
    $this->assertEqual($translation, 'English message for [site:current-user].', 'Translation of deleted action has been deleted.');
    // Now delete the whole config and make sure all translations are deleted.
    $rule->delete();
    $translation = entity_i18n_string("rules_config:{$rule->name}:label", 'label-en', 'de');
    $this->assertEqual($translation, 'label-en', 'Translation of deleted config has been deleted.');
}