function JsonApiRegressionTest::testEmptyMapFieldTypeDenormalization

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testEmptyMapFieldTypeDenormalization()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testEmptyMapFieldTypeDenormalization()
  3. 10 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testEmptyMapFieldTypeDenormalization()

Ensure optional `@FieldType=map` fields are denormalized correctly.

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testEmptyMapFieldTypeDenormalization() : void {
  $this->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  // Set up data model.
  $this->assertTrue($this->container
    ->get('module_installer')
    ->install([
    'entity_test',
  ], TRUE), 'Installed modules.');
  // Create data.
  $entity = EntityTestMapField::create([
    'name' => 'foo',
  ]);
  $entity->save();
  $user = $this->drupalCreateUser([
    'administer entity_test content',
  ]);
  // Test.
  $url = Url::fromUri(sprintf('internal:/jsonapi/entity_test_map_field/entity_test_map_field/%s', $entity->uuid()));
  $request_options = [
    RequestOptions::AUTH => [
      $user->getAccountName(),
      $user->pass_raw,
    ],
  ];
  // Retrieve the current representation of the entity.
  $response = $this->request('GET', $url, $request_options);
  $doc = $this->getDocumentFromResponse($response);
  $this->assertSame(200, $response->getStatusCode());
  // Modify the title. The @FieldType=map normalization is not changed. (The
  // name of this field is confusingly also 'data'.)
  $doc['data']['attributes']['name'] = 'bar';
  $request_options[RequestOptions::HEADERS] = [
    'Content-Type' => 'application/vnd.api+json',
    'Accept' => 'application/vnd.api+json',
  ];
  $request_options[RequestOptions::BODY] = Json::encode($doc);
  $response = $this->request('PATCH', $url, $request_options);
  $patched_document = $this->getDocumentFromResponse($response);
  $this->assertSame(200, $response->getStatusCode());
  $this->assertSame($doc['data']['attributes']['data'], $patched_document['data']['attributes']['data']);
}

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