From eb9dc11c05e8e01b79663e823279294d86186acd Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 22 Jul 2019 09:31:06 +0100
Subject: [PATCH] Issue #2958925 by alexpott, voleger, Vlad Bo, Wim Leers:
 Properly deprecate drupal_get_profile()

---
 core/includes/bootstrap.inc                   |  4 +-
 core/includes/install.core.inc                |  4 +-
 core/includes/install.inc                     |  4 +-
 core/includes/module.inc                      |  5 +-
 .../lib/Drupal/Core/Config/InstallStorage.php |  2 +-
 .../Drupal/Core/Database/Install/Tasks.php    |  3 +-
 .../Core/Extension/ExtensionDiscovery.php     |  2 +-
 core/modules/simpletest/src/TestBase.php      |  2 +-
 core/modules/system/system.install            |  4 +-
 .../Installer/InstallerTest.php               |  7 ++
 .../Installer/SingleVisibleProfileTest.php    |  2 +-
 .../Bootstrap/DrupalGetProfileLegacyTest.php  | 87 +++++++++++++++++++
 12 files changed, 111 insertions(+), 15 deletions(-)
 create mode 100644 core/tests/Drupal/Tests/Core/Bootstrap/DrupalGetProfileLegacyTest.php

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index dcfd1dfff396..0bc11958a287 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -778,7 +778,7 @@ function drupal_installation_attempted() {
  *   currently active. This is the case for example during the first steps of
  *   the installer or during unit tests.
  *
- * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0.
+ * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0.
  *   Use the install_profile container parameter or \Drupal::installProfile()
  *   instead. If you are accessing the value before it is written to
  *   configuration during the installer use the $install_state global. If you
@@ -791,6 +791,8 @@ function drupal_installation_attempted() {
 function drupal_get_profile() {
   global $install_state;
 
+  @trigger_error('drupal_get_profile() is deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use the install_profile container parameter or \Drupal::installProfile() instead. If you are accessing the value before it is written to configuration during the installer use the $install_state global. If you need to access the value before container is available you can use BootstrapConfigStorageFactory to load the value directly from configuration. See https://www.drupal.org/node/2538996', E_USER_DEPRECATED);
+
   if (drupal_installation_attempted()) {
     // If the profile has been selected return it.
     if (isset($install_state['parameters']['profile'])) {
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 12516b4cf602..6910245723d5 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1655,7 +1655,7 @@ function install_profile_themes(&$install_state) {
  *   An array of information about the current installation state.
  */
 function install_install_profile(&$install_state) {
-  \Drupal::service('module_installer')->install([drupal_get_profile()], FALSE);
+  \Drupal::service('module_installer')->install([$install_state['parameters']['profile']], FALSE);
   // Install all available optional config. During installation the module order
   // is determined by dependencies. If there are no dependencies between modules
   // then the order in which they are installed is dependent on random factors
@@ -1853,7 +1853,7 @@ function install_finish_translations(&$install_state) {
  *   A message informing the user that the installation is complete.
  */
 function install_finished(&$install_state) {
-  $profile = drupal_get_profile();
+  $profile = $install_state['parameters']['profile'];
 
   // Installation profiles are always loaded last.
   module_set_weight($profile, 1000);
diff --git a/core/includes/install.inc b/core/includes/install.inc
index 2cb4b212114c..d07d7d66e5c9 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -107,7 +107,7 @@ function drupal_install_profile_distribution_name() {
   }
   // At all other times, we load the profile via standard methods.
   else {
-    $profile = drupal_get_profile();
+    $profile = \Drupal::installProfile();
     $info = system_get_info('module', $profile);
   }
   return isset($info['distribution']['name']) ? $info['distribution']['name'] : 'Drupal';
@@ -132,7 +132,7 @@ function drupal_install_profile_distribution_version() {
   }
   // At all other times, we load the profile via standard methods.
   else {
-    $profile = drupal_get_profile();
+    $profile = \Drupal::installProfile();
     $info = system_get_info('module', $profile);
     return $info['version'];
   }
diff --git a/core/includes/module.inc b/core/includes/module.inc
index f83e0bd7cc35..4f298416dca8 100644
--- a/core/includes/module.inc
+++ b/core/includes/module.inc
@@ -155,9 +155,8 @@ function drupal_required_modules() {
   $required = [];
 
   // Unless called by the installer, an installation profile is required and
-  // must always be loaded. drupal_get_profile() also returns the installation
-  // profile in the installer, but only after it has been selected.
-  if ($profile = drupal_get_profile()) {
+  // must always be loaded.
+  if ($profile = \Drupal::installProfile()) {
     $required[] = $profile;
   }
 
diff --git a/core/lib/Drupal/Core/Config/InstallStorage.php b/core/lib/Drupal/Core/Config/InstallStorage.php
index c8d189e480e0..1ad7017f5d9c 100644
--- a/core/lib/Drupal/Core/Config/InstallStorage.php
+++ b/core/lib/Drupal/Core/Config/InstallStorage.php
@@ -155,7 +155,7 @@ protected function getAllFolders() {
       // yet because the system module may not yet be enabled during install.
       // @todo Remove as part of https://www.drupal.org/node/2186491
       $listing = new ExtensionDiscovery(\Drupal::root());
-      if ($profile = drupal_get_profile()) {
+      if ($profile = \Drupal::installProfile()) {
         $profile_list = $listing->scan('profile');
         if (isset($profile_list[$profile])) {
           // Prime the drupal_get_filename() static cache with the profile info
diff --git a/core/lib/Drupal/Core/Database/Install/Tasks.php b/core/lib/Drupal/Core/Database/Install/Tasks.php
index 95d4accc8f2e..7dfe5d02888d 100644
--- a/core/lib/Drupal/Core/Database/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Install/Tasks.php
@@ -242,7 +242,8 @@ public function getFormOptions(array $database) {
       '#weight' => 10,
     ];
 
-    $profile = drupal_get_profile();
+    global $install_state;
+    $profile = $install_state['parameters']['profile'];
     $db_prefix = ($profile == 'standard') ? 'drupal_' : $profile . '_';
     $form['advanced_options']['prefix'] = [
       '#type' => 'textfield',
diff --git a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
index 17c0e5b4a311..717afc3be5d4 100644
--- a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
+++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
@@ -228,7 +228,7 @@ public function scan($type, $include_tests = NULL) {
    */
   public function setProfileDirectoriesFromSettings() {
     $this->profileDirectories = [];
-    $profile = drupal_get_profile();
+    $profile = \Drupal::installProfile();
     // For SimpleTest to be able to test modules packaged together with a
     // distribution we need to include the profile of the parent site (in
     // which test runs are triggered).
diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php
index 1af310bc80eb..fccb7893fd5f 100644
--- a/core/modules/simpletest/src/TestBase.php
+++ b/core/modules/simpletest/src/TestBase.php
@@ -1108,7 +1108,7 @@ private function prepareEnvironment() {
     // Use the original files directory to avoid nesting it within an existing
     // simpletest directory if a test is executed within a test.
     $this->originalFileDirectory = Settings::get('file_public_path', $site_path . '/files');
-    $this->originalProfile = drupal_get_profile();
+    $this->originalProfile = $this->originalContainer->getParameter('install_profile');
     $this->originalUser = isset($user) ? clone $user : NULL;
 
     // Prevent that session data is leaked into the UI test runner by closing
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index a4d467d2539f..4723a73958df 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -44,7 +44,7 @@ function system_requirements($phase) {
 
     // Display the currently active installation profile, if the site
     // is not running the default installation profile.
-    $profile = drupal_get_profile();
+    $profile = \Drupal::installProfile();
     if ($profile != 'standard') {
       $info = system_get_info('module', $profile);
       $requirements['install_profile'] = [
@@ -820,7 +820,7 @@ function system_requirements($phase) {
 
   // Display an error if a newly introduced dependency in a module is not resolved.
   if ($phase == 'update') {
-    $profile = drupal_get_profile();
+    $profile = \Drupal::installProfile();
     $files = \Drupal::service('extension.list.module')->getList();
     foreach ($files as $module => $file) {
       // Ignore disabled modules and installation profiles.
diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php
index 92aed1917a03..97d59d055376 100644
--- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php
+++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php
@@ -27,6 +27,13 @@ public function testInstaller() {
     // Ensure that the timezone is correct for sites under test after installing
     // interactively.
     $this->assertEqual($this->config('system.date')->get('timezone.default'), 'Australia/Sydney');
+
+    // Ensure the profile has a weight of 1000.
+    $module_extension_list = \Drupal::service('extension.list.module');
+    $extensions = $module_extension_list->getList();
+
+    $this->assertArrayHasKey('testing', $extensions);
+    $this->assertEquals(1000, $extensions['testing']->weight);
   }
 
   /**
diff --git a/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php b/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php
index a11fd05a0141..a6366ee76079 100644
--- a/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php
+++ b/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php
@@ -56,7 +56,7 @@ public function testInstalled() {
     // Confirm that we are logged-in after installation.
     $this->assertText($this->rootUser->getAccountName());
     // Confirm that the minimal profile was installed.
-    $this->assertEqual(drupal_get_profile(), 'minimal');
+    $this->assertEqual(\Drupal::installProfile(), 'minimal');
   }
 
 }
diff --git a/core/tests/Drupal/Tests/Core/Bootstrap/DrupalGetProfileLegacyTest.php b/core/tests/Drupal/Tests/Core/Bootstrap/DrupalGetProfileLegacyTest.php
new file mode 100644
index 000000000000..4f8343e4e76c
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Bootstrap/DrupalGetProfileLegacyTest.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Drupal\Tests\Core\Bootstrap;
+
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests drupal_get_profile().
+ *
+ * @group Bootstrap
+ * @group legacy
+ * @see drupal_get_profile()
+ * @runTestsInSeparateProcesses
+ * @preserveGlobalState disabled
+ */
+class DrupalGetProfileLegacyTest extends UnitTestCase {
+
+  /**
+   * Config storage profile.
+   *
+   * @var string
+   */
+  protected $bootstrapConfigStorageProfile;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+    include $this->root . '/core/includes/bootstrap.inc';
+  }
+
+  /**
+   * Tests drupal_get_profile() deprecation.
+   *
+   * @expectedDeprecation drupal_get_profile() is deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use the install_profile container parameter or \Drupal::installProfile() instead. If you are accessing the value before it is written to configuration during the installer use the $install_state global. If you need to access the value before container is available you can use BootstrapConfigStorageFactory to load the value directly from configuration. See https://www.drupal.org/node/2538996
+   * @dataProvider providerDrupalGetProfileInstallState
+   */
+  public function testDrupalGetProfileLegacyInstallState($expected, array $install_state_array = NULL, $container_parameter = FALSE) {
+    // Set up global for install state.
+    global $install_state;
+    $install_state = $install_state_array;
+
+    // Set up the container.
+    $container = new ContainerBuilder();
+    $container->setParameter('install_profile', $container_parameter);
+    \Drupal::setContainer($container);
+
+    // Do test.
+    $this->assertEquals($expected, drupal_get_profile());
+  }
+
+  /**
+   * Data provider for testDrupalGetProfileInstallState().
+   *
+   * @return array
+   *   Test data.
+   *
+   * @see testDrupalGetProfileInstallState
+   */
+  public function providerDrupalGetProfileInstallState() {
+    $tests = [];
+    $tests['install_state_with_profile'] = [
+      'test_profile', [
+        'parameters' => [
+          'profile' => 'test_profile',
+        ],
+      ],
+    ];
+    $tests['install_state_with_no_profile_overriding_container_profile'] = [
+      NULL,
+      [
+        'parameters' => [],
+      ],
+      'test_profile',
+    ];
+    $tests['no_install_state_with_container_profile'] = [
+      'container_profile',
+      NULL,
+      'container_profile',
+    ];
+
+    return $tests;
+  }
+
+}
-- 
GitLab