1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Test case for multilingual blocks
|
5
|
*/
|
6
|
|
7
|
class i18nBlocksTestCase extends Drupali18nTestCase {
|
8
|
|
9
|
public static function getInfo() {
|
10
|
return array(
|
11
|
'name' => 'Block translation',
|
12
|
'group' => 'Internationalization',
|
13
|
'description' => 'Block translation functions'
|
14
|
);
|
15
|
}
|
16
|
|
17
|
function setUp() {
|
18
|
parent::setUp('i18n_block');
|
19
|
parent::setUpLanguages();
|
20
|
$this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
|
21
|
|
22
|
$format = filter_default_format();
|
23
|
variable_set('i18n_string_allowed_formats', array($format => $format));
|
24
|
$this->drupalLogin($this->admin_user);
|
25
|
}
|
26
|
|
27
|
function testBlockTranslation() {
|
28
|
|
29
|
$block_translater = $this->drupalCreateUser(array('administer blocks', 'translate interface', 'translate user-defined strings'));
|
30
|
|
31
|
// Display Language switcher block
|
32
|
$switcher = array('module' => 'locale', 'delta' => 'language', 'title' => t('Languages'));
|
33
|
$this->moveBlockToRegion($switcher);
|
34
|
// Add a custom title to language switcher block and check it displays translated
|
35
|
$title = $this->randomName(10);
|
36
|
$this->updateBlock($switcher, array('title' => $title, 'i18n_mode' => I18N_MODE_LOCALIZE));
|
37
|
$this->assertText($title, "The new custom title is displayed on the home page.");
|
38
|
$translations = $this->createStringTranslation('blocks', $title);
|
39
|
$this->i18nAssertTranslations($translations);
|
40
|
|
41
|
// Create a translatable block and test block visibility per language.
|
42
|
$block = $this->i18nCreateBlock();
|
43
|
// Now set a language for the block and confirm it shows just for that one (without translation)
|
44
|
$languages = $this->getEnabledLanguages();
|
45
|
$setlanguage = array_shift($languages);
|
46
|
$otherlanguage = array_shift($languages);
|
47
|
$this->setBlockLanguages($block, array($setlanguage->language));
|
48
|
// Show in block's language but not translated
|
49
|
$this->i18nGet($setlanguage);
|
50
|
$this->assertText($block['title']);
|
51
|
// Do not show in the other language
|
52
|
$this->i18nGet($otherlanguage);
|
53
|
$this->assertNoText($block['title']);
|
54
|
|
55
|
// Create a new block, translate it and check the right translations are displayed for title and body
|
56
|
$box2 = $this->i18nCreateBlock();
|
57
|
// Create translations for title and body, source strings should be already there
|
58
|
$translations = $this->i18nTranslateBlock($box2);
|
59
|
$this->i18nAssertTranslations($translations['title'], '', 'Custom block title translation displayed.');
|
60
|
$this->i18nAssertTranslations($translations['body'], '', 'Custom block body translation displayed.');
|
61
|
|
62
|
// Test the translate tab.
|
63
|
$this->drupalLogin($this->admin_user);
|
64
|
$this->drupalGet('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure');
|
65
|
$this->assertNoFieldByName('save_and_translate');
|
66
|
|
67
|
$this->drupalLogin($block_translater);
|
68
|
$this->drupalPost('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure', array(), t('Save and translate'));
|
69
|
// @todo Improve these checks.
|
70
|
$this->assertText(t('Spanish'));
|
71
|
$this->assertText(t('translated'));
|
72
|
|
73
|
$this->clickLink(t('translate'));
|
74
|
|
75
|
// Title is a textarea, body is a text_format.
|
76
|
$this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':title]', $translations['title']['es']);
|
77
|
$this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':body][value]', $translations['body']['es']);
|
78
|
|
79
|
// Update the translation.
|
80
|
$translations['title']['es'] = $this->randomName(10);
|
81
|
$translations['body']['es'] = $this->randomName(20);
|
82
|
$edit = array(
|
83
|
'strings[blocks:block:' . $box2['delta'] . ':title]' => $translations['title']['es'],
|
84
|
'strings[blocks:block:' . $box2['delta'] . ':body][value]' => $translations['body']['es'],
|
85
|
);
|
86
|
$this->drupalPost(NULL, $edit, t('Save translation'));
|
87
|
$this->i18nAssertTranslations($translations['title'], '', 'Updated block title translation displayed.');
|
88
|
$this->i18nAssertTranslations($translations['body'], '', 'Updated block body translation displayed.');
|
89
|
|
90
|
// Test a block translation with filtering and text formats
|
91
|
$box3 = $this->i18nCreateBlock(array(
|
92
|
'title' => '<div><script>alert(0)</script>Title</script>',
|
93
|
'body' => "Dangerous text\nOne line\nTwo lines<script>alert(1)</script>",
|
94
|
));
|
95
|
// This should be the actual HTML displayed
|
96
|
$title = check_plain($box3['title']);
|
97
|
$body = check_markup($box3['body'], $box3['format']);
|
98
|
$this->drupalGet('');
|
99
|
$this->assertRaw($title, "Title being displayed for default language: " . $title);
|
100
|
$this->assertRaw($body, "Body being displayed for default language: " . $body);
|
101
|
|
102
|
// We add language name to the body just to make sure we get the right translation later
|
103
|
// This won't work for block titles as they don't have input format thus scripts will be blocked by locale
|
104
|
$translations = array();
|
105
|
foreach ($this->getOtherLanguages() as $langcode => $language) {
|
106
|
$translations[$langcode] = $box3['body'] . "\n" . $language->name;
|
107
|
$filtered[$langcode] = check_markup($translations[$langcode], $box3['format']);
|
108
|
}
|
109
|
// We need to find the string by this part alone, the rest will be filtered
|
110
|
$this->createStringTranslation('blocks', 'Dangerous text', $translations);
|
111
|
// Check the right filtered strings are displayed
|
112
|
$this->i18nAssertTranslations($filtered);
|
113
|
|
114
|
// Assert translatable descriptions.
|
115
|
$this->drupalLogin($this->admin_user);
|
116
|
$this->drupalGet('admin/structure/block/manage/system/powered-by/configure');
|
117
|
$this->assertText(t('This block has generated content, only the title can be translated here.'));
|
118
|
|
119
|
$this->drupalGet('admin/structure/block/manage/system/navigation/configure');
|
120
|
$this->assertText(t('To translate the block content itself, translate the menu that is being shown.'));
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Translate block fields to all languages
|
125
|
*/
|
126
|
function i18nTranslateBlock($block) {
|
127
|
$translations['title'] = $this->createStringTranslation('blocks', $block['title']);
|
128
|
$translations['body'] = $this->createStringTranslation('blocks', $block['body']);
|
129
|
return $translations;
|
130
|
}
|
131
|
/**
|
132
|
* Test creating custom block (i.e. box), moving it to a specific region and then deleting it.
|
133
|
*/
|
134
|
function i18nCreateBlock($block = array(), $region = 'sidebar_first', $check_display = TRUE) {
|
135
|
$this->drupalLogin($this->admin_user);
|
136
|
// Add a new custom block by filling out the input form on the admin/structure/block/add page.
|
137
|
$block += array(
|
138
|
'info' => $this->randomName(8),
|
139
|
'title' => $this->randomName(8),
|
140
|
'i18n_mode' => I18N_MODE_LOCALIZE,
|
141
|
'body' => $this->randomName(16),
|
142
|
);
|
143
|
$custom_block = array(
|
144
|
'info' => $block['info'],
|
145
|
'title' => $block['title'],
|
146
|
'i18n_mode' => $block['i18n_mode'],
|
147
|
'body[value]' => $block['body'],
|
148
|
);
|
149
|
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
150
|
|
151
|
// Confirm that the custom block has been created, and then query the created bid.
|
152
|
$this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
|
153
|
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $block['info']))->fetchField();
|
154
|
|
155
|
// Check to see if the custom block was created by checking that it's in the database.
|
156
|
$this->assertNotNull($bid, t('Custom block found in database'));
|
157
|
|
158
|
// Check that block_block_view() returns the correct title and content.
|
159
|
$data = block_block_view($bid);
|
160
|
$format = db_query("SELECT format FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
|
161
|
$this->assertTrue(array_key_exists('subject', $data) && empty($data['subject']), t('block_block_view() provides an empty block subject, since custom blocks do not have default titles.'));
|
162
|
$this->assertEqual(check_markup($block['body'], $format), $data['content'], t('block_block_view() provides correct block content.'));
|
163
|
|
164
|
// Check if the block can be moved to all available regions.
|
165
|
$block['module'] = 'block';
|
166
|
$block['delta'] = $bid;
|
167
|
$block['format'] = $format;
|
168
|
$this->moveBlockToRegion($block, $region);
|
169
|
|
170
|
return $block;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Update block i18n mode
|
175
|
*/
|
176
|
function setBlockMode($block, $mode = I18N_MODE_LOCALIZE) {
|
177
|
$edit['i18n_mode'] = $mode;
|
178
|
$this->updateBlock($block, $edit);
|
179
|
}
|
180
|
/**
|
181
|
* Update block visibility for languages
|
182
|
*/
|
183
|
function setBlockLanguages($block, $languages = array()) {
|
184
|
$edit = array();
|
185
|
foreach ($this->getEnabledLanguages() as $langcode => $language) {
|
186
|
$edit["languages[$langcode]"] = in_array($langcode, $languages) ? TRUE : FALSE;
|
187
|
}
|
188
|
$this->updateBlock($block, $edit);
|
189
|
}
|
190
|
/**
|
191
|
* Update block
|
192
|
*/
|
193
|
function updateBlock($block, $edit) {
|
194
|
$this->drupalLogin($this->admin_user);
|
195
|
$this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
|
196
|
}
|
197
|
|
198
|
}
|