Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / rules_i18n / rules_i18n.test @ 950416da

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
  /**
14
   * Declares test metadata.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Rules I18n',
19
      'description' => 'Tests translating Rules configs.',
20
      'group' => 'Rules',
21
      'dependencies' => array('i18n_string'),
22
    );
23
  }
24

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

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

    
44
    if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
45
      // Doesn't have language installed so add it.
46
      $edit = array();
47
      $edit['langcode'] = $language_code;
48
      $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
49

    
50
      // Make sure we are not using a stale list.
51
      drupal_static_reset('language_list');
52
      $languages = language_list('language');
53
      $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
54

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

    
71
  /**
72
   * Tests translating rules configs.
73
   */
74
  public function testRulesConfigTranslation() {
75
    // Create a rule and translate it.
76
    $rule = rule();
77
    $rule->label = 'label-en';
78
    $rule->action('drupal_message', array('message' => 'English message for [site:current-user].'));
79
    $rule->save();
80

    
81
    $actions = $rule->actions();
82
    $id = $actions[0]->elementId();
83

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

    
88
    // Execute the Rule and make sure the translated message has been output.
89
    // To do so, set the global language to German.
90
    $languages = language_list();
91
    $GLOBALS['language'] = $languages['de'];
92

    
93
    // Clear messages and execute the rule.
94
    i18n_string_textgroup('rules')->cache_reset();
95
    drupal_get_messages();
96
    $rule->execute();
97

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

    
101
    // Test re-naming the rule.
102
    $rule->name = 'rules_i18n_name_2';
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 a name change.');
106

    
107
    // Test updating and make sure the translation stays.
108
    $rule->label = 'Label new';
109
    $rule->save();
110
    $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
111
    $this->assertEqual($translation, 'label-de', 'Translation survives an update.');
112

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

    
119
    // Now delete the whole config and make sure all translations are deleted.
120
    $rule->delete();
121
    $translation = entity_i18n_string("rules_config:{$rule->name}:label", 'label-en', 'de');
122
    $this->assertEqual($translation, 'label-en', 'Translation of deleted config has been deleted.');
123
  }
124

    
125
  /**
126
   * Tests the "Translate a text" action.
127
   */
128
  public function testI18nActionT() {
129
    $set = rules_action_set(array());
130
    $set->action('rules_i18n_t', array(
131
      'text' => 'untranslated',
132
      'language' => 'de',
133
    ));
134
    $set->action('drupal_message', array('message:select' => 'text'));
135
    $set->save('rules_i18n_test');
136

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

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

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

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

    
159
  /**
160
   * Tests the "Select a translated value" action.
161
   */
162
  public function testI18nActionSelect() {
163
    // Make the body field and the node type 'page' translatable.
164
    $field = field_info_field('body');
165
    $field['translatable'] = TRUE;
166
    field_update_field($field);
167
    variable_set('language_content_type_page', 1);
168

    
169
    $set = rules_action_set(array('node' => array('type' => 'node')));
170
    $set->action('rules_i18n_select', array(
171
      'data:select' => 'node:body:value',
172
      'language' => 'de',
173
      'data_translated:var' => 'body',
174
    ));
175
    $set->action('drupal_message', array('message:select' => 'body'));
176
    $set->save();
177

    
178
    $body['en'][0] = array('value' => 'English body.');
179
    $body['de'][0] = array('value' => 'German body.');
180
    $node = $this->drupalCreateNode(array('language' => 'en', 'body' => $body));
181

    
182
    // Clear messages and execute it.
183
    drupal_get_messages();
184
    $set->execute($node);
185

    
186
    $messages = drupal_get_messages();
187
    $this->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
188
  }
189

    
190
}