From 5b973fdf7a5a32ffbae9c44bb3dcc1be8b88a34d Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Thu, 22 Nov 2018 11:18:09 +0100 Subject: [PATCH 01/12] Support construct queries. --- driver/sparql/Connection.php | 31 +++++++++++++++++++ modules/rdf_export/src/RdfSerializer.php | 2 +- .../Driver/sparql/ConnectionInterface.php | 22 +++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/driver/sparql/Connection.php b/driver/sparql/Connection.php index b83b93e3..627a1ee5 100644 --- a/driver/sparql/Connection.php +++ b/driver/sparql/Connection.php @@ -8,6 +8,7 @@ use Drupal\rdf_entity\Database\Driver\sparql\ConnectionInterface; use Drupal\rdf_entity\Database\Driver\sparql\StatementStub; use Drupal\rdf_entity\Exception\SparqlQueryException; +use EasyRdf\Graph; use EasyRdf\Http\Exception as EasyRdfException; use EasyRdf\Sparql\Client; use EasyRdf\Sparql\Result; @@ -89,6 +90,36 @@ public function __construct(Client $easy_rdf_client, array $connection_options) * {@inheritdoc} */ public function query(string $query, array $args = [], array $options = []): Result { + return $this->doQuery($query, $args, $options); + } + + /** + * {@inheritdoc} + */ + public function constructQuery(string $query, array $args = [], array $options = []): Graph { + return $this->doQuery($query, $args, $options); + } + + /** + * Execute the query against the endpoint. + * + * @param string $query + * The string query to execute. + * @param array $args + * An array of arguments for the query. + * @param array $options + * An associative array of options to control how the query is run. + * + * @return \EasyRdf\Sparql\Result | \EasyRdf\Graph + * The query result. + * + * @throws \InvalidArgumentException + * If $args value is passed but arguments replacement is not yet + * supported. To be removed in #55. + * + * @see https://github.com/ec-europa/rdf_entity/issues/55 + */ + public function doQuery(string $query, array $args = [], array $options = []) { // @todo Remove this in #55. // @see https://github.com/ec-europa/rdf_entity/issues/55 if ($args) { diff --git a/modules/rdf_export/src/RdfSerializer.php b/modules/rdf_export/src/RdfSerializer.php index b80ac345..14e9ba7a 100644 --- a/modules/rdf_export/src/RdfSerializer.php +++ b/modules/rdf_export/src/RdfSerializer.php @@ -49,7 +49,7 @@ public function serializeEntity(RdfInterface $entity, string $format = 'turtle') SPARQL; /** @var \EasyRdf\Graph $graph */ - $graph = $this->sparqlEndpoint->query($query); + $graph = $this->sparqlEndpoint->constructQuery($query); return $graph->serialise($format); } diff --git a/src/Database/Driver/sparql/ConnectionInterface.php b/src/Database/Driver/sparql/ConnectionInterface.php index 5d447020..1bfdd194 100644 --- a/src/Database/Driver/sparql/ConnectionInterface.php +++ b/src/Database/Driver/sparql/ConnectionInterface.php @@ -5,6 +5,7 @@ namespace Drupal\rdf_entity\Database\Driver\sparql; use Drupal\Core\Database\Log; +use EasyRdf\Graph; use EasyRdf\Sparql\Client; use EasyRdf\Sparql\Result; @@ -34,6 +35,27 @@ interface ConnectionInterface { */ public function query(string $query, array $args = [], array $options = []): Result; + /** + * Executes the actual query against the Sparql endpoint. + * + * @param string $query + * The string query to execute. + * @param array $args + * An array of arguments for the query. + * @param array $options + * An associative array of options to control how the query is run. + * + * @return \EasyRdf\Sparql\Result + * The query result. + * + * @throws \InvalidArgumentException + * If $args value is passed but arguments replacement is not yet + * supported. To be removed in #55. + * + * @see https://github.com/ec-europa/rdf_entity/issues/55 + */ + public function constructQuery(string $query, array $args = [], array $options = []): Graph; + /** * Execute the actual update query against the Sparql endpoint. * From 254908a215d532b45c3ec0e30c08526294baacb4 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Thu, 22 Nov 2018 11:45:59 +0100 Subject: [PATCH 02/12] Coding standards fixes. --- driver/sparql/Connection.php | 4 +++- rdf_entity.module | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/driver/sparql/Connection.php b/driver/sparql/Connection.php index 627a1ee5..e3da290f 100644 --- a/driver/sparql/Connection.php +++ b/driver/sparql/Connection.php @@ -110,12 +110,14 @@ public function constructQuery(string $query, array $args = [], array $options = * @param array $options * An associative array of options to control how the query is run. * - * @return \EasyRdf\Sparql\Result | \EasyRdf\Graph + * @return \EasyRdf\Sparql\Result|\EasyRdf\Graph * The query result. * * @throws \InvalidArgumentException * If $args value is passed but arguments replacement is not yet * supported. To be removed in #55. + * @throws SparqlQueryException + * Exception during query execution, e.g. timeout. * * @see https://github.com/ec-europa/rdf_entity/issues/55 */ diff --git a/rdf_entity.module b/rdf_entity.module index 59989c15..19fb0438 100755 --- a/rdf_entity.module +++ b/rdf_entity.module @@ -351,7 +351,7 @@ function rdf_entity_type_mapping_submit(array &$form, FormStateInterface $form_s /** @var \Drupal\rdf_entity\RdfEntityTypeInterface $entity */ $bundle_entity = $form_state->getFormObject()->getEntity(); $mapping = RdfEntityMapping::create([ - 'entity_type_id' => $bundle_entity->getEntityType()->getBundleOf() , + 'entity_type_id' => $bundle_entity->getEntityType()->getBundleOf(), 'bundle' => $form_state->getFormObject()->getEntity()->id(), ]); } From 2cbc272eae35c850f8b3910de70a085e25f6dfd0 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Mon, 26 Nov 2018 13:53:57 +0100 Subject: [PATCH 03/12] Turn doQuery into a protected method, as this should not be external. --- driver/sparql/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/sparql/Connection.php b/driver/sparql/Connection.php index e3da290f..e0964911 100644 --- a/driver/sparql/Connection.php +++ b/driver/sparql/Connection.php @@ -121,7 +121,7 @@ public function constructQuery(string $query, array $args = [], array $options = * * @see https://github.com/ec-europa/rdf_entity/issues/55 */ - public function doQuery(string $query, array $args = [], array $options = []) { + protected function doQuery(string $query, array $args = [], array $options = []) { // @todo Remove this in #55. // @see https://github.com/ec-europa/rdf_entity/issues/55 if ($args) { From 8d9172c81d0924eb58e8c86d61447747b47b3898 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Mon, 26 Nov 2018 14:07:30 +0100 Subject: [PATCH 04/12] Improve interface documentation. --- src/Database/Driver/sparql/ConnectionInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Driver/sparql/ConnectionInterface.php b/src/Database/Driver/sparql/ConnectionInterface.php index 1bfdd194..0d36053d 100644 --- a/src/Database/Driver/sparql/ConnectionInterface.php +++ b/src/Database/Driver/sparql/ConnectionInterface.php @@ -15,7 +15,7 @@ interface ConnectionInterface { /** - * Executes the actual query against the Sparql endpoint. + * Execute a select/insert/update query, returning a query result. * * @param string $query * The string query to execute. @@ -36,7 +36,7 @@ interface ConnectionInterface { public function query(string $query, array $args = [], array $options = []): Result; /** - * Executes the actual query against the Sparql endpoint. + * Execute a construct query, returning a graph of triples. * * @param string $query * The string query to execute. From 6c9b0d5450432aedf386b9ee4f13d357000a9970 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Mon, 26 Nov 2018 14:36:41 +0100 Subject: [PATCH 05/12] Fix docblock with correct return type. --- src/Database/Driver/sparql/ConnectionInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Driver/sparql/ConnectionInterface.php b/src/Database/Driver/sparql/ConnectionInterface.php index 0d36053d..0d667657 100644 --- a/src/Database/Driver/sparql/ConnectionInterface.php +++ b/src/Database/Driver/sparql/ConnectionInterface.php @@ -45,8 +45,8 @@ public function query(string $query, array $args = [], array $options = []): Res * @param array $options * An associative array of options to control how the query is run. * - * @return \EasyRdf\Sparql\Result - * The query result. + * @return \EasyRdf\Graph + * The set of triples. * * @throws \InvalidArgumentException * If $args value is passed but arguments replacement is not yet From 0bb2e65f04402d2cb83b4b3b209655bee0b2cbc9 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Fri, 30 Nov 2018 10:02:04 +0100 Subject: [PATCH 06/12] Add content negotiation to rdf_export. --- modules/rdf_export/rdf_export.services.yml | 29 ++++++++++ modules/rdf_export/src/Encoder/RdfEncoder.php | 57 +++++++++++++++++++ .../src/EventSubscriber/RdfSubscriber.php | 37 ++++++++++++ .../src/Normalizer/NormalizerBase.php | 20 +++++++ .../src/Normalizer/RdfEntityNormalizer.php | 39 +++++++++++++ .../Driver/sparql/ConnectionInterface.php | 4 +- 6 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 modules/rdf_export/src/Encoder/RdfEncoder.php create mode 100644 modules/rdf_export/src/EventSubscriber/RdfSubscriber.php create mode 100644 modules/rdf_export/src/Normalizer/NormalizerBase.php create mode 100644 modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php diff --git a/modules/rdf_export/rdf_export.services.yml b/modules/rdf_export/rdf_export.services.yml index 857b025f..fcf86f69 100644 --- a/modules/rdf_export/rdf_export.services.yml +++ b/modules/rdf_export/rdf_export.services.yml @@ -7,3 +7,32 @@ services: rdf_export.serializer: class: Drupal\rdf_export\RdfSerializer arguments: ['@sparql_endpoint'] + rdf_export.encoder.jsonld: + class: Drupal\rdf_export\Encoder\RdfEncoder + tags: + - { name: encoder, format: jsonld } + rdf_export.encoder.rdfxml: + class: Drupal\rdf_export\Encoder\RdfEncoder + tags: + - { name: encoder, format: rdfxml } + rdf_export.encoder.ntriples: + class: Drupal\rdf_export\Encoder\RdfEncoder + tags: + - { name: encoder, format: ntriples } + rdf_export.encoder.turtle: + class: Drupal\rdf_export\Encoder\RdfEncoder + tags: + - { name: encoder, format: turtle } + rdf_export.encoder.n3: + class: Drupal\rdf_export\Encoder\RdfEncoder + tags: + - { name: encoder, format: n3 } + rdf_export.rdfsubscriber: + class: Drupal\rdf_export\EventSubscriber\RdfSubscriber + tags: + - { name: event_subscriber } + serializer.normalizer.entity.rdf: + class: Drupal\rdf_export\Normalizer\RdfEntityNormalizer + arguments: ['@rdf_export.serializer'] + tags: + - { name: normalizer, priority: 10 } \ No newline at end of file diff --git a/modules/rdf_export/src/Encoder/RdfEncoder.php b/modules/rdf_export/src/Encoder/RdfEncoder.php new file mode 100644 index 00000000..42f0a110 --- /dev/null +++ b/modules/rdf_export/src/Encoder/RdfEncoder.php @@ -0,0 +1,57 @@ +getMimeTypes()); + $event->getRequest()->setFormat($format->getName(), $mime); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[KernelEvents::REQUEST][] = ['onKernelRequest']; + return $events; + } + +} diff --git a/modules/rdf_export/src/Normalizer/NormalizerBase.php b/modules/rdf_export/src/Normalizer/NormalizerBase.php new file mode 100644 index 00000000..bcbdd819 --- /dev/null +++ b/modules/rdf_export/src/Normalizer/NormalizerBase.php @@ -0,0 +1,20 @@ +rdfSerializer = $rdfSerializer; + } + + /** + * {@inheritdoc} + */ + public function normalize($entity, $format = NULL, array $context = []) { + return ['_rdf_entity' => $this->rdfSerializer->serializeEntity($entity, $format)]; + } + +} diff --git a/src/Database/Driver/sparql/ConnectionInterface.php b/src/Database/Driver/sparql/ConnectionInterface.php index 1bfdd194..bcce0436 100644 --- a/src/Database/Driver/sparql/ConnectionInterface.php +++ b/src/Database/Driver/sparql/ConnectionInterface.php @@ -45,8 +45,8 @@ public function query(string $query, array $args = [], array $options = []): Res * @param array $options * An associative array of options to control how the query is run. * - * @return \EasyRdf\Sparql\Result - * The query result. + * @return \EasyRdf\Graph + * The set of triples. * * @throws \InvalidArgumentException * If $args value is passed but arguments replacement is not yet From d343bb4f3c35e9eec2e2e026917d6ca3d12a6c0a Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Mon, 3 Dec 2018 09:23:26 +0100 Subject: [PATCH 07/12] Newline :( --- modules/rdf_export/rdf_export.services.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/rdf_export/rdf_export.services.yml b/modules/rdf_export/rdf_export.services.yml index fcf86f69..4324e215 100644 --- a/modules/rdf_export/rdf_export.services.yml +++ b/modules/rdf_export/rdf_export.services.yml @@ -35,4 +35,4 @@ services: class: Drupal\rdf_export\Normalizer\RdfEntityNormalizer arguments: ['@rdf_export.serializer'] tags: - - { name: normalizer, priority: 10 } \ No newline at end of file + - { name: normalizer, priority: 10 } From 71713d7dc619694a88e6d58eb25b080abb8ef656 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Mon, 3 Dec 2018 17:12:06 +0200 Subject: [PATCH 08/12] Now, rdf_export depends on serialization module. --- modules/rdf_export/rdf_export.info.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/rdf_export/rdf_export.info.yml b/modules/rdf_export/rdf_export.info.yml index e0637b4a..14b66c6e 100644 --- a/modules/rdf_export/rdf_export.info.yml +++ b/modules/rdf_export/rdf_export.info.yml @@ -6,4 +6,5 @@ core: 8.x # These modules are required by the tests, must be available at bootstrap time dependencies: - - rdf_entity + - rdf_entity:rdf_entity + - drupal:serialization From 1059e01100fdbf96730fb094addce197551c8447 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 4 Dec 2018 10:21:35 +0200 Subject: [PATCH 09/12] Make it easier to add new formats. --- modules/rdf_export/rdf_export.services.yml | 13 ++++--- modules/rdf_export/src/Encoder/RdfEncoder.php | 36 ++++++++----------- .../src/EventSubscriber/RdfSubscriber.php | 2 +- .../src/Normalizer/NormalizerBase.php | 2 +- .../rdf_export/src/RdfEncoderCompilerPass.php | 33 +++++++++++++++++ .../rdf_export/src/RdfEncoderInterface.php | 22 ++++++++++++ .../src/RdfExportServiceProvider.php | 24 +++++++++++++ 7 files changed, 103 insertions(+), 29 deletions(-) create mode 100644 modules/rdf_export/src/RdfEncoderCompilerPass.php create mode 100644 modules/rdf_export/src/RdfEncoderInterface.php create mode 100644 modules/rdf_export/src/RdfExportServiceProvider.php diff --git a/modules/rdf_export/rdf_export.services.yml b/modules/rdf_export/rdf_export.services.yml index 4324e215..f1ee2e5c 100644 --- a/modules/rdf_export/rdf_export.services.yml +++ b/modules/rdf_export/rdf_export.services.yml @@ -7,24 +7,27 @@ services: rdf_export.serializer: class: Drupal\rdf_export\RdfSerializer arguments: ['@sparql_endpoint'] - rdf_export.encoder.jsonld: + rdf_export.encoder_base: + abstract: true class: Drupal\rdf_export\Encoder\RdfEncoder + rdf_export.encoder.jsonld: + parent: rdf_export.encoder_base tags: - { name: encoder, format: jsonld } rdf_export.encoder.rdfxml: - class: Drupal\rdf_export\Encoder\RdfEncoder + parent: rdf_export.encoder_base tags: - { name: encoder, format: rdfxml } rdf_export.encoder.ntriples: - class: Drupal\rdf_export\Encoder\RdfEncoder + parent: rdf_export.encoder_base tags: - { name: encoder, format: ntriples } rdf_export.encoder.turtle: - class: Drupal\rdf_export\Encoder\RdfEncoder + parent: rdf_export.encoder_base tags: - { name: encoder, format: turtle } rdf_export.encoder.n3: - class: Drupal\rdf_export\Encoder\RdfEncoder + parent: rdf_export.encoder_base tags: - { name: encoder, format: n3 } rdf_export.rdfsubscriber: diff --git a/modules/rdf_export/src/Encoder/RdfEncoder.php b/modules/rdf_export/src/Encoder/RdfEncoder.php index 42f0a110..4cdac675 100644 --- a/modules/rdf_export/src/Encoder/RdfEncoder.php +++ b/modules/rdf_export/src/Encoder/RdfEncoder.php @@ -2,38 +2,30 @@ namespace Drupal\rdf_export\Encoder; +use Drupal\rdf_export\RdfEncoderInterface; use EasyRdf\Format; -use Symfony\Component\Serializer\Encoder\EncoderInterface; /** * Adds RDF encoder support for the Serialization API. */ -class RdfEncoder implements EncoderInterface { +class RdfEncoder implements RdfEncoderInterface { /** - * The formats that this encoder supports. + * Static cache for supported formats. * - * @var array + * @var \EasyRdf\Serialiser[] */ - protected static $supportedFormats = [ - 'jsonld', - 'rdfxml', - 'ntriples', - 'turtle', - 'n3', - ]; + protected static $supportedFormats; /** * {@inheritdoc} */ public function supportsEncoding($format) { - return in_array($format, static::supportedFormats()); + return !empty(static::getSupportedFormats()[$format]); } /** * {@inheritdoc} - * - * Uses HTML-safe strings, with several characters escaped. */ public function encode($data, $format, array $context = []) { if (isset($data['_rdf_entity'])) { @@ -43,15 +35,15 @@ public function encode($data, $format, array $context = []) { } /** - * Build a list of supported formats. - * - * @return \EasyRdf\Format[] - * List of supported formats. + * {@inheritdoc} */ - public static function supportedFormats(): array { - $formats = Format::getFormats(); - /** @var \EasyRdf\Format[] $supported_formats */ - return array_intersect($formats, static::$supportedFormats); + public static function getSupportedFormats(): array { + if (!isset(static::$supportedFormats)) { + $container_registered_formats = \Drupal::getContainer()->getParameter('rdf_export.encoders'); + $rdf_serializers = Format::getFormats(); + static::$supportedFormats = array_intersect_key($rdf_serializers, $container_registered_formats); + } + return static::$supportedFormats; } } diff --git a/modules/rdf_export/src/EventSubscriber/RdfSubscriber.php b/modules/rdf_export/src/EventSubscriber/RdfSubscriber.php index 21afc188..b69e72c0 100644 --- a/modules/rdf_export/src/EventSubscriber/RdfSubscriber.php +++ b/modules/rdf_export/src/EventSubscriber/RdfSubscriber.php @@ -20,7 +20,7 @@ class RdfSubscriber implements EventSubscriberInterface { */ public function onKernelRequest(GetResponseEvent $event) { /** @var \EasyRdf\Format $format */ - foreach (RdfEncoder::supportedFormats() as $format) { + foreach (RdfEncoder::getSupportedFormats() as $format) { $mime = array_keys($format->getMimeTypes()); $event->getRequest()->setFormat($format->getName(), $mime); } diff --git a/modules/rdf_export/src/Normalizer/NormalizerBase.php b/modules/rdf_export/src/Normalizer/NormalizerBase.php index bcbdd819..c40c1cc2 100644 --- a/modules/rdf_export/src/Normalizer/NormalizerBase.php +++ b/modules/rdf_export/src/Normalizer/NormalizerBase.php @@ -14,7 +14,7 @@ abstract class NormalizerBase extends SerializationNormalizerBase { * {@inheritdoc} */ protected function checkFormat($format = NULL) { - return in_array($format, RdfEncoder::supportedFormats()); + return !empty(RdfEncoder::getSupportedFormats()[$format]); } } diff --git a/modules/rdf_export/src/RdfEncoderCompilerPass.php b/modules/rdf_export/src/RdfEncoderCompilerPass.php new file mode 100644 index 00000000..7fa2ac51 --- /dev/null +++ b/modules/rdf_export/src/RdfEncoderCompilerPass.php @@ -0,0 +1,33 @@ +findTaggedServiceIds('encoder') as $id => $attributes) { + $class = $container->getDefinition($id)->getClass(); + $interfaces = class_implements($class); + $format = $attributes[0]['format']; + if (isset($interfaces[RdfEncoderInterface::class]) && in_array($format, $rdf_formats)) { + $encoders[$format] = $format; + } + $container->setParameter('rdf_export.encoders', $encoders); + } + } + +} diff --git a/modules/rdf_export/src/RdfEncoderInterface.php b/modules/rdf_export/src/RdfEncoderInterface.php new file mode 100644 index 00000000..dee45271 --- /dev/null +++ b/modules/rdf_export/src/RdfEncoderInterface.php @@ -0,0 +1,22 @@ +addCompilerPass(new RdfEncoderCompilerPass(), PassConfig::TYPE_OPTIMIZE, -10); + } + +} From 719c500500d1e28d9d10c7f7520b6876315f780a Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 4 Dec 2018 10:55:53 +0200 Subject: [PATCH 10/12] Fix remaining QA remarks. --- modules/rdf_export/src/Encoder/RdfEncoder.php | 13 ++++++++----- .../src/Normalizer/RdfEntityNormalizer.php | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/rdf_export/src/Encoder/RdfEncoder.php b/modules/rdf_export/src/Encoder/RdfEncoder.php index 4cdac675..7367f777 100644 --- a/modules/rdf_export/src/Encoder/RdfEncoder.php +++ b/modules/rdf_export/src/Encoder/RdfEncoder.php @@ -1,9 +1,12 @@ Date: Tue, 4 Dec 2018 16:15:54 +0200 Subject: [PATCH 11/12] Add testing for rdf_export. --- .../src/Normalizer/RdfEntityNormalizer.php | 6 +- .../tests/fixtures/content-negotiation/jsonld | 1 + .../tests/fixtures/content-negotiation/n3 | 6 ++ .../fixtures/content-negotiation/ntriples | 3 + .../tests/fixtures/content-negotiation/rdfxml | 11 +++ .../tests/fixtures/content-negotiation/turtle | 6 ++ .../rdf_entity.mapping.rdf_entity.fruit.yml | 26 +++++ .../install/rdf_entity.rdfentity.fruit.yml | 7 ++ .../rest.resource.entity.rdf_entity.yml | 21 ++++ .../rdf_export_test/rdf_export_test.info.yml | 8 ++ .../tests/src/Functional/RdfExportTest.php | 98 +++++++++++++++++++ 11 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 modules/rdf_export/tests/fixtures/content-negotiation/jsonld create mode 100644 modules/rdf_export/tests/fixtures/content-negotiation/n3 create mode 100644 modules/rdf_export/tests/fixtures/content-negotiation/ntriples create mode 100644 modules/rdf_export/tests/fixtures/content-negotiation/rdfxml create mode 100644 modules/rdf_export/tests/fixtures/content-negotiation/turtle create mode 100644 modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.mapping.rdf_entity.fruit.yml create mode 100644 modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.rdfentity.fruit.yml create mode 100644 modules/rdf_export/tests/modules/rdf_export_test/config/install/rest.resource.entity.rdf_entity.yml create mode 100644 modules/rdf_export/tests/modules/rdf_export_test/rdf_export_test.info.yml create mode 100644 modules/rdf_export/tests/src/Functional/RdfExportTest.php diff --git a/modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php b/modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php index bd99d885..4fb0e794 100644 --- a/modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php +++ b/modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php @@ -23,11 +23,11 @@ class RdfEntityNormalizer extends NormalizerBase { /** * RdfEntityNormalizer constructor. * - * @param \Drupal\rdf_export\RdfSerializer $rdfSerializer + * @param \Drupal\rdf_export\RdfSerializer $rdf_serializer * RDF Serializer service. */ - public function __construct(RdfSerializer $rdfSerializer) { - $this->rdfSerializer = $rdfSerializer; + public function __construct(RdfSerializer $rdf_serializer) { + $this->rdfSerializer = $rdf_serializer; } /** diff --git a/modules/rdf_export/tests/fixtures/content-negotiation/jsonld b/modules/rdf_export/tests/fixtures/content-negotiation/jsonld new file mode 100644 index 00000000..66d30160 --- /dev/null +++ b/modules/rdf_export/tests/fixtures/content-negotiation/jsonld @@ -0,0 +1 @@ +[{"@id":"http://example.com/apple","@type":["http://example.com/type/fruit"],"http://example.com/fruit/uid":[{"@value":0}],"http://example.com/fruit/label":[{"@value":"Apple","@language":"en"}]},{"@id":"http://example.com/type/fruit"}] diff --git a/modules/rdf_export/tests/fixtures/content-negotiation/n3 b/modules/rdf_export/tests/fixtures/content-negotiation/n3 new file mode 100644 index 00000000..59a3c762 --- /dev/null +++ b/modules/rdf_export/tests/fixtures/content-negotiation/n3 @@ -0,0 +1,6 @@ +@prefix ns0: . + + + a ; + ns0:uid 0 ; + ns0:label "Apple"@en . diff --git a/modules/rdf_export/tests/fixtures/content-negotiation/ntriples b/modules/rdf_export/tests/fixtures/content-negotiation/ntriples new file mode 100644 index 00000000..2fab24a3 --- /dev/null +++ b/modules/rdf_export/tests/fixtures/content-negotiation/ntriples @@ -0,0 +1,3 @@ + . + "0"^^ . + "Apple"@en . diff --git a/modules/rdf_export/tests/fixtures/content-negotiation/rdfxml b/modules/rdf_export/tests/fixtures/content-negotiation/rdfxml new file mode 100644 index 00000000..8ec38e39 --- /dev/null +++ b/modules/rdf_export/tests/fixtures/content-negotiation/rdfxml @@ -0,0 +1,11 @@ + + + + + + 0 + Apple + + + diff --git a/modules/rdf_export/tests/fixtures/content-negotiation/turtle b/modules/rdf_export/tests/fixtures/content-negotiation/turtle new file mode 100644 index 00000000..59a3c762 --- /dev/null +++ b/modules/rdf_export/tests/fixtures/content-negotiation/turtle @@ -0,0 +1,6 @@ +@prefix ns0: . + + + a ; + ns0:uid 0 ; + ns0:label "Apple"@en . diff --git a/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.mapping.rdf_entity.fruit.yml b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.mapping.rdf_entity.fruit.yml new file mode 100644 index 00000000..687f052b --- /dev/null +++ b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.mapping.rdf_entity.fruit.yml @@ -0,0 +1,26 @@ +langcode: en +status: true +dependencies: + config: + - rdf_entity.graph.default + - rdf_entity.rdfentity.fruit +third_party_settings: { } +id: rdf_entity.fruit +entity_type_id: rdf_entity +bundle: fruit +rdf_type: 'http://example.com/type/fruit' +base_fields_mapping: + rid: + target_id: + predicate: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + format: resource + uid: + target_id: + predicate: 'http://example.com/fruit/uid' + format: 'xsd:integer' + label: + value: + predicate: 'http://example.com/fruit/label' + format: t_literal +graph: + default: 'http://example.com/fruit/published' diff --git a/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.rdfentity.fruit.yml b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.rdfentity.fruit.yml new file mode 100644 index 00000000..aca72646 --- /dev/null +++ b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rdf_entity.rdfentity.fruit.yml @@ -0,0 +1,7 @@ +langcode: en +status: true +dependencies: { } +third_party_settings: { } +name: Fruit +rid: fruit +description: '' diff --git a/modules/rdf_export/tests/modules/rdf_export_test/config/install/rest.resource.entity.rdf_entity.yml b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rest.resource.entity.rdf_entity.yml new file mode 100644 index 00000000..4a9600e3 --- /dev/null +++ b/modules/rdf_export/tests/modules/rdf_export_test/config/install/rest.resource.entity.rdf_entity.yml @@ -0,0 +1,21 @@ +langcode: en +status: true +dependencies: + module: + - rdf_export + - serialization + - user +id: entity.rdf_entity +plugin_id: 'entity:rdf_entity' +granularity: resource +configuration: + methods: + - GET + formats: + - jsonld + - rdfxml + - ntriples + - turtle + - n3 + authentication: + - cookie diff --git a/modules/rdf_export/tests/modules/rdf_export_test/rdf_export_test.info.yml b/modules/rdf_export/tests/modules/rdf_export_test/rdf_export_test.info.yml new file mode 100644 index 00000000..eceb9ae8 --- /dev/null +++ b/modules/rdf_export/tests/modules/rdf_export_test/rdf_export_test.info.yml @@ -0,0 +1,8 @@ +name: 'RDF Entity Export test' +type: module +description: 'Provides a testing RDF Entity Export module.' +core: 8.x +package: Testing +dependencies: +- rdf_export +- rest diff --git a/modules/rdf_export/tests/src/Functional/RdfExportTest.php b/modules/rdf_export/tests/src/Functional/RdfExportTest.php new file mode 100644 index 00000000..2dbb7190 --- /dev/null +++ b/modules/rdf_export/tests/src/Functional/RdfExportTest.php @@ -0,0 +1,98 @@ +setUpSparql(); + parent::setUp(); + + $this->entity = Rdf::create([ + 'rid' => 'fruit', + 'id' => 'http://example.com/apple', + 'label' => 'Apple', + ]); + $this->entity->save(); + } + + /** + * Tests the RDF export functionality. + */ + public function testRdfExport() { + $this->drupalLogin($this->drupalCreateUser(['export rdf metadata'])); + + $this->drupalGet($this->entity->toUrl('rdf-export')); + $page = $this->getSession()->getPage(); + $assert_session = $this->assertSession(); + $page->clickLink('Turtle Terse RDF Triple Language'); + $assert_session->statusCodeEquals(200); + $assert_session->responseContains('ns0:fruit "Apple"'); + + $this->drupalGet($this->entity->toUrl('rdf-export')); + $page = $this->getSession()->getPage(); + $assert_session = $this->assertSession(); + $page->clickLink('RDF/XML'); + $assert_session->statusCodeEquals(200); + $assert_session->responseContains('Apple'); + } + + /** + * Tests content negotiation. + */ + public function testContentNegotiation() { + $serializers = [ + 'jsonld' => 'application/ld+json', + 'n3' => 'text/n3; charset=UTF-8', + 'ntriples' => 'application/n-triples', + 'rdfxml' => 'application/rdf+xml', + 'turtle' => 'text/turtle; charset=UTF-8', + ]; + + $this->drupalLogin($this->drupalCreateUser(['view rdf entity'])); + + foreach ($serializers as $format => $content_type) { + $url = $this->entity->toUrl('canonical', ['query' => ['_format' => $format]]); + $this->drupalGet($url); + $content = $this->getSession()->getPage()->getContent(); + $expected_content = trim(file_get_contents(__DIR__ . "/../../fixtures/content-negotiation/$format")); + $this->assertEquals($expected_content, $content); + $this->assertSession()->responseHeaderEquals('Content-Type', $content_type); + } + } + + /** + * {@inheritdoc} + */ + public function tearDown() { + $this->entity->delete(); + parent::tearDown(); + } + +} From f66229a7900e50bc710b407bed6b93f0713c9233 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 4 Dec 2018 17:03:48 +0200 Subject: [PATCH 12/12] Provide the ml/json-ld library. --- composer.json | 3 ++- .../tests/src/Functional/RdfExportTest.php | 14 ++++++++------ tests/travis-ci/fixtures/composer.json.dist | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index c2fe6749..5eca7571 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "license": "GPL-2.0+", "require": { "php": ">=7.1", - "easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2" + "easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2", + "ml/json-ld": "^1.0" }, "autoload": { "classmap": [ diff --git a/modules/rdf_export/tests/src/Functional/RdfExportTest.php b/modules/rdf_export/tests/src/Functional/RdfExportTest.php index 2dbb7190..b126aed3 100644 --- a/modules/rdf_export/tests/src/Functional/RdfExportTest.php +++ b/modules/rdf_export/tests/src/Functional/RdfExportTest.php @@ -50,17 +50,19 @@ public function testRdfExport() { $this->drupalGet($this->entity->toUrl('rdf-export')); $page = $this->getSession()->getPage(); - $assert_session = $this->assertSession(); $page->clickLink('Turtle Terse RDF Triple Language'); - $assert_session->statusCodeEquals(200); - $assert_session->responseContains('ns0:fruit "Apple"'); + $this->assertSession()->statusCodeEquals(200); + $actual_content = $page->getContent(); + $expected_content = trim(file_get_contents(__DIR__ . "/../../fixtures/content-negotiation/turtle")); + $this->assertEquals($expected_content, $actual_content); $this->drupalGet($this->entity->toUrl('rdf-export')); $page = $this->getSession()->getPage(); - $assert_session = $this->assertSession(); $page->clickLink('RDF/XML'); - $assert_session->statusCodeEquals(200); - $assert_session->responseContains('Apple'); + $this->assertSession()->statusCodeEquals(200); + $actual_content = $page->getContent(); + $expected_content = trim(file_get_contents(__DIR__ . "/../../fixtures/content-negotiation/rdfxml")); + $this->assertEquals($expected_content, $actual_content); } /** diff --git a/tests/travis-ci/fixtures/composer.json.dist b/tests/travis-ci/fixtures/composer.json.dist index 1d22cc9c..f7150608 100644 --- a/tests/travis-ci/fixtures/composer.json.dist +++ b/tests/travis-ci/fixtures/composer.json.dist @@ -8,7 +8,8 @@ "drupal/core": "~8", "drupal/rdf_entity": "*", "drush/drush": "~9", - "easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2" + "easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2", + "ml/json-ld": "^1.0" }, "require-dev": { "behat/mink-goutte-driver": "~1.2",