You are here

public function ResourceTestBase::testRelationships in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRelationships()
  2. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRelationships()

Tests CRUD of individual resource relationship data.

Unlike the "related" routes, relationship routes only return information about the "relationship" itself, not the targeted resources. For JSON:API with Drupal, relationship routes are like looking at an entity reference field without loading the entities. It only reveals the type of the targeted resource and the target resource IDs. These type+ID combos are referred to as "resource identifiers."

3 methods override ResourceTestBase::testRelationships()
FileUploadTest::testRelationships in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
@requires module irrelevant_for_this_test
ItemTest::testRelationships in core/modules/jsonapi/tests/src/Functional/ItemTest.php
Tests CRUD of individual resource relationship data.
MessageTest::testRelationships in core/modules/jsonapi/tests/src/Functional/MessageTest.php
Tests CRUD of individual resource relationship data.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 1310

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testRelationships() {
  if ($this->entity instanceof ConfigEntityInterface) {
    $this
      ->markTestSkipped('Configuration entities cannot have relationships.');
  }
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());

  // Test GET.
  $this
    ->doTestRelationshipGet($request_options);
  $this
    ->setUpAuthorization('GET');
  $this
    ->doTestRelationshipGet($request_options);

  // Test POST.
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $this
    ->doTestRelationshipMutation($request_options);

  // Grant entity-level edit access.
  $this
    ->setUpAuthorization('PATCH');
  $this
    ->doTestRelationshipMutation($request_options);

  // Field edit access is still forbidden, grant it.
  $this
    ->grantPermissionsToTestedRole([
    'field_jsonapi_test_entity_ref view access',
    'field_jsonapi_test_entity_ref edit access',
    'field_jsonapi_test_entity_ref update access',
  ]);
  $this
    ->doTestRelationshipMutation($request_options);
}