You are here

public function ResourceTestBase::testRelationships in JSON:API 8.2

Same name and namespace in other branches
  1. 8 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."

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

File

tests/src/Functional/ResourceTestBase.php, line 1309

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);
}