diff --git a/tests/src/Functional/InternalEntitiesTest.php b/tests/src/Functional/InternalEntitiesTest.php index db8c2ce..0bd9869 100644 --- a/tests/src/Functional/InternalEntitiesTest.php +++ b/tests/src/Functional/InternalEntitiesTest.php @@ -83,6 +83,24 @@ class InternalEntitiesTest extends BrowserTestBase { drupal_flush_all_caches(); } + /** + * Ensures that internal resources types aren't present in the entry point. + */ + public function testEntryPoint() { + if (!method_exists(EntityTypeInterface::class, 'isInternal')) { + $this->markTestSkipped('The Drupal Core version must be >= 8.5'); + return; + } + $this->drupalLogin($this->testUser); + $response = $this->drupalGet('/jsonapi', [], ['Accept' => 'application/vnd.api+json']); + $decoded = Json::decode($response); + $this->assertArrayNotHasKey( + "{$this->internalEntity->getEntityTypeId()}--{$this->internalEntity->bundle()}", + $decoded['links'], + 'The entry point should not contain links to internal resource type routes.' + ); + } + /** * Ensures that internal resources types aren't present in the routes. */ diff --git a/tests/src/Kernel/Controller/EntityResourceTest.php b/tests/src/Kernel/Controller/EntityResourceTest.php index 7242c91..946853c 100644 --- a/tests/src/Kernel/Controller/EntityResourceTest.php +++ b/tests/src/Kernel/Controller/EntityResourceTest.php @@ -943,7 +943,7 @@ class EntityResourceTest extends JsonapiKernelTestBase { * The entity type ID. * @param string $bundle * The bundle. - * @param array $relatable_resource_types + * @param \Drupal\jsonapi\ResourceType\ResourceType[] $relatable_resource_types * An array of relatable resource types, keyed by field. * @param bool $internal * Whether the primary resource type is internal. diff --git a/tests/src/Kernel/Controller/EntryPointTest.php b/tests/src/Kernel/Controller/EntryPointTest.php index 7e71bff..25ebd83 100644 --- a/tests/src/Kernel/Controller/EntryPointTest.php +++ b/tests/src/Kernel/Controller/EntryPointTest.php @@ -4,8 +4,6 @@ namespace Drupal\Tests\jsonapi\Kernel\Controller; use Drupal\Core\Cache\CacheableJsonResponse; use Drupal\jsonapi\Controller\EntryPoint; -use Drupal\jsonapi\ResourceType\ResourceType; -use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface; use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase; /** @@ -29,72 +27,24 @@ class EntryPointTest extends JsonapiKernelTestBase { ]; /** - * The subject under test. - * - * @var \Drupal\jsonapi\Controller\EntryPoint - */ - protected $entryPoint; - - /** - * {@inheritdoc} + * @covers ::index */ - public function setUp() { - parent::setUp(); - $resource_repository = $this->prophesize(ResourceTypeRepositoryInterface::class); - $resource_repository->all()->willReturn([ - new ResourceType('user', 'user', NULL), - new ResourceType('node_type', 'node_type', NULL), - new ResourceType('menu', 'menu', NULL, TRUE), - ]); - $this->entryPoint = new EntryPoint( - $resource_repository->reveal(), + public function testIndex() { + $controller = new EntryPoint( + \Drupal::service('jsonapi.resource_type.repository'), \Drupal::service('renderer'), new CacheableJsonResponse() ); - } - - /** - * @covers ::index - * - * @param string $path - * The regular expression which should validate the link. - * @param string $link - * The link key to test. - * - * @dataProvider indexProvider - */ - public function testIndex($path, $link) { - $data = json_decode($this->entryPoint->index()->getContent(), TRUE); - $this->assertRegExp($path, $data['links'][$link]); - } - - /** - * Provides test cases to test entry point route generation. - */ - public function indexProvider() { - return [ - 'root' => ['path' => '/.*\/jsonapi/', 'link' => 'self'], - 'user' => ['path' => '/.*\/jsonapi\/user\/user/', 'link' => 'user--user'], - 'node_type' => ['path' => '/.*\/jsonapi\/node_type\/node_type/', 'link' => 'node_type--node_type'], - ]; - } - - /** - * Ensures that internal resources are not present in the entry point. - */ - public function testInternalResource() { - $data = json_decode($this->entryPoint->index()->getContent(), TRUE); - $this->assertArrayNotHasKey('menu--menu', $data['links']); - } - - /** - * Ensures that the site name cache context was added. - */ - public function testCacheContext() { - $this->assertArraySubset( + $processed_response = $controller->index(); + $this->assertEquals( ['url.site'], - $this->entryPoint->index()->getCacheableMetadata()->getCacheContexts() + $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']); } }