You are here

function i18n_Blocks_Test::testBlockTranslation in Internationalization 6

File

tests/i18n_blocks.test, line 29

Class

i18n_Blocks_Test

Code

function testBlockTranslation() {

  // Create a translatable block
  $box = $this
    ->i18nCreateBox(array(
    'language' => I18N_BLOCK_LOCALIZE,
  ));
  $i18nblock = i18nblocks_load('block', $box->bid);
  $this
    ->assertTrue($i18nblock->ibid && $i18nblock->language == I18N_BLOCK_LOCALIZE, "The block has been created with the right i18n settings.");

  // Create translations for title and body, source strings should be already there
  $translations = $this
    ->i18nTranslateBlock('block', $box - bid, array(
    'title',
    'body',
  ));

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

  // Do not show in default language
  $this
    ->drupalGet('');
  $this
    ->assertNoText($box->title);

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

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

  // Add a custom title to any other block: Navigation (user, 1)
  $title = $this
    ->randomName(10);
  $this
    ->i18nUpdateBlock('user', 1, array(
    'title' => $title,
  ));
  $this
    ->assertText($title, "The new custom title is displayed on the home page.");
  $translate = $this
    ->i18nTranslateBlock('user', 1, array(
    'title',
  ));
  $this
    ->drupalGet('');

  // Refresh block strings, the ones for the first box should be gone. Not the others
  $box2 = $this
    ->i18nCreateBox(array(
    'language' => I18N_BLOCK_LOCALIZE,
  ));
  $translations = $this
    ->i18nTranslateBlock('block', $box2->bid, array(
    'title',
    'body',
  ));
  i18nstrings_refresh_group('blocks', TRUE);
  $this
    ->assertFalse(i18nstrings_get_source("blocks:block:{$box->bid}:title", $box->title), "The string for the box title is gone.");
  $this
    ->assertFalse(i18nstrings_get_source("blocks:block:{$box->bid}:body", $box->body), "The string for the box body is gone.");
  $this
    ->assertTrue(i18nstrings_get_source("blocks:user:1:title"), "We have a string for the Navigation block title");
  $this
    ->assertTrue(i18nstrings_get_source("blocks:block:{$box2->bid}:title", $box2->title), "The string for the second box title is still there.");
  $this
    ->assertTrue(i18nstrings_get_source("blocks:block:{$box2->bid}:body", $box2->body), "The string for the second box body is still there.");

  // Test a block with filtering and input formats
  $box3 = $this
    ->i18nCreateBox(array(
    'title' => '<div><script>alert(0)</script>Title</script>',
    'body' => "One line\nTwo lines<script>alert(1)</script>",
    'language' => I18N_BLOCK_LOCALIZE,
  ));
  $language = current($this
    ->getOtherLanguages());

  // We add language name to the title just to make sure we get the right translation later
  $this
    ->i18nstringsSaveTranslation("blocks:block:{$box3->bid}:title", $language->language, $box3->title . $language->name);
  $this
    ->i18nstringsSaveTranslation("blocks:block:{$box3->bid}:body", $language->language, $box3->body);

  // This should be the actual HTML displayed
  $title = check_plain($box3->title);
  $body = check_markup($box3->body);
  $this
    ->drupalGet('');
  $this
    ->assertRaw($title, "Title being displayed for default language: " . $title);
  $this
    ->assertRaw($body, "Body being displayed for default language: " . check_plain($body));
  $this
    ->i18nGet($language);
  $this
    ->assertRaw($title . $language->name, "Translated title displayed with right filtering.");
  $this
    ->assertRaw($body, "Translated body displayed with right filtering.");
}