You are here

public function JsonApiRegressionTest::testDenormalizeAliasedRelationshipFromIssue2953207 in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testDenormalizeAliasedRelationshipFromIssue2953207()

Ensures denormalizing relationships with aliased field names works.

See also

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

https://www.drupal.org/project/jsonapi_extras/issues/3004582#comment-128...

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 532

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testDenormalizeAliasedRelationshipFromIssue2953207() {
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // Since the JSON:API module does not have an explicit mechanism to set up
  // field aliases, create a strange data model so that automatic aliasing
  // allows us to test aliased relationships.
  // @see \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getFieldMapping()
  $internal_relationship_field_name = 'type';
  $public_relationship_field_name = 'taxonomy_term_' . $internal_relationship_field_name;

  // Set up data model.
  $this
    ->createEntityReferenceField('taxonomy_term', 'tags', $internal_relationship_field_name, NULL, 'user');
  $this
    ->rebuildAll();

  // Create data.
  Term::create([
    'name' => 'foobar',
    'vid' => 'tags',
    'type' => [
      'target_id' => 1,
    ],
  ])
    ->save();

  // Test.
  $user = $this
    ->drupalCreateUser([
    'edit terms in tags',
  ]);
  $body = [
    'data' => [
      'type' => 'user--user',
      'id' => User::load(0)
        ->uuid(),
    ],
  ];

  // Test.
  $response = $this
    ->request('PATCH', Url::fromUri(sprintf('internal:/jsonapi/taxonomy_term/tags/%s/relationships/%s', Term::load(1)
    ->uuid(), $public_relationship_field_name)), [
    RequestOptions::AUTH => [
      $user
        ->getAccountName(),
      $user->pass_raw,
    ],
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/vnd.api+json',
    ],
    RequestOptions::BODY => Json::encode($body),
  ]);
  $this
    ->assertSame(204, $response
    ->getStatusCode());
}