diff --git a/core/modules/jsonld/jsonld.info b/core/modules/jsonld/jsonld.info new file mode 100644 index 0000000..371969d --- /dev/null +++ b/core/modules/jsonld/jsonld.info @@ -0,0 +1,5 @@ +name = JSON-LD +description = Serializes entities using JSON-LD format. +package = Core +core = 8.x +dependencies[] = entity_test diff --git a/core/modules/jsonld/jsonld.module b/core/modules/jsonld/jsonld.module new file mode 100644 index 0000000..fdb75bf --- /dev/null +++ b/core/modules/jsonld/jsonld.module @@ -0,0 +1,17 @@ +' . t('The JSON-LD module serializes entities to the JSON-LD data format. To request JSON-LD instead of HTML, a client should add an Accept header to the request. This module will respond with JSON-LD if the Accept header value is one of the following: application/ld_json, which returns an easy-to-use data structure which is compatible with many external schemas, and application/vnd.drupal.ld+json, which is more expressive and is appropriate for content staging.', array( + '@jsonld_org' => 'http://json-ld.org/', + )) . '

'; + return $output; + } +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php new file mode 100644 index 0000000..33dab76 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php @@ -0,0 +1,27 @@ +entity->values; + unset($properties['id']); + + return $properties; + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php new file mode 100644 index 0000000..fec373b --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php @@ -0,0 +1,32 @@ +entity = $entity; + } + + /** + * Get the Entity's URI for the @id attribute. + */ + public function getId() { + $uri_info = $this->entity->uri(); + return url($uri_info['path'], array('absolute' => TRUE)); + } + + /** + * Get properties, excluding JSON-LD specific properties. + * + * Formats Entity properties in the JSON-LD array structure and removes + * unwanted values. + */ + public function getProperties() { + // @todo Add property handling based on http://drupal.org/node/1813328. + return array(); + } +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php new file mode 100644 index 0000000..6bd6db9 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php @@ -0,0 +1,68 @@ +entityWrapperClass($object); + + $attributes = $entityWrapper->getProperties(); + $attributes = array('@id' => $entityWrapper->getId()) + $attributes; + return $attributes; + } + + /** + * Checks whether the data and format are supported by this normalizer. + * + * @param mixed $data + * Data to normalize. + * @param string $format + * Format the normalization result will be encoded as. + * + * @return bool + * Returns TRUE if the normalizer can handle the request. + */ + public function supportsNormalization($data, $format = NULL) { + // If this is an Entity object and the request is for JSON-LD. + return is_object($data) && is_subclass_of($data, 'Drupal\Core\Entity\EntityNG') && static::$format === $format; + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/DrupalJsonldNormalizerTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/DrupalJsonldNormalizerTest.php new file mode 100644 index 0000000..6eceb62 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/DrupalJsonldNormalizerTest.php @@ -0,0 +1,82 @@ + 'Drupal JSON-LD Normalizer', + 'description' => "Test Drupal's vendor specific JSON-LD normalizer.", + 'group' => 'JSON-LD', + ); + } + + /** + * Add the normalizer to be tested. + */ + function setUp() { + parent::setUp(); + + $this->normalizer = new DrupalJsonldNormalizer(); + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getJsonldArray(). + * + * @return array + * Returns the expected JSON-LD array. + */ + function getJsonldArray() { + $values = $this->testValues; + return array( + '@id' => $this->getEntityId(), + 'name' => array( + LANGUAGE_NOT_SPECIFIED => array( + array( + 'value' => $values['name'], + ), + ), + ), + 'field_test_text' => array( + LANGUAGE_NOT_SPECIFIED => array( + array( + 'value' => $values['field_test_text']['value'], + 'format' => $values['field_test_text']['format'], + ), + ), + ), + ); + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getSupportedFormat(). + * + * @return string + * Returns the format token. + */ + protected function getSupportedFormat() { + return 'drupal_jsonld'; + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getUnsupportedFormat(). + * + * @return string + * Returns the format token. + */ + protected function getUnsupportedFormat() { + return 'jsonld'; + } +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php new file mode 100644 index 0000000..70b88c8 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php @@ -0,0 +1,66 @@ + 'JSON-LD Normalizer', + 'description' => "Test the JSON-LD normalizers.", + 'group' => 'JSON-LD', + ); + } + + /** + * Add the normalizer to be tested. + */ + function setUp() { + parent::setUp(); + + $this->normalizer = new JsonldNormalizer(); + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getJsonldArray(). + * + * @return array + * Returns the expected JSON-LD array. + */ + function getJsonldArray() { + return array( + '@id' => $this->getEntityId(), + ); + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getSupportedFormat(). + * + * @return string + * Returns the format token. + */ + protected function getSupportedFormat() { + return 'jsonld'; + } + + /** + * Implements Drupal\jsonld\Tests\JsonldNormalizerTestBase::getUnsupportedFormat(). + * + * @return string + * Returns the format token. + */ + protected function getUnsupportedFormat() { + return 'drupal_jsonld'; + } +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTestBase.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTestBase.php new file mode 100644 index 0000000..02a055d --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTestBase.php @@ -0,0 +1,147 @@ +testValues = array( + 'name' => $this->randomName(), + 'field_test_text' => array( + 'value' => $this->randomName(), + 'format' => 'full_html', + ), + ); + + // Create entity. + $this->testEntity = entity_create('entity_test', $this->testValues); + $this->testEntity->save(); + } + + /** + * Tests the supportsNormalization function. + */ + public function testSupportsNormalization() { + $class = get_class($this->normalizer); + // Supported entity, supported format. + $this->assertTrue($this->normalizer->supportsNormalization($this->testEntity, $this->getSupportedFormat()), "$class::supportsNormalization returns TRUE for supported format."); + // Supported entity, unsupported format. + $this->assertFalse($this->normalizer->supportsNormalization($this->testEntity, $this->getUnsupportedFormat()), "$class::supportsNormalization returns FALSE for unsupported format."); + // Unsupported entity, supported format. + $unsupportedEntity = new ConfigEntityTest(); + $this->assertFalse($this->normalizer->supportsNormalization($unsupportedEntity, $this->getSupportedFormat()), "$class::supportsNormalization returns FALSE for unsupported entity type."); + } + + /** + * Tests the normalize function. + */ + public function testNormalize() { + $class = get_class($this->normalizer); + $normalized = $this->normalizer->normalize($this->testEntity); + // Remove extraneous array elements and test that the normalized array + // matches the expected normalized array. + $normalized = $this->prepareNormalizedArray($normalized); + $this->assertEqual($normalized, $this->getJsonldArray(), "$class creates expected normalized array."); + } + + /** + * Get the Entity ID. + * + * @return string + * Return the entity URI. + */ + protected function getEntityId() { + global $base_url; + $entity = $this->testEntity; + $uriInfo = $entity->uri(); + return $base_url . '/' . $uriInfo['path']; + } + + /** + * Prepare the array returned by normalize(). + * + * Entity API and other modules add fields to the entity when it is saved, + * such as UUID and langcode. Don't test the functionality of those other + * modules here, remove extraneous fields from the array. + * + * @param array $normalized + * Normalized array. + * + * @return array + * Returns the array with extraneous fields removed. + */ + protected function prepareNormalizedArray($normalized) { + // Implemented JSON-LD syntax tokens. + $jsonldKeys = array('@id'); + return array_intersect_key($normalized, array_merge($this->testValues, array_fill_keys($jsonldKeys, ' '))); + } + + /** + * Get the array expected after normalization. + * + * This is declared as abstract because each normalizer will produce a + * differently structured array. + * + * @return array + * Returns the expected JSON-LD array. + */ + abstract protected function getJsonldArray(); + + /** + * Get the supported format. + * + * @return string + * Returns the format token. + */ + abstract protected function getSupportedFormat(); + + /** + * Get an unsupported format. + * + * @return string + * Returns the format token. + */ + abstract protected function getUnsupportedFormat(); +}