You are here

function i18nBlocksTestCase::testBlockTranslation in Internationalization 7

File

i18n_block/i18n_block.test, line 27
Test case for multilingual blocks

Class

i18nBlocksTestCase
@file Test case for multilingual blocks

Code

function testBlockTranslation() {
  $block_translater = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'translate interface',
    'translate user-defined strings',
  ));

  // Display Language switcher block
  $switcher = array(
    'module' => 'locale',
    'delta' => 'language',
    'title' => t('Languages'),
  );
  $this
    ->moveBlockToRegion($switcher);

  // Add a custom title to language switcher block and check it displays translated
  $title = $this
    ->randomName(10);
  $this
    ->updateBlock($switcher, array(
    'title' => $title,
    'i18n_mode' => I18N_MODE_LOCALIZE,
  ));
  $this
    ->assertText($title, "The new custom title is displayed on the home page.");
  $translations = $this
    ->createStringTranslation('blocks', $title);
  $this
    ->i18nAssertTranslations($translations);

  // Create a translatable block and test block visibility per language.
  $block = $this
    ->i18nCreateBlock();

  // Now set a language for the block and confirm it shows just for that one (without translation)
  $languages = $this
    ->getEnabledLanguages();
  $setlanguage = array_shift($languages);
  $otherlanguage = array_shift($languages);
  $this
    ->setBlockLanguages($block, array(
    $setlanguage->language,
  ));

  // Show in block's language but not translated
  $this
    ->i18nGet($setlanguage);
  $this
    ->assertText($block['title']);

  // Do not show in the other language
  $this
    ->i18nGet($otherlanguage);
  $this
    ->assertNoText($block['title']);

  // Create a new block, translate it and check the right translations are displayed for title and body
  $box2 = $this
    ->i18nCreateBlock();

  // Create translations for title and body, source strings should be already there
  $translations = $this
    ->i18nTranslateBlock($box2);
  $this
    ->i18nAssertTranslations($translations['title'], '', 'Custom block title translation displayed.');
  $this
    ->i18nAssertTranslations($translations['body'], '', 'Custom block body translation displayed.');

  // Test the translate tab.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure');
  $this
    ->assertNoFieldByName('save_and_translate');
  $this
    ->drupalLogin($block_translater);
  $this
    ->drupalPost('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure', array(), t('Save and translate'));

  // @todo Improve these checks.
  $this
    ->assertText(t('Spanish'));
  $this
    ->assertText(t('translated'));
  $this
    ->clickLink(t('translate'));

  // Title is a textarea, body is a text_format.
  $this
    ->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':title]', $translations['title']['es']);
  $this
    ->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':body][value]', $translations['body']['es']);

  // Update the translation.
  $translations['title']['es'] = $this
    ->randomName(10);
  $translations['body']['es'] = $this
    ->randomName(20);
  $edit = array(
    'strings[blocks:block:' . $box2['delta'] . ':title]' => $translations['title']['es'],
    'strings[blocks:block:' . $box2['delta'] . ':body][value]' => $translations['body']['es'],
  );
  $this
    ->drupalPost(NULL, $edit, t('Save translation'));
  $this
    ->i18nAssertTranslations($translations['title'], '', 'Updated block title translation displayed.');
  $this
    ->i18nAssertTranslations($translations['body'], '', 'Updated block body translation displayed.');

  // Test a block translation with filtering and text formats
  $box3 = $this
    ->i18nCreateBlock(array(
    'title' => '<div><script>alert(0)</script>Title</script>',
    'body' => "Dangerous text\nOne line\nTwo lines<script>alert(1)</script>",
  ));

  // This should be the actual HTML displayed
  $title = check_plain($box3['title']);
  $body = check_markup($box3['body'], $box3['format']);
  $this
    ->drupalGet('');
  $this
    ->assertRaw($title, "Title being displayed for default language: " . $title);
  $this
    ->assertRaw($body, "Body being displayed for default language: " . $body);

  // We add language name to the body just to make sure we get the right translation later
  // This won't work for block titles as they don't have input format thus scripts will be blocked by locale
  $translations = array();
  foreach ($this
    ->getOtherLanguages() as $langcode => $language) {
    $translations[$langcode] = $box3['body'] . "\n" . $language->name;
    $filtered[$langcode] = check_markup($translations[$langcode], $box3['format']);
  }

  // We need to find the string by this part alone, the rest will be filtered
  $this
    ->createStringTranslation('blocks', 'Dangerous text', $translations);

  // Check the right filtered strings are displayed
  $this
    ->i18nAssertTranslations($filtered);

  // Assert translatable descriptions.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('admin/structure/block/manage/system/powered-by/configure');
  $this
    ->assertText(t('This block has generated content, only the title can be translated here.'));
  $this
    ->drupalGet('admin/structure/block/manage/system/navigation/configure');
  $this
    ->assertText(t('To translate the block content itself, translate the menu that is being shown.'));
}