Browse Source

Swapping file and media urls in jsonld (#136)

pull/729/head
dannylamb 6 years ago committed by Jared Whiklo
parent
commit
8e02b05b7a
  1. 3
      src/Plugin/ContextReaction/MappingUriPredicateReaction.php
  2. 58
      tests/src/Functional/MappingUriPredicateReactionTest.php

3
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])) {

58
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.'
);
}
}

Loading…
Cancel
Save