View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit;
use Drupal\acquia_contenthub\Entity\ContentHubEntityTypeConfig;
use Drupal\acquia_contenthub\EntityManager;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
class EntityManagerTest extends UnitTestCase {
private $loggerFactory;
private $configFactory;
private $clientManager;
private $contentHubEntitiesTracking;
private $entityTypeManager;
private $entityTypeBundleInfoManager;
private $kernel;
private $settings;
private $contentEntityType;
private $configEntityType;
protected function setUp() {
parent::setUp();
$this->loggerFactory = $this
->getMockBuilder('Drupal\\Core\\Logger\\LoggerChannelFactoryInterface')
->disableOriginalConstructor()
->getMock();
$this->configFactory = $this
->getMockBuilder('Drupal\\Core\\Config\\ConfigFactoryInterface')
->disableOriginalConstructor()
->getMock();
$this->clientManager = $this
->createMock('Drupal\\acquia_contenthub\\Client\\ClientManagerInterface');
$this->contentHubEntitiesTracking = $this
->getMockBuilder('Drupal\\acquia_contenthub\\ContentHubEntitiesTracking')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entityTypeBundleInfoManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeBundleInfoInterface');
$this->kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$this->settings = $this
->getMockBuilder('Drupal\\Core\\Config\\Config')
->disableOriginalConstructor()
->getMock();
$this->configFactory
->expects($this
->at(0))
->method('get')
->with('acquia_contenthub.admin_settings')
->willReturn($this->settings);
$this->contentEntityType = $this
->createMock('Drupal\\Core\\Entity\\ContentEntityTypeInterface');
$this->configEntityType = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
}
public function invokeMethod(&$object, $methodName, array $parameters = []) {
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection
->getMethod($methodName);
$method
->setAccessible(TRUE);
return $method
->invokeArgs($object, $parameters);
}
protected function getContentHubEntityTypeConfigEntity($id) {
$bundles = [
'entity_type_1' => [
'bundle_11' => [
'enable_index' => 1,
'enable_viewmodes' => 1,
'rendering' => [
'view_1',
'view_2',
'view_3',
],
],
],
'entity_type_2' => [
'bundle_21' => [
'enable_index' => 1,
'enable_viewmodes' => 0,
'rendering' => [],
],
'bundle_22' => [
'enable_index' => 0,
'enable_viewmodes' => 0,
'rendering' => [
'view_4',
],
],
'bundle_23' => [
'enable_index' => 1,
'enable_viewmodes' => 1,
'rendering' => [],
],
],
'entity_type_3' => [
'bundle_31' => [
'enable_index' => 0,
'enable_viewmodes' => 0,
'rendering' => [],
],
],
];
$values = [
'id' => $id,
];
$config_entity = new ContentHubEntityTypeConfig($values, 'acquia_contenthub_entity_config');
$config_entity
->setBundles($bundles[$id]);
return $config_entity;
}
private function getContentHubConfigStorage() {
$config_entity1 = $this
->getContentHubEntityTypeConfigEntity('entity_type_1');
$config_entity2 = $this
->getContentHubEntityTypeConfigEntity('entity_type_2');
$config_entity3 = $this
->getContentHubEntityTypeConfigEntity('entity_type_3');
$config_entities = [
'entity_type_1' => $config_entity1,
'entity_type_2' => $config_entity2,
'entity_type_3' => $config_entity3,
];
$config_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$config_storage
->method('loadMultiple')
->willReturn($config_entities);
return $config_storage;
}
public function testGetContentHubEnabledEntityTypeIds() {
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$config_storage = $this
->getContentHubConfigStorage();
$this->entityTypeManager
->method('getStorage')
->with('acquia_contenthub_entity_config')
->willReturn($config_storage);
$enabled_entity_type_ids = $entity_manager
->getContentHubEnabledEntityTypeIds();
$expected_entity_type_ids = [
'entity_type_1',
'entity_type_2',
];
$this
->assertEquals($expected_entity_type_ids, $enabled_entity_type_ids);
}
public function testGetAllowedEntityTypes() {
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$entity_types = [
'content_entity_1' => $this->contentEntityType,
'content_entity_2' => $this->contentEntityType,
'comment' => $this->contentEntityType,
'user' => $this->contentEntityType,
'config_entity_1' => $this->configEntityType,
'config_entity_2' => $this->configEntityType,
];
$this->entityTypeManager
->expects($this
->once())
->method('getDefinitions')
->willReturn($entity_types);
$bundles = [
'bundle1' => [
'label' => 'bundle1',
],
'bundle2' => [
'label' => 'bundle2',
],
];
$this->entityTypeBundleInfoManager
->expects($this
->at(0))
->method('getBundleInfo')
->with('content_entity_1')
->willReturn($bundles);
$this->entityTypeBundleInfoManager
->expects($this
->at(1))
->method('getBundleInfo')
->with('content_entity_2')
->willReturn(NULL);
$entity_types = $entity_manager
->getAllowedEntityTypes();
$expected_entity_types = [
'content_entity_1' => [
'bundle1' => 'bundle1',
'bundle2' => 'bundle2',
],
];
$this
->assertEquals($expected_entity_types, $entity_types);
}
public function testSupportedContentHubEntity() {
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$enabled_methods = [
'getEntityTypeId',
];
$content_entity = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods($enabled_methods)
->getMockForAbstractClass();
$content_entity
->expects($this
->at(0))
->method('getEntityTypeId')
->willReturn('node');
$content_entity
->expects($this
->at(1))
->method('getEntityTypeId')
->willReturn('taxonomy_term');
$content_entity
->expects($this
->at(2))
->method('getEntityTypeId')
->willReturn('user');
$this
->assertTrue($entity_manager
->isSupportedContentHubEntity($content_entity));
$this
->assertTrue($entity_manager
->isSupportedContentHubEntity($content_entity));
$this
->assertFalse($entity_manager
->isSupportedContentHubEntity($content_entity));
}
public function testGetBulkResourceUrl() {
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
\Drupal::setContainer($container);
$bulk_route_name = 'acquia_contenthub.acquia_contenthub_bulk_cdf';
$url_options = [
'option1' => 'option_value_1',
];
$url_generator = $this
->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$url_generator
->method('generateFromRoute')
->with($bulk_route_name, $url_options)
->willReturn('/node/1');
$container
->expects($this
->once())
->method('get')
->with('url_generator')
->willReturn($url_generator);
$this->settings
->expects($this
->once())
->method('get')
->with('rewrite_domain')
->willReturn('http://my-rewrite-domain.com');
$result_url = $entity_manager
->getBulkResourceUrl($url_options);
$expected_url = 'http://my-rewrite-domain.com/node/1';
$this
->assertEquals($expected_url, $result_url);
}
public function testGetBulkResourceUrlIsExternal() {
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
\Drupal::setContainer($container);
$bulk_route_name = 'acquia_contenthub.acquia_contenthub_bulk_cdf';
$url_options = [
'option1' => 'option_value_1',
];
$url_generator = $this
->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$url_generator
->method('generateFromRoute')
->with($bulk_route_name, $url_options)
->willReturn('http://localhost/node/1');
$container
->expects($this
->once())
->method('get')
->with('url_generator')
->willReturn($url_generator);
$this->settings
->expects($this
->once())
->method('get')
->with('rewrite_domain')
->willReturn('http://my-rewrite-domain.com');
$result_url = $entity_manager
->getBulkResourceUrl($url_options);
$expected_url = 'http://localhost/node/1';
$this
->assertEquals($expected_url, $result_url);
}
private function getContentHubEntityTypeConfigEntityId($id) {
$bundles = [
'node' => [
'article' => [
'enable_index' => 1,
'enable_viewmodes' => 1,
'rendering' => [
'view_node1',
'view_node2',
'view_node3',
],
],
],
'file' => [
'image' => [
'enable_index' => 1,
'enable_viewmodes' => 1,
'rendering' => [
'view_file1',
'view_file2',
'view_file3',
],
],
],
];
$values = [
'id' => $id,
];
$config_entity = new ContentHubEntityTypeConfig($values, 'acquia_contenthub_entity_config');
$config_entity
->setBundles($bundles[$id]);
return $config_entity;
}
public function testIsEligibleDependency() {
$container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
\Drupal::setContainer($container);
$container
->method('get')
->willReturnCallback(function ($argument) {
$module_handler = $this
->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$module_handler
->expects($this
->any())
->method('invokeAll')
->willReturn([]);
$config1 = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMock();
$config2 = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMock();
$config2
->method('get')
->with('user_role')
->willReturn(AccountInterface::ANONYMOUS_ROLE);
$config1
->method('get')
->with('acquia_contenthub.entity_config')
->willReturn($config2);
switch ($argument) {
case 'config.factory':
return $config1;
case 'module_handler':
return $module_handler;
}
});
$node_config_entity = $this
->getContentHubEntityTypeConfigEntityId('node');
$file_config_entity = $this
->getContentHubEntityTypeConfigEntityId('file');
$config_entities = [
'node' => $node_config_entity,
'file' => $file_config_entity,
];
$config_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$config_storage
->method('loadMultiple')
->willReturn($config_entities);
$this->entityTypeManager
->method('getStorage')
->with('acquia_contenthub_entity_config')
->willReturn($config_storage);
$entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);
$access = $this
->createMock('\\Drupal\\Core\\Access\\AccessResultInterface');
$access
->expects($this
->any())
->method('isAllowed')
->willReturn(TRUE);
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->method('id')
->willReturn(1);
$node
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('node');
$node
->expects($this
->any())
->method('bundle')
->willReturn('article');
$node
->expects($this
->any())
->method('access')
->withAnyParameters()
->willReturn($access);
$node->__contenthub_entity_syncing = TRUE;
$result = $entity_manager
->isEligibleDependency($node);
$this
->assertFalse($result);
unset($node->__contenthub_entity_syncing);
$node
->expects($this
->at(0))
->method('bundle')
->willReturn('page');
$result = $entity_manager
->isEligibleDependency($node);
$this
->assertFalse($result);
$type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$type
->expects($this
->any())
->method('hasKey')
->willReturnMap([
[
'status',
TRUE,
],
[
'revision',
TRUE,
],
]);
$type
->expects($this
->any())
->method('getKey')
->willReturnMap([
[
'status',
'status',
],
[
'revision',
'vid',
],
]);
$field_storage = $this
->createMock('\\Drupal\\Core\\Field\\FieldStorageDefinitionInterface');
$field_storage
->expects($this
->any())
->method('getMainPropertyName')
->willReturn('value');
$field_definition = $this
->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->any())
->method('getFieldStorageDefinition')
->willReturn($field_storage);
$node
->expects($this
->any())
->method('getFieldDefinition')
->with('status')
->willReturn($field_definition);
$status_field = new \stdClass();
$status_field->value = TRUE;
$node
->expects($this
->any())
->method('get')
->with('status')
->willReturn($status_field);
$node
->expects($this
->any())
->method('getEntityType')
->willReturn($type);
$node
->expects($this
->at(1))
->method('bundle')
->willReturn('article');
$this->contentHubEntitiesTracking
->expects($this
->any())
->method('loadImportedByDrupalEntity')
->willReturn(FALSE);
$this->contentHubEntitiesTracking
->expects($this
->any())
->method('loadExportedByUuid')
->willReturn(FALSE);
$result = $entity_manager
->isEligibleDependency($node);
$this
->assertTrue($result);
$entity_type = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type
->method('hasKey')
->willReturn(FALSE);
$file = $this
->createMock('\\Drupal\\file\\FileInterface');
$file
->method('id')
->willReturn(1);
$file
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('file');
$file
->expects($this
->any())
->method('bundle')
->willReturn('image');
$file
->expects($this
->any())
->method('access')
->withAnyParameters()
->willReturn($access);
$file
->expects($this
->any())
->method('getEntityType')
->willReturn($entity_type);
$file
->expects($this
->at(3))
->method('isTemporary')
->willReturn(TRUE);
$file
->expects($this
->at(4))
->method('isTemporary')
->willReturn(FALSE);
$result = $entity_manager
->isEligibleDependency($file);
$this
->assertFalse($result);
$type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$type
->expects($this
->any())
->method('hasKey')
->willReturnMap([
[
'status',
FALSE,
],
[
'revision',
FALSE,
],
]);
$file
->expects($this
->any())
->method('getEntityType')
->willReturn($type);
$result = $entity_manager
->isEligibleDependency($file);
$this
->assertTrue($result);
}
}