Projet

Général

Profil

Paste
Télécharger (8,77 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_block / i18n_block.test @ 76df55b7

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
    $this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':title]', $translations['title']['es']);
76
    $this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':body]', $translations['body']['es']);
77

    
78
    // Update the translation.
79
    $translations['title']['es'] = $this->randomName(10);
80
    $translations['body']['es'] = $this->randomName(20);
81
    $edit = array(
82
      'strings[blocks:block:' . $box2['delta'] . ':title]' => $translations['title']['es'],
83
      'strings[blocks:block:' . $box2['delta'] . ':body]' => $translations['body']['es'],
84
    );
85
    $this->drupalPost(NULL, $edit, t('Save translation'));
86
    $this->i18nAssertTranslations($translations['title'], '', 'Updated block title translation displayed.');
87
    $this->i18nAssertTranslations($translations['body'], '', 'Updated block body translation displayed.');
88

    
89
    // Test a block translation with filtering and text formats
90
    $box3 = $this->i18nCreateBlock(array(
91
      'title' => '<div><script>alert(0)</script>Title</script>',
92
      'body' => "Dangerous text\nOne line\nTwo lines<script>alert(1)</script>",
93
    ));
94
    // This should be the actual HTML displayed
95
    $title = check_plain($box3['title']);
96
    $body = check_markup($box3['body'], $box3['format']);
97
    $this->drupalGet('');
98
    $this->assertRaw($title, "Title being displayed for default language: " . $title);
99
    $this->assertRaw($body, "Body being displayed for default language: " . $body);
100

    
101
    // We add language name to the body just to make sure we get the right translation later
102
    // This won't work for block titles as they don't have input format thus scripts will be blocked by locale
103
    $translations = array();
104
    foreach ($this->getOtherLanguages() as $langcode => $language) {
105
      $translations[$langcode] = $box3['body'] . "\n" . $language->name;
106
      $filtered[$langcode] = check_markup($translations[$langcode], $box3['format']);
107
    }
108
    // We need to find the string by this part alone, the rest will be filtered
109
    $this->createStringTranslation('blocks', 'Dangerous text', $translations);
110
    // Check the right filtered strings are displayed
111
    $this->i18nAssertTranslations($filtered);
112

    
113
    // Assert translatable descriptions.
114
    $this->drupalLogin($this->admin_user);
115
    $this->drupalGet('admin/structure/block/manage/system/powered-by/configure');
116
    $this->assertText(t('This block has generated content, only the title can be translated here.'));
117

    
118
    $this->drupalGet('admin/structure/block/manage/system/navigation/configure');
119
    $this->assertText(t('To translate the block content itself, translate the menu that is being shown.'));
120
  }
121

    
122
  /**
123
   * Translate block fields to all languages
124
   */
125
  function i18nTranslateBlock($block) {
126
    $translations['title'] = $this->createStringTranslation('blocks', $block['title']);
127
    $translations['body'] = $this->createStringTranslation('blocks', $block['body']);
128
    return $translations;
129
  }
130
  /**
131
   * Test creating custom block (i.e. box), moving it to a specific region and then deleting it.
132
   */
133
  function i18nCreateBlock($block = array(), $region = 'sidebar_first', $check_display = TRUE) {
134
    $this->drupalLogin($this->admin_user);
135
    // Add a new custom block by filling out the input form on the admin/structure/block/add page.
136
    $block += array(
137
      'info' => $this->randomName(8),
138
      'title' => $this->randomName(8),
139
      'i18n_mode' => I18N_MODE_LOCALIZE,
140
      'body' => $this->randomName(16),
141
    );
142
    $custom_block = array(
143
      'info' => $block['info'],
144
      'title' => $block['title'],
145
      'i18n_mode' => $block['i18n_mode'],
146
      'body[value]' => $block['body'],
147
    );
148
    $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
149

    
150
    // Confirm that the custom block has been created, and then query the created bid.
151
    $this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
152
    $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $block['info']))->fetchField();
153

    
154
    // Check to see if the custom block was created by checking that it's in the database.
155
    $this->assertNotNull($bid, t('Custom block found in database'));
156

    
157
    // Check that block_block_view() returns the correct title and content.
158
    $data = block_block_view($bid);
159
    $format = db_query("SELECT format FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
160
    $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.'));
161
    $this->assertEqual(check_markup($block['body'], $format), $data['content'], t('block_block_view() provides correct block content.'));
162

    
163
    // Check if the block can be moved to all available regions.
164
    $block['module'] = 'block';
165
    $block['delta'] = $bid;
166
    $block['format'] = $format;
167
    $this->moveBlockToRegion($block, $region);
168

    
169
    return $block;
170
  }
171

    
172
  /**
173
   * Update block i18n mode
174
   */
175
  function setBlockMode($block, $mode = I18N_MODE_LOCALIZE) {
176
    $edit['i18n_mode'] = $mode;
177
    $this->updateBlock($block, $edit);
178
  }
179
  /**
180
   * Update block visibility for languages
181
   */
182
  function setBlockLanguages($block, $languages = array()) {
183
    $edit = array();
184
    foreach ($this->getEnabledLanguages() as $langcode => $language) {
185
      $edit["languages[$langcode]"] = in_array($langcode, $languages) ? TRUE : FALSE;
186
    }
187
    $this->updateBlock($block, $edit);
188
  }
189
  /**
190
   * Update block
191
   */
192
  function updateBlock($block, $edit) {
193
    $this->drupalLogin($this->admin_user);
194
    $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
195
  }
196

    
197
}