function JsonApiRegressionTest::testMapFieldTypeNormalizationFromIssue3040590

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

Ensure `@FieldType=map` fields are normalized correctly.

See also

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

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testMapFieldTypeNormalizationFromIssue3040590() : void {
  $this->assertTrue($this->container
    ->get('module_installer')
    ->install([
    'entity_test',
  ], TRUE), 'Installed modules.');
  // Create data.
  $entity_a = EntityTestMapField::create([
    'name' => 'A',
    'data' => [
      'foo' => 'bar',
      'baz' => 'qux',
    ],
  ]);
  $entity_a->save();
  $entity_b = EntityTestMapField::create([
    'name' => 'B',
  ]);
  $entity_b->save();
  $user = $this->drupalCreateUser([
    'administer entity_test content',
  ]);
  // Test.
  $url = Url::fromUri('internal:/jsonapi/entity_test_map_field/entity_test_map_field?sort=drupal_internal__id');
  $request_options = [
    RequestOptions::AUTH => [
      $user->getAccountName(),
      $user->pass_raw,
    ],
  ];
  $response = $this->request('GET', $url, $request_options);
  $data = $this->getDocumentFromResponse($response);
  $this->assertSame(200, $response->getStatusCode());
  $this->assertSame([
    'foo' => 'bar',
    'baz' => 'qux',
  ], $data['data'][0]['attributes']['data']);
  $this->assertNull($data['data'][1]['attributes']['data']);
  $entity_a->set('data', [
    'foo' => 'bar',
  ])
    ->save();
  $response = $this->request('GET', $url, $request_options);
  $data = $this->getDocumentFromResponse($response);
  $this->assertSame(200, $response->getStatusCode());
  $this->assertSame([
    'foo' => 'bar',
  ], $data['data'][0]['attributes']['data']);
}

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