function JsonApiPatchRegressionTest::testBundlelessRelationshipMutationFromIssue2973681

Same name in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiPatchRegressionTest::testBundlelessRelationshipMutationFromIssue2973681()

Ensure POST and PATCH works for bundle-less relationship routes.

See also

https://www.drupal.org/project/drupal/issues/2976371

File

core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php, line 87

Class

JsonApiPatchRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testBundlelessRelationshipMutationFromIssue2973681() : void {
    $this->config('jsonapi.settings')
        ->set('read_only', FALSE)
        ->save(TRUE);
    // Set up data model.
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $this->createEntityReferenceField('node', 'page', 'field_test', NULL, 'user', 'default', [
        'target_bundles' => NULL,
    ], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->rebuildAll();
    // Create data.
    $node = Node::create([
        'title' => 'test article',
        'type' => 'page',
    ]);
    $node->save();
    $target = $this->createUser();
    // Test.
    $user = $this->drupalCreateUser([
        'bypass node access',
    ]);
    $url = Url::fromRoute('jsonapi.node--page.field_test.relationship.post', [
        'entity' => $node->uuid(),
    ]);
    $request_options = [
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/vnd.api+json',
            'Accept' => 'application/vnd.api+json',
        ],
        RequestOptions::AUTH => [
            $user->getAccountName(),
            $user->pass_raw,
        ],
        RequestOptions::JSON => [
            'data' => [
                [
                    'type' => 'user--user',
                    'id' => $target->uuid(),
                ],
            ],
        ],
    ];
    $response = $this->request('POST', $url, $request_options);
    $this->assertSame(204, $response->getStatusCode(), (string) $response->getBody());
}

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