View source
<?php
namespace Drupal\Tests\field\Unit;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\UnitTestCase;
class FieldConfigEntityUnitTest extends UnitTestCase {
protected $entityType;
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityTypeId;
protected $uuid;
protected $fieldStorage;
protected $fieldTypePluginManager;
protected function setUp() : void {
$this->entityTypeId = $this
->randomMachineName();
$this->entityType = $this
->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityFieldManager = $this
->createMock(EntityFieldManagerInterface::class);
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->fieldTypePluginManager = $this
->createMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
$container = new ContainerBuilder();
$container
->set('entity_field.manager', $this->entityFieldManager);
$container
->set('entity_type.manager', $this->entityTypeManager);
$container
->set('uuid', $this->uuid);
$container
->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
\Drupal::setContainer($container);
$this->fieldStorage = $this
->createMock('\\Drupal\\field\\FieldStorageConfigInterface');
$this->fieldStorage
->expects($this
->any())
->method('getType')
->will($this
->returnValue('test_field'));
$this->fieldStorage
->expects($this
->any())
->method('getName')
->will($this
->returnValue('field_test'));
$this->fieldStorage
->expects($this
->any())
->method('getSettings')
->willReturn([]);
$this->entityFieldManager
->expects($this
->any())
->method('getFieldStorageDefinitions')
->with('test_entity_type')
->will($this
->returnValue([
$this->fieldStorage
->getName() => $this->fieldStorage,
]));
}
public function testCalculateDependencies() {
$target_entity_type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$target_entity_type
->expects($this
->any())
->method('getBundleConfigDependency')
->will($this
->returnValue([
'type' => 'config',
'name' => 'test.test_entity_type.id',
]));
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->willReturnMap([
[
$this->entityTypeId,
TRUE,
$this->entityType,
],
[
'test_entity_type',
TRUE,
$target_entity_type,
],
]);
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefinition')
->with('test_field')
->willReturn([
'provider' => 'test_module',
'config_dependencies' => [
'module' => [
'test_module2',
],
],
'class' => '\\Drupal\\Tests\\field\\Unit\\DependencyFieldItem',
]);
$this->fieldStorage
->expects($this
->once())
->method('getConfigDependencyName')
->will($this
->returnValue('field.storage.test_entity_type.test_field'));
$field = new FieldConfig([
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'field_type' => 'test_field',
], $this->entityTypeId);
$dependencies = $field
->calculateDependencies()
->getDependencies();
$this
->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
$this
->assertContains('test.test_entity_type.id', $dependencies['config']);
$this
->assertEquals([
'test_module',
'test_module2',
'test_module3',
], $dependencies['module']);
}
public function testCalculateDependenciesIncorrectBundle() {
$storage = $this
->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
$storage
->expects($this
->any())
->method('load')
->with('test_bundle_not_exists')
->will($this
->returnValue(NULL));
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->with('bundle_entity_type')
->will($this
->returnValue($storage));
$target_entity_type = new EntityType([
'id' => 'test_entity_type',
'bundle_entity_type' => 'bundle_entity_type',
]);
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->willReturnMap([
[
$this->entityTypeId,
TRUE,
$this->entityType,
],
[
'test_entity_type',
TRUE,
$target_entity_type,
],
]);
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefinition')
->with('test_field')
->willReturn([
'provider' => 'test_module',
'config_dependencies' => [
'module' => [
'test_module2',
],
],
'class' => '\\Drupal\\Tests\\field\\Unit\\DependencyFieldItem',
]);
$field = new FieldConfig([
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle_not_exists',
'field_type' => 'test_field',
], $this->entityTypeId);
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage('Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists.');
$field
->calculateDependencies();
}
public function testOnDependencyRemoval() {
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefinition')
->with('test_field')
->willReturn([
'class' => '\\Drupal\\Tests\\field\\Unit\\DependencyFieldItem',
]);
$field = new FieldConfig([
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'field_type' => 'test_field',
'dependencies' => [
'module' => [
'fruiter',
],
],
'third_party_settings' => [
'fruiter' => [
'fruit' => 'apple',
],
],
]);
$changed = $field
->onDependencyRemoval([
'module' => [
'fruiter',
],
]);
$this
->assertTrue($changed);
}
public function testToArray() {
$field = new FieldConfig([
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'field_type' => 'test_field',
], $this->entityTypeId);
$expected = [
'id' => 'test_entity_type.test_bundle.field_test',
'uuid' => NULL,
'status' => TRUE,
'langcode' => 'en',
'field_name' => 'field_test',
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'label' => '',
'description' => '',
'required' => FALSE,
'default_value' => [],
'default_value_callback' => '',
'settings' => [],
'dependencies' => [],
'field_type' => 'test_field',
];
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->entityType
->expects($this
->once())
->method('getKey')
->with('id')
->will($this
->returnValue('id'));
$this->entityType
->expects($this
->once())
->method('getPropertiesToExport')
->with('test_entity_type.test_bundle.field_test')
->will($this
->returnValue(array_combine(array_keys($expected), array_keys($expected))));
$export = $field
->toArray();
$this
->assertEquals($expected, $export);
}
public function testGetType() {
$this->entityFieldManager
->expects($this
->never())
->method('getFieldStorageDefinitions');
$this->fieldStorage
->expects($this
->never())
->method('getType');
$field = new FieldConfig([
'field_name' => $this->fieldStorage
->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'field_type' => 'test_field',
], $this->entityTypeId);
$this
->assertEquals('test_field', $field
->getType());
}
}
class DependencyFieldItem {
public static function calculateDependencies(FieldDefinitionInterface $definition) {
return [
'module' => [
'test_module3',
],
];
}
public static function onDependencyRemoval($field_config, $dependencies) {
}
}