Issue #2881981 by foxtrotcharlie, Vidushi Mehta, vakulrai, Mile23, larowlan, dawehner, heddn: Mitigate Extension dependency on DRUPAL_ROOT

8.7.x
Alex Pott 2018-09-18 11:41:15 +01:00
parent a0608f0cc5
commit b097188b70
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 42 additions and 1 deletions

View File

@ -185,7 +185,7 @@ class Extension implements \Serializable {
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'];

View File

@ -0,0 +1,41 @@
<?php
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');
\Drupal::setContainer($container);
// 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();
$extension->unserialize($data);
$this->assertEquals('/dummy/app/root', $this->readAttribute($extension, 'root'));
}
}