From 88fd8ddd8228969b75826963f9433ac818cd4b00 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Fri, 16 Mar 2018 09:18:39 -0500 Subject: [PATCH] Move context controller from islandora to jsonld (#80) --- islandora.routing.yml | 10 +- src/Controller/JsonLdContextController.php | 90 ------------------ src/Tests/Web/IslandoraWebTestBase.php | 41 -------- .../Web/JsonldContextGeneratorWebTest.php | 94 ------------------- 4 files changed, 1 insertion(+), 234 deletions(-) delete mode 100644 src/Controller/JsonLdContextController.php delete mode 100644 src/Tests/Web/IslandoraWebTestBase.php delete mode 100644 src/Tests/Web/JsonldContextGeneratorWebTest.php diff --git a/islandora.routing.yml b/islandora.routing.yml index 615232ac..ef394fbd 100644 --- a/islandora.routing.yml +++ b/islandora.routing.yml @@ -7,7 +7,7 @@ system.admin_config_islandora: requirements: _permission: 'access administration pages' -# Core Islandora configuration form +# Core Islandora configuration form system.islandora_settings: path: '/admin/config/islandora/core' defaults: @@ -16,14 +16,6 @@ system.islandora_settings: requirements: _permission: 'administer site configuration' -# Islandora JSON-LD Routing definition -islandora.jsonldcontext: - path: '/context/{entity_type}/{bundle}' - defaults: - _controller: '\Drupal\islandora\Controller\JsonLdContextController::content' - requirements: - _permission: 'access content' - islandora.media_source_update: path: '/media/{media}/source' defaults: diff --git a/src/Controller/JsonLdContextController.php b/src/Controller/JsonLdContextController.php deleted file mode 100644 index abb093fb..00000000 --- a/src/Controller/JsonLdContextController.php +++ /dev/null @@ -1,90 +0,0 @@ -jsonldContextGenerator = $jsonld_context_generator; - } - - /** - * Controller's create method for dependecy injection. - * - * @param \Symfony\Component\DependencyInjection\ContainerInterface $container - * The App Container. - * - * @return static - * An instance of our jsonld.contextgenerator service. - */ - public static function create(ContainerInterface $container) { - return new static($container->get('jsonld.contextgenerator')); - } - - /** - * Returns an JSON-LD Context for a entity bundle. - * - * @param string $entity_type - * Route argument, an entity type. - * @param string $bundle - * Route argument, a bundle. - * @param \Symfony\Component\HttpFoundation\Request $request - * The Symfony Http Request. - * - * @return \Symfony\Component\HttpFoundation\Response - * An Http response. - */ - public function content($entity_type, $bundle, Request $request) { - - // TODO: expose cached/not cached through - // more varied HTTP response codes. - try { - $context = $this->jsonldContextGenerator->getContext("$entity_type.$bundle"); - $response = new CacheableJsonResponse(json_decode($context), 200); - $response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK); - $response->headers->set('X-Powered-By', 'Islandora CLAW API'); - $response->headers->set('Content-Type', 'application/ld+json'); - - // For now deal with Cache dependencies manually. - $meta = new CacheableMetadata(); - $meta->setCacheContexts(['user.permissions', 'ip', 'url']); - $meta->setCacheTags(RdfMapping::load("$entity_type.$bundle")->getCacheTags()); - $meta->setCacheMaxAge(Cache::PERMANENT); - $response->addCacheableDependency($meta); - } - catch (\Exception $e) { - $response = new Response($e->getMessage(), 400); - } - - return $response; - } - -} diff --git a/src/Tests/Web/IslandoraWebTestBase.php b/src/Tests/Web/IslandoraWebTestBase.php deleted file mode 100644 index dafd2f99..00000000 --- a/src/Tests/Web/IslandoraWebTestBase.php +++ /dev/null @@ -1,41 +0,0 @@ - 'test_type', - 'label' => 'Test Type', - ]); - $test_type->save(); - - // Give it a basic rdf mapping. - $mapping = RdfMapping::create([ - 'id' => 'node.test_type', - 'targetEntityType' => 'node', - 'bundle' => 'test_type', - 'types' => ['schema:Thing'], - 'fieldMappings' => [ - 'title' => [ - 'properties' => ['dc11:title'], - ], - ], - ]); - $mapping->save(); - - $this->user = $this->drupalCreateUser([ - 'administer site configuration', - 'administer nodes', - ]); - - // Login. - $this->drupalLogin($this->user); - } - - /** - * Tests that the Context Response Page can be reached. - */ - public function testJsonldcontextPageExists() { - $url = Url::fromRoute('islandora.jsonldcontext', ['entity_type' => 'node', 'bundle' => 'test_type']); - $this->drupalGet($url); - $this->assertResponse(200); - } - - /** - * Tests that the response is in fact application/ld+json. - */ - public function testJsonldcontextContentypeheaderResponseIsValid() { - $url = Url::fromRoute('islandora.jsonldcontext', ['entity_type' => 'node', 'bundle' => 'test_type']); - $this->drupalGet($url); - $this->assertEqual($this->drupalGetHeader('Content-Type'), 'application/ld+json', 'Correct JSON-LD mime type was returned'); - } - - /** - * Tests that the Context received has the basic structural needs. - */ - public function testJsonldcontextResponseIsValid() { - $url = Url::fromRoute('islandora.jsonldcontext', ['entity_type' => 'node', 'bundle' => 'test_type']); - $this->drupalGet($url); - $jsonldarray = json_decode($this->getRawContent(), TRUE); - // Check if the only key is "@context". - $this->assertTrue(count(array_keys($jsonldarray)) == 1 && (key($jsonldarray) == '@context'), "JSON-LD to array encoded response has just one key and that key is @context"); - } - -}