View source
<?php
namespace Drupal\Tests\jsonapi\Unit\Normalizer;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\jsonapi\Context\FieldResolver;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer;
use Drupal\jsonapi\LinkManager\LinkManager;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
class JsonApiDocumentTopLevelNormalizerTest extends UnitTestCase {
protected $normalizer;
public function setUp() {
$link_manager = $this
->prophesize(LinkManager::class);
$resource_type_repository = $this
->prophesize(ResourceTypeRepository::class);
$field_resolver = $this
->prophesize(FieldResolver::class);
$resource_type_repository
->getByTypeName(Argument::any())
->willReturn(new ResourceType('node', 'article', NULL));
$entity_storage = $this
->prophesize(EntityStorageInterface::class);
$self = $this;
$uuid_to_id = [
'76dd5c18-ea1b-4150-9e75-b21958a2b836' => 1,
'fcce1b61-258e-4054-ae36-244d25a9e04c' => 2,
];
$entity_storage
->loadByProperties(Argument::type('array'))
->will(function ($args) use ($self, $uuid_to_id) {
$result = [];
foreach ($args[0]['uuid'] as $uuid) {
$entity = $self
->prophesize(EntityInterface::class);
$entity
->uuid()
->willReturn($uuid);
$entity
->id()
->willReturn($uuid_to_id[$uuid]);
$result[$uuid] = $entity
->reveal();
}
return $result;
});
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('node')
->willReturn($entity_storage
->reveal());
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->getKey('uuid')
->willReturn('uuid');
$entity_type_manager
->getDefinition('node')
->willReturn($entity_type
->reveal());
$this->normalizer = new JsonApiDocumentTopLevelNormalizer($link_manager
->reveal(), $entity_type_manager
->reveal(), $resource_type_repository
->reveal(), $field_resolver
->reveal());
$serializer = $this
->prophesize(DenormalizerInterface::class);
$serializer
->willImplement(SerializerInterface::class);
$serializer
->denormalize(Argument::type('array'), Argument::type('string'), Argument::type('string'), Argument::type('array'))
->willReturnArgument(0);
$this->normalizer
->setSerializer($serializer
->reveal());
}
public function testDenormalize($input, $expected) {
$context = [
'resource_type' => new ResourceType($this
->randomMachineName(), $this
->randomMachineName(), FieldableEntityInterface::class),
];
$denormalized = $this->normalizer
->denormalize($input, NULL, 'api_json', $context);
$this
->assertSame($expected, $denormalized);
}
public function denormalizeProvider() {
return [
[
[
'data' => [
'type' => 'lorem',
'id' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
'attributes' => [
'title' => 'dummy_title',
],
],
],
[
'title' => 'dummy_title',
'uuid' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
'relationships' => [
'field_dummy' => [
'data' => [
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
],
],
],
],
],
[
'uuid' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
'field_dummy' => [
[
'target_id' => 1,
],
],
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'relationships' => [
'field_dummy' => [
'data' => [
[
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
],
[
'type' => 'node',
'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
],
],
],
],
],
],
[
'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'field_dummy' => [
[
'target_id' => 1,
],
[
'target_id' => 2,
],
],
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'relationships' => [
'field_dummy' => [
'data' => [
[
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
'meta' => [
'foo' => 'bar',
],
],
[
'type' => 'node',
'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
],
],
],
],
],
],
[
'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'field_dummy' => [
[
'target_id' => 1,
'foo' => 'bar',
],
[
'target_id' => 2,
],
],
],
],
];
}
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($this
->randomMachineName(), $this
->randomMachineName(), FieldableEntityInterface::class),
]);
if (isset($id)) {
$this
->assertSame($id, $denormalized['uuid']);
}
else {
$this
->assertArrayNotHasKey('uuid', $denormalized);
}
}
public function denormalizeUuidProvider() {
return [
'valid' => [
'76dd5c18-ea1b-4150-9e75-b21958a2b836',
FALSE,
],
'missing' => [
NULL,
FALSE,
],
'invalid_empty' => [
'',
TRUE,
],
'invalid_alpha' => [
'invalid',
TRUE,
],
'invalid_numeric' => [
1234,
TRUE,
],
'invalid_alphanumeric' => [
'abc123',
TRUE,
],
];
}
}