5 * Contains \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest.
8 namespace Drupal\Tests\field\Unit;
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 use Drupal\Core\Field\FieldTypePluginManagerInterface;
13 use Drupal\field\Entity\FieldStorageConfig;
14 use Drupal\Tests\UnitTestCase;
17 * @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
21 class FieldStorageConfigEntityUnitTest extends UnitTestCase {
24 * The entity manager used for testing.
26 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $entityManager;
31 * The ID of the type of the entity under test.
35 protected $entityTypeId;
38 * The UUID generator used for testing.
40 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
45 * The field type manager.
47 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
49 protected $fieldTypeManager;
54 protected function setUp() {
55 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
56 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
57 $this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
59 $container = new ContainerBuilder();
60 $container->set('entity.manager', $this->entityManager);
61 $container->set('uuid', $this->uuid);
62 $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
63 \Drupal::setContainer($container);
67 * @covers ::calculateDependencies
69 public function testCalculateDependencies() {
70 // Create a mock entity type for FieldStorageConfig.
71 $fieldStorageConfigentityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
72 $fieldStorageConfigentityType->expects($this->any())
73 ->method('getProvider')
74 ->will($this->returnValue('field'));
76 // Create a mock entity type to attach the field to.
77 $attached_entity_type_id = $this->randomMachineName();
78 $attached_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
79 $attached_entity_type->expects($this->any())
80 ->method('getProvider')
81 ->will($this->returnValue('entity_provider_module'));
83 // Get definition is called three times. Twice in
84 // ConfigEntityBase::addDependency() to get the provider of the field config
85 // entity type and once in FieldStorageConfig::calculateDependencies() to
86 // get the provider of the entity type that field is attached to.
87 $this->entityManager->expects($this->any())
88 ->method('getDefinition')
90 ['field_storage_config', TRUE, $fieldStorageConfigentityType],
91 [$attached_entity_type_id, TRUE, $attached_entity_type],
94 $this->fieldTypeManager->expects($this->atLeastOnce())
95 ->method('getDefinition')
96 ->with('test_field_type', FALSE)
98 'class' => TestFieldType::class,
101 $field_storage = new FieldStorageConfig([
102 'entity_type' => $attached_entity_type_id,
103 'field_name' => 'test_field',
104 'type' => 'test_field_type',
105 'module' => 'test_module',
108 $dependencies = $field_storage->calculateDependencies()->getDependencies();
109 $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']);
110 $this->assertEquals(['stark'], $dependencies['theme']);
116 * A test class to test field storage dependencies.
118 * @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
120 class TestFieldType {
125 public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
127 $dependencies['module'] = ['entity_test'];
128 $dependencies['theme'] = ['stark'];
130 return $dependencies;