You are here

public function JsonApiRegressionTest::testEmptyMapFieldTypeDenormalization 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::testEmptyMapFieldTypeDenormalization()

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

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testEmptyMapFieldTypeDenormalization() {
  $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);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $doc = Json::decode((string) $response
    ->getBody());

  // 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);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertSame($doc['data']['attributes']['data'], Json::decode((string) $response
    ->getBody())['data']['attributes']['data']);
}