1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Test case for multilingual taxonomy
|
5
|
*/
|
6
|
|
7
|
|
8
|
class i18nTaxonomyTestCase extends Drupali18nTestCase {
|
9
|
|
10
|
public static function getInfo() {
|
11
|
return array(
|
12
|
'name' => 'Taxonomy translation',
|
13
|
'group' => 'Internationalization',
|
14
|
'description' => 'Taxonomy translation functions'
|
15
|
);
|
16
|
}
|
17
|
|
18
|
function setUp() {
|
19
|
parent::setUp(array('i18n_taxonomy', 'field_test'));
|
20
|
parent::setUpLanguages();
|
21
|
|
22
|
// Create users.
|
23
|
$filtered_html_format = filter_format_load('filtered_html');
|
24
|
$full_html_format = filter_format_load('full_html');
|
25
|
$this->admin_user = $this->drupalCreateUser(array(
|
26
|
'access field_test content',
|
27
|
'administer field_test content',
|
28
|
'administer taxonomy',
|
29
|
'administer languages',
|
30
|
'administer site configuration',
|
31
|
filter_permission_name($filtered_html_format),
|
32
|
filter_permission_name($full_html_format),
|
33
|
));
|
34
|
$this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
|
35
|
}
|
36
|
|
37
|
function testTaxonomyTermLocalize() {
|
38
|
$this->drupalLogin($this->admin_user);
|
39
|
// Make Input Format "Filter Text" translatable
|
40
|
$edit = array(
|
41
|
'i18n_string_allowed_formats[filtered_html]' => 'filtered_html',
|
42
|
'i18n_string_allowed_formats[plain_text]' => 'plain_text',
|
43
|
);
|
44
|
$this->drupalPost('admin/config/regional/i18n/strings', $edit, t('Save configuration'));
|
45
|
|
46
|
// Create a localizable vocabulary.
|
47
|
$vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_LOCALIZE));
|
48
|
$this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_LOCALIZE, 'A vocabulary has been created and it is localizable.');
|
49
|
|
50
|
$this->field_name = $this->createTermField($vocab->machine_name);
|
51
|
|
52
|
// Create a term to be localized. We use a common prefix to facilitate the testing of autocomplete suggestions.
|
53
|
$prefix = $this->randomName() . '_';
|
54
|
$term = $this->createTerm(array('vid' => $vocab->vid, 'name' => $prefix . $this->randomName()));
|
55
|
|
56
|
$this->drupalLogin($this->translator);
|
57
|
|
58
|
// Create and Save Spanish translation, again using the same prefix.
|
59
|
$term_translation = array(
|
60
|
'name' => $this->createStringTranslation('taxonomy', $term->name, array($this->secondary_language => $prefix . $this->randomName())),
|
61
|
'description' => $this->createStringTranslation('taxonomy', $term->description, array($this->secondary_language => $prefix . $this->randomName())),
|
62
|
);
|
63
|
|
64
|
$this->drupalLogin($this->admin_user);
|
65
|
|
66
|
$langcode = LANGUAGE_NONE;
|
67
|
$edit = array(
|
68
|
"{$this->field_name}[$langcode]" => array($term->tid),
|
69
|
);
|
70
|
|
71
|
// Test the widgets in the original language.
|
72
|
$this->drupalGet('test-entity/add/test-bundle');
|
73
|
$this->assertText($term->name, 'Widget values are displayed correctly in default language.');
|
74
|
|
75
|
$this->drupalPost(NULL, $edit, t('Save'));
|
76
|
$this->assertText($term->name, 'Field values are displayed correctly in default language.');
|
77
|
|
78
|
// Terms should be localized in the field widget.
|
79
|
$this->drupalGet($this->secondary_language . '/test-entity/add/test-bundle');
|
80
|
$this->assertText($term_translation['name'][$this->secondary_language], 'Widget values are displayed correctly in non-default languages.');
|
81
|
|
82
|
$this->drupalPost(NULL, $edit, t('Save'));
|
83
|
$this->assertText($term_translation['name'][$this->secondary_language], 'Field values are displayed correctly in non-default languages.');
|
84
|
|
85
|
// Term name and term description should be localized
|
86
|
$this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->default_language)));
|
87
|
$this->assertText($term->name, 'Term title is displayed correctly in default language.');
|
88
|
$this->assertText($term->description, 'Term description is displayed correctly in default language.');
|
89
|
|
90
|
// Term name and term description should be localized
|
91
|
$this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->secondary_language)));
|
92
|
$this->assertText($term_translation['name'][$this->secondary_language], 'Term title is displayed correctly in non-default language.');
|
93
|
$this->assertText($term_translation['description'][$this->secondary_language], 'Term description is displayed correctly in non-default language.');
|
94
|
|
95
|
// Autocomplete should respect localization.
|
96
|
$autocomplete_path = 'taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
|
97
|
$autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
|
98
|
$this->assertTrue(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in default language.');
|
99
|
$this->assertFalse(isset($autocomplete_values[$term_translation['name'][$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages');
|
100
|
|
101
|
// Autocomplete should respect localization, but doesn't.
|
102
|
// $autocomplete_path = $this->secondary_language . '/taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
|
103
|
// $autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
|
104
|
// $this->assertFalse(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in non-default languages.');
|
105
|
// $this->assertTrue(isset($autocomplete_values[$term_translation[$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages.');
|
106
|
}
|
107
|
|
108
|
function testTaxonomyTermTranslate() {
|
109
|
// Create a translateable vocabulary.
|
110
|
$vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_TRANSLATE));
|
111
|
$this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_TRANSLATE, 'A vocabulary has been created and it is translateable.');
|
112
|
|
113
|
$this->field_select = $this->createTermField($vocab->machine_name);
|
114
|
$this->field_autocomplete = $this->createTermField($vocab->machine_name, 'taxonomy_autocomplete');
|
115
|
|
116
|
// Create a term to be translated.
|
117
|
$en_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->default_language));
|
118
|
$es_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->secondary_language));
|
119
|
|
120
|
$this->drupalLogin($this->admin_user);
|
121
|
|
122
|
// Set terms as translations of each other.
|
123
|
$edit = array(
|
124
|
'translations[' . $this->default_language . ']' => $en_term->name,
|
125
|
'translations[' . $this->secondary_language . ']' => $es_term->name,
|
126
|
);
|
127
|
$this->drupalPost('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets/add', $edit, t('Save'));
|
128
|
$this->drupalGet('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets');
|
129
|
|
130
|
// Freetagging creates terms with the correct language.
|
131
|
$new_term_name = $this->randomName();
|
132
|
$langcode = LANGUAGE_NONE;
|
133
|
$edit = array(
|
134
|
"{$this->field_autocomplete}[$langcode]" => $new_term_name,
|
135
|
);
|
136
|
$this->drupalPost($this->secondary_language . '/test-entity/add/test-bundle', $edit, t('Save'));
|
137
|
$new_term = current(taxonomy_get_term_by_name($new_term_name));
|
138
|
$this->assertEqual($new_term->language, $this->secondary_language, 'Freetagging creates terms with the correct language.');
|
139
|
|
140
|
// Term translations are used for language switching.
|
141
|
$language_switcher = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, 'taxonomy/term/' . $en_term->tid);
|
142
|
$this->assertEqual($language_switcher->links[$this->secondary_language]['href'], 'taxonomy/term/' . $es_term->tid, 'Term translations are used for language switching.');
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Tests the implementation of 'options_list_callback' for term reference fields.
|
147
|
* Enable and disable the callback properly. Avoid WSOD!
|
148
|
*/
|
149
|
function testTaxonomyFieldCallback() {
|
150
|
$field_name = 'taxonomy_term_test_field';
|
151
|
$field = field_create_field(array(
|
152
|
'field_name' => $field_name,
|
153
|
'type' => 'taxonomy_term_reference',
|
154
|
));
|
155
|
$field = field_info_field($field_name);
|
156
|
$callback = 'i18n_taxonomy_allowed_values';
|
157
|
$this->assertTrue(function_exists($callback), "Function $callback exists.");
|
158
|
$this->assertEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
|
159
|
module_disable(array('i18n_taxonomy'));
|
160
|
$field = field_info_field($field_name);
|
161
|
$this->assertNotEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
|
162
|
}
|
163
|
|
164
|
// Create vocabulary with given fields
|
165
|
function drupalCreateVocabulary($vocab = array()) {
|
166
|
$vocab += array('name' => $this->randomName(10), 'description' => $this->randomName(20));
|
167
|
taxonomy_vocabulary_save($vocab);
|
168
|
return (object)$vocab;
|
169
|
}
|
170
|
// Create term with given fields
|
171
|
function drupalCreateTerms($number = 1, $data = array()) {
|
172
|
$list = array();
|
173
|
for ($i = 1; $i <= $number ; $i++ ) {
|
174
|
$term = $this->createTerm($data);
|
175
|
$list[$term->tid] = $term;
|
176
|
}
|
177
|
return $list;
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Returns a new vocabulary with random properties.
|
182
|
*/
|
183
|
function createVocabulary($data = array()) {
|
184
|
// Create a vocabulary.
|
185
|
$data += array(
|
186
|
'i18n_mode' => I18N_MODE_LOCALIZE,
|
187
|
'name' => $this->randomName(),
|
188
|
'description' => $this->randomName(),
|
189
|
'machine_name' => drupal_strtolower($this->randomName()),
|
190
|
'help' => '',
|
191
|
'nodes' => array('article' => 'article'),
|
192
|
'weight' => mt_rand(0, 10),
|
193
|
);
|
194
|
$vocabulary = (object)$data;
|
195
|
taxonomy_vocabulary_save($vocabulary);
|
196
|
return $vocabulary;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Returns a new term with random properties in vocabulary $vid.
|
201
|
*/
|
202
|
function createTerm($data = array()) {
|
203
|
$data += array(
|
204
|
'name' => $this->randomName(),
|
205
|
'description' => $this->randomName(),
|
206
|
// Use the first available text format and vocabulary.
|
207
|
'format' => filter_default_format(),
|
208
|
'vid' => 1,
|
209
|
);
|
210
|
$term = (object)$data;
|
211
|
taxonomy_term_save($term);
|
212
|
return $term;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Setup a field and instance.
|
217
|
*/
|
218
|
function createTermField($machine_name, $widget = 'options_select') {
|
219
|
$field_name = drupal_strtolower($this->randomName());
|
220
|
|
221
|
$this->field = array(
|
222
|
'field_name' => $field_name,
|
223
|
'type' => 'taxonomy_term_reference',
|
224
|
'settings' => array(
|
225
|
'allowed_values' => array(
|
226
|
array(
|
227
|
'vocabulary' => $machine_name,
|
228
|
'parent' => '0',
|
229
|
),
|
230
|
),
|
231
|
)
|
232
|
);
|
233
|
field_create_field($this->field);
|
234
|
$this->instance = array(
|
235
|
'field_name' => $field_name,
|
236
|
'entity_type' => 'test_entity',
|
237
|
'bundle' => 'test_bundle',
|
238
|
'widget' => array(
|
239
|
'type' => $widget,
|
240
|
),
|
241
|
'display' => array(
|
242
|
'full' => array(
|
243
|
'type' => 'taxonomy_term_reference_link',
|
244
|
),
|
245
|
),
|
246
|
);
|
247
|
field_create_instance($this->instance);
|
248
|
|
249
|
return $field_name;
|
250
|
}
|
251
|
}
|