View source
<?php
namespace Drupal\Tests\serialization\Unit\Normalizer;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
use Drupal\Tests\UnitTestCase;
class ContentEntityNormalizerTest extends UnitTestCase {
protected $serializer;
protected $contentEntityNormalizer;
protected function setUp() {
$entity_field_manager = $this
->createMock(EntityFieldManagerInterface::class);
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_repository = $this
->createMock(EntityTypeRepositoryInterface::class);
$this->contentEntityNormalizer = new ContentEntityNormalizer($entity_type_manager, $entity_type_repository, $entity_field_manager);
$this->serializer = $this
->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
->disableOriginalConstructor()
->setMethods([
'normalize',
])
->getMock();
$this->contentEntityNormalizer
->setSerializer($this->serializer);
}
public function testSupportsNormalization() {
$content_mock = $this
->createMock('Drupal\\Core\\Entity\\ContentEntityInterface');
$config_mock = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$this
->assertTrue($this->contentEntityNormalizer
->supportsNormalization($content_mock));
$this
->assertFalse($this->contentEntityNormalizer
->supportsNormalization($config_mock));
}
public function testNormalize() {
$this->serializer
->expects($this
->any())
->method('normalize')
->with($this
->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'test_format', [
'account' => NULL,
])
->will($this
->returnValue('test'));
$definitions = [
'field_accessible_external' => $this
->createMockFieldListItem(TRUE, FALSE),
'field_non-accessible_external' => $this
->createMockFieldListItem(FALSE, FALSE),
'field_accessible_internal' => $this
->createMockFieldListItem(TRUE, TRUE),
'field_non-accessible_internal' => $this
->createMockFieldListItem(FALSE, TRUE),
];
$content_entity_mock = $this
->createMockForContentEntity($definitions);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'test_format');
$this
->assertArrayHasKey('field_accessible_external', $normalized);
$this
->assertEquals('test', $normalized['field_accessible_external']);
$this
->assertArrayNotHasKey('field_non-accessible_external', $normalized);
$this
->assertArrayNotHasKey('field_accessible_internal', $normalized);
$this
->assertArrayNotHasKey('field_non-accessible_internal', $normalized);
}
public function testNormalizeWithAccountContext() {
$mock_account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$context = [
'account' => $mock_account,
];
$this->serializer
->expects($this
->any())
->method('normalize')
->with($this
->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'test_format', $context)
->will($this
->returnValue('test'));
$definitions = [
'field_1' => $this
->createMockFieldListItem(TRUE, FALSE, $mock_account),
'field_2' => $this
->createMockFieldListItem(FALSE, FALSE, $mock_account),
];
$content_entity_mock = $this
->createMockForContentEntity($definitions);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'test_format', $context);
$this
->assertArrayHasKey('field_1', $normalized);
$this
->assertEquals('test', $normalized['field_1']);
$this
->assertArrayNotHasKey('field_2', $normalized);
}
public function createMockForContentEntity($definitions) {
$content_entity_mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods([
'getTypedData',
])
->getMockForAbstractClass();
$typed_data = $this
->prophesize(ComplexDataInterface::class);
$typed_data
->getProperties(TRUE)
->willReturn($definitions)
->shouldBeCalled();
$content_entity_mock
->expects($this
->any())
->method('getTypedData')
->will($this
->returnValue($typed_data
->reveal()));
return $content_entity_mock;
}
protected function createMockFieldListItem($access, $internal, AccountInterface $user_context = NULL) {
$data_definition = $this
->prophesize(DataDefinitionInterface::class);
$mock = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$mock
->expects($this
->once())
->method('getDataDefinition')
->will($this
->returnValue($data_definition
->reveal()));
$data_definition
->isInternal()
->willReturn($internal)
->shouldBeCalled();
if (!$internal) {
$mock
->expects($this
->once())
->method('access')
->with('view', $user_context)
->will($this
->returnValue($access));
}
return $mock;
}
}