rules_i18n.test
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @file
* Rules i18n tests.
*/
/**
* Test the i18n integration.
*/
class RulesI18nTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Rules I18n',
'description' => 'Tests translating Rules configs.',
'group' => 'Rules',
'dependencies' => array('i18n_string'),
);
}
public 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 evaluted.
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.');
}
}