You are here

public function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  2. 10 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()

Ensures only valid UUIDs can be specified.

@covers ::denormalize @dataProvider denormalizeUuidProvider

Parameters

string $id: The input UUID. May be invalid.

bool $expect_exception: Whether to expect an exception.

File

core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 213

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer

Code

public function testDenormalizeUuid($id, $expect_exception) {
  $data['data'] = isset($id) ? [
    'type' => 'node--article',
    'id' => $id,
  ] : [
    'type' => 'node--article',
  ];
  if ($expect_exception) {
    $this
      ->expectException(UnprocessableEntityHttpException::class);
    $this
      ->expectExceptionMessage('IDs should be properly generated and formatted UUIDs as described in RFC 4122.');
  }
  $denormalized = $this->normalizer
    ->denormalize($data, NULL, 'api_json', [
    'resource_type' => new ResourceType('node', 'article', FieldableEntityInterface::class),
  ]);
  if (isset($id)) {
    $this
      ->assertSame($id, $denormalized['uuid']);
  }
  else {
    $this
      ->assertArrayNotHasKey('uuid', $denormalized);
  }
}