Browse Source

Refactoring URL generation into util functions (#144)

* Refactoring URL generation into util functions

* Coding standards

* Ignoring schema errors on media.settings in functional tests

* Missed tests in submodules
pull/729/head
dannylamb 5 years ago committed by Jared Whiklo
parent
commit
9af957f4e3
  1. 8
      islandora.services.yml
  2. 3
      modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php
  3. 20
      modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php
  4. 3
      modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php
  5. 3
      modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php
  6. 21
      src/ContextReaction/NormalizerAlterReaction.php
  7. 19
      src/Controller/MediaSourceController.php
  8. 36
      src/EventGenerator/EventGenerator.php
  9. 30
      src/EventSubscriber/LinkHeaderSubscriber.php
  10. 3
      src/EventSubscriber/MediaLinkHeaderSubscriber.php
  11. 65
      src/EventSubscriber/NodeLinkHeaderSubscriber.php
  12. 12
      src/Flysystem/Adapter/FedoraAdapter.php
  13. 10
      src/Flysystem/Fedora.php
  14. 74
      src/IslandoraUtils.php
  15. 2
      src/Plugin/Action/AbstractGenerateDerivative.php
  16. 13
      src/Plugin/ContextReaction/MappingUriPredicateReaction.php
  17. 5
      src/PresetReaction/PresetReaction.php
  18. 3
      tests/src/Functional/GenerateDerivativeTestBase.php
  19. 26
      tests/src/Functional/IslandoraFunctionalTestBase.php
  20. 2
      tests/src/Kernel/EventGeneratorTest.php
  21. 5
      tests/src/Kernel/GeminiClientFactoryTest.php
  22. 35
      tests/src/Kernel/GeminiLookupTest.php

8
islandora.services.yml

@ -3,7 +3,7 @@
services: services:
islandora.eventgenerator: islandora.eventgenerator:
class: Drupal\islandora\EventGenerator\EventGenerator class: Drupal\islandora\EventGenerator\EventGenerator
arguments: ['@language_manager', '@islandora.media_source_service'] arguments: ['@islandora.utils', '@islandora.media_source_service']
islandora.stomp: islandora.stomp:
class: Stomp\StatefulStomp class: Stomp\StatefulStomp
factory: ['Drupal\islandora\StompFactory', create] factory: ['Drupal\islandora\StompFactory', create]
@ -16,12 +16,12 @@ services:
- { name: event_subscriber } - { name: event_subscriber }
islandora.media_link_header_subscriber: islandora.media_link_header_subscriber:
class: Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber class: Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber
arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager'] arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@islandora.utils']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
islandora.node_link_header_subscriber: islandora.node_link_header_subscriber:
class: Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber class: Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber
arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager', '@islandora.utils'] arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@islandora.utils']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
islandora.admin_view_route_subscriber: islandora.admin_view_route_subscriber:
@ -51,7 +51,7 @@ services:
arguments: ['@entity_type.manager', '@current_user', '@language_manager', '@entity.query', '@file_system', '@islandora.utils'] arguments: ['@entity_type.manager', '@current_user', '@language_manager', '@entity.query', '@file_system', '@islandora.utils']
islandora.utils: islandora.utils:
class: Drupal\islandora\IslandoraUtils class: Drupal\islandora\IslandoraUtils
arguments: ['@entity_type.manager', '@entity_field.manager', '@entity.query', '@context.manager', '@flysystem_factory'] arguments: ['@entity_type.manager', '@entity_field.manager', '@entity.query', '@context.manager', '@flysystem_factory', '@language_manager']
islandora.gemini.client: islandora.gemini.client:
class: Islandora\Crayfish\Commons\Client\GeminiClient class: Islandora\Crayfish\Commons\Client\GeminiClient
factory: ['Drupal\islandora\GeminiClientFactory', create] factory: ['Drupal\islandora\GeminiClientFactory', create]

