-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n_access.test
208 lines (180 loc) · 7.21 KB
/
i18n_access.test
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* @file
* Test suite for i18n_access.module
*/
class i18nAccessTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Translation Access'),
'description' => t('Test suite for the i18n_access module.'),
'group' => t('i18n'),
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp('locale', 'translation', 'i18n_access');
$this->admin_user = $this->drupalCreateUser(array('administer languages', 'administer site configuration', 'access administration pages', 'administer content types', 'administer nodes', 'administer users'));
$this->translator = $this->drupalCreateUser(array('create story content', 'edit own story content', 'translate content'));
$this->visitor = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($this->admin_user);
$this->addLanguage('fr');
$this->addLanguage('de');
$this->setLanguagePermissions($this->translator, array('en', 'fr'));
// Set Story content type to use multilingual support with translation.
$edit = array();
$edit['language_content_type'] = 2;
$this->drupalPost('admin/content/node-type/story', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Story')), t('Story content type has been updated.'));
}
/**
* Enable the specified language if it has not been already.
*
* @param string $language_code
* The language code to enable.
*/
function addLanguage($language_code) {
// Check to make sure that language has not already been installed.
$this->drupalGet('admin/settings/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/settings/language/add', $edit, t('Add language'));
$languages = language_list('language', TRUE); // Make sure not using cached version.
$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.', array('%language' => $languages[$language_code]->name)), t('Language has been created.'));
}
}
else {
// Ensure that it is enabled.
$this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
$this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
}
}
/**
* Sets the language permission for the specified user. Must be logged in as
* an 'administer users' privileged user before calling this.
*
* @param $account
* The user account to modify
* @param $languages
* An array of language codes to give permission for
*/
function setLanguagePermissions($account, $languages = array()) {
$this->assertTrue(user_access('administer users'), t('User has permission to administer users'));
foreach ($languages as $langcode) {
$key = 'i18n_access[' . $langcode . ']';
$edit[$key] = $langcode;
$expected[$langcode] = $langcode;
}
$this->drupalPost('user/' . $account->uid . '/edit', $edit, t('Save'));
$actual = i18n_access_load_permissions($account->uid);
$this->assertEqual($expected, $actual, t('Language permissions set correctly.'), 'i18n_access');
}
/**
* Assert that a language option exists in the language select field on the
* current page.
* @param $langcode
* Value of the language option to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
function assertLanguageOption($langcode, $message, $group = 'Other') {
$xpath = '//select[@name="language"]/option';
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if (isset($langcode)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $langcode) {
$found = TRUE;
}
}
}
}
return $this->assertTrue($fields && $found, $message, $group);
}
/**
* Assert that a language option does not exist in the language select field
* on the current page.
* @param $langcode
* Value of the language option to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
function assertNoLanguageOption($langcode, $message, $group = 'Other') {
$xpath = '//select[@name="language"]/option';
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if (isset($langcode)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $langcode) {
$found = TRUE;
}
}
}
}
return $this->assertFalse($fields && $found, $message, $group);
}
function dsm($object) {
$this->error('<pre>' . check_plain(print_r($object, 1)) . '</pre>');
}
/**
* Test translator user. User with 'create story content' and 'edit own story
* content' permissions should be able to create and edit story nodes only in
* the languages that they have permissions for.
*/
function testTranslatorUser() {
$this->drupalLogin($this->translator);
$this->drupalGet('node/add/story');
$this->assertField('language', t('Found language selector.'));
$perms = i18n_access_load_permissions($this->translator->uid);
$languages = language_list();
$languages[I18N_ACCESS_LANGUAGE_NEUTRAL] = (object)array('language' => '', 'name' => 'Language Neutral');
foreach ($languages as $key => $language) {
// TODO: Add in check for language neutral
if (isset($perms[$key]) && $perms[$key]) {
$this->assertLanguageOption($language->language, t('Option found for %language in language selector.', array('%language' => $language->name)));
}
else {
$this->assertNoLanguageOption($language->language, t('Option not found for %language in language selector.', array('%language' => $language->name)));
}
}
}
/**
* Test admin user. User with 'administer nodes' permission should be able to
* create and edit nodes regardless of the language
*/
function testAdminUser() {
$this->drupalLogin($this->admin_user);
$this->drupalGet('node/add/story');
$this->assertField('language', t('Found language selector.'));
$perms = i18n_access_load_permissions($this->admin_user->uid);
$languages = language_list();
$languages[I18N_ACCESS_LANGUAGE_NEUTRAL] = (object)array('language' => '', 'name' => 'Language Neutral');
foreach ($languages as $language) {
// TODO: Add in check for language neutral
$this->assertLanguageOption($language->language, t('Option found for %language, regardless of permission, for administrator.', array('%language' => $language->name)));
}
}
}