5 * Contains \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest.
8 namespace Drupal\Tests\field\Unit;
10 use Drupal\Core\Entity\EntityType;
11 use Drupal\Core\Field\FieldDefinitionInterface;
12 use Drupal\Core\DependencyInjection\ContainerBuilder;
13 use Drupal\Core\Entity\EntityFieldManagerInterface;
14 use Drupal\Core\Entity\EntityManager;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\field\Entity\FieldConfig;
17 use Drupal\Tests\UnitTestCase;
20 * @coversDefaultClass \Drupal\field\Entity\FieldConfig
23 class FieldConfigEntityUnitTest extends UnitTestCase {
26 * The entity type used for testing.
28 * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
30 protected $entityType;
33 * The entity manager used for testing.
35 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
37 protected $entityManager;
40 * The entity type manager used for testing.
42 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected $entityTypeManager;
47 * The entity field manager used for testing.
49 * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $entityFieldManager;
54 * The ID of the type of the entity under test.
58 protected $entityTypeId;
61 * The UUID generator used for testing.
63 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
68 * The mock field storage.
70 * @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
72 protected $fieldStorage;
75 * The mock field type plugin manager;
77 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
79 protected $fieldTypePluginManager;
84 protected function setUp() {
85 $this->entityTypeId = $this->randomMachineName();
86 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
88 $this->entityManager = new EntityManager();
89 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
90 $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
92 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
94 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
96 $container = new ContainerBuilder();
97 $container->set('entity.manager', $this->entityManager);
98 $container->set('entity_field.manager', $this->entityFieldManager);
99 $container->set('entity_type.manager', $this->entityTypeManager);
100 $container->set('uuid', $this->uuid);
101 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
102 // Inject the container into entity.manager so it can defer to
103 // entity_type.manager, etc.
104 $this->entityManager->setContainer($container);
105 \Drupal::setContainer($container);
107 // Create a mock FieldStorageConfig object.
108 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
109 $this->fieldStorage->expects($this->any())
111 ->will($this->returnValue('test_field'));
112 $this->fieldStorage->expects($this->any())
114 ->will($this->returnValue('field_test'));
115 $this->fieldStorage->expects($this->any())
116 ->method('getSettings')
118 // Place the field in the mocked entity manager's field registry.
119 $this->entityFieldManager->expects($this->any())
120 ->method('getFieldStorageDefinitions')
121 ->with('test_entity_type')
122 ->will($this->returnValue([
123 $this->fieldStorage->getName() => $this->fieldStorage,
128 * @covers ::calculateDependencies
130 public function testCalculateDependencies() {
131 // Mock the interfaces necessary to create a dependency on a bundle entity.
132 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
133 $target_entity_type->expects($this->any())
134 ->method('getBundleConfigDependency')
135 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
137 $this->entityTypeManager->expects($this->at(0))
138 ->method('getDefinition')
139 ->with($this->entityTypeId)
140 ->willReturn($this->entityType);
141 $this->entityTypeManager->expects($this->at(1))
142 ->method('getDefinition')
143 ->with($this->entityTypeId)
144 ->willReturn($this->entityType);
145 $this->entityTypeManager->expects($this->at(2))
146 ->method('getDefinition')
147 ->with($this->entityTypeId)
148 ->willReturn($this->entityType);
149 $this->entityTypeManager->expects($this->at(3))
150 ->method('getDefinition')
151 ->with('test_entity_type')
152 ->willReturn($target_entity_type);
154 $this->fieldTypePluginManager->expects($this->any())
155 ->method('getDefinition')
157 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
159 $this->fieldStorage->expects($this->once())
160 ->method('getConfigDependencyName')
161 ->will($this->returnValue('field.storage.test_entity_type.test_field'));
163 $field = new FieldConfig([
164 'field_name' => $this->fieldStorage->getName(),
165 'entity_type' => 'test_entity_type',
166 'bundle' => 'test_bundle',
167 'field_type' => 'test_field',
168 ], $this->entityTypeId);
169 $dependencies = $field->calculateDependencies()->getDependencies();
170 $this->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
171 $this->assertContains('test.test_entity_type.id', $dependencies['config']);
172 $this->assertEquals(['test_module', 'test_module2', 'test_module3'], $dependencies['module']);
176 * Test that invalid bundles are handled.
178 public function testCalculateDependenciesIncorrectBundle() {
179 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
180 $storage->expects($this->any())
182 ->with('test_bundle_not_exists')
183 ->will($this->returnValue(NULL));
185 $this->entityTypeManager->expects($this->any())
186 ->method('getStorage')
187 ->with('bundle_entity_type')
188 ->will($this->returnValue($storage));
190 $target_entity_type = new EntityType([
191 'id' => 'test_entity_type',
192 'bundle_entity_type' => 'bundle_entity_type',
195 $this->entityTypeManager->expects($this->at(0))
196 ->method('getDefinition')
197 ->with($this->entityTypeId)
198 ->willReturn($this->entityType);
199 $this->entityTypeManager->expects($this->at(1))
200 ->method('getDefinition')
201 ->with($this->entityTypeId)
202 ->willReturn($this->entityType);
203 $this->entityTypeManager->expects($this->at(2))
204 ->method('getDefinition')
205 ->with($this->entityTypeId)
206 ->willReturn($this->entityType);
207 $this->entityTypeManager->expects($this->at(3))
208 ->method('getDefinition')
209 ->with('test_entity_type')
210 ->willReturn($target_entity_type);
212 $this->fieldTypePluginManager->expects($this->any())
213 ->method('getDefinition')
215 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
217 $field = new FieldConfig([
218 'field_name' => $this->fieldStorage->getName(),
219 'entity_type' => 'test_entity_type',
220 'bundle' => 'test_bundle_not_exists',
221 'field_type' => 'test_field',
222 ], $this->entityTypeId);
223 $this->setExpectedException(\LogicException::class, 'Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists.');
224 $field->calculateDependencies();
228 * @covers ::onDependencyRemoval
230 public function testOnDependencyRemoval() {
231 $this->fieldTypePluginManager->expects($this->any())
232 ->method('getDefinition')
234 ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
236 $field = new FieldConfig([
237 'field_name' => $this->fieldStorage->getName(),
238 'entity_type' => 'test_entity_type',
239 'bundle' => 'test_bundle',
240 'field_type' => 'test_field',
246 'third_party_settings' => [
252 $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
253 $this->assertTrue($changed);
259 public function testToArray() {
260 $field = new FieldConfig([
261 'field_name' => $this->fieldStorage->getName(),
262 'entity_type' => 'test_entity_type',
263 'bundle' => 'test_bundle',
264 'field_type' => 'test_field',
265 ], $this->entityTypeId);
268 'id' => 'test_entity_type.test_bundle.field_test',
272 'field_name' => 'field_test',
273 'entity_type' => 'test_entity_type',
274 'bundle' => 'test_bundle',
278 'default_value' => [],
279 'default_value_callback' => '',
281 'dependencies' => [],
282 'field_type' => 'test_field',
284 $this->entityTypeManager->expects($this->any())
285 ->method('getDefinition')
286 ->with($this->entityTypeId)
287 ->will($this->returnValue($this->entityType));
288 $this->entityType->expects($this->once())
291 ->will($this->returnValue('id'));
292 $this->entityType->expects($this->once())
293 ->method('getPropertiesToExport')
294 ->with('test_entity_type.test_bundle.field_test')
295 ->will($this->returnValue(array_combine(array_keys($expected), array_keys($expected))));
297 $export = $field->toArray();
298 $this->assertEquals($expected, $export);
304 public function testGetType() {
305 // Ensure that FieldConfig::getType() is not delegated to
307 $this->entityFieldManager->expects($this->never())
308 ->method('getFieldStorageDefinitions');
309 $this->fieldStorage->expects($this->never())
312 $field = new FieldConfig([
313 'field_name' => $this->fieldStorage->getName(),
314 'entity_type' => 'test_entity_type',
315 'bundle' => 'test_bundle',
316 'field_type' => 'test_field',
317 ], $this->entityTypeId);
319 $this->assertEquals('test_field', $field->getType());
327 * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
329 class DependencyFieldItem {
331 public static function calculateDependencies(FieldDefinitionInterface $definition) {
332 return ['module' => ['test_module3']];
335 public static function onDependencyRemoval($field_config, $dependencies) {