function LocaleCompareTest::testCompareCheckLocal

Checks if local or remote translation sources are detected.

This test requires 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 txt extension. Another directory is designated for local translation files.

The translation status process by default checks the status of the installed projects. For testing purpose a predefined set of modules with fixed file names and release versions is used. Using a hook_locale_translation_projects_alter implementation in the locale_test module this custom project definition is applied.

This test generates a set of local and remote translation files in their respective local and remote translation directory. The test checks whether the most recent files are selected in the different check scenarios: check for local files only, check for remote files only, check for both local and remote files.

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleCompareTest.php, line 180
Definition of Drupal\locale\Tests\LocaleCompareTest.

Class

LocaleCompareTest
Tests for comparing status of existing project translations with available translations.

Namespace

Drupal\locale\Tests

Code

function testCompareCheckLocal() {
  $config = config('locale.settings');

  // A flag is set to let the locale_test module replace the project data with
  // a set of test projects.
  state()
    ->set('locale_translation_test_projects', TRUE);

  // Setup timestamps to identify old and new translation sources.
  $timestamp_old = REQUEST_TIME - 100;
  $timestamp_new = REQUEST_TIME;

  // Set up the environment.
  $public_path = variable_get('file_public_path', conf_path() . '/files');
  $this
    ->setTranslationsDirectory($public_path . '/local');
  $config
    ->set('translation.default_filename', '%project-%version.%language.txt')
    ->save();

  // 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/8.x/contrib_module_one', 'contrib_module_one-8.x-1.1.de.txt', $timestamp_new);
  $this
    ->makePoFile('remote/8.x/contrib_module_two', 'contrib_module_two-8.x-2.0-beta4.de.txt', $timestamp_old);
  $this
    ->makePoFile('remote/8.x/contrib_module_three', 'contrib_module_three-8.x-1.0.de.txt', $timestamp_old);

  // 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.txt', $timestamp_old);
  $this
    ->makePoFile('local', 'contrib_module_two-8.x-2.0-beta4.de.txt', $timestamp_new);
  $this
    ->makePoFile('local', 'custom_module_one.de.po', $timestamp_new);

  // Get status of translation sources at local file system.
  $config
    ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
    ->save();
  $this
    ->drupalGet('admin/reports/translations/check');
  $result = state()
    ->get('locale_translation_status');
  $this
    ->assertEqual($result['contrib_module_one']['de']->type, 'local', 'Translation of contrib_module_one found');
  $this
    ->assertEqual($result['contrib_module_one']['de']->timestamp, $timestamp_old, 'Translation timestamp found');
  $this
    ->assertEqual($result['contrib_module_two']['de']->type, 'local', 'Translation of contrib_module_two found');
  $this
    ->assertEqual($result['contrib_module_two']['de']->timestamp, $timestamp_new, 'Translation timestamp found');
  $this
    ->assertEqual($result['locale_test']['de']->type, 'local', 'Translation of locale_test found');
  $this
    ->assertEqual($result['custom_module_one']['de']->type, 'local', 'Translation of custom_module_one found');

  // Get status of translation sources at both local and remote the locations.
  $config
    ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL)
    ->save();
  $this
    ->drupalGet('admin/reports/translations/check');
  $result = state()
    ->get('locale_translation_status');
  $this
    ->assertEqual($result['contrib_module_one']['de']->type, 'remote', 'Translation of contrib_module_one found');
  $this
    ->assertEqual($result['contrib_module_one']['de']->timestamp, $timestamp_new, 'Translation timestamp found');
  $this
    ->assertEqual($result['contrib_module_two']['de']->type, 'local', 'Translation of contrib_module_two found');
  $this
    ->assertEqual($result['contrib_module_two']['de']->timestamp, $timestamp_new, 'Translation timestamp found');
  $this
    ->assertEqual($result['contrib_module_three']['de']->type, 'remote', 'Translation of contrib_module_three found');
  $this
    ->assertEqual($result['contrib_module_three']['de']->timestamp, $timestamp_old, 'Translation timestamp found');
  $this
    ->assertEqual($result['locale_test']['de']->type, 'local', 'Translation of locale_test found');
  $this
    ->assertEqual($result['custom_module_one']['de']->type, 'local', 'Translation of custom_module_one found');
}