diff --git a/tests/src/Functional/InternalEntitiesTest.php b/tests/src/Functional/InternalEntitiesTest.php index 9a0ac98..aa3085e 100644 --- a/tests/src/Functional/InternalEntitiesTest.php +++ b/tests/src/Functional/InternalEntitiesTest.php @@ -38,6 +38,13 @@ class InternalEntitiesTest extends BrowserTestBase { */ protected $testUser; + /** + * An entity of an internal entity type. + * + * @var \Drupal\Core\Entity\EntityInterface + */ + protected $internalEntity; + /** * An entity referencing an internal entity. * @@ -66,16 +73,49 @@ class InternalEntitiesTest extends BrowserTestBase { 'Internal Entities', 'jsonapi_entity_test_no_label' ); - $internal_entity = EntityTestNoLabel::create([]); - $internal_entity->save(); + $this->internalEntity = EntityTestNoLabel::create([]); + $this->internalEntity->save(); $this->referencingEntity = EntityTestWithBundle::create([ 'type' => 'internal_referencer', - 'field_internal' => $internal_entity->id(), + 'field_internal' => $this->internalEntity->id(), ]); $this->referencingEntity->save(); drupal_flush_all_caches(); } + /** + * Ensures that internal resources types aren't present in the routes. + */ + public function testRoutes() { + if (!method_exists(EntityTypeInterface::class, 'isInternal')) { + $this->markTestSkipped('The Drupal Core version must be >= 8.5'); + return; + } + $this->drupalLogin($this->testUser); + $internal_entity_type_id = $this->internalEntity->getEntityTypeId(); + $internal_bundle = $this->internalEntity->bundle(); + $internal_uuid = $this->internalEntity->uuid(); + $referencing_entity_type_id = $this->referencingEntity->getEntityTypeId(); + $referencing_bundle = $this->referencingEntity->bundle(); + $referencing_uuid = $this->referencingEntity->uuid(); + // This cannot be in a data provider because it needs values created by the + // setUp method. + $paths = [ + 'individual' => "/jsonapi/{$internal_entity_type_id}/{$internal_bundle}/{$internal_uuid}", + 'collection' => "/jsonapi/{$internal_entity_type_id}/{$internal_bundle}", + 'related' => "/jsonapi/{$referencing_entity_type_id}/{$referencing_bundle}/{$referencing_uuid}/field_internal", + ]; + foreach ($paths as $type => $path) { + $response = $this->drupalGet($path, [], ['Accept' => 'application/vnd.api+json']); + $decoded = Json::decode($response); + $this->assertEquals( + 404, + $decoded['errors'][0]['status'], + "The '{$type}' route ({$path}) should not be available for internal resource types.'" + ); + } + } + /** * Asserts that internal entities are not included in compound documents. */ @@ -88,7 +128,7 @@ class InternalEntitiesTest extends BrowserTestBase { $entity_type_id = $this->referencingEntity->getEntityTypeId(); $bundle = $this->referencingEntity->bundle(); $path = "/jsonapi/{$entity_type_id}/{$bundle}/{$this->referencingEntity->uuid()}?include=field_internal"; - $response = $this->drupalGet($path); + $response = $this->drupalGet($path, [], ['Accept' => 'application/vnd.api+json']); $decoded = Json::decode($response); $this->assertArrayNotHasKey( 'included', diff --git a/tests/src/Kernel/Controller/EntityResourceTest.php b/tests/src/Kernel/Controller/EntityResourceTest.php index 950d5da..b753dd0 100644 --- a/tests/src/Kernel/Controller/EntityResourceTest.php +++ b/tests/src/Kernel/Controller/EntityResourceTest.php @@ -943,6 +943,10 @@ class EntityResourceTest extends JsonapiKernelTestBase { * The entity type ID. * @param string $bundle * The bundle. + * @param array $relatable_resource_types + * An array of relatable resource types, keyed by field. + * @param bool $internal + * Whether the primary resource type is internal. * * @return \Drupal\jsonapi\Controller\EntityResource * The resource.