View source
<?php
namespace Drupal\Tests\field\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\UnitTestCase;
class FieldStorageConfigEntityUnitTest extends UnitTestCase {
protected $entityManager;
protected $entityTypeId;
protected $uuid;
protected $fieldTypeManager;
protected function setUp() {
$this->entityManager = $this
->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
$this->uuid = $this
->getMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->fieldTypeManager = $this
->getMock(FieldTypePluginManagerInterface::class);
$container = new ContainerBuilder();
$container
->set('entity.manager', $this->entityManager);
$container
->set('uuid', $this->uuid);
$container
->set('plugin.manager.field.field_type', $this->fieldTypeManager);
\Drupal::setContainer($container);
}
public function testCalculateDependencies() {
$fieldStorageConfigentityType = $this
->getMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$fieldStorageConfigentityType
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('field'));
$attached_entity_type_id = $this
->randomMachineName();
$attached_entity_type = $this
->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$attached_entity_type
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('entity_provider_module'));
$this->entityManager
->expects($this
->any())
->method('getDefinition')
->willReturnMap([
[
'field_storage_config',
TRUE,
$fieldStorageConfigentityType,
],
[
$attached_entity_type_id,
TRUE,
$attached_entity_type,
],
]);
$this->fieldTypeManager
->expects($this
->atLeastOnce())
->method('getDefinition')
->with('test_field_type', FALSE)
->willReturn([
'class' => TestFieldType::class,
]);
$field_storage = new FieldStorageConfig([
'entity_type' => $attached_entity_type_id,
'field_name' => 'test_field',
'type' => 'test_field_type',
'module' => 'test_module',
]);
$dependencies = $field_storage
->calculateDependencies()
->getDependencies();
$this
->assertEquals([
'entity_provider_module',
'entity_test',
'test_module',
], $dependencies['module']);
$this
->assertEquals([
'stark',
], $dependencies['theme']);
}
}
class TestFieldType {
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
$dependencies = [];
$dependencies['module'] = [
'entity_test',
];
$dependencies['theme'] = [
'stark',
];
return $dependencies;
}
}