You are here

public function JsonApiRegressionTest::testBundlelessRelationshipMutationFromIssue2973681 in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testBundlelessRelationshipMutationFromIssue2973681()
  2. 10 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::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/JsonApiRegressionTest.php, line 158

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testBundlelessRelationshipMutationFromIssue2973681() {
  $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());
}