You are here

public function JsonApiRegressionTest::testBundlelessRelationshipMutationFromIssue2973681 in JSON:API 8

Same name and namespace in other branches
  1. 8.2 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/jsonapi/issues/2976371

File

tests/src/Functional/JsonApiRegressionTest.php, line 133

Class

JsonApiRegressionTest
JSON API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testBundlelessRelationshipMutationFromIssue2973681() {

  // 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.relationship', [
    'node' => $node
      ->uuid(),
    'related' => 'field_test',
  ]);
  $request_options = [
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/vnd.api+json',
      'Accept' => 'application/vnd.api+json',
    ],
    RequestOptions::AUTH => [
      $user
        ->getUsername(),
      $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());
}