diff --git a/src/Controller/EntryPoint.php b/src/Controller/EntryPoint.php index 8587ce5..c5a322f 100644 --- a/src/Controller/EntryPoint.php +++ b/src/Controller/EntryPoint.php @@ -63,19 +63,22 @@ class EntryPoint extends ControllerBase { // executing the underlying entity query. $context = new RenderContext(); /** @var \Drupal\Core\Cache\CacheableResponseInterface $response */ - $urls = $this->renderer - ->executeInRenderContext($context, function () { - $self = Url::fromRoute('jsonapi.resource_list')->setOption('absolute', TRUE); - return array_reduce($this->resourceTypeRepository->all(), function (array $carry, ResourceType $resource_type) { - // TODO: Learn how to invalidate the cache for this page when a new entity - // type or bundle gets added, removed or updated. - // $this->response->addCacheableDependency($definition); - $url = Url::fromRoute(sprintf('jsonapi.%s.collection', $resource_type->getTypeName())); - $url->setOption('absolute', TRUE); - $carry[$resource_type->getTypeName()] = $url->toString(); - return $carry; - }, ['self' => $self->toString()]); - }); + $do_build_urls = function () { + $self = Url::fromRoute('jsonapi.resource_list') + ->setOption('absolute', TRUE); + + return array_reduce($this->resourceTypeRepository->all(), function (array $carry, ResourceType $resource_type) { + // TODO: Learn how to invalidate the cache for this page when a new entity + // type or bundle gets added, removed or updated. + // $this->response->addCacheableDependency($definition); + $url = Url::fromRoute(sprintf('jsonapi.%s.collection', $resource_type->getTypeName())); + $url->setOption('absolute', TRUE); + $carry[$resource_type->getTypeName()] = $url->toString(); + + return $carry; + }, ['self' => $self->toString()]); + }; + $urls = $this->renderer->executeInRenderContext($context, $do_build_urls); if (!$context->isEmpty()) { $this->response->addCacheableDependency($context->pop()); } diff --git a/tests/src/Kernel/Controller/EntryPointTest.php b/tests/src/Kernel/Controller/EntryPointTest.php new file mode 100644 index 0000000..e538ad1 --- /dev/null +++ b/tests/src/Kernel/Controller/EntryPointTest.php @@ -0,0 +1,47 @@ +index(); + $this->assertEquals( + ['url.site'], + $processed_response->getCacheableMetadata()->getCacheContexts() + ); + $data = json_decode($processed_response->getContent(), TRUE); + $links = $data['links']; + $this->assertRegExp('/.*\/jsonapi/', $links['self']); + $this->assertRegExp('/.*\/jsonapi\/user\/user/', $links['user--user']); + $this->assertRegExp('/.*\/jsonapi\/node_type\/node_type/', $links['node_type--node_type']); + } + +}