3
modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php

@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase;
*/ */
class GenerateAudioDerivativeTest extends GenerateDerivativeTestBase { class GenerateAudioDerivativeTest extends GenerateDerivativeTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['context_ui', 'islandora_audio']; protected static $modules = ['context_ui', 'islandora_audio'];
/** /**

20
modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php

@ -24,12 +24,32 @@ class BreadcrumbsTest extends IslandoraFunctionalTestBase {
]; ];
/**
* A node.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeA; protected $nodeA;
/**
* Another node.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeB; protected $nodeB;
/**
* Yet another node.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeC; protected $nodeC;
/**
* Another one.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeD; protected $nodeD;
/** /**

3
modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php

@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase;
*/ */
class GenerateImageDerivativeTest extends GenerateDerivativeTestBase { class GenerateImageDerivativeTest extends GenerateDerivativeTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['context_ui', 'islandora_image']; protected static $modules = ['context_ui', 'islandora_image'];
/** /**

3
modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php

@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase;
*/ */
class GenerateVideoDerivativeTest extends GenerateDerivativeTestBase { class GenerateVideoDerivativeTest extends GenerateDerivativeTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['context_ui', 'islandora_video']; protected static $modules = ['context_ui', 'islandora_video'];
/** /**

21
src/ContextReaction/NormalizerAlterReaction.php

@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jsonld\Form\JsonLdSettingsForm; use Drupal\jsonld\Form\JsonLdSettingsForm;
use Drupal\islandora\IslandoraUtils;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
@ -25,16 +26,25 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme
*/ */
protected $jsonldConfig; protected $jsonldConfig;
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(array $configuration, public function __construct(array $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
ConfigFactoryInterface $config_factory) { ConfigFactoryInterface $config_factory,
IslandoraUtils $utils) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->jsonldConfig = $config_factory->get(JsonLdSettingsForm::CONFIG_NAME); $this->jsonldConfig = $config_factory->get(JsonLdSettingsForm::CONFIG_NAME);
$this->utils = $utils;
} }
/** /**
@ -45,7 +55,8 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('config.factory') $container->get('config.factory'),
$container->get('islandora.utils')
); );
} }
@ -71,11 +82,11 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme
* The url. * The url.
*/ */
protected function getSubjectUrl(EntityInterface $entity) { protected function getSubjectUrl(EntityInterface $entity) {
$url = $entity->toUrl('canonical', ['absolute' => TRUE]); $format = '';
if (!$this->jsonldConfig->get(JsonLdSettingsForm::REMOVE_JSONLD_FORMAT)) { if (!$this->jsonldConfig->get(JsonLdSettingsForm::REMOVE_JSONLD_FORMAT)) {
$url->setRouteParameter('_format', 'jsonld'); $format = 'jsonld';
} }
return $url->toString(); return $this->utils->getRestUrl($entity, $format);
} }
} }

19
src/Controller/MediaSourceController.php

