diff --git a/core/modules/jsonld/jsonld.info b/core/modules/jsonld/jsonld.info index 371969d..70d5d40 100644 --- a/core/modules/jsonld/jsonld.info +++ b/core/modules/jsonld/jsonld.info @@ -2,4 +2,3 @@ name = JSON-LD description = Serializes entities using JSON-LD format. package = Core core = 8.x -dependencies[] = entity_test diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php index 0e607c1..16cd980 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php @@ -7,6 +7,8 @@ namespace Drupal\jsonld; +use Drupal\Core\Entity\EntityNG; + /** * Provide an interface for JsonldNormalizer to get required properties. */ 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, ' '))); + } +}