i18n_block.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // Title is a textarea, body is a text_format.
  63. $this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':title]', $translations['title']['es']);
  64. $this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':body][value]', $translations['body']['es']);
  65. // Update the translation.
  66. $translations['title']['es'] = $this->randomName(10);
  67. $translations['body']['es'] = $this->randomName(20);
  68. $edit = array(
  69. 'strings[blocks:block:' . $box2['delta'] . ':title]' => $translations['title']['es'],
  70. 'strings[blocks:block:' . $box2['delta'] . ':body][value]' => $translations['body']['es'],
  71. );
  72. $this->drupalPost(NULL, $edit, t('Save translation'));
  73. $this->i18nAssertTranslations($translations['title'], '', 'Updated block title translation displayed.');
  74. $this->i18nAssertTranslations($translations['body'], '', 'Updated block body translation displayed.');
  75. // Test a block translation with filtering and text formats
  76. $box3 = $this->i18nCreateBlock(array(
  77. 'title' => '<div><script>alert(0)</script>Title</script>',
  78. 'body' => "Dangerous text\nOne line\nTwo lines<script>alert(1)</script>",
  79. ));
  80. // This should be the actual HTML displayed
  81. $title = check_plain($box3['title']);
  82. $body = check_markup($box3['body'], $box3['format']);
  83. $this->drupalGet('');
  84. $this->assertRaw($title, "Title being displayed for default language: " . $title);
  85. $this->assertRaw($body, "Body being displayed for default language: " . $body);
  86. // We add language name to the body just to make sure we get the right translation later
  87. // This won't work for block titles as they don't have input format thus scripts will be blocked by locale
  88. $translations = array();
  89. foreach ($this->getOtherLanguages() as $langcode => $language) {
  90. $translations[$langcode] = $box3['body'] . "\n" . $language->name;
  91. $filtered[$langcode] = check_markup($translations[$langcode], $box3['format']);
  92. }
  93. // We need to find the string by this part alone, the rest will be filtered
  94. $this->createStringTranslation('blocks', 'Dangerous text', $translations);
  95. // Check the right filtered strings are displayed
  96. $this->i18nAssertTranslations($filtered);
  97. // Assert translatable descriptions.
  98. $this->drupalLogin($this->admin_user);
  99. $this->drupalGet('admin/structure/block/manage/system/powered-by/configure');
  100. $this->assertText(t('This block has generated content, only the title can be translated here.'));
  101. $this->drupalGet('admin/structure/block/manage/system/navigation/configure');
  102. $this->assertText(t('To translate the block content itself, translate the menu that is being shown.'));
  103. }
  104. /**
  105. * Translate block fields to all languages
  106. */
  107. function i18nTranslateBlock($block) {
  108. $translations['title'] = $this->createStringTranslation('blocks', $block['title']);
  109. $translations['body'] = $this->createStringTranslation('blocks', $block['body']);
  110. return $translations;
  111. }
  112. /**
  113. * Test creating custom block (i.e. box), moving it to a specific region and then deleting it.
  114. */
  115. function i18nCreateBlock($block = array(), $region = 'sidebar_first', $check_display = TRUE) {
  116. $this->drupalLogin($this->admin_user);
  117. // Add a new custom block by filling out the input form on the admin/structure/block/add page.
  118. $block += array(
  119. 'info' => $this->randomName(8),
  120. 'title' => $this->randomName(8),
  121. 'i18n_mode' => I18N_MODE_LOCALIZE,
  122. 'body' => $this->randomName(16),
  123. );
  124. $custom_block = array(
  125. 'info' => $block['info'],
  126. 'title' => $block['title'],
  127. 'i18n_mode' => $block['i18n_mode'],
  128. 'body[value]' => $block['body'],
  129. );
  130. $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
  131. // Confirm that the custom block has been created, and then query the created bid.
  132. $this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
  133. $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $block['info']))->fetchField();
  134. // Check to see if the custom block was created by checking that it's in the database.
  135. $this->assertNotNull($bid, t('Custom block found in database'));
  136. // Check that block_block_view() returns the correct title and content.
  137. $data = block_block_view($bid);
  138. $format = db_query("SELECT format FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
  139. $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.'));
  140. $this->assertEqual(check_markup($block['body'], $format), $data['content'], t('block_block_view() provides correct block content.'));
  141. // Check if the block can be moved to all available regions.
  142. $block['module'] = 'block';
  143. $block['delta'] = $bid;
  144. $block['format'] = $format;
  145. $this->moveBlockToRegion($block, $region);
  146. return $block;
  147. }
  148. /**
  149. * Update block i18n mode
  150. */
  151. function setBlockMode($block, $mode = I18N_MODE_LOCALIZE) {
  152. $edit['i18n_mode'] = $mode;
  153. $this->updateBlock($block, $edit);
  154. }
  155. /**
  156. * Update block visibility for languages
  157. */
  158. function setBlockLanguages($block, $languages = array()) {
  159. $edit = array();
  160. foreach ($this->getEnabledLanguages() as $langcode => $language) {
  161. $edit["languages[$langcode]"] = in_array($langcode, $languages) ? TRUE : FALSE;
  162. }
  163. $this->updateBlock($block, $edit);
  164. }
  165. /**
  166. * Update block
  167. */
  168. function updateBlock($block, $edit) {
  169. $this->drupalLogin($this->admin_user);
  170. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
  171. }
  172. }