View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestNoLabel;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\Tests\BrowserTestBase;
class InternalEntitiesTest extends BrowserTestBase {
use EntityReferenceTestTrait;
protected static $modules = [
'jsonapi',
'entity_test',
'serialization',
];
protected $testUser;
protected $internalEntity;
protected $referencingEntity;
public function setUp() {
parent::setUp();
$this->testUser = $this
->drupalCreateUser([
'access jsonapi resource list',
'view test entity',
'administer entity_test_with_bundle content',
], $this
->randomString(), TRUE);
EntityTestBundle::create([
'id' => 'internal_referencer',
'label' => 'Entity Test Internal Referencer',
])
->save();
$this
->createEntityReferenceField('entity_test_with_bundle', 'internal_referencer', 'field_internal', 'Internal Entities', 'entity_test_no_label');
$this->internalEntity = EntityTestNoLabel::create([]);
$this->internalEntity
->save();
$this->referencingEntity = EntityTestWithBundle::create([
'type' => 'internal_referencer',
'field_internal' => $this->internalEntity
->id(),
]);
$this->referencingEntity
->save();
drupal_flush_all_caches();
}
public function testEntryPoint() {
$this
->skipIfIsInternalIsNotSupported();
$document = $this
->jsonapiGet('/jsonapi');
$this
->assertArrayNotHasKey("{$this->internalEntity->getEntityTypeId()}--{$this->internalEntity->bundle()}", $document['links'], 'The entry point should not contain links to internal resource type routes.');
}
public function testRoutes() {
$this
->skipIfIsInternalIsNotSupported();
$paths = [
'individual' => $this
->getIndividual($this->internalEntity),
'collection' => $this
->jsonapiGet("/jsonapi/{$this->internalEntity->getEntityTypeId()}/{$this->internalEntity->bundle()}"),
'related' => $this
->getRelated($this->referencingEntity, 'field_internal'),
];
foreach ($paths as $type => $document) {
$this
->assertSame(404, $document['errors'][0]['status'], "The '{$type}' route should not be available for internal resource types.'");
}
}
public function testIncludes() {
$this
->skipIfIsInternalIsNotSupported();
$document = $this
->getIndividual($this->referencingEntity, [
'query' => [
'include' => 'field_internal',
],
]);
$this
->assertArrayNotHasKey('included', $document, 'Internal entities should not be included in compound documents.');
}
public function testLinks() {
$this
->skipIfIsInternalIsNotSupported();
$document = $this
->getIndividual($this->referencingEntity);
$this
->assertArrayNotHasKey('related', $document['data']['relationships']['field_internal']['links'], 'Links to internal-only related routes should not be in the document.');
}
protected function getIndividual(EntityInterface $entity, array $options = []) {
$entity_type_id = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}";
return $this
->jsonapiGet($path, $options);
}
protected function getRelated(EntityInterface $entity, $relationship, array $options = []) {
$entity_type_id = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}/{$relationship}";
return $this
->jsonapiGet($path, $options);
}
protected function jsonapiGet($path, array $options = []) {
$this
->drupalLogin($this->testUser);
$response = $this
->drupalGet($path, $options, [
'Accept' => 'application/vnd.api+json',
]);
return Json::decode($response);
}
protected function skipIfIsInternalIsNotSupported() {
if (floatval(\Drupal::VERSION) < 8.5) {
$this
->markTestSkipped('The Drupal Core version must be >= 8.5');
}
}
}