diff --git a/core/modules/jsonld/jsonld.info b/core/modules/jsonld/jsonld.info new file mode 100644 index 0000000..70d5d40 --- /dev/null +++ b/core/modules/jsonld/jsonld.info @@ -0,0 +1,4 @@ +name = JSON-LD +description = Serializes entities using JSON-LD format. +package = Core +core = 8.x 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..6a1b510 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/DrupalJsonldNormalizerTest.php @@ -0,0 +1,99 @@ + '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(); + } + + /** + * Tests the supportsNormalization function. + */ + public function testSupportsNormalization() { + $function = 'DrupalJsonldNormalizer::supportsNormlization'; + $supportedFormat = 'drupal_jsonld'; + $unsupportedFormat = 'jsonld'; + $supportedEntity = entity_create('entity_test', array()); + $unsupportedEntity = new ConfigEntityTest(); + + // Supported entity, supported format. + $this->assertTrue($this->normalizer->supportsNormalization($supportedEntity, $supportedFormat), "$function returns TRUE for supported format."); + // Supported entity, unsupported format. + $this->assertFalse($this->normalizer->supportsNormalization($supportedEntity, $unsupportedFormat), "$function returns FALSE for unsupported format."); + // Unsupported entity, supported format. + $this->assertFalse($this->normalizer->supportsNormalization($unsupportedEntity, $supportedFormat), "$function returns FALSE for unsupported entity type."); + } + + /** + * Tests the normalize function. + */ + public function testNormalize() { + $values = array( + 'name' => $this->randomName(), + 'field_test_text' => array( + 'value' => $this->randomName(), + 'format' => 'full_html', + ), + ); + + $entity = entity_create('entity_test', $values); + $entity->save(); + + $expectedArray = array( + '@id' => $this->getEntityId($entity), + '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'], + ), + ), + ), + ); + + $normalized = $this->normalizer->normalize($entity); + // Remove extraneous array elements and test that the normalized array + // matches the expected normalized array. + $normalized = $this->prepareNormalizedArray($normalized, $values); + $this->assertEqual($normalized, $expectedArray, "DrupalJsonldNormalizer creates expected normalized array."); + } + +} 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..ed44e77 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php @@ -0,0 +1,84 @@ + 'JSON-LD Normalizer', + 'description' => "Test the JSON-LD normalizer.", + 'group' => 'JSON-LD', + ); + } + + /** + * Add the normalizer to be tested. + */ + function setUp() { + parent::setUp(); + + $this->normalizer = new JsonldNormalizer(); + } + + /** + * Tests the supportsNormalization function. + */ + public function testSupportsNormalization() { + $function = 'JsonldNormalizer::supportsNormlization'; + $supportedFormat = 'jsonld'; + $unsupportedFormat = 'drupal_jsonld'; + $supportedEntity = entity_create('entity_test', array()); + $unsupportedEntity = new ConfigEntityTest(); + + // Supported entity, supported format. + $this->assertTrue($this->normalizer->supportsNormalization($supportedEntity, $supportedFormat), "$function returns TRUE for supported format."); + // Supported entity, unsupported format. + $this->assertFalse($this->normalizer->supportsNormalization($supportedEntity, $unsupportedFormat), "$function returns FALSE for unsupported format."); + // Unsupported entity, supported format. + $this->assertFalse($this->normalizer->supportsNormalization($unsupportedEntity, $supportedFormat), "$function returns FALSE for unsupported entity type."); + } + + /** + * Tests the normalize function. + */ + public function testNormalize() { + $values = array( + 'name' => $this->randomName(), + 'field_test_text' => array( + 'value' => $this->randomName(), + 'format' => 'full_html', + ), + ); + + $entity = entity_create('entity_test', $values); + $entity->save(); + + $expectedArray = array( + '@id' => $this->getEntityId($entity), + ); + + $normalized = $this->normalizer->normalize($entity); + // Remove extraneous array elements and test that the normalized array + // matches the expected normalized array. + $normalized = $this->prepareNormalizedArray($normalized, $values); + $this->assertEqual($normalized, $expectedArray, "JsonldNormalizer creates expected normalized array."); + } + +} 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..8807514 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTestBase.php @@ -0,0 +1,59 @@ +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. + * @param array $values + * The original values that the entity was instantiated with. + * + * @return array + * Returns the array with extraneous fields removed. + */ + protected function prepareNormalizedArray($normalized, $values) { + // Implemented JSON-LD syntax tokens. + $jsonldKeys = array('@id'); + return array_intersect_key($normalized, array_merge($values, array_fill_keys($jsonldKeys, ' '))); + } +}