You are here

public function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid in JSON:API 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  2. 8.2 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
Same name and namespace in other branches
  1. 8 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

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
      ->setExpectedException(UnprocessableEntityHttpException::class, '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);
  }
}