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 typed configuration manager used for testing.
77 * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
79 protected $typedConfigManager;
82 * The mock field type plugin manager;
84 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
86 protected $fieldTypePluginManager;
91 protected function setUp() {
92 $this->entityTypeId = $this->randomMachineName();
93 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
95 $this->entityManager = new EntityManager();
96 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
97 $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
99 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
101 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
103 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
105 $container = new ContainerBuilder();
106 $container->set('entity.manager', $this->entityManager);
107 $container->set('entity_field.manager', $this->entityFieldManager);
108 $container->set('entity_type.manager', $this->entityTypeManager);
109 $container->set('uuid', $this->uuid);
110 $container->set('config.typed', $this->typedConfigManager);
111 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
112 // Inject the container into entity.manager so it can defer to
113 // entity_type.manager, etc.
114 $this->entityManager->setContainer($container);
115 \Drupal::setContainer($container);
117 // Create a mock FieldStorageConfig object.
118 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
119 $this->fieldStorage->expects($this->any())
121 ->will($this->returnValue('test_field'));
122 $this->fieldStorage->expects($this->any())
124 ->will($this->returnValue('field_test'));
125 $this->fieldStorage->expects($this->any())
126 ->method('getSettings')
128 // Place the field in the mocked entity manager's field registry.
129 $this->entityFieldManager->expects($this->any())
130 ->method('getFieldStorageDefinitions')
131 ->with('test_entity_type')
132 ->will($this->returnValue([
133 $this->fieldStorage->getName() => $this->fieldStorage,
138 * @covers ::calculateDependencies
140 public function testCalculateDependencies() {
141 // Mock the interfaces necessary to create a dependency on a bundle entity.
142 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
143 $target_entity_type->expects($this->any())
144 ->method('getBundleConfigDependency')
145 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
147 $this->entityTypeManager->expects($this->at(0))
148 ->method('getDefinition')
149 ->with($this->entityTypeId)
150 ->willReturn($this->entityType);
151 $this->entityTypeManager->expects($this->at(1))
152 ->method('getDefinition')
153 ->with($this->entityTypeId)
154 ->willReturn($this->entityType);
155 $this->entityTypeManager->expects($this->at(2))
156 ->method('getDefinition')
157 ->with($this->entityTypeId)
158 ->willReturn($this->entityType);
159 $this->entityTypeManager->expects($this->at(3))
160 ->method('getDefinition')
161 ->with('test_entity_type')
162 ->willReturn($target_entity_type);
164 $this->fieldTypePluginManager->expects($this->any())
165 ->method('getDefinition')
167 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
169 $this->fieldStorage->expects($this->once())
170 ->method('getConfigDependencyName')
171 ->will($this->returnValue('field.storage.test_entity_type.test_field'));
173 $field = new FieldConfig([
174 'field_name' => $this->fieldStorage->getName(),
175 'entity_type' => 'test_entity_type',
176 'bundle' => 'test_bundle',
177 'field_type' => 'test_field',
178 ], $this->entityTypeId);
179 $dependencies = $field->calculateDependencies()->getDependencies();
180 $this->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
181 $this->assertContains('test.test_entity_type.id', $dependencies['config']);
182 $this->assertEquals(['test_module', 'test_module2', 'test_module3'], $dependencies['module']);
186 * Test that invalid bundles are handled.
188 public function testCalculateDependenciesIncorrectBundle() {
189 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
190 $storage->expects($this->any())
192 ->with('test_bundle_not_exists')
193 ->will($this->returnValue(NULL));
195 $this->entityTypeManager->expects($this->any())
196 ->method('getStorage')
197 ->with('bundle_entity_type')
198 ->will($this->returnValue($storage));
200 $target_entity_type = new EntityType([
201 'id' => 'test_entity_type',
202 'bundle_entity_type' => 'bundle_entity_type',
205 $this->entityTypeManager->expects($this->at(0))
206 ->method('getDefinition')
207 ->with($this->entityTypeId)
208 ->willReturn($this->entityType);
209 $this->entityTypeManager->expects($this->at(1))
210 ->method('getDefinition')
211 ->with($this->entityTypeId)
212 ->willReturn($this->entityType);
213 $this->entityTypeManager->expects($this->at(2))
214 ->method('getDefinition')
215 ->with($this->entityTypeId)
216 ->willReturn($this->entityType);
217 $this->entityTypeManager->expects($this->at(3))
218 ->method('getDefinition')
219 ->with('test_entity_type')
220 ->willReturn($target_entity_type);
222 $this->fieldTypePluginManager->expects($this->any())
223 ->method('getDefinition')
225 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
227 $field = new FieldConfig([
228 'field_name' => $this->fieldStorage->getName(),
229 'entity_type' => 'test_entity_type',
230 'bundle' => 'test_bundle_not_exists',
231 'field_type' => 'test_field',
232 ], $this->entityTypeId);
233 $this->setExpectedException(\LogicException::class, 'Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists.');
234 $field->calculateDependencies();
238 * @covers ::onDependencyRemoval
240 public function testOnDependencyRemoval() {
241 $this->fieldTypePluginManager->expects($this->any())
242 ->method('getDefinition')
244 ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
246 $field = new FieldConfig([
247 'field_name' => $this->fieldStorage->getName(),
248 'entity_type' => 'test_entity_type',
249 'bundle' => 'test_bundle',
250 'field_type' => 'test_field',
256 'third_party_settings' => [
262 $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
263 $this->assertTrue($changed);
269 public function testToArray() {
270 $field = new FieldConfig([
271 'field_name' => $this->fieldStorage->getName(),
272 'entity_type' => 'test_entity_type',
273 'bundle' => 'test_bundle',
274 'field_type' => 'test_field',
275 ], $this->entityTypeId);
278 'id' => 'test_entity_type.test_bundle.field_test',
282 'field_name' => 'field_test',
283 'entity_type' => 'test_entity_type',
284 'bundle' => 'test_bundle',
288 'default_value' => [],
289 'default_value_callback' => '',
291 'dependencies' => [],
292 'field_type' => 'test_field',
294 $this->entityTypeManager->expects($this->any())
295 ->method('getDefinition')
296 ->with($this->entityTypeId)
297 ->will($this->returnValue($this->entityType));
298 $this->entityType->expects($this->once())
301 ->will($this->returnValue('id'));
302 $this->typedConfigManager->expects($this->once())
303 ->method('getDefinition')
304 ->will($this->returnValue(['mapping' => array_fill_keys(array_keys($expected), '')]));
306 $export = $field->toArray();
307 $this->assertEquals($expected, $export);
313 public function testGetType() {
314 // Ensure that FieldConfig::getType() is not delegated to
316 $this->entityFieldManager->expects($this->never())
317 ->method('getFieldStorageDefinitions');
318 $this->fieldStorage->expects($this->never())
321 $field = new FieldConfig([
322 'field_name' => $this->fieldStorage->getName(),
323 'entity_type' => 'test_entity_type',
324 'bundle' => 'test_bundle',
325 'field_type' => 'test_field',
326 ], $this->entityTypeId);
328 $this->assertEquals('test_field', $field->getType());
336 * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
338 class DependencyFieldItem {
340 public static function calculateDependencies(FieldDefinitionInterface $definition) {
341 return ['module' => ['test_module3']];
344 public static function onDependencyRemoval($field_config, $dependencies) {