Browse Source

Drupal uri predicate (#729)

* Switch the label and summary of contextReaction map_uri_predicate.

* Switch config form to Self-reference predicate.

* Rename files to JsonldSelfReference... .

* Coding standards.
pull/743/head
dannylamb 5 years ago committed by GitHub
parent
commit
1e0b98e50a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      src/Plugin/ContextReaction/JsonldSelfReferenceReaction.php
  2. 16
      tests/src/Functional/JsonldSelfReferenceReactionTest.php
  3. 2
      tests/src/Functional/JsonldTypeAlterReactionTest.php

56
src/Plugin/ContextReaction/MappingUriPredicateReaction.php → src/Plugin/ContextReaction/JsonldSelfReferenceReaction.php

@ -13,16 +13,18 @@ use Drupal\islandora\IslandoraUtils;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Map URI to predicate context reaction. * Create a self-reference in RDF when creating JSON-LD.
*
* Formerly called "Map URI to predicate". Renamed for clarity.
* *
* @ContextReaction( * @ContextReaction(
* id = "islandora_map_uri_predicate", * id = "islandora_map_uri_predicate",
* label = @Translation("Map URI to predicate") * label = @Translation("JSON-LD self-reference")
* ) * )
*/ */
class MappingUriPredicateReaction extends NormalizerAlterReaction { class JsonldSelfReferenceReaction extends NormalizerAlterReaction {
const URI_PREDICATE = 'drupal_uri_predicate'; const SELF_REFERENCE_PREDICATE = 'drupal_uri_predicate';
/** /**
* Media source service. * Media source service.
@ -69,7 +71,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function summary() { public function summary() {
return $this->t('Map Drupal URI to configured predicate.'); return $this->t('When creating the JSON-LD for this Drupal entity, add a relationship to itself using this predicate.');
} }
/** /**
@ -77,11 +79,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
*/ */
public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) { public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) {
$config = $this->getConfiguration(); $config = $this->getConfiguration();
$drupal_predicate = $config[self::URI_PREDICATE]; $self_ref_predicate = $config[self::SELF_REFERENCE_PREDICATE];
if (!is_null($drupal_predicate) && !empty($drupal_predicate)) { if (!is_null($self_ref_predicate) && !empty($self_ref_predicate)) {
$url = $this->getSubjectUrl($entity); $url = $this->getSubjectUrl($entity);
if ($context['needs_jsonldcontext'] === FALSE) { if ($context['needs_jsonldcontext'] === FALSE) {
$drupal_predicate = NormalizerBase::escapePrefix($drupal_predicate, $context['namespaces']); $self_ref_predicate = NormalizerBase::escapePrefix($self_ref_predicate, $context['namespaces']);
} }
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) { if (isset($normalized['@graph']) && is_array($normalized['@graph'])) {
foreach ($normalized['@graph'] as &$graph) { foreach ($normalized['@graph'] as &$graph) {
@ -91,24 +93,24 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
$file = $this->mediaSource->getSourceFile($entity); $file = $this->mediaSource->getSourceFile($entity);
$graph['@id'] = $this->utils->getDownloadUrl($file); $graph['@id'] = $this->utils->getDownloadUrl($file);
} }
if (isset($graph[$drupal_predicate])) { if (isset($graph[$self_ref_predicate])) {
if (!is_array($graph[$drupal_predicate])) { if (!is_array($graph[$self_ref_predicate])) {
if ($graph[$drupal_predicate] == $url) { if ($graph[$self_ref_predicate] == $url) {
// Don't add it if it already exists. // Don't add it if it already exists.
return; return;
} }
$tmp = $graph[$drupal_predicate]; $tmp = $graph[$self_ref_predicate];
$graph[$drupal_predicate] = [$tmp]; $graph[$self_ref_predicate] = [$tmp];
} }
elseif (array_search($url, array_column($graph[$drupal_predicate], '@id'))) { elseif (array_search($url, array_column($graph[$self_ref_predicate], '@id'))) {
// Don't add it if it already exists. // Don't add it if it already exists.
return; return;
} }
} }
else { else {
$graph[$drupal_predicate] = []; $graph[$self_ref_predicate] = [];
} }
$graph[$drupal_predicate][] = ["@id" => $url]; $graph[$self_ref_predicate][] = ["@id" => $url];
return; return;
} }
} }
@ -121,11 +123,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfiguration(); $config = $this->getConfiguration();
$form[self::URI_PREDICATE] = [ $form[self::SELF_REFERENCE_PREDICATE] = [
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('Drupal URI predicate'), '#title' => $this->t('Self-reference predicate'),
'#description' => $this->t("The Drupal object's URI will be added to the resource with this predicate. Must use a defined prefix."), '#description' => $this->t("When creating the JSON-LD for this Drupal entity, add a relationship from the entity to itself using this predicate. It must use a defined RDF namespace prefix."),
'#default_value' => isset($config[self::URI_PREDICATE]) ? $config[self::URI_PREDICATE] : '', '#default_value' => isset($config[self::SELF_REFERENCE_PREDICATE]) ? $config[self::SELF_REFERENCE_PREDICATE] : '',
'#size' => 35, '#size' => 35,
]; ];
return $form; return $form;
@ -135,19 +137,19 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$drupal_predicate = $form_state->getValue(self::URI_PREDICATE); $self_ref_predicate = $form_state->getValue(self::SELF_REFERENCE_PREDICATE);
if (!is_null($drupal_predicate) and !empty($drupal_predicate)) { if (!is_null($self_ref_predicate) and !empty($self_ref_predicate)) {
if (preg_match('/^https?:\/\//', $drupal_predicate)) { if (preg_match('/^https?:\/\//', $self_ref_predicate)) {
// Can't validate all URIs so we have to trust them. // Can't validate all URIs so we have to trust them.
return; return;
} }
elseif (preg_match('/^([^\s:]+):/', $drupal_predicate, $matches)) { elseif (preg_match('/^([^\s:]+):/', $self_ref_predicate, $matches)) {
$predicate_prefix = $matches[1]; $predicate_prefix = $matches[1];
$rdf = rdf_get_namespaces(); $rdf = rdf_get_namespaces();
$rdf_prefixes = array_keys($rdf); $rdf_prefixes = array_keys($rdf);
if (!in_array($predicate_prefix, $rdf_prefixes)) { if (!in_array($predicate_prefix, $rdf_prefixes)) {
$form_state->setErrorByName( $form_state->setErrorByName(
self::URI_PREDICATE, self::SELF_REFERENCE_PREDICATE,
$this->t('Namespace prefix @prefix is not registered.', $this->t('Namespace prefix @prefix is not registered.',
['@prefix' => $predicate_prefix] ['@prefix' => $predicate_prefix]
) )
@ -156,7 +158,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
} }
else { else {
$form_state->setErrorByName( $form_state->setErrorByName(
self::URI_PREDICATE, self::SELF_REFERENCE_PREDICATE,
$this->t('Predicate must use a defined prefix or be a full URI') $this->t('Predicate must use a defined prefix or be a full URI')
); );
} }
@ -168,7 +170,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->setConfiguration([self::URI_PREDICATE => $form_state->getValue(self::URI_PREDICATE)]); $this->setConfiguration([self::SELF_REFERENCE_PREDICATE => $form_state->getValue(self::SELF_REFERENCE_PREDICATE)]);
} }
} }

16
tests/src/Functional/MappingUriPredicateReactionTest.php → tests/src/Functional/JsonldSelfReferenceReactionTest.php

@ -8,7 +8,7 @@ namespace Drupal\Tests\islandora\Functional;
* @package Drupal\Tests\islandora\Functional * @package Drupal\Tests\islandora\Functional
* @group islandora * @group islandora
*/ */
class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase { class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -37,7 +37,7 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
} }
/** /**
* @covers \Drupal\islandora\Plugin\ContextReaction\MappingUriPredicateReaction * @covers \Drupal\islandora\Plugin\ContextReaction\JsonldSelfReferenceReaction
*/ */
public function testMappingReaction() { public function testMappingReaction() {
$account = $this->drupalCreateUser([ $account = $this->drupalCreateUser([
@ -79,21 +79,21 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
$this->drupalGet("admin/structure/context/$context_name"); $this->drupalGet("admin/structure/context/$context_name");
// Can't use an undefined prefix. // Can't use an undefined prefix.
$this->getSession()->getPage() $this->getSession()->getPage()
->fillField("Drupal URI predicate", "bob:smith"); ->fillField("Self-reference predicate", "bob:smith");
$this->getSession()->getPage()->pressButton("Save and continue"); $this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession() $this->assertSession()
->pageTextContains("Namespace prefix bob is not registered"); ->pageTextContains("Namespace prefix bob is not registered");
// Can't use a straight string. // Can't use a straight string.
$this->getSession()->getPage() $this->getSession()->getPage()
->fillField("Drupal URI predicate", "woohoo"); ->fillField("Self-reference predicate", "woohoo");
$this->getSession()->getPage()->pressButton("Save and continue"); $this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession() $this->assertSession()
->pageTextContains("Predicate must use a defined prefix or be a full URI"); ->pageTextContains("Predicate must use a defined prefix or be a full URI");
// Use an existing prefix. // Use an existing prefix.
$this->getSession()->getPage() $this->getSession()->getPage()
->fillField("Drupal URI predicate", "owl:sameAs"); ->fillField("Self-reference predicate", "owl:sameAs");
$this->getSession()->getPage()->pressButton("Save and continue"); $this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession() $this->assertSession()
->pageTextContains("The context $context_name has been saved"); ->pageTextContains("The context $context_name has been saved");
@ -114,7 +114,7 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
$this->drupalGet("admin/structure/context/$context_name"); $this->drupalGet("admin/structure/context/$context_name");
// Change to a random URL. // Change to a random URL.
$this->getSession()->getPage() $this->getSession()->getPage()
->fillField("Drupal URI predicate", "http://example.org/first/second"); ->fillField("Self-reference predicate", "http://example.org/first/second");
$this->getSession()->getPage()->pressButton("Save and continue"); $this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession() $this->assertSession()
->pageTextContains("The context $context_name has been saved"); ->pageTextContains("The context $context_name has been saved");
@ -135,7 +135,7 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
} }
/** /**
* @covers \Drupal\islandora\Plugin\ContextReaction\MappingUriPredicateReaction * @covers \Drupal\islandora\Plugin\ContextReaction\JsonldSelfReferenceReaction
*/ */
public function testMappingReactionForMedia() { public function testMappingReactionForMedia() {
$account = $this->drupalCreateUser([ $account = $this->drupalCreateUser([
@ -172,7 +172,7 @@ class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
// Use an existing prefix. // Use an existing prefix.
$this->getSession()->getPage() $this->getSession()->getPage()
->fillField("Drupal URI predicate", "iana:describedby"); ->fillField("Self-reference predicate", "iana:describedby");
$this->getSession()->getPage()->pressButton("Save and continue"); $this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession() $this->assertSession()
->pageTextContains("The context $context_name has been saved"); ->pageTextContains("The context $context_name has been saved");

2
tests/src/Functional/JsonldTypeAlterReactionTest.php

@ -8,7 +8,7 @@ namespace Drupal\Tests\islandora\Functional;
* @package Drupal\Tests\islandora\Functional * @package Drupal\Tests\islandora\Functional
* @group islandora * @group islandora
*/ */
class JsonldTypeAlterReactionTest extends MappingUriPredicateReactionTest { class JsonldTypeAlterReactionTest extends JsonldSelfReferenceReactionTest {
/** /**
* @covers \Drupal\islandora\Plugin\ContextReaction\JsonldTypeAlterReaction * @covers \Drupal\islandora\Plugin\ContextReaction\JsonldTypeAlterReaction

Loading…
Cancel
Save