From 4d769bcc78ed0b492abbc58e41cd87cfb49cc55b Mon Sep 17 00:00:00 2001
From: Francesco Placella <plach@183211.no-reply.drupal.org>
Date: Wed, 11 Apr 2018 01:31:01 +0200
Subject: [PATCH] Issue #2955869 by alexpott, dawehner, plach: Fix multilingual
 install on Drupal dev version for CLI utilities

---
 core/includes/install.core.inc                | 18 +++++-
 .../LocaleNonInteractiveDevInstallTest.php    | 21 +++++++
 .../LocaleNonInteractiveInstallTest.php       | 57 +++++++++++++++++++
 .../Functional/NodeTypeTranslationTest.php    |  5 ++
 4 files changed, 98 insertions(+), 3 deletions(-)
 create mode 100644 core/modules/locale/tests/src/Functional/LocaleNonInteractiveDevInstallTest.php
 create mode 100644 core/modules/locale/tests/src/Functional/LocaleNonInteractiveInstallTest.php

diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 5cc1236c1b95..c27f75e1782e 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1432,7 +1432,7 @@ function install_check_localization_server($uri) {
  *
  * @param string $version
  *   Version info string (e.g., 8.0.0, 8.1.0, 8.0.0-dev, 8.0.0-unstable1,
- *   8.0.0-alpha2, 8.0.0-beta3, and 8.0.0-rc4).
+ *   8.0.0-alpha2, 8.0.0-beta3, 8.6.x, and 8.0.0-rc4).
  *
  * @return array
  *   Associative array of version info:
@@ -1450,7 +1450,7 @@ function _install_get_version_info($version) {
       \.          # .
       (?P<minor>[0-9]+)    # Minor release number.
       \.          # .
-      (?P<patch>[0-9]+)    # Patch release number.
+      (?P<patch>[0-9]+|x)  # Patch release number.
     )             #
     (             #
       -           # - separator for "extra" version information.
@@ -1722,6 +1722,9 @@ function _install_prepare_import($langcodes, $server_pattern) {
           ];
           \Drupal::service('locale.project')->set($data['name'], $data);
           module_load_include('compare.inc', 'locale');
+          // Reset project information static cache so that it uses the data
+          // set above.
+          locale_translation_clear_cache_projects();
           locale_translation_check_projects_local(['drupal'], [$langcode]);
         }
       }
@@ -1857,10 +1860,19 @@ function install_check_translations($langcode, $server_pattern) {
     return;
   }
 
+  $version = \Drupal::VERSION;
+  // For dev releases, remove the '-dev' part and trust the translation server
+  // to fall back to the latest stable release for that branch.
+  // @see locale_translation_build_projects()
+  if (preg_match("/^(\d+\.\d+\.).*-dev$/", $version, $matches)) {
+    // Example match: 8.0.0-dev => 8.0.x (Drupal core)
+    $version = $matches[1] . 'x';
+  }
+
   // Build URL for the translation file and the translation server.
   $variables = [
     '%project' => 'drupal',
-    '%version' => \Drupal::VERSION,
+    '%version' => $version,
     '%core' => \Drupal::CORE_COMPATIBILITY,
     '%language' => $langcode,
   ];
diff --git a/core/modules/locale/tests/src/Functional/LocaleNonInteractiveDevInstallTest.php b/core/modules/locale/tests/src/Functional/LocaleNonInteractiveDevInstallTest.php
new file mode 100644
index 000000000000..080616af91ca
--- /dev/null
+++ b/core/modules/locale/tests/src/Functional/LocaleNonInteractiveDevInstallTest.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Drupal\Tests\locale\Functional;
+
+/**
+ * Tests installing in a different language with a dev version string.
+ *
+ * @group locale
+ */
+class LocaleNonInteractiveDevInstallTest extends LocaleNonInteractiveInstallTest {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getVersionStringToTest() {
+    include_once $this->root . '/core/includes/install.core.inc';
+    $version = _install_get_version_info(\Drupal::VERSION);
+    return $version['major'] . '.' . $version['minor'] . '.x';
+  }
+
+}
diff --git a/core/modules/locale/tests/src/Functional/LocaleNonInteractiveInstallTest.php b/core/modules/locale/tests/src/Functional/LocaleNonInteractiveInstallTest.php
new file mode 100644
index 000000000000..ee12a6713ae3
--- /dev/null
+++ b/core/modules/locale/tests/src/Functional/LocaleNonInteractiveInstallTest.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace Drupal\Tests\locale\Functional;
+
+use Drupal\Tests\BrowserTestBase;
+
+/**
+ * Tests installing in a different language with a non-dev version string.
+ *
+ * @group locale
+ */
+class LocaleNonInteractiveInstallTest extends BrowserTestBase {
+
+  /**
+   * Gets the version string to use in the translation file.
+   *
+   * @return string
+   *   The version string to test, for example, '8.0.0' or '8.6.x'.
+   */
+  protected function getVersionStringToTest() {
+    include_once $this->root . '/core/includes/install.core.inc';
+    $version = _install_get_version_info(\Drupal::VERSION);
+    return $version['major'] . '.0.0';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function installParameters() {
+    $parameters = parent::installParameters();
+    // Install Drupal in German.
+    $parameters['parameters']['langcode'] = 'de';
+    // Create a po file so we don't attempt to download one from
+    // localize.drupal.org and to have a test translation that will not change.
+    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
+    $contents = <<<ENDPO
+msgid ""
+msgstr ""
+
+msgid "Enter the password that accompanies your username."
+msgstr "Geben sie das Passwort für ihren Benutzernamen ein."
+
+ENDPO;
+    $version = $this->getVersionStringToTest();
+    file_put_contents($this->publicFilesDirectory . "/translations/drupal-{$version}.de.po", $contents);
+    return $parameters;
+  }
+
+  /**
+   * Tests that the expected translated text appears on the login screen.
+   */
+  public function testInstallerTranslations() {
+    $this->drupalGet('user/login');
+    $this->assertSession()->responseContains('Geben sie das Passwort für ihren Benutzernamen ein.');
+  }
+
+}
diff --git a/core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php b/core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php
index 07e61a036dd6..264bada062f7 100644
--- a/core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php
+++ b/core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php
@@ -83,6 +83,11 @@ protected function setUp() {
   protected function installParameters() {
     $parameters = parent::installParameters();
     $parameters['parameters']['langcode'] = $this->defaultLangcode;
+    // Create an empty po file so we don't attempt to download one from
+    // localize.drupal.org. It does not need to match the version exactly as the
+    // multi-lingual system will fallback.
+    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
+    file_put_contents($this->publicFilesDirectory . "/translations/drupal-8.0.0.{$this->defaultLangcode}.po", '');
     return $parameters;
   }
 
-- 
GitLab