You are here

public function LocaleUpdateTest::testUpdateImportSourceRemote in Drupal 9

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

Tests translation import from remote sources.

Test conditions:

  • Source: remote and local files
  • Import overwrite: all existing translations

File

core/modules/locale/tests/src/Functional/LocaleUpdateTest.php, line 113

Class

LocaleUpdateTest
Tests for updating the interface translations of projects.

Namespace

Drupal\Tests\locale\Functional

Code

public function testUpdateImportSourceRemote() {
  $config = $this
    ->config('locale.settings');

  // Build the test environment.
  $this
    ->setTranslationFiles();
  $this
    ->setCurrentTranslations();
  $config
    ->set('translation.default_filename', '%project-%version.%language._po');

  // Set the update conditions for this test.
  $edit = [
    'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
    'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
  ];
  $this
    ->drupalGet('admin/config/regional/translate/settings');
  $this
    ->submitForm($edit, 'Save configuration');

  // Get the translation status.
  $this
    ->drupalGet('admin/reports/translations/check');

  // Check the status on the Available translation status page.
  $this
    ->assertSession()
    ->responseContains('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>');
  $this
    ->assertSession()
    ->pageTextContains('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test');

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = $this->container
    ->get('date.formatter');
  $this
    ->assertSession()
    ->pageTextContains('Contributed module one (' . $date_formatter
    ->format($this->timestampNew, 'html_date') . ')');
  $this
    ->assertSession()
    ->pageTextContains('Contributed module two (' . $date_formatter
    ->format($this->timestampNew, 'html_date') . ')');

  // Execute the translation update.
  $this
    ->drupalGet('admin/reports/translations');
  $this
    ->submitForm([], 'Update translations');

  // Check if the translation has been updated, using the status cache.
  $status = locale_translation_get_status();
  $this
    ->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
  $this
    ->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
  $this
    ->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_three']['de']->type, 'Translation of contrib_module_three found');

  // Check the new translation status.
  // The static cache needs to be flushed first to get the most recent data
  // from the database. The function was called earlier during this test.
  drupal_static_reset('locale_translation_get_file_history');
  $history = locale_translation_get_file_history();

  // Verify that the translation of contrib_module_one is imported and
  // updated.
  $this
    ->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->timestamp);
  $this
    ->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->last_checked);
  $this
    ->assertEquals($this->timestampNew, $history['contrib_module_two']['de']->timestamp, 'Translation of contrib_module_two is imported');

  // Verify that the translation of contrib_module_two is updated.
  $this
    ->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_two']['de']->last_checked);
  $this
    ->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->timestamp, 'Translation of contrib_module_three is not imported');
  $this
    ->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->last_checked, 'Translation of contrib_module_three is not updated');

  // Check whether existing translations have (not) been overwritten.
  // cSpell:disable
  $this
    ->assertEquals('Januar_1', t('January', [], [
    'langcode' => 'de',
  ]), 'Translation of January');
  $this
    ->assertEquals('Februar_2', t('February', [], [
    'langcode' => 'de',
  ]), 'Translation of February');
  $this
    ->assertEquals('Marz_2', t('March', [], [
    'langcode' => 'de',
  ]), 'Translation of March');
  $this
    ->assertEquals('April_2', t('April', [], [
    'langcode' => 'de',
  ]), 'Translation of April');
  $this
    ->assertEquals('Mai_customized', t('May', [], [
    'langcode' => 'de',
  ]), 'Translation of May');
  $this
    ->assertEquals('Juni', t('June', [], [
    'langcode' => 'de',
  ]), 'Translation of June');
  $this
    ->assertEquals('Montag', t('Monday', [], [
    'langcode' => 'de',
  ]), 'Translation of Monday');

  // cSpell:enable
}