You are here

protected function LocaleUpdateBase::setTranslationFiles in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Functional/LocaleUpdateBase.php \Drupal\Tests\locale\Functional\LocaleUpdateBase::setTranslationFiles()
  2. 9 core/modules/locale/tests/src/Functional/LocaleUpdateBase.php \Drupal\Tests\locale\Functional\LocaleUpdateBase::setTranslationFiles()

Setup the environment containing local and remote translation files.

Update tests require a simulated environment for local and remote files. Normally remote files are located at a remote server (e.g. ftp.drupal.org). For testing we can not rely on this. A directory in the file system of the test site is designated for remote files and is addressed using an absolute URL. Because Drupal does not allow files with a po extension to be accessed (denied in .htaccess) the translation files get a _po extension. Another directory is designated for local translation files.

The environment is set up with the following files. File creation times are set to create different variations in test conditions. contrib_module_one

  • remote file: timestamp new
  • local file: timestamp old

contrib_module_two

  • remote file: timestamp old
  • local file: timestamp new

contrib_module_three

  • remote file: timestamp old
  • local file: timestamp old

custom_module_one

  • local file: timestamp new

Time stamp of current translation set by setCurrentTranslations() is always timestamp medium. This makes it easy to predict which translation will be imported.

File

core/modules/locale/tests/src/Functional/LocaleUpdateBase.php, line 177

Class

LocaleUpdateBase
Base class for testing updates to string translations.

Namespace

Drupal\Tests\locale\Functional

Code

protected function setTranslationFiles() {
  $config = $this
    ->config('locale.settings');

  // A flag is set to let the locale_test module replace the project data with
  // a set of test projects which match the below project files.
  \Drupal::state()
    ->set('locale.test_projects_alter', TRUE);
  \Drupal::state()
    ->set('locale.remove_core_project', FALSE);

  // Setup the environment.
  $public_path = PublicStream::basePath();
  $this
    ->setTranslationsDirectory($public_path . '/local');
  $config
    ->set('translation.default_filename', '%project-%version.%language._po')
    ->save();

  // Setting up sets of translations for the translation files.
  $translations_one = [
    'January' => 'Januar_1',
    'February' => 'Februar_1',
    'March' => 'Marz_1',
  ];
  $translations_two = [
    'February' => 'Februar_2',
    'March' => 'Marz_2',
    'April' => 'April_2',
  ];
  $translations_three = [
    'April' => 'April_3',
    'May' => 'Mai_3',
    'June' => 'Juni_3',
  ];

  // Add a number of files to the local file system to serve as remote
  // translation server and match the project definitions set in
  // locale_test_locale_translation_projects_alter().
  $this
    ->makePoFile('remote/all/contrib_module_one', 'contrib_module_one-8.x-1.1.de._po', $this->timestampNew, $translations_one);
  $this
    ->makePoFile('remote/all/contrib_module_two', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampOld, $translations_two);
  $this
    ->makePoFile('remote/all/contrib_module_three', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);

  // Add a number of files to the local file system to serve as local
  // translation files and match the project definitions set in
  // locale_test_locale_translation_projects_alter().
  $this
    ->makePoFile('local', 'contrib_module_one-8.x-1.1.de._po', $this->timestampOld, $translations_one);
  $this
    ->makePoFile('local', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampNew, $translations_two);
  $this
    ->makePoFile('local', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);
  $this
    ->makePoFile('local', 'custom_module_one.de.po', $this->timestampNew);
}