diff -u b/jsonapi.services.yml b/jsonapi.services.yml --- b/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -132,7 +132,7 @@ # Event subscribers. jsonapi.resource_response.subscriber: class: Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber - arguments: ['@serializer', '@renderer', '@logger.channel.jsonapi', '@module_handler'] + arguments: ['@serializer', '@renderer', '@logger.channel.jsonapi', '@module_handler', '@app.root'] calls: - [setValidator, []] - [setSchemaFactory, ['@?schemata.schema_factory']] # This is only injected when the service is available. diff -u b/src/EventSubscriber/ResourceResponseSubscriber.php b/src/EventSubscriber/ResourceResponseSubscriber.php --- b/src/EventSubscriber/ResourceResponseSubscriber.php +++ b/src/EventSubscriber/ResourceResponseSubscriber.php @@ -88,4 +88,11 @@ /** + * The application's root file path. + * + * @var string + */ + protected $app_root; + + /** * Constructs a ResourceResponseSubscriber object. * @@ -97,12 +104,15 @@ * The JSON API logger channel. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param string $app_root + * The application's root file path. */ - public function __construct(SerializerInterface $serializer, RendererInterface $renderer, LoggerInterface $logger, ModuleHandlerInterface $module_handler) { + public function __construct(SerializerInterface $serializer, RendererInterface $renderer, LoggerInterface $logger, ModuleHandlerInterface $module_handler, $app_root) { $this->serializer = $serializer; $this->renderer = $renderer; $this->logger = $logger; $this->moduleHandler = $module_handler; + $this->appRoot = $app_root; } /** @@ -268,7 +278,7 @@ $schema_ref = sprintf( 'file://%s/%s/%s', - DRUPAL_ROOT, + $this->appRoot, $this->moduleHandler->getModule('jsonapi')->getPath(), 'schema.json' ); diff -u b/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php b/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php --- b/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php +++ b/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php @@ -17,8 +17,6 @@ use Symfony\Component\Routing\Route; use Symfony\Component\Serializer\Serializer; -define('DRUPAL_ROOT', dirname(dirname(dirname(dirname(dirname(__DIR__)))))); - /** * @coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber * @group jsonapi @@ -36,6 +34,7 @@ * {@inheritdoc} */ public function setUp() { + parent::setUp(); // Check that the validation class is available. if (!class_exists("\\JsonSchema\\Validator")) { $this->fail('The JSON Schema validator is missing. You can install it with `composer require justinrainbow/json-schema`.'); @@ -43,15 +42,15 @@ $module_handler = $this->prophesize(ModuleHandlerInterface::class); $module = $this->prophesize(Extension::class); - $module->getPath()->willReturn('jsonapi'); - $module_handler->getModule('jsonapi')->willReturn( - $module->reveal() - ); + $module_path = substr(dirname(dirname(dirname(__DIR__))), 0, strlen($this->root)); + $module->getPath()->willReturn($module_path); + $module_handler->getModule('jsonapi')->willReturn($module->reveal()); $subscriber = new ResourceResponseSubscriber( new Serializer([], [new JsonSchemaEncoder()]), $this->prophesize(RendererInterface::class)->reveal(), $this->prophesize(LoggerInterface::class)->reveal(), - $module_handler->reveal() + $module_handler->reveal(), + $this->root ); $subscriber->setValidator(); $this->subscriber = $subscriber;