View source
<?php
namespace Drupal\Tests\serialization\Unit\Normalizer;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
use Drupal\Tests\UnitTestCase;
class ContentEntityNormalizerTest extends UnitTestCase {
protected $entityManager;
protected $serializer;
protected $contentEntityNormalizer;
protected function setUp() {
$this->entityManager = $this
->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$this->contentEntityNormalizer = new ContentEntityNormalizer($this->entityManager);
$this->serializer = $this
->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
->disableOriginalConstructor()
->setMethods(array(
'normalize',
))
->getMock();
$this->contentEntityNormalizer
->setSerializer($this->serializer);
}
public function testSupportsNormalization() {
$content_mock = $this
->getMock('Drupal\\Core\\Entity\\ContentEntityInterface');
$config_mock = $this
->getMock('Drupal\\Core\\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 = array(
'field_1' => $this
->createMockFieldListItem(),
'field_2' => $this
->createMockFieldListItem(FALSE),
);
$content_entity_mock = $this
->createMockForContentEntity($definitions);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'test_format');
$this
->assertArrayHasKey('field_1', $normalized);
$this
->assertEquals('test', $normalized['field_1']);
$this
->assertArrayNotHasKey('field_2', $normalized);
}
public function testNormalizeWithAccountContext() {
$mock_account = $this
->getMock('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 = array(
'field_1' => $this
->createMockFieldListItem(TRUE, $mock_account),
'field_2' => $this
->createMockFieldListItem(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(array(
'getFields',
))
->getMockForAbstractClass();
$content_entity_mock
->expects($this
->once())
->method('getFields')
->will($this
->returnValue($definitions));
return $content_entity_mock;
}
protected function createMockFieldListItem($access = TRUE, $user_context = NULL) {
$mock = $this
->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
$mock
->expects($this
->once())
->method('access')
->with('view', $user_context)
->will($this
->returnValue($access));
return $mock;
}
}