Unverified Commit b097188b authored by alexpott's avatar alexpott

Issue #2881981 by foxtrotcharlie, Vidushi Mehta, vakulrai, Mile23, larowlan,...

Issue #2881981 by foxtrotcharlie, Vidushi Mehta, vakulrai, Mile23, larowlan, dawehner, heddn: Mitigate Extension dependency on DRUPAL_ROOT
parent a0608f0c
......@@ -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'];
namespace Drupal\Tests\Core\Extension;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Extension\Extension;
use Drupal\Core\DependencyInjection\ContainerBuilder;
* Tests preferred use of service application root over DRUPAL_ROOT.
* @coversDefaultClass \Drupal\Core\Extension\Extension
* @group Extension
class ExtensionUnserializedServiceAppRootUsageTest extends UnitTestCase {
* 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() {
// The assumption of our test is that DRUPAL_ROOT is not defined.
$this->assertFalse(defined('DRUPAL_ROOT'), 'Constant DRUPAL_ROOT is defined.');
$container = new ContainerBuilder();
// Set a dummy container app.root to test against.
$container->set('app.root', '/dummy/app/root');
// Instantiate an Extension object for testing unserialization.
$extension = new Extension($container->get('app.root'), 'module', 'core/modules/system/system.info.yml', 'system.module');
$data = $extension->serialize();
$this->assertEquals('/dummy/app/root', $this->readAttribute($extension, 'root'));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment