annotate core/modules/jsonapi/tests/src/Functional/InternalEntitiesTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\Tests\jsonapi\Functional;
Chris@5 4
Chris@5 5 use Drupal\Component\Serialization\Json;
Chris@5 6 use Drupal\Core\Entity\EntityInterface;
Chris@5 7 use Drupal\entity_test\Entity\EntityTestBundle;
Chris@5 8 use Drupal\entity_test\Entity\EntityTestNoLabel;
Chris@5 9 use Drupal\entity_test\Entity\EntityTestWithBundle;
Chris@5 10 use Drupal\Tests\BrowserTestBase;
Chris@5 11 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
Chris@5 12
Chris@5 13 /**
Chris@5 14 * Makes assertions about the JSON:API behavior for internal entities.
Chris@5 15 *
Chris@5 16 * @group jsonapi
Chris@5 17 *
Chris@5 18 * @internal
Chris@5 19 */
Chris@5 20 class InternalEntitiesTest extends BrowserTestBase {
Chris@5 21
Chris@5 22 use EntityReferenceTestTrait;
Chris@5 23
Chris@5 24 /**
Chris@5 25 * {@inheritdoc}
Chris@5 26 */
Chris@5 27 protected static $modules = [
Chris@5 28 'jsonapi',
Chris@5 29 'entity_test',
Chris@5 30 'serialization',
Chris@5 31 ];
Chris@5 32
Chris@5 33 /**
Chris@5 34 * A test user.
Chris@5 35 *
Chris@5 36 * @var \Drupal\user\UserInterface
Chris@5 37 */
Chris@5 38 protected $testUser;
Chris@5 39
Chris@5 40 /**
Chris@5 41 * An entity of an internal entity type.
Chris@5 42 *
Chris@5 43 * @var \Drupal\Core\Entity\EntityInterface
Chris@5 44 */
Chris@5 45 protected $internalEntity;
Chris@5 46
Chris@5 47 /**
Chris@5 48 * An entity referencing an internal entity.
Chris@5 49 *
Chris@5 50 * @var \Drupal\Core\Entity\EntityInterface
Chris@5 51 */
Chris@5 52 protected $referencingEntity;
Chris@5 53
Chris@5 54 /**
Chris@5 55 * {@inheritdoc}
Chris@5 56 */
Chris@5 57 public function setUp() {
Chris@5 58 parent::setUp();
Chris@5 59 $this->testUser = $this->drupalCreateUser([
Chris@5 60 'view test entity',
Chris@5 61 'administer entity_test_with_bundle content',
Chris@5 62 ], $this->randomString(), TRUE);
Chris@5 63 EntityTestBundle::create([
Chris@5 64 'id' => 'internal_referencer',
Chris@5 65 'label' => 'Entity Test Internal Referencer',
Chris@5 66 ])->save();
Chris@5 67 $this->createEntityReferenceField(
Chris@5 68 'entity_test_with_bundle',
Chris@5 69 'internal_referencer',
Chris@5 70 'field_internal',
Chris@5 71 'Internal Entities',
Chris@5 72 'entity_test_no_label'
Chris@5 73 );
Chris@5 74 $this->internalEntity = EntityTestNoLabel::create([]);
Chris@5 75 $this->internalEntity->save();
Chris@5 76 $this->referencingEntity = EntityTestWithBundle::create([
Chris@5 77 'type' => 'internal_referencer',
Chris@5 78 'field_internal' => $this->internalEntity->id(),
Chris@5 79 ]);
Chris@5 80 $this->referencingEntity->save();
Chris@5 81 drupal_flush_all_caches();
Chris@5 82 }
Chris@5 83
Chris@5 84 /**
Chris@5 85 * Ensures that internal resources types aren't present in the entry point.
Chris@5 86 */
Chris@5 87 public function testEntryPoint() {
Chris@5 88 $document = $this->jsonapiGet('/jsonapi');
Chris@5 89 $this->assertArrayNotHasKey(
Chris@5 90 "{$this->internalEntity->getEntityTypeId()}--{$this->internalEntity->bundle()}",
Chris@5 91 $document['links'],
Chris@5 92 'The entry point should not contain links to internal resource type routes.'
Chris@5 93 );
Chris@5 94 }
Chris@5 95
Chris@5 96 /**
Chris@5 97 * Ensures that internal resources types aren't present in the routes.
Chris@5 98 */
Chris@5 99 public function testRoutes() {
Chris@5 100 // This cannot be in a data provider because it needs values created by the
Chris@5 101 // setUp method.
Chris@5 102 $paths = [
Chris@5 103 'individual' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}",
Chris@5 104 'collection' => "/jsonapi/entity_test_no_label/entity_test_no_label",
Chris@5 105 'related' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}/field_internal",
Chris@5 106 ];
Chris@5 107 $this->drupalLogin($this->testUser);
Chris@5 108 foreach ($paths as $type => $path) {
Chris@5 109 $this->drupalGet($path, ['Accept' => 'application/vnd.api+json']);
Chris@5 110 $this->assertSame(404, $this->getSession()->getStatusCode());
Chris@5 111 }
Chris@5 112 }
Chris@5 113
Chris@5 114 /**
Chris@5 115 * Asserts that internal entities are not included in compound documents.
Chris@5 116 */
Chris@5 117 public function testIncludes() {
Chris@5 118 $document = $this->getIndividual($this->referencingEntity, [
Chris@5 119 'query' => ['include' => 'field_internal'],
Chris@5 120 ]);
Chris@5 121 $this->assertArrayNotHasKey(
Chris@5 122 'included',
Chris@5 123 $document,
Chris@5 124 'Internal entities should not be included in compound documents.'
Chris@5 125 );
Chris@5 126 }
Chris@5 127
Chris@5 128 /**
Chris@5 129 * Asserts that links to internal relationships aren't generated.
Chris@5 130 */
Chris@5 131 public function testLinks() {
Chris@5 132 $document = $this->getIndividual($this->referencingEntity);
Chris@5 133 $this->assertArrayNotHasKey(
Chris@5 134 'related',
Chris@5 135 $document['data']['relationships']['field_internal']['links'],
Chris@5 136 'Links to internal-only related routes should not be in the document.'
Chris@5 137 );
Chris@5 138 }
Chris@5 139
Chris@5 140 /**
Chris@5 141 * Returns the decoded JSON:API document for the for the given entity.
Chris@5 142 *
Chris@5 143 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@5 144 * The entity to request.
Chris@5 145 * @param array $options
Chris@5 146 * URL options.
Chris@5 147 *
Chris@5 148 * @return array
Chris@5 149 * The decoded response document.
Chris@5 150 */
Chris@5 151 protected function getIndividual(EntityInterface $entity, array $options = []) {
Chris@5 152 $entity_type_id = $entity->getEntityTypeId();
Chris@5 153 $bundle = $entity->bundle();
Chris@5 154 $path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}";
Chris@5 155 return $this->jsonapiGet($path, $options);
Chris@5 156 }
Chris@5 157
Chris@5 158 /**
Chris@5 159 * Performs an authenticated request and returns the decoded document.
Chris@5 160 *
Chris@5 161 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@5 162 * The entity to request.
Chris@5 163 * @param string $relationship
Chris@5 164 * The field name of the relationship to request.
Chris@5 165 * @param array $options
Chris@5 166 * URL options.
Chris@5 167 *
Chris@5 168 * @return array
Chris@5 169 * The decoded response document.
Chris@5 170 */
Chris@5 171 protected function getRelated(EntityInterface $entity, $relationship, array $options = []) {
Chris@5 172 $entity_type_id = $entity->getEntityTypeId();
Chris@5 173 $bundle = $entity->bundle();
Chris@5 174 $path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}/{$relationship}";
Chris@5 175 return $this->jsonapiGet($path, $options);
Chris@5 176 }
Chris@5 177
Chris@5 178 /**
Chris@5 179 * Performs an authenticated request and returns the decoded document.
Chris@5 180 */
Chris@5 181 protected function jsonapiGet($path, array $options = []) {
Chris@5 182 $this->drupalLogin($this->testUser);
Chris@5 183 $response = $this->drupalGet($path, $options, ['Accept' => 'application/vnd.api+json']);
Chris@5 184 return Json::decode($response);
Chris@5 185 }
Chris@5 186
Chris@5 187 }