@ -11,6 +11,7 @@ use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface; use Drupal\media\MediaTypeInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\TermInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\islandora\MediaSource\MediaSourceService;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -39,6 +40,13 @@ class MediaSourceController extends ControllerBase {
*/ */
protected $database; protected $database;
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/** /**
* MediaSourceController constructor. * MediaSourceController constructor.
* *
@ -46,13 +54,17 @@ class MediaSourceController extends ControllerBase {
* Service for business logic. * Service for business logic.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* Database connection. * Database connection.
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
*/ */
public function __construct( public function __construct(
MediaSourceService $service, MediaSourceService $service,
Connection $database Connection $database,
IslandoraUtils $utils
) { ) {
$this->service = $service; $this->service = $service;
$this->database = $database; $this->database = $database;
$this->utils = $utils;
} }
/** /**
@ -67,7 +79,8 @@ class MediaSourceController extends ControllerBase {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('islandora.media_source_service'), $container->get('islandora.media_source_service'),
$container->get('database') $container->get('database'),
$container->get('islandora.utils')
); );
} }
@ -162,7 +175,7 @@ class MediaSourceController extends ControllerBase {
// We return the media if it was newly created. // We return the media if it was newly created.
if ($media) { if ($media) {
$response = new Response("", 201); $response = new Response("", 201);
$response->headers->set("Location", $media->url('canonical', ['absolute' => TRUE])); $response->headers->set("Location", $this->utils->getEntityUrl($media));
} }
else { else {
$response = new Response("", 204); $response = new Response("", 204);

36
src/EventGenerator/EventGenerator.php

@ -3,8 +3,7 @@
namespace Drupal\islandora\EventGenerator; namespace Drupal\islandora\EventGenerator;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\islandora\IslandoraUtils;
use Drupal\Core\Url;
use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
@ -16,11 +15,11 @@ use Drupal\user\UserInterface;
class EventGenerator implements EventGeneratorInterface { class EventGenerator implements EventGeneratorInterface {
/** /**
* Language manager. * Islandora utils.
* *
* @var \Drupal\Core\Language\LanguageManagerInterface * @var \Drupal\islandora\IslandoraUtils
*/ */
protected $languageManager; protected $utils;
/** /**
* Media source service. * Media source service.
@ -32,13 +31,13 @@ class EventGenerator implements EventGeneratorInterface {
/** /**
* Constructor. * Constructor.
* *
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\islandora\IslandoraUtils $utils
* Language manager. * Islandora utils.
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source * @param \Drupal\islandora\MediaSource\MediaSourceService $media_source
* Media source service. * Media source service.
*/ */
public function __construct(LanguageManagerInterface $language_manager, MediaSourceService $media_source) { public function __construct(IslandoraUtils $utils, MediaSourceService $media_source) {
$this->languageManager = $language_manager; $this->utils = $utils;
$this->mediaSource = $media_source; $this->mediaSource = $media_source;
} }
@ -47,21 +46,16 @@ class EventGenerator implements EventGeneratorInterface {
*/ */
public function generateEvent(EntityInterface $entity, UserInterface $user, array $data) { public function generateEvent(EntityInterface $entity, UserInterface $user, array $data) {
$user_url = $user->toUrl()->setAbsolute()->toString(); $user_url = $this->utils->getEntityUrl($user);
$entity_type = $entity->getEntityTypeId();
$undefined = $this->languageManager->getLanguage('und'); $entity_type = $entity->getEntityTypeId();
if ($entity_type == 'file') { if ($entity_type == 'file') {
$entity_url = $entity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]); $entity_url = $this->utils->getDownloadUrl($entity);
$mimetype = $entity->getMimeType(); $mimetype = $entity->getMimeType();
} }
else { else {
$entity_url = Url::fromRoute( $entity_url = $this->utils->getEntityUrl($entity);
"rest.entity.$entity_type.GET",
[$entity_type => $entity->id()],
['absolute' => TRUE, 'language' => $undefined]
)->toString();
$mimetype = 'text/html'; $mimetype = 'text/html';
} }
@ -110,14 +104,14 @@ class EventGenerator implements EventGeneratorInterface {
$event['object']['url'][] = [ $event['object']['url'][] = [
"name" => "JSON", "name" => "JSON",
"type" => "Link", "type" => "Link",
"href" => "$entity_url?_format=json", "href" => $this->utils->getRestUrl($entity, 'json'),
"mediaType" => "application/json", "mediaType" => "application/json",
"rel" => "alternate", "rel" => "alternate",
]; ];
$event['object']['url'][] = [ $event['object']['url'][] = [
"name" => "JSONLD", "name" => "JSONLD",
"type" => "Link", "type" => "Link",
"href" => "$entity_url?_format=jsonld", "href" => $this->utils->getRestUrl($entity, 'jsonld'),
"mediaType" => "application/ld+json", "mediaType" => "application/ld+json",
"rel" => "alternate", "rel" => "alternate",
]; ];
@ -130,7 +124,7 @@ class EventGenerator implements EventGeneratorInterface {
$event['object']['url'][] = [ $event['object']['url'][] = [
"name" => "Describes", "name" => "Describes",
"type" => "Link", "type" => "Link",
"href" => $file->url('canonical', ['absolute' => TRUE, 'language' => $undefined]), "href" => $this->utils->getDownloadUrl($file),
"mediaType" => $file->getMimeType(), "mediaType" => $file->getMimeType(),
"rel" => "describes", "rel" => "describes",
]; ];

30
src/EventSubscriber/LinkHeaderSubscriber.php

@ -6,10 +6,9 @@ use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url; use Drupal\islandora\IslandoraUtils;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -66,11 +65,11 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
protected $requestStack; protected $requestStack;
/** /**
* Language manager. * Islandora utils.
* *
* @var \Drupal\Core\Language\LanguageManagerInterface * @var \Drupal\islandora\IslandoraUtils
*/ */
protected $languageManager; protected $utils;
/** /**
* Constructor. * Constructor.
@ -87,8 +86,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
* The route match object. * The route match object.
* @param Symfony\Component\HttpFoundation\RequestStack $request_stack * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack (for current request). * Request stack (for current request).
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\islandora\IslandoraUtils $utils
* Language manager. * Islandora utils.
*/ */
public function __construct( public function __construct(
EntityTypeManager $entity_type_manager, EntityTypeManager $entity_type_manager,
@ -97,7 +96,7 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
AccountInterface $account, AccountInterface $account,
RouteMatchInterface $route_match, RouteMatchInterface $route_match,
RequestStack $request_stack, RequestStack $request_stack,
LanguageManagerInterface $language_manager IslandoraUtils $utils
) { ) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
@ -107,7 +106,7 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
$this->accessManager = $access_manager; $this->accessManager = $access_manager;
$this->account = $account; $this->account = $account;
$this->requestStack = $request_stack; $this->requestStack = $request_stack;
$this->languageManager = $language_manager; $this->utils = $utils;
} }
/** /**
@ -206,6 +205,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
// Headers are subject to an access check. // Headers are subject to an access check.
if ($referencedEntity->access('view')) { if ($referencedEntity->access('view')) {
$entity_url = $this->utils->getEntityUrl($referencedEntity);
// Taxonomy terms are written out as // Taxonomy terms are written out as
// <url>; rel="tag"; title="Tag Name" // <url>; rel="tag"; title="Tag Name"
// where url is defined in field_same_as. // where url is defined in field_same_as.
@ -213,7 +214,6 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
// it becomes the taxonomy term's local uri. // it becomes the taxonomy term's local uri.
if ($referencedEntity->getEntityTypeId() == 'taxonomy_term') { if ($referencedEntity->getEntityTypeId() == 'taxonomy_term') {
$rel = "tag"; $rel = "tag";
$entity_url = $referencedEntity->url('canonical', ['absolute' => TRUE]);
if ($referencedEntity->hasField('field_external_uri')) { if ($referencedEntity->hasField('field_external_uri')) {
$external_uri = $referencedEntity->get('field_external_uri')->getValue(); $external_uri = $referencedEntity->get('field_external_uri')->getValue();
if (!empty($external_uri) && isset($external_uri[0]['uri'])) { if (!empty($external_uri) && isset($external_uri[0]['uri'])) {
@ -228,7 +228,6 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
// <url>; rel="related"; title="Field Label" // <url>; rel="related"; title="Field Label"
// and the url is the local uri. // and the url is the local uri.
$rel = "related"; $rel = "related";
$entity_url = $referencedEntity->url('canonical', ['absolute' => TRUE]);
$title = $field_definition->label(); $title = $field_definition->label();
} }
$links[] = "<$entity_url>; rel=\"$rel\"; title=\"$title\""; $links[] = "<$entity_url>; rel=\"$rel\"; title=\"$title\"";
@ -287,19 +286,16 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
continue; continue;
} }
// Skip route if the user doesn't have access.
$meta_route_name = "rest.entity.$entity_type.GET"; $meta_route_name = "rest.entity.$entity_type.GET";
$route_params = [$entity_type => $entity->id()]; $route_params = [$entity_type => $entity->id()];
if (!$this->accessManager->checkNamedRoute($meta_route_name, $route_params, $this->account)) { if (!$this->accessManager->checkNamedRoute($meta_route_name, $route_params, $this->account)) {
continue; continue;
} }
$meta_url = Url::fromRoute($meta_route_name, $route_params) $meta_url = $this->utils->getRestUrl($entity, $format);
->setAbsolute()
->toString();
$links[] = "<$meta_url?_format=$format>; rel=\"alternate\"; type=\"$mime\""; $links[] = "<$meta_url>; rel=\"alternate\"; type=\"$mime\"";
} }
} }

3
src/EventSubscriber/MediaLinkHeaderSubscriber.php

@ -77,10 +77,9 @@ class MediaLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSub
} }
// Collect file links for the media. // Collect file links for the media.
$undefined = $this->languageManager->getLanguage('und');
foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) { foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) {
if ($referencedEntity->access('view')) { if ($referencedEntity->access('view')) {
$file_url = $referencedEntity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]); $file_url = $this->utils->getDownloadUrl($referencedEntity);
$links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\""; $links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\"";
} }
} }

65
src/EventSubscriber/NodeLinkHeaderSubscriber.php

@ -2,17 +2,8 @@
namespace Drupal\islandora\EventSubscriber; namespace Drupal\islandora\EventSubscriber;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\islandora\IslandoraUtils;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
@ -22,55 +13,6 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/ */
class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubscriberInterface { class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubscriberInterface {
/**
* Derivative utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Access\AccessManagerInterface $access_manager
* The access manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
* @param Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack (for current request).
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager.
* @param \Drupal\islandora\IslandoraUtils $utils
* Derivative utils.
*/
public function __construct(
EntityTypeManager $entity_type_manager,
EntityFieldManager $entity_field_manager,
AccessManagerInterface $access_manager,
AccountInterface $account,
RouteMatchInterface $route_match,
RequestStack $request_stack,
LanguageManagerInterface $language_manager,
IslandoraUtils $utils
) {
parent::__construct(
$entity_type_manager,
$entity_field_manager,
$access_manager,
$account,
$route_match,
$request_stack,
$language_manager
);
$this->utils = $utils;
}
/** /**
* Adds node-specific link headers to appropriate responses. * Adds node-specific link headers to appropriate responses.
* *
@ -105,13 +47,8 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs
*/ */
protected function generateRelatedMediaLinks(NodeInterface $node) { protected function generateRelatedMediaLinks(NodeInterface $node) {
$links = []; $links = [];
$undefined = $this->languageManager->getLanguage('und');
foreach ($this->utils->getMedia($node) as $media) { foreach ($this->utils->getMedia($node) as $media) {
$url = Url::fromRoute( $url = $this->utils->getEntityUrl($media);
"rest.entity.media.GET",
['media' => $media->id()],
['absolute' => TRUE, 'language' => $undefined]
)->toString();
foreach ($media->referencedEntities() as $term) { foreach ($media->referencedEntities() as $term) {
if ($term->getEntityTypeId() == 'taxonomy_term' && $term->hasField('field_external_uri')) { if ($term->getEntityTypeId() == 'taxonomy_term' && $term->hasField('field_external_uri')) {
$field = $term->get('field_external_uri'); $field = $term->get('field_external_uri');

12
src/Flysystem/Adapter/FedoraAdapter.php

@ -20,7 +20,18 @@ class FedoraAdapter implements AdapterInterface {
use StreamedCopyTrait; use StreamedCopyTrait;
use NotSupportingVisibilityTrait; use NotSupportingVisibilityTrait;
/**
* Fedora client.
*
* @var \Islandora\Chullo\IFedoraApi
*/
protected $fedora; protected $fedora;
/**
* Mimetype guesser.
*
* @var \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser; protected $mimeTypeGuesser;
/** /**
@ -75,7 +86,6 @@ class FedoraAdapter implements AdapterInterface {
$meta = $this->getMetadataFromHeaders($response); $meta = $this->getMetadataFromHeaders($response);
$meta['path'] = $path; $meta['path'] = $path;
if ($meta['type'] == 'file') { if ($meta['type'] == 'file') {
$meta['stream'] = StreamWrapper::getResource($response->getBody()); $meta['stream'] = StreamWrapper::getResource($response->getBody());
} }

10
src/Flysystem/Fedora.php

@ -27,8 +27,18 @@ class Fedora implements FlysystemPluginInterface, ContainerFactoryPluginInterfac
use FlysystemUrlTrait; use FlysystemUrlTrait;
/**
* Fedora client.
*
* @var \Islandora\Chullo\IFedoraApi
*/
protected $fedora; protected $fedora;
/**
* Mimetype guesser.
*
* @var \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser; protected $mimeTypeGuesser;
/** /**

74
src/IslandoraUtils.php

@ -4,12 +4,15 @@ namespace Drupal\islandora;
use Drupal\context\ContextManager; use Drupal\context\ContextManager;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\Query\QueryException; use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\flysystem\FlysystemFactory; use Drupal\flysystem\FlysystemFactory;
use Drupal\islandora\ContextProvider\NodeContextProvider; use Drupal\islandora\ContextProvider\NodeContextProvider;
@ -64,6 +67,13 @@ class IslandoraUtils {
*/ */
protected $flysystemFactory; protected $flysystemFactory;
/**
* Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/** /**
* Constructor. * Constructor.
* *
@ -77,19 +87,23 @@ class IslandoraUtils {
* Context manager. * Context manager.
* @param \Drupal\flysystem\FlysystemFactory $flysystem_factory * @param \Drupal\flysystem\FlysystemFactory $flysystem_factory
* Flysystem factory. * Flysystem factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager.
*/ */
public function __construct( public function __construct(
EntityTypeManager $entity_type_manager, EntityTypeManager $entity_type_manager,
EntityFieldManager $entity_field_manager, EntityFieldManager $entity_field_manager,
QueryFactory $entity_query, QueryFactory $entity_query,
ContextManager $context_manager, ContextManager $context_manager,
FlysystemFactory $flysystem_factory FlysystemFactory $flysystem_factory,
LanguageManagerInterface $language_manager
) { ) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
$this->entityQuery = $entity_query; $this->entityQuery = $entity_query;
$this->contextManager = $context_manager; $this->contextManager = $context_manager;
$this->flysystemFactory = $flysystem_factory; $this->flysystemFactory = $flysystem_factory;
$this->languageManager = $language_manager;
} }
/** /**
@ -498,4 +512,62 @@ class IslandoraUtils {
return $condition; return $condition;
} }
/**
* Gets the id URL of an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose URL you want.
*
* @return string
* The entity URL.
*/
public function getEntityUrl(EntityInterface $entity) {
$undefined = $this->languageManager->getLanguage('und');
$entity_type = $entity->getEntityTypeId();
return Url::fromRoute(
"entity.$entity_type.canonical",
[$entity_type => $entity->id()],
['absolute' => TRUE, 'language' => $undefined]
)->toString();
}
/**
* Gets the downloadable URL for a file.
*
* @param \Drupal\file\FileInterface $file
* The file whose URL you want.
*
* @return string
* The file URL.
*/
public function getDownloadUrl(FileInterface $file) {
$undefined = $this->languageManager->getLanguage('und');
return $file->url('canonical', ['absolute' => TRUE, 'language' => $undefined]);
}
/**
* Gets the URL for an entity's REST endpoint.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose REST endpoint you want.
* @param string $format
* REST serialization format.
*
* @return string
* The REST URL.
*/
public function getRestUrl(EntityInterface $entity, $format = '') {
$undefined = $this->languageManager->getLanguage('und');
$entity_type = $entity->getEntityTypeId();
$rest_url = Url::fromRoute(
"rest.entity.$entity_type.GET",
[$entity_type => $entity->id()],
['absolute' => TRUE, 'language' => $undefined]
)->toString();
if (!empty($format)) {
$rest_url .= "?_format=$format";
}
return $rest_url;
}
} }

2
src/Plugin/Action/AbstractGenerateDerivative.php

@ -155,7 +155,7 @@ class AbstractGenerateDerivative extends EmitEvent {
throw new \RuntimeException("Could not locate source file for media {$source_media->id()}", 500); throw new \RuntimeException("Could not locate source file for media {$source_media->id()}", 500);
} }
$data['source_uri'] = $source_file->url('canonical', ['absolute' => TRUE]); $data['source_uri'] = $this->utils->getDownloadUrl($source_file);
// Find the term for the derivative and use it to set the destination url // Find the term for the derivative and use it to set the destination url
// in the data array. // in the data array.

13
src/Plugin/ContextReaction/MappingUriPredicateReaction.php

@ -9,6 +9,7 @@ use Drupal\islandora\ContextReaction\NormalizerAlterReaction;
use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\jsonld\Normalizer\NormalizerBase; use Drupal\jsonld\Normalizer\NormalizerBase;
use Drupal\media\MediaInterface; use Drupal\media\MediaInterface;
use Drupal\islandora\IslandoraUtils;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
@ -23,6 +24,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
const URI_PREDICATE = 'drupal_uri_predicate'; const URI_PREDICATE = 'drupal_uri_predicate';
/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
protected $mediaSource; protected $mediaSource;
/** /**
@ -32,13 +38,15 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
ConfigFactoryInterface $config_factory, ConfigFactoryInterface $config_factory,
IslandoraUtils $utils,
MediaSourceService $media_source) { MediaSourceService $media_source) {
parent::__construct( parent::__construct(
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$config_factory $config_factory,
$utils
); );
$this->mediaSource = $media_source; $this->mediaSource = $media_source;
} }
@ -52,6 +60,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('config.factory'), $container->get('config.factory'),
$container->get('islandora.utils'),
$container->get('islandora.media_source_service') $container->get('islandora.media_source_service')
); );
} }
@ -80,7 +89,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
// Swap media and file urls. // Swap media and file urls.
if ($entity instanceof MediaInterface) { if ($entity instanceof MediaInterface) {
$file = $this->mediaSource->getSourceFile($entity); $file = $this->mediaSource->getSourceFile($entity);
$graph['@id'] = $file->url('canonical', ['absolute' => TRUE]); $graph['@id'] = $this->utils->getDownloadUrl($file);
} }
if (isset($graph[$drupal_predicate])) { if (isset($graph[$drupal_predicate])) {
if (!is_array($graph[$drupal_predicate])) { if (!is_array($graph[$drupal_predicate])) {

5
src/PresetReaction/PresetReaction.php

@ -14,6 +14,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/ */
class PresetReaction extends ContextReactionPluginBase implements ContainerFactoryPluginInterface { class PresetReaction extends ContextReactionPluginBase implements ContainerFactoryPluginInterface {
/**
* Action storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $actionStorage; protected $actionStorage;
/** /**

3
tests/src/Functional/GenerateDerivativeTestBase.php

@ -7,6 +7,9 @@ namespace Drupal\Tests\islandora\Functional;
*/ */
abstract class GenerateDerivativeTestBase extends IslandoraFunctionalTestBase { abstract class GenerateDerivativeTestBase extends IslandoraFunctionalTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['context_ui']; protected static $modules = ['context_ui'];
/** /**

26
tests/src/Functional/IslandoraFunctionalTestBase.php

@ -20,8 +20,14 @@ class IslandoraFunctionalTestBase extends BrowserTestBase {
use TestFileCreationTrait; use TestFileCreationTrait;
use MediaTypeCreationTrait; use MediaTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['context_ui', 'field_ui', 'islandora']; protected static $modules = ['context_ui', 'field_ui', 'islandora'];
/**
* {@inheritdoc}
*/
protected static $configSchemaCheckerExclusions = [ protected static $configSchemaCheckerExclusions = [
'jwt.config', 'jwt.config',
'context.context.test', 'context.context.test',
@ -29,12 +35,28 @@ class IslandoraFunctionalTestBase extends BrowserTestBase {
'context.context.media', 'context.context.media',
'context.context.file', 'context.context.file',
'key.key.test', 'key.key.test',
'media.settings',
]; ];
/**
* Test node type.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $testType; protected $testType;
/**
* Test media type.
*
* @var \Drupal\media\Entity\MediaType
*/
protected $testMediaType; protected $testMediaType;
/**
* Test vocabulary.
*
* @var \Drupal\taxonomy\Entity\Vocabulary
*/
protected $testVocabulary; protected $testVocabulary;
/** /**
@ -170,6 +192,10 @@ EOD;
$destination->write($name, $source->read($name)); $destination->write($name, $source->read($name));
} }
$media_settings = $this->container->get('config.factory')->getEditable('media.settings');
$media_settings->set('standalone_url', TRUE);
$media_settings->save(TRUE);
// Cache clear / rebuild. // Cache clear / rebuild.
drupal_flush_all_caches(); drupal_flush_all_caches();
$this->container->get('router.builder')->rebuild(); $this->container->get('router.builder')->rebuild();

2
tests/src/Kernel/EventGeneratorTest.php

@ -65,7 +65,7 @@ class EventGeneratorTest extends IslandoraKernelTestBase {
// Create the event generator so we can test it. // Create the event generator so we can test it.
$this->eventGenerator = new EventGenerator( $this->eventGenerator = new EventGenerator(
$this->container->get('language_manager'), $this->container->get('islandora.utils'),
$this->container->get('islandora.media_source_service') $this->container->get('islandora.media_source_service')
); );
} }

5
tests/src/Kernel/GeminiClientFactoryTest.php

@ -18,6 +18,11 @@ use Psr\Log\LoggerInterface;
*/ */
class GeminiClientFactoryTest extends IslandoraKernelTestBase { class GeminiClientFactoryTest extends IslandoraKernelTestBase {
/**
* Logger.
*
* @var \Psr\Log\LoggerInterface
*/
private $logger; private $logger;
/** /**

35
tests/src/Kernel/GeminiLookupTest.php

@ -23,18 +23,53 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
*/ */
class GeminiLookupTest extends IslandoraKernelTestBase { class GeminiLookupTest extends IslandoraKernelTestBase {
/**
* JWT Auth.
*
* @var \Drupal\jwt\Authentication\Provider\JwtAuth
*/
private $jwtAuth; private $jwtAuth;
/**
* Logger.
*
* @var \Psr\Log\LoggerInterface
*/
private $logger; private $logger;
/**
* Guzzle.
*
* @var \GuzzleHttp\Client
*/
private $guzzle; private $guzzle;
/**
* Gemini client.
*
* @var \Islandora\Crayfish\Commons\Client\GeminiClient
*/
private $geminiClient; private $geminiClient;
/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
private $mediaSource; private $mediaSource;
/**
* An entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
private $entity; private $entity;
/**
* A media.
*
* @var \Drupal\media\MediaInterface
*/
private $media; private $media;
/** /**

Loading…
Cancel
Save