i18n_block.test 8.8 KB

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