From a088ca264cac1ff5a414744f1ecabdcefa6a3cf7 Mon Sep 17 00:00:00 2001
From: foxtrotcharlie <charles@parkroad.co.za>
Date: Wed, 5 Jul 2017 21:22:24 +0200
Subject: [PATCH] Remove Extension dependency on DRUPAL_ROOT

---
 core/lib/Drupal/Core/Extension/Extension.php       |  2 +-
 ...xtensionUnserializedServiceAppRootUsageTest.php | 55 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 core/tests/Drupal/KernelTests/Core/Extension/ExtensionUnserializedServiceAppRootUsageTest.php

diff --git a/core/lib/Drupal/Core/Extension/Extension.php b/core/lib/Drupal/Core/Extension/Extension.php
index 7cb6b56be8..830c395b82 100644
--- a/core/lib/Drupal/Core/Extension/Extension.php
+++ b/core/lib/Drupal/Core/Extension/Extension.php
@@ -185,7 +185,7 @@ public function serialize() {
   public function unserialize($data) {
     $data = unserialize($data);
     // Get the app root from the container.
-    $this->root = DRUPAL_ROOT;
+    $this->root = \Drupal::hasService('app.root') ? \Drupal::root() : DRUPAL_ROOT;
     $this->type = $data['type'];
     $this->pathname = $data['pathname'];
     $this->filename = $data['filename'];
diff --git a/core/tests/Drupal/KernelTests/Core/Extension/ExtensionUnserializedServiceAppRootUsageTest.php b/core/tests/Drupal/KernelTests/Core/Extension/ExtensionUnserializedServiceAppRootUsageTest.php
new file mode 100644
index 0000000000..f32463e687
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Extension/ExtensionUnserializedServiceAppRootUsageTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Extension;
+
+use Drupal\Core\Extension\Extension;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests preferred use of service application root over DRUPAL_ROOT.
+ *
+ * @group Extension
+ */
+class ExtensionUnserializedServiceAppRootUsageTest extends KernelTestBase {
+
+  /**
+   * @var \Drupal\Core\Extension\Extension
+   */
+  protected $extension;
+
+  /**
+   * The fake app root used for testing.
+   *
+   * @var string
+   */
+  protected $dummyAppRoot;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+    // Instantiate an Extension object for testing unserialization.
+    $this->extension = new Extension($this->container->get('app.root'), 'module', 'core/modules/system/system.info.yml', 'system.module');
+    // Set a dummy container app.root for testing.
+    $this->dummyAppRoot = '/dummy/app/root';
+    $this->container->set('app.root', $this->dummyAppRoot);
+  }
+
+  /**
+   * Tests that the Extension class unserialize method uses the preferred root.
+   *
+   * When the Extension unserialize method is called on serialized Extension
+   * object data, test that the Extension object's root property is set to
+   * the container's app.root and not the DRUPAL_ROOT constant, if the service
+   * container app.root is available.
+   *
+   * @covers ::unserialize
+   */
+  public function testServiceAppRouteUsage() {
+    $data = $this->extension->serialize();
+    $this->extension->unserialize($data);
+    self::assertEquals($this->dummyAppRoot, $this->readAttribute($this->extension, 'root'));
+  }
+
+}
-- 
2.13.1

