1 locale.test LocaleTranslationFunctionalTest::testJavaScriptTranslation()

File

core/modules/locale/tests/locale.test, line 341
Tests for locale.module.

Class

LocaleTranslationFunctionalTest
Functional test for string translation and validation.

Code

function testJavaScriptTranslation() {
  $user = $this->backdropCreateUser(array('translate interface', 'administer languages', 'access administration pages'));
  $this->backdropLogin($user);

  $langcode = 'xx';
  // The English name for the language. This will be translated.
  $name = $this->randomName(16);
  // The native name for the language.
  $native = $this->randomName(16);

  // Add custom language.
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'name' => $name,
    'native' => $native,
    'direction' => '0',
  );
  $this->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  backdrop_static_reset('language_list');

  // Build the JavaScript translation file.
  $this->backdropGet('admin/config/regional/translate/translate');

  // Retrieve the id of the first string available in the {locales_source}
  // table and translate it.
  $query = db_select('locales_source', 'l');
  $query->addExpression('min(l.lid)', 'lid');
  $result = $query->condition('l.location', '%.js%', 'LIKE')->execute();
  $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid;
  $edit = array('translations[' . $langcode . ']' => $this->randomName());
  $this->backdropPost($url, $edit, t('Save translations'));

  // Trigger JavaScript translation parsing and building.
  require_once BACKDROP_ROOT . '/core/includes/locale.inc';
  _locale_rebuild_js($langcode);

  $locale_javascripts = state_get('locale_translation_javascript', array());
  $js_file = 'public://' . settings_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
  $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file created: %file', array('%file' => $result ? $js_file : 'not found')));

  // Test JavaScript translation rebuilding.
  file_unmanaged_delete($js_file);
  $this->assertTrue($result = !file_exists($js_file), format_string('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found')));
  cache_clear_all();
  _locale_rebuild_js($langcode);
  $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : 'not found')));
}