Projet

Général

Profil

Paste
Télécharger (7,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_i18n / rules_i18n.test @ 99781f3b

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules i18n tests.
6
 */
7

    
8
/**
9
 * Test the i18n integration.
10
 */
11
class RulesI18nTestCase extends DrupalWebTestCase {
12

    
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Rules I18n',
16
      'description' => 'Tests translating Rules configs.',
17
      'group' => 'Rules',
18
      'dependencies' => array('i18n_string'),
19
    );
20
  }
21

    
22
  public function setUp() {
23
    parent::setUp('rules_i18n');
24
    $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages'));
25
    $this->drupalLogin($this->admin_user);
26
    $this->addLanguage('de');
27
  }
28

    
29
  /**
30
   * Copied from i18n module (class Drupali18nTestCase).
31
   *
32
   * We cannot extend from Drupali18nTestCase as else the test-bot would die.
33
   */
34
  public function addLanguage($language_code) {
35
    // Check to make sure that language has not already been installed.
36
    $this->drupalGet('admin/config/regional/language');
37

    
38
    if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
39
      // Doesn't have language installed so add it.
40
      $edit = array();
41
      $edit['langcode'] = $language_code;
42
      $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
43

    
44
      // Make sure we are not using a stale list.
45
      drupal_static_reset('language_list');
46
      $languages = language_list('language');
47
      $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
48

    
49
      if (array_key_exists($language_code, $languages)) {
50
        $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.'));
51
      }
52
    }
53
    elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
54
      // It's installed and enabled. No need to do anything.
55
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
56
    }
57
    else {
58
      // It's installed but not enabled. Enable it.
59
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
60
      $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
61
      $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
62
    }
63
  }
64

    
65
  /**
66
   * Tests translating rules configs.
67
   */
68
  public function testRulesConfigTranslation() {
69
    // Create a rule and translate it.
70
    $rule = rule();
71
    $rule->label = 'label-en';
72
    $rule->action('drupal_message', array('message' => 'English message for [site:current-user].'));
73
    $rule->save();
74

    
75
    $actions = $rule->actions();
76
    $id = $actions[0]->elementId();
77

    
78
    // Add a translation.
79
    i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:label", 'de', 'label-de');
80
    i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:$id:message", 'de', 'German message für [site:current-user].');
81

    
82
    // Execute the Rule and make sure the translated message has been output.
83
    // To do so, set the global language to German.
84
    $languages = language_list();
85
    $GLOBALS['language'] = $languages['de'];
86

    
87
    // Clear messages and execute the rule.
88
    i18n_string_textgroup('rules')->cache_reset();
89
    drupal_get_messages();
90
    $rule->execute();
91

    
92
    $messages = drupal_get_messages();
93
    $this->assertEqual($messages['status'][0], 'German message für ' . $GLOBALS['user']->name . '.', 'Translated message has been output.');
94

    
95
    // Test re-naming the rule.
96
    $rule->name = 'rules_i18n_name_2';
97
    $rule->save();
98
    $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
99
    $this->assertEqual($translation, 'label-de', 'Translation survives a name change.');
100

    
101
    // Test updating and make sure the translation stays.
102
    $rule->label = 'Label new';
103
    $rule->save();
104
    $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
105
    $this->assertEqual($translation, 'label-de', 'Translation survives an update.');
106

    
107
    // Test deleting the action and make sure the string is deleted too.
108
    $actions[0]->delete();
109
    $rule->save();
110
    $translation = entity_i18n_string("rules_config:{$rule->name}:$id:message", 'English message for [site:current-user].', 'de');
111
    $this->assertEqual($translation, 'English message for [site:current-user].', 'Translation of deleted action has been deleted.');
112

    
113
    // Now delete the whole config and make sure all translations are deleted.
114
    $rule->delete();
115
    $translation = entity_i18n_string("rules_config:{$rule->name}:label", 'label-en', 'de');
116
    $this->assertEqual($translation, 'label-en', 'Translation of deleted config has been deleted.');
117
  }
118

    
119
  /**
120
   * Tests the "Translate a text" action.
121
   */
122
  public function testI18nActionT() {
123
    $set = rules_action_set(array());
124
    $set->action('rules_i18n_t', array(
125
      'text' => 'untranslated',
126
      'language' => 'de',
127
    ));
128
    $set->action('drupal_message', array('message:select' => 'text'));
129
    $set->save('rules_i18n_test');
130

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

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

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

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

    
153
  /**
154
   * Tests the "Select a translated value" action.
155
   */
156
  public function testI18nActionSelect() {
157
    // Make the body field and the node type 'page' translatable.
158
    $field = field_info_field('body');
159
    $field['translatable'] = TRUE;
160
    field_update_field($field);
161
    variable_set('language_content_type_page', 1);
162

    
163
    $set = rules_action_set(array('node' => array('type' => 'node')));
164
    $set->action('rules_i18n_select', array(
165
        'data:select' => 'node:body:value',
166
        'language' => 'de',
167
        'data_translated:var' => 'body',
168
    ));
169
    $set->action('drupal_message', array('message:select' => 'body'));
170
    $set->save();
171

    
172
    $body['en'][0] = array('value' => 'English body.');
173
    $body['de'][0] = array('value' => 'German body.');
174
    $node = $this->drupalCreateNode(array('language' => 'en', 'body' => $body));
175

    
176
    // Clear messages and execute it.
177
    drupal_get_messages();
178
    $set->execute($node);
179

    
180
    $messages = drupal_get_messages();
181
    $this->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
182
  }
183
}