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 0000000000000000000000000000000000000000..6eceb62cec29f9d75808802d7288b96e6f47c5fa --- /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 0000000000000000000000000000000000000000..70b88c8af57ee2ed4136bff6e3e8ed9dc68bf263 --- /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 0000000000000000000000000000000000000000..02a055d5dc61170b97dcf62d963537d478f0e11c --- /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(); +}