From 8e02b05b7a04e3a4ccf831a234e60131ae25db89 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Thu, 23 May 2019 17:56:24 -0300 Subject: [PATCH] Swapping file and media urls in jsonld (#136) --- .../MappingUriPredicateReaction.php | 3 +- .../MappingUriPredicateReactionTest.php | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/Plugin/ContextReaction/MappingUriPredicateReaction.php b/src/Plugin/ContextReaction/MappingUriPredicateReaction.php index 3fc15a9b..a5ad5880 100644 --- a/src/Plugin/ContextReaction/MappingUriPredicateReaction.php +++ b/src/Plugin/ContextReaction/MappingUriPredicateReaction.php @@ -77,9 +77,10 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction { if (isset($normalized['@graph']) && is_array($normalized['@graph'])) { foreach ($normalized['@graph'] as &$graph) { if (isset($graph['@id']) && $graph['@id'] == $url) { + // Swap media and file urls. if ($entity instanceof MediaInterface) { $file = $this->mediaSource->getSourceFile($entity); - $url = $file->url('canonical', ['absolute' => TRUE]); + $graph['@id'] = $file->url('canonical', ['absolute' => TRUE]); } if (isset($graph[$drupal_predicate])) { if (!is_array($graph[$drupal_predicate])) { diff --git a/tests/src/Functional/MappingUriPredicateReactionTest.php b/tests/src/Functional/MappingUriPredicateReactionTest.php index 31d27bd5..45ca762d 100644 --- a/tests/src/Functional/MappingUriPredicateReactionTest.php +++ b/tests/src/Functional/MappingUriPredicateReactionTest.php @@ -134,4 +134,62 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase { ); } + /** + * @covers \Drupal\islandora\Plugin\ContextReaction\MappingUriPredicateReaction + */ + public function testMappingReactionForMedia() { + $account = $this->drupalCreateUser([ + 'create media', + 'view media', + 'administer contexts', + ]); + $this->drupalLogin($account); + + $context_name = 'test'; + $reaction_id = 'islandora_map_uri_predicate'; + + list($file, $media) = $this->makeMediaAndFile($account); + $media_url = $media->url('canonical', ['absolute' => TRUE]); + $file_url = $file->url('canonical', ['absolute' => TRUE]); + + $this->drupalGet($media_url); + $this->assertSession()->statusCodeEquals(200); + + $contents = $this->drupalGet($media_url . '?_format=jsonld'); + $this->assertSession()->statusCodeEquals(200); + $json = \GuzzleHttp\json_decode($contents, TRUE); + $this->assertEquals( + "$media_url?_format=jsonld", + $json['@graph'][0]['@id'], + 'Swapped file and media urls when not configured' + ); + $this->assertArrayNotHasKey('http://www.iana.org/assignments/relation/describedby', + $json['@graph'][0], 'Has predicate when not configured'); + + $this->createContext('Test', $context_name); + $this->drupalGet("admin/structure/context/$context_name/reaction/add/$reaction_id"); + $this->assertSession()->statusCodeEquals(200); + + // Use an existing prefix. + $this->getSession()->getPage() + ->fillField("Drupal URI predicate", "iana:describedby"); + $this->getSession()->getPage()->pressButton("Save and continue"); + $this->assertSession() + ->pageTextContains("The context $context_name has been saved"); + + $new_contents = $this->drupalGet($media_url . '?_format=jsonld'); + $json = \GuzzleHttp\json_decode($new_contents, TRUE); + $this->assertEquals( + "$media_url?_format=jsonld", + $json['@graph'][0]['http://www.iana.org/assignments/relation/describedby'][0]['@value'], + 'Missing alter added predicate.' + ); + $this->assertEquals( + $file_url, + $json['@graph'][0]['@id'], + 'Alter did not swap "@id" of media with file url.' + ); + + } + }