Chris@18: testUser = $this->drupalCreateUser([ Chris@18: 'view test entity', Chris@18: 'administer entity_test_with_bundle content', Chris@18: ], $this->randomString(), TRUE); Chris@18: EntityTestBundle::create([ Chris@18: 'id' => 'internal_referencer', Chris@18: 'label' => 'Entity Test Internal Referencer', Chris@18: ])->save(); Chris@18: $this->createEntityReferenceField( Chris@18: 'entity_test_with_bundle', Chris@18: 'internal_referencer', Chris@18: 'field_internal', Chris@18: 'Internal Entities', Chris@18: 'entity_test_no_label' Chris@18: ); Chris@18: $this->internalEntity = EntityTestNoLabel::create([]); Chris@18: $this->internalEntity->save(); Chris@18: $this->referencingEntity = EntityTestWithBundle::create([ Chris@18: 'type' => 'internal_referencer', Chris@18: 'field_internal' => $this->internalEntity->id(), Chris@18: ]); Chris@18: $this->referencingEntity->save(); Chris@18: drupal_flush_all_caches(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Ensures that internal resources types aren't present in the entry point. Chris@18: */ Chris@18: public function testEntryPoint() { Chris@18: $document = $this->jsonapiGet('/jsonapi'); Chris@18: $this->assertArrayNotHasKey( Chris@18: "{$this->internalEntity->getEntityTypeId()}--{$this->internalEntity->bundle()}", Chris@18: $document['links'], Chris@18: 'The entry point should not contain links to internal resource type routes.' Chris@18: ); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Ensures that internal resources types aren't present in the routes. Chris@18: */ Chris@18: public function testRoutes() { Chris@18: // This cannot be in a data provider because it needs values created by the Chris@18: // setUp method. Chris@18: $paths = [ Chris@18: 'individual' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}", Chris@18: 'collection' => "/jsonapi/entity_test_no_label/entity_test_no_label", Chris@18: 'related' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}/field_internal", Chris@18: ]; Chris@18: $this->drupalLogin($this->testUser); Chris@18: foreach ($paths as $type => $path) { Chris@18: $this->drupalGet($path, ['Accept' => 'application/vnd.api+json']); Chris@18: $this->assertSame(404, $this->getSession()->getStatusCode()); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Asserts that internal entities are not included in compound documents. Chris@18: */ Chris@18: public function testIncludes() { Chris@18: $document = $this->getIndividual($this->referencingEntity, [ Chris@18: 'query' => ['include' => 'field_internal'], Chris@18: ]); Chris@18: $this->assertArrayNotHasKey( Chris@18: 'included', Chris@18: $document, Chris@18: 'Internal entities should not be included in compound documents.' Chris@18: ); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Asserts that links to internal relationships aren't generated. Chris@18: */ Chris@18: public function testLinks() { Chris@18: $document = $this->getIndividual($this->referencingEntity); Chris@18: $this->assertArrayNotHasKey( Chris@18: 'related', Chris@18: $document['data']['relationships']['field_internal']['links'], Chris@18: 'Links to internal-only related routes should not be in the document.' Chris@18: ); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Returns the decoded JSON:API document for the for the given entity. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity to request. Chris@18: * @param array $options Chris@18: * URL options. Chris@18: * Chris@18: * @return array Chris@18: * The decoded response document. Chris@18: */ Chris@18: protected function getIndividual(EntityInterface $entity, array $options = []) { Chris@18: $entity_type_id = $entity->getEntityTypeId(); Chris@18: $bundle = $entity->bundle(); Chris@18: $path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}"; Chris@18: return $this->jsonapiGet($path, $options); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Performs an authenticated request and returns the decoded document. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity to request. Chris@18: * @param string $relationship Chris@18: * The field name of the relationship to request. Chris@18: * @param array $options Chris@18: * URL options. Chris@18: * Chris@18: * @return array Chris@18: * The decoded response document. Chris@18: */ Chris@18: protected function getRelated(EntityInterface $entity, $relationship, array $options = []) { Chris@18: $entity_type_id = $entity->getEntityTypeId(); Chris@18: $bundle = $entity->bundle(); Chris@18: $path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}/{$relationship}"; Chris@18: return $this->jsonapiGet($path, $options); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Performs an authenticated request and returns the decoded document. Chris@18: */ Chris@18: protected function jsonapiGet($path, array $options = []) { Chris@18: $this->drupalLogin($this->testUser); Chris@18: $response = $this->drupalGet($path, $options, ['Accept' => 'application/vnd.api+json']); Chris@18: return Json::decode($response); Chris@18: } Chris@18: Chris@18: }