function ResourceTestBase::testRelationships

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRelationships()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRelationships()
  3. 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."

2 methods override ResourceTestBase::testRelationships()
FileUploadTest::testRelationships in core/modules/jsonapi/tests/src/Functional/FileUploadTest.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 1340

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testRelationships() : void {
    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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.