From 50585b2250ed6f26824e7d7c15fabf03170c2428 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Wed, 9 Sep 2015 14:47:23 -0700
Subject: [PATCH] =?UTF-8?q?Issue=20#2113955=20by=20Sutharsan,=20japerry,?=
 =?UTF-8?q?=20G=C3=A1bor=20Hojtsy,=20basic:=20Rely=20on=20proper=20server?=
 =?UTF-8?q?=20side=20version=20fallback=20for=20translations?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/includes/install.core.inc                | 154 ++----------------
 .../InstallerTranslationVersionUnitTest.php   | 145 -----------------
 2 files changed, 12 insertions(+), 287 deletions(-)
 delete mode 100644 core/modules/system/src/Tests/Installer/InstallerTranslationVersionUnitTest.php

diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index ad3959059291..b25d28cb2a36 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1404,129 +1404,6 @@ function install_check_localization_server($uri) {
   }
 }
 
-/**
- * Gets the core release version and release alternatives for localization.
- *
- * In case core is a development version or the translation file for the
- * release is not available, fall back to an earlier release.
- * For example, 8.2.0-dev might fall back to 8.1.0 and 8.0.0-dev
- * might fall back to 7.0.
- *
- * @param string $version
- *   (optional) Version of core trying to find translation files for.
- *
- * @return array
- *   Array of release data. Each array element is an associative array with:
- *   - core: Core compatibility version (e.g., 8.x).
- *   - version: Release version (e.g., 8.1.0).
- */
-function install_get_localization_release($version = \Drupal::VERSION) {
-  $releases = array();
-  $alternatives = array();
-
-  $info = _install_get_version_info($version);
-
-  // This code assumes there is a first alpha available as "alpha1". For
-  // Drupal 8.0.0 that isn't the case. In addition the semantic versioning
-  // wasn't introduced before "alpha14". However, we have already gotten to
-  // "beta1" so we are ignoring this for now.
-
-  // The fallback detection is relaxed - removing version duplicates at the end.
-
-  // Check if the version is a regular stable release (no 'rc', 'beta', 'alpha',
-  // 'dev', etc.)
-  if (!isset($info['extra_text'])) {
-    // First version alternative: the current version.
-    $alternatives[] = $version;
-
-    // Patch-level: Previous and zero patch level (e.g., 8.2.4 falls back to
-    // 8.2.3 and 8.2.0).
-    if ($info['patch'] > 0) {
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.' . ($info['patch'] - 1);
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0';
-    }
-    // Zero patch: First release candidate (e.g., 8.0.0 falls back to
-    // 8.0.0-rc1).
-    else {
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-rc1';
-    }
-    // Point-releases: Previous and zero minor release (e.g., 8.2.x falls back
-    // to 8.1.0 and 8.0.0).
-    if ($info['minor'] > 0) {
-      $alternatives[] = $info['major'] . '.' . ($info['minor'] - 1) . '.0';
-      $alternatives[] = $info['major'] . '.0.0';
-    }
-  }
-  else {
-    // Below we assume that for all dev, alpha, beta or rc releases the patch
-    // level is 0.
-
-    $release_levels = array('rc', 'beta', 'alpha');
-
-    if ($info['extra_text'] == 'dev') {
-      // Dev release: Any unstable release (e.g., 8.2.0-dev falls back to
-      // 8.2.0-rc1, 8.2.0-beta1 and 8.2.0-alpha1).
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-rc1';
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-beta1';
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-alpha1';
-    }
-    elseif (in_array($info['extra_text'], $release_levels)) {
-      // All other unstable release always includes the current version.
-      $alternatives[] = $version;
-      // Alpha release: Previous alpha release (e.g. 8.0.0-alpha2 falls back to
-      // 8.0.0-alpha1).
-      // Beta release: Previous beta release and first alpha release (e.g.
-      // 8.0.0-beta2 falls back to 8.0.0-beta1 and 8.0.0-alpha1).
-      // Release candidate: Previous release candidate and the first beta
-      // release (e.g. 8.0.0-rc2 falls back to 8.0.0-rc1 and 8.0.0-beta1).
-      $release_level_key = array_search($info['extra_text'], $release_levels);
-      if ($info['extra_number'] > 1) {
-        $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-' . $info['extra_text'] . ($info['extra_number'] - 1);
-      }
-      if ($info['extra_text'] != 'alpha') {
-        $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0-' . $release_levels[$release_level_key + 1] . '1';
-      }
-    }
-    else {
-      $alternatives[] = $info['major'] . '.' . $info['minor'] . '.0';
-    }
-    // All unstable releases except the zero release: The previous minor release
-    // and the zero release.
-    if ($info['minor'] > 0) {
-      $alternatives[] = $info['major'] . '.' . ($info['minor'] - 1) . '.0';
-      $alternatives[] = $info['major'] . '.0.0';
-    }
-  }
-
-  $alternatives = array_unique($alternatives);
-  foreach ($alternatives as $alternative) {
-    list($core) = explode('.', $alternative);
-    $releases[] = array(
-      'core' => $core . '.x',
-      'version' => $alternative,
-    );
-  }
-
-  // All releases may fall back to the previous major release (e.g., 8.1.0
-  // may fall back to 7.0). This will probably only be used for early dev
-  // releases or languages with an inactive translation team.
-  if ($info['major'] == 8) {
-    $releases[] = array(
-      'core' => '7.x',
-      'version' => '7.0',
-    );
-  }
-  else {
-    $prev_major = $info['major'] - 1;
-    $releases[] = array(
-      'core' => $prev_major . '.x',
-      'version' => $prev_major . '.0.0',
-    );
-  }
-
-  return $releases;
-}
-
 /**
  * Extracts version information from a drupal core version string.
  *
@@ -1937,7 +1814,6 @@ function install_check_translations($langcode, $server_pattern) {
   $files_directory =  $site_path . '/files';
   $translations_directory = $site_path . '/files/translations';
   $translations_directory_exists = FALSE;
-  $translation_available = FALSE;
   $online = FALSE;
 
   // First attempt to create or make writable the files directory.
@@ -1957,19 +1833,16 @@ function install_check_translations($langcode, $server_pattern) {
     return;
   }
 
-  // Build URLs for the translation file and the translation server.
-  $releases = install_get_localization_release();
-  $translation_urls = array();
-  foreach ($releases as $release) {
-    $variables = array(
-      '%project' => 'drupal',
-      '%version' => $release['version'],
-      '%core' => $release['core'],
-      '%language' => $langcode,
-    );
-    $translation_urls[] = strtr($server_pattern, $variables);
-  }
-  $elements = parse_url(reset($translation_urls));
+  // Build URL for the translation file and the translation server.
+  $variables = array(
+    '%project' => 'drupal',
+    '%version' => \Drupal::VERSION,
+    '%core' => \Drupal::CORE_COMPATIBILITY,
+    '%language' => $langcode,
+  );
+  $translation_url = strtr($server_pattern, $variables);
+
+  $elements = parse_url($translation_url);
   $server_url = $elements['scheme'] . '://' . $elements['host'];
 
   // Build the language name for display.
@@ -1979,11 +1852,8 @@ function install_check_translations($langcode, $server_pattern) {
   // Check if any of the desired translation files are available or if the
   // translation server can be reached. In other words, check if we are online
   // and have an internet connection.
-  foreach ($translation_urls as $translation_url) {
-    if ($translation_available = install_check_localization_server($translation_url)) {
-      $online = TRUE;
-      break;
-    }
+  if ($translation_available = install_check_localization_server($translation_url)) {
+    $online = TRUE;
   }
   if (!$translation_available) {
     if (install_check_localization_server($server_url)) {
diff --git a/core/modules/system/src/Tests/Installer/InstallerTranslationVersionUnitTest.php b/core/modules/system/src/Tests/Installer/InstallerTranslationVersionUnitTest.php
deleted file mode 100644
index 5e16171d4c95..000000000000
--- a/core/modules/system/src/Tests/Installer/InstallerTranslationVersionUnitTest.php
+++ /dev/null
@@ -1,145 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\system\Tests\Installer\InstallerTranslationVersionUnitTest.
- */
-
-namespace Drupal\system\Tests\Installer;
-
-use Drupal\simpletest\KernelTestBase;
-
-/**
- * Tests the translation version fallback used during site installation to
- * determine available translation files.
- *
- * @group Installer
- */
-class InstallerTranslationVersionUnitTest extends KernelTestBase {
-
-  protected function setUp() {
-    parent::setUp();
-    require_once \Drupal::root() . '/core/includes/install.core.inc';
-  }
-
-  /**
-   * Asserts version fallback results of install_get_localization_release().
-   *
-   * @param $version
-   *   Version string for which to determine version fallbacks.
-   * @param $fallback
-   *   Array of fallback versions ordered for most to least significant.
-   * @param string $message
-   *   (optional) A message to display with the assertion.
-   * @param string $group
-   *   (optional) The group this message is in.
-   *
-   * @return bool
-   *   TRUE if the assertion succeeded, FALSE otherwise.
-   */
-  protected function assertVersionFallback($version, $fallback, $message = '', $group = 'Other') {
-    $equal = TRUE;
-    $results = install_get_localization_release($version);
-    // Check the calculated results with the required results.
-    // The $results is an array of arrays, each containing:
-    //   'version': A release version (e.g. 8.0)
-    //   'core'   : The matching core version (e.g. 8.x)
-    if (count($fallback) == count($results)) {
-      foreach($results as $key => $result) {
-        $equal &= $result['version'] == $fallback[$key];
-        list($major_release) = explode('.', $fallback[$key]);
-        $equal &= $result['core'] == $major_release . '.x';
-      }
-    }
-    else {
-      $equal = FALSE;
-    }
-    $message = $message ? $message : t('Version fallback for @version.', array('@version' => $version));
-    return $this->assert((bool) $equal, $message, $group);
-  }
-
-  /**
-   * Tests version fallback of install_get_localization_release().
-   */
-  public function testVersionFallback() {
-    $version = '8.0.0';
-    $fallback = array('8.0.0', '8.0.0-rc1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.1.0';
-    $fallback = array('8.1.0', '8.1.0-rc1', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.12.0';
-    $fallback = array('8.12.0', '8.12.0-rc1', '8.11.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-dev';
-    $fallback = array('8.0.0-rc1', '8.0.0-beta1', '8.0.0-alpha1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.9.0-dev';
-    $fallback = array('8.9.0-rc1', '8.9.0-beta1', '8.9.0-alpha1', '8.8.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-alpha3';
-    $fallback = array('8.0.0-alpha3', '8.0.0-alpha2', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-alpha1';
-    $fallback = array('8.0.0-alpha1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-beta2';
-    $fallback = array('8.0.0-beta2', '8.0.0-beta1', '8.0.0-alpha1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-beta1';
-    $fallback = array('8.0.0-beta1', '8.0.0-alpha1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-rc8';
-    $fallback = array('8.0.0-rc8', '8.0.0-rc7', '8.0.0-beta1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-rc1';
-    $fallback = array('8.0.0-rc1', '8.0.0-beta1', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.2.0-beta1';
-    $fallback = array('8.2.0-beta1', '8.2.0-alpha1', '8.1.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.2.0-beta7';
-    $fallback = array('8.2.0-beta7', '8.2.0-beta6', '8.2.0-alpha1', '8.1.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.2.0-rc1';
-    $fallback = array('8.2.0-rc1', '8.2.0-beta1', '8.1.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.2.0-rc2';
-    $fallback = array('8.2.0-rc2', '8.2.0-rc1', '8.2.0-beta1', '8.1.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.0-foo2';
-    $fallback = array('8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.0.4';
-    $fallback = array('8.0.4', '8.0.3', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '8.3.5';
-    $fallback = array('8.3.5', '8.3.4', '8.3.0', '8.2.0', '8.0.0', '7.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '99.0.1';
-    $fallback = array('99.0.1', '99.0.0' ,'98.0.0');
-    $this->assertVersionFallback($version, $fallback);
-
-    $version = '99.7.1';
-    $fallback = array('99.7.1', '99.7.0', '99.6.0', '99.0.0' ,'98.0.0');
-    $this->assertVersionFallback($version, $fallback);
-  }
-}
-- 
GitLab