View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\Normalizer;
use Acquia\ContentHubClient\Entity;
use Drupal\acquia_contenthub\Normalizer\ContentEntityCdfNormalizer;
use Drupal\acquia_contenthub\Session\ContentHubUserSession;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
require_once __DIR__ . '/../Polyfill/Drupal.php';
class ContentEntityNormalizerTest extends UnitTestCase {
protected $backupGlobals = FALSE;
protected $configEntity = [
'dependency_depth' => 3,
'user_role' => AccountInterface::ANONYMOUS_ROLE,
];
protected $container;
protected $serializer;
protected $contentEntityNormalizer;
protected $contentEntityViewModesExtractor;
protected $configFactory;
protected $moduleHandler;
protected $entityRepository;
protected $kernel;
protected $renderer;
protected $entityManager;
protected $entityTypeRepository;
protected $entityTypeManager;
private $loggerFactory;
protected $languageManager;
protected $userContext;
protected function setUp() {
$this->container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
\Drupal::setContainer($this->container);
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->method('get')
->willReturnCallback(function ($argument) {
if ($argument == 'acquia_contenthub.admin_settings') {
return $this
->createMockForContentHubAdminConfig();
}
elseif ($argument == 'acquia_contenthub.entity_config') {
return $this
->createMockForContentHubEntityConfig();
}
return NULL;
});
$this->loggerFactory = $this
->getMockBuilder('Drupal\\Core\\Logger\\LoggerChannelFactoryInterface')
->disableOriginalConstructor()
->getMock();
$this->contentEntityViewModesExtractor = $this
->createMock('Drupal\\acquia_contenthub\\Normalizer\\ContentEntityViewModesExtractorInterface');
$this->moduleHandler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->entityRepository = $this
->createMock('Drupal\\Core\\Entity\\EntityRepositoryInterface');
$this->kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$this->renderer = $this
->createMock('Drupal\\Core\\Render\\RendererInterface');
$this->entityManager = $this
->getMockBuilder('Drupal\\acquia_contenthub\\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->getKey('bundle')
->willReturn('type');
$entity_type
->getKey('langcode')
->willReturn('langcode');
$this->entityTypeManager
->method('getDefinition')
->willReturn($entity_type
->reveal());
$this->languageManager = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->userContext = new ContentHubUserSession(AccountInterface::ANONYMOUS_ROLE);
$this->contentEntityNormalizer = new ContentEntityCdfNormalizer($this->configFactory, $this->contentEntityViewModesExtractor, $this->moduleHandler, $this->entityRepository, $this->kernel, $this->renderer, $this->entityManager, $this->entityTypeManager, $this->loggerFactory, $this->languageManager);
}
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 testGetBaseRoot() {
$GLOBALS['base_root'] = 'test';
$this
->assertEquals('test', $this->contentEntityNormalizer
->getBaseRoot());
unset($GLOBALS['base_root']);
$this
->assertEquals('', $this->contentEntityNormalizer
->getBaseRoot());
}
public function testNormalizeIncompatibleClass() {
$config_mock = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$normalized = $this->contentEntityNormalizer
->normalize($config_mock, 'acquia_contenthub_cdf');
$this
->assertNull($normalized);
}
public function testNormalizeOneField() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals('custom-uuid', $normalized_entity
->getUuid());
$this
->assertNotEmpty($normalized_entity
->getCreated());
$this
->assertNotEmpty($normalized_entity
->getModified());
$this
->assertEquals('test-origin', $normalized_entity
->getOrigin());
$this
->assertEquals('node', $normalized_entity
->getType());
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'test',
],
]);
}
public function testNormalizeOneFieldMultiValued() {
$this
->mockContainerResponseForNormalize();
$this->container
->expects($this
->at(2))
->method('get')
->with('entity_type.manager')
->willReturn($this->entityTypeManager);
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
[
'value' => 'test',
],
[
'value' => 'test2',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
'nl',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals('custom-uuid', $normalized_entity
->getUuid());
$this
->assertNotEmpty($normalized_entity
->getCreated());
$this
->assertNotEmpty($normalized_entity
->getModified());
$this
->assertEquals('test-origin', $normalized_entity
->getOrigin());
$this
->assertEquals('node', $normalized_entity
->getType());
$expected_output = [
'en' => [
'test',
'test2',
],
'nl' => [
'test',
'test2',
],
];
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), $expected_output);
}
public function testNormalizeWithCreatedAndChanged() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
'created' => $this
->createMockFieldListItem('created', 'timestamp', TRUE, $this->userContext, [
'0' => [
'value' => '1458811508',
],
]),
'changed' => $this
->createMockFieldListItem('changed', 'timestamp', TRUE, $this->userContext, [
'0' => [
'value' => '1458811509',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getCreated(), date('c', 1458811508));
$this
->assertNotEquals($normalized_entity
->getModified(), date('c', 1458811509));
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'test',
],
]);
$this
->assertFalse($normalized_entity
->getAttribute('created'));
$this
->assertFalse($normalized_entity
->getAttribute('changed'));
}
public function testNormalizeWithNoFieldValue() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, []),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues()['en'], NULL);
}
public function testNormalizeWithFieldNameAsType() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'title' => $this
->createMockFieldListItem('title', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('title')
->getValues(), [
'en' => 'test',
]);
}
public function testNormalizeWithNonStringFieldType() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'voted' => $this
->createMockFieldListItem('voted', 'boolean', TRUE, $this->userContext, [
'0' => [
'value' => TRUE,
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('voted')
->getValues(), [
'en' => [
TRUE,
],
]);
}
public function testNormalizeWithComplexFieldValues() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
'random_key' => 'random_data',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'{"value":"test","random_key":"random_data"}',
],
]);
}
public function testNormalizeWithFieldWithoutAccess() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
'field_2' => $this
->createMockFieldListItem('field_2', 'string', FALSE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'test',
],
]);
$this
->assertFalse($normalized_entity
->getAttribute('field_2'));
}
public function testNormalizeWithAccountContext() {
$this
->mockContainerResponseForNormalize();
$mock_account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$context = [
'account' => $mock_account,
];
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $mock_account, [
'0' => [
'value' => 'test',
],
]),
'field_2' => $this
->createMockFieldListItem('field_2', 'string', FALSE, $mock_account, [
'0' => [
'value' => 'test',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $mock_account);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf', $context);
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'test',
],
]);
$this
->assertFalse($normalized_entity
->getAttribute('field_2'));
}
public function testNormalizeWithRevisionId() {
$this
->mockContainerResponseForNormalize();
$mock_account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$context = [
'account' => $mock_account,
];
$definitions = [
'field_1' => $this
->createMockFieldListItem('field_1', 'string', TRUE, $mock_account, [
'0' => [
'value' => 'test',
],
]),
'vid' => $this
->createMockFieldListItem('vid', 'string', TRUE, $mock_account, [
'0' => [
'value' => '1',
],
]),
];
$serializer = $this
->getFieldsSerializer($definitions, $mock_account);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf', $context);
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_1')
->getValues(), [
'en' => [
'test',
],
]);
$this
->assertFalse($normalized_entity
->getAttribute('vid'));
}
public function testNormalizeReferenceField() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_ref' => $this
->createMockEntityReferenceFieldItemList('field_ref', TRUE, $this->userContext),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_ref')
->getValues(), [
'en' => [
'test-uuid-reference-1',
'test-uuid-reference-2',
],
]);
}
public function testNormalizeImageReferenceField() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_image' => $this
->createMockImageEntityReferenceFieldItemList('field_image', TRUE, $this->userContext),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_image')
->getValues(), [
'en' => [
0 => '{"alt":"test-alt-image-value","title":"test-alt-image-text","target_uuid":"[test-uuid-image-1]"}',
],
]);
}
public function testNormalizeTypeReferenceField() {
$this
->mockContainerResponseForNormalize();
$definitions = [
'field_ref' => $this
->createMockEntityReferenceFieldItemList('field_ref', TRUE, $this->userContext),
];
$serializer = $this
->getFieldsSerializer($definitions, $this->userContext);
$this->contentEntityNormalizer
->setSerializer($serializer);
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'acquia_contenthub_cdf');
$this
->doTestValidResultForOneEntity($normalized);
$normalized_entity = $this
->getContentHubEntityFromResult($normalized);
$this
->assertEquals($normalized_entity
->getAttribute('field_ref')
->getType(), 'array<reference>');
}
public function testGetFieldTypeMapping() {
$definitions = [
'title' => $this
->createMockFieldListItem('title', 'string', TRUE, $this->userContext, [
'0' => [
'value' => 'test',
],
]),
];
$content_entity_mock = $this
->createMockForContentEntity($definitions, [
'en',
]);
$mapping = $this->contentEntityNormalizer
->getFieldTypeMapping($content_entity_mock);
$this
->assertNotEmpty($mapping);
$this
->assertEquals('array<boolean>', $mapping['boolean']);
$this
->assertEquals(NULL, $mapping['password']);
$this
->assertEquals('array<number>', $mapping['decimal']);
$this
->assertEquals('array<reference>', $mapping['entity_reference']);
$this
->assertEquals('array<string>', $mapping['fallback']);
$this
->assertEquals('string', $mapping['title']);
$this
->assertEquals('string', $mapping['langcode']);
}
public function testDenormalize() {
$denormalized = $this->contentEntityNormalizer
->denormalize(NULL, NULL);
$this
->assertNull($denormalized);
}
private function doTestValidResultForOneEntity($normalized) {
$this
->assertArrayHasKey('entities', $normalized);
$this
->assertCount(1, $normalized['entities']);
}
private function getContentHubEntityFromResult(array $normalized) {
$normalized_entity = array_pop($normalized['entities']);
$this
->assertTrue($normalized_entity instanceof Entity);
return $normalized_entity;
}
protected function getFieldsSerializer(array $definitions, AccountInterface $user_context = NULL) {
$serializer = $this
->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
->disableOriginalConstructor()
->setMethods([
'normalize',
])
->getMock();
$serializer
->method('normalize')
->with($this
->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'json', [
'account' => $user_context,
'query_params' => [],
'entity_type' => 'node',
])
->willReturnCallback(function ($field, $format, $context) {
if ($field) {
return $field
->getValue();
}
return NULL;
});
return $serializer;
}
public function createMockForContentEntity(array $definitions, array $languages, $is_new = FALSE) {
$enabled_methods = [
'getFields',
'getEntityTypeId',
'uuid',
'get',
'getTranslationLanguages',
'getTranslation',
'hasField',
'toUrl',
'access',
'hasLinkTemplate',
'isNew',
];
$content_entity_mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods($enabled_methods)
->getMockForAbstractClass();
$content_entity_mock
->method('getFields')
->willReturn($definitions);
$content_entity_mock
->method('isNew')
->willReturn($is_new);
$content_entity_mock
->method('get')
->willReturnCallback(function ($name) use ($definitions) {
if (isset($definitions[$name])) {
return $definitions[$name];
}
return NULL;
});
$content_entity_mock
->method('hasField')
->willReturnCallback(function ($name) use ($definitions) {
if (isset($definitions[$name])) {
return TRUE;
}
return FALSE;
});
$content_entity_mock
->method('getEntityTypeId')
->willReturn('node');
$content_entity_mock
->method('uuid')
->willReturn('custom-uuid');
$content_entity_mock
->method('getTranslation')
->willReturn($content_entity_mock);
$languages = $this
->createMockLanguageList($languages);
$content_entity_mock
->method('getTranslationLanguages')
->willReturn($languages);
$url = $this
->getMockBuilder('Drupal\\Core\\Url')
->disableOriginalConstructor()
->getMock();
$url
->method('toString')
->willReturn('http://localhost/node/1');
$content_entity_mock
->method('toUrl')
->willReturn($url);
$access = $this
->createMock('Drupal\\Core\\Access\\AccessResultInterface');
$access
->method('isAllowed')
->willReturn(TRUE);
$content_entity_mock
->method('access')
->withAnyParameters()
->willReturn($access);
$content_entity_mock
->method('hasLinkTemplate')
->willReturn(TRUE);
return $content_entity_mock;
}
public function createMockForContentHubAdminConfig() {
$contenthub_admin_config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMockForAbstractClass();
$contenthub_admin_config
->method('get')
->with('origin')
->willReturn('test-origin');
return $contenthub_admin_config;
}
public function createMockForContentHubEntityConfig() {
$contenthub_entity_config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMockForAbstractClass();
$contenthub_entity_config
->method('get')
->willReturnCallback(function ($argument) {
if (isset($this->configEntity[$argument])) {
return $this->configEntity[$argument];
}
return NULL;
});
return $contenthub_entity_config;
}
protected function createMockFieldListItem($name, $type = 'string', $access = TRUE, $user_context = NULL, array $return_value = []) {
$mock = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$mock
->method('access')
->with('view', $user_context)
->will($this
->returnValue($access));
$field_def = $this
->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_def
->method('getName')
->willReturn($name);
$field_def
->method('getType')
->willReturn($type);
$mock
->method('getValue')
->willReturn($return_value);
$mock
->method('getFieldDefinition')
->willReturn($field_def);
return $mock;
}
protected function createMockEntityReferenceFieldItemList($name, $access = TRUE, $user_context = NULL) {
$mock = $this
->createMock('Drupal\\Core\\Field\\EntityReferenceFieldItemListInterface');
$mock
->method('access')
->with('view', $user_context)
->will($this
->returnValue($access));
$field_def = $this
->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_def
->method('getName')
->willReturn($name);
$field_def
->method('getType')
->willReturn('entity_reference');
$mock
->method('getValue')
->willReturn('bla');
$referenced_entities = [];
$entity1 = $this
->createMock('\\Drupal\\Core\\Entity\\EntityInterface');
$entity1
->method('id')
->willReturn('test-id-reference-1');
$entity1
->method('uuid')
->willReturn('test-uuid-reference-1');
$referenced_entities[] = $entity1;
$entity2 = $this
->createMock('\\Drupal\\Core\\Entity\\EntityInterface');
$entity2
->method('id')
->willReturn('test-id-reference-2');
$entity2
->method('uuid')
->willReturn('test-uuid-reference-2');
$referenced_entities[] = $entity2;
$mock
->method('getFieldDefinition')
->willReturn($field_def);
$mock
->method('referencedEntities')
->willReturn($referenced_entities);
return $mock;
}
protected function createMockImageEntityReferenceFieldItemList($name, $access = TRUE, $user_context = NULL) {
$mock = $this
->createMock('Drupal\\Core\\Field\\EntityReferenceFieldItemListInterface');
$mock
->method('access')
->with('view', $user_context)
->will($this
->returnValue($access));
$field_def = $this
->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_def
->method('getName')
->willReturn($name);
$field_def
->method('getType')
->willReturn('image');
$mock
->method('getValue')
->willReturn([
0 => [
'target_id' => 'test-id-image-1',
'alt' => 'test-alt-image-value',
'title' => 'test-alt-image-text',
'width' => '100',
'height' => '100',
],
]);
$referenced_entities = [];
$methods = [
'id',
'uuid',
'getFileUri',
];
$image1 = $this
->getMockBuilder('\\Drupal\\image\\Plugin\\Field\\FieldType\\ImageItem')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
$image1
->method('id')
->willReturn('test-id-image-1');
$image1
->method('uuid')
->willReturn('test-uuid-image-1');
$image1
->method('getFileUri')
->willReturn('public://test-image-1.jpg');
$referenced_entities[] = $image1;
$mock
->method('getFieldDefinition')
->willReturn($field_def);
$mock
->method('referencedEntities')
->willReturn($referenced_entities);
return $mock;
}
protected function createMockLanguageList($languages = [
'en',
]) {
$language_objects = [];
foreach ($languages as $language) {
$mock = $this
->createMock('Drupal\\Core\\Language\\LanguageInterface');
$mock
->method('getId')
->willReturn($language);
$language_objects[$language] = $mock;
}
return $language_objects;
}
protected function mockContainerResponseForNormalize() {
$request_stack = $this
->createMock('Symfony\\Component\\HttpFoundation\\RequestStack');
$request = $this
->createMock('Symfony\\Component\\HttpFoundation\\Request');
$request
->method('getRequestUri')
->willReturn('http://localhost/node/1');
$request_stack
->method('getCurrentRequest')
->willReturn($request);
$url_generator = $this
->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$url_generator
->method('generateFromRoute')
->with('entity.node.canonical', [
'node' => 1,
], [], FALSE)
->willReturn('http://localhost/node/1');
$entity_type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type
->expects($this
->any())
->method('getKeys')
->willReturn([
'uid' => 'uid',
'id' => 'nid',
'revision' => 'vid',
'uuid' => 'uuid',
]);
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entityTypeManager
->method('getDefinition')
->with('node')
->willReturn($entity_type);
$this->container
->expects($this
->at(0))
->method('get')
->with('request_stack')
->willReturn($request_stack);
$this->container
->expects($this
->at(1))
->method('get')
->with('entity_type.manager')
->willReturn($this->entityTypeManager);
}
}