You are here

rules_i18n.test in Rules 7.2

Rules i18n tests.

File

rules_i18n/rules_i18n.test
View source
<?php

/**
 * @file
 * Rules i18n tests.
 */

/**
 * Test the i18n integration.
 */
class RulesI18nTestCase extends DrupalWebTestCase {

  /**
   * Declares test metadata.
   */
  public static function getInfo() {
    return array(
      'name' => 'Rules I18n',
      'description' => 'Tests translating Rules configs.',
      'group' => 'Rules',
      'dependencies' => array(
        'i18n_string',
      ),
    );
  }

  /**
   * Overrides DrupalWebTestCase::setUp().
   */
  protected function setUp() {
    parent::setUp('rules_i18n');
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'bypass node access',
      'administer nodes',
      'administer languages',
      'administer content types',
      'administer blocks',
      'access administration pages',
    ));
    $this
      ->drupalLogin($this->admin_user);
    $this
      ->addLanguage('de');
  }

  /**
   * Copied from i18n module (class Drupali18nTestCase).
   *
   * We cannot extend from Drupali18nTestCase as else the test-bot would die.
   */
  public function addLanguage($language_code) {

    // Check to make sure that language has not already been installed.
    $this
      ->drupalGet('admin/config/regional/language');
    if (strpos($this
      ->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {

      // Doesn't have language installed so add it.
      $edit = array();
      $edit['langcode'] = $language_code;
      $this
        ->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

      // Make sure we are not using a stale list.
      drupal_static_reset('language_list');
      $languages = language_list('language');
      $this
        ->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
      if (array_key_exists($language_code, $languages)) {
        $this
          ->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array(
          '%language' => $languages[$language_code]->name,
          '@locale-help' => url('admin/help/locale'),
        )), t('Language has been created.'));
      }
    }
    elseif ($this
      ->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(
      ':name' => 'enabled[' . $language_code . ']',
    ))) {

      // It's installed and enabled. No need to do anything.
      $this
        ->assertTrue(TRUE, 'Language [' . $language_code . '] already installed and enabled.');
    }
    else {

      // It's installed but not enabled. Enable it.
      $this
        ->assertTrue(TRUE, 'Language [' . $language_code . '] already installed.');
      $this
        ->drupalPost(NULL, array(
        'enabled[' . $language_code . ']' => TRUE,
      ), t('Save configuration'));
      $this
        ->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
    }
  }

  /**
   * Tests translating rules configs.
   */
  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.');
  }

  /**
   * Tests the "Translate a text" action.
   */
  public function testI18nActionT() {
    $set = rules_action_set(array());
    $set
      ->action('rules_i18n_t', array(
      'text' => 'untranslated',
      'language' => 'de',
    ));
    $set
      ->action('drupal_message', array(
      'message:select' => 'text',
    ));
    $set
      ->save('rules_i18n_test');

    // Add a translation.
    $actions = $set
      ->getIterator();
    $id = $actions[0]
      ->elementId();
    i18n_string_textgroup('rules')
      ->update_translation("rules_config:{$set->name}:{$id}:text", 'de', 'text-de');

    // Clear messages and execute it.
    drupal_get_messages();
    $set
      ->execute();
    $messages = drupal_get_messages();
    $this
      ->assertEqual($messages['status'][0], 'text-de', 'Text has been successfully translated.');

    // Enable the PHP module and make sure PHP in translations is not evaluated.
    module_enable(array(
      'php',
    ));
    i18n_string_textgroup('rules')
      ->update_translation("rules_config:{$set->name}:{$id}:text", 'de', 'text <?php echo "eval";?>');

    // Clear messages and execute it.
    drupal_get_messages();
    $set
      ->execute();
    $messages = drupal_get_messages();
    $this
      ->assertEqual($messages['status'][0], check_plain('text <?php echo "eval";?>'), 'PHP in translated text is not executed.');
  }

  /**
   * Tests the "Select a translated value" action.
   */
  public function testI18nActionSelect() {

    // Make the body field and the node type 'page' translatable.
    $field = field_info_field('body');
    $field['translatable'] = TRUE;
    field_update_field($field);
    variable_set('language_content_type_page', 1);
    $set = rules_action_set(array(
      'node' => array(
        'type' => 'node',
      ),
    ));
    $set
      ->action('rules_i18n_select', array(
      'data:select' => 'node:body:value',
      'language' => 'de',
      'data_translated:var' => 'body',
    ));
    $set
      ->action('drupal_message', array(
      'message:select' => 'body',
    ));
    $set
      ->save();
    $body['en'][0] = array(
      'value' => 'English body.',
    );
    $body['de'][0] = array(
      'value' => 'German body.',
    );
    $node = $this
      ->drupalCreateNode(array(
      'language' => 'en',
      'body' => $body,
    ));

    // Clear messages and execute it.
    drupal_get_messages();
    $set
      ->execute($node);
    $messages = drupal_get_messages();
    $this
      ->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
  }

}

Classes

Namesort descending Description
RulesI18nTestCase Test the i18n integration.