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\field\Entity\FieldConfig;
14 use Drupal\Tests\UnitTestCase;
17 * @coversDefaultClass \Drupal\field\Entity\FieldConfig
20 class FieldConfigEntityUnitTest extends UnitTestCase {
23 * The entity type used for testing.
25 * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityType;
30 * The entity manager used for testing.
32 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
34 protected $entityManager;
37 * The ID of the type of the entity under test.
41 protected $entityTypeId;
44 * The UUID generator used for testing.
46 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
51 * The mock field storage.
53 * @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
55 protected $fieldStorage;
58 * The typed configuration manager used for testing.
60 * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
62 protected $typedConfigManager;
65 * The mock field type plugin manager;
67 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
69 protected $fieldTypePluginManager;
74 protected function setUp() {
75 $this->entityTypeId = $this->randomMachineName();
76 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
78 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
80 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
82 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
84 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
86 $container = new ContainerBuilder();
87 $container->set('entity.manager', $this->entityManager);
88 $container->set('uuid', $this->uuid);
89 $container->set('config.typed', $this->typedConfigManager);
90 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
91 \Drupal::setContainer($container);
93 // Create a mock FieldStorageConfig object.
94 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
95 $this->fieldStorage->expects($this->any())
97 ->will($this->returnValue('test_field'));
98 $this->fieldStorage->expects($this->any())
100 ->will($this->returnValue('field_test'));
101 $this->fieldStorage->expects($this->any())
102 ->method('getSettings')
104 // Place the field in the mocked entity manager's field registry.
105 $this->entityManager->expects($this->any())
106 ->method('getFieldStorageDefinitions')
107 ->with('test_entity_type')
108 ->will($this->returnValue([
109 $this->fieldStorage->getName() => $this->fieldStorage,
114 * @covers ::calculateDependencies
116 public function testCalculateDependencies() {
117 // Mock the interfaces necessary to create a dependency on a bundle entity.
118 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
119 $target_entity_type->expects($this->any())
120 ->method('getBundleConfigDependency')
121 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
123 $this->entityManager->expects($this->at(0))
124 ->method('getDefinition')
125 ->with($this->entityTypeId)
126 ->willReturn($this->entityType);
127 $this->entityManager->expects($this->at(1))
128 ->method('getDefinition')
129 ->with($this->entityTypeId)
130 ->willReturn($this->entityType);
131 $this->entityManager->expects($this->at(2))
132 ->method('getDefinition')
133 ->with($this->entityTypeId)
134 ->willReturn($this->entityType);
135 $this->entityManager->expects($this->at(3))
136 ->method('getDefinition')
137 ->with('test_entity_type')
138 ->willReturn($target_entity_type);
140 $this->fieldTypePluginManager->expects($this->any())
141 ->method('getDefinition')
143 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
145 $this->fieldStorage->expects($this->once())
146 ->method('getConfigDependencyName')
147 ->will($this->returnValue('field.storage.test_entity_type.test_field'));
149 $field = new FieldConfig([
150 'field_name' => $this->fieldStorage->getName(),
151 'entity_type' => 'test_entity_type',
152 'bundle' => 'test_bundle',
153 'field_type' => 'test_field',
154 ], $this->entityTypeId);
155 $dependencies = $field->calculateDependencies()->getDependencies();
156 $this->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
157 $this->assertContains('test.test_entity_type.id', $dependencies['config']);
158 $this->assertEquals(['test_module', 'test_module2', 'test_module3'], $dependencies['module']);
162 * Test that invalid bundles are handled.
164 public function testCalculateDependenciesIncorrectBundle() {
165 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
166 $storage->expects($this->any())
168 ->with('test_bundle_not_exists')
169 ->will($this->returnValue(NULL));
171 $this->entityManager->expects($this->any())
172 ->method('getStorage')
173 ->with('bundle_entity_type')
174 ->will($this->returnValue($storage));
176 $target_entity_type = new EntityType([
177 'id' => 'test_entity_type',
178 'bundle_entity_type' => 'bundle_entity_type',
181 $this->entityManager->expects($this->at(0))
182 ->method('getDefinition')
183 ->with($this->entityTypeId)
184 ->willReturn($this->entityType);
185 $this->entityManager->expects($this->at(1))
186 ->method('getDefinition')
187 ->with($this->entityTypeId)
188 ->willReturn($this->entityType);
189 $this->entityManager->expects($this->at(2))
190 ->method('getDefinition')
191 ->with($this->entityTypeId)
192 ->willReturn($this->entityType);
193 $this->entityManager->expects($this->at(3))
194 ->method('getDefinition')
195 ->with('test_entity_type')
196 ->willReturn($target_entity_type);
198 $this->fieldTypePluginManager->expects($this->any())
199 ->method('getDefinition')
201 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
203 $field = new FieldConfig([
204 'field_name' => $this->fieldStorage->getName(),
205 'entity_type' => 'test_entity_type',
206 'bundle' => 'test_bundle_not_exists',
207 'field_type' => 'test_field',
208 ], $this->entityTypeId);
209 $this->setExpectedException(\LogicException::class, 'Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists.');
210 $field->calculateDependencies();
214 * @covers ::onDependencyRemoval
216 public function testOnDependencyRemoval() {
217 $this->fieldTypePluginManager->expects($this->any())
218 ->method('getDefinition')
220 ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
222 $field = new FieldConfig([
223 'field_name' => $this->fieldStorage->getName(),
224 'entity_type' => 'test_entity_type',
225 'bundle' => 'test_bundle',
226 'field_type' => 'test_field',
232 'third_party_settings' => [
238 $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
239 $this->assertTrue($changed);
245 public function testToArray() {
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',
251 ], $this->entityTypeId);
254 'id' => 'test_entity_type.test_bundle.field_test',
258 'field_name' => 'field_test',
259 'entity_type' => 'test_entity_type',
260 'bundle' => 'test_bundle',
264 'default_value' => [],
265 'default_value_callback' => '',
267 'dependencies' => [],
268 'field_type' => 'test_field',
270 $this->entityManager->expects($this->any())
271 ->method('getDefinition')
272 ->with($this->entityTypeId)
273 ->will($this->returnValue($this->entityType));
274 $this->entityType->expects($this->once())
277 ->will($this->returnValue('id'));
278 $this->typedConfigManager->expects($this->once())
279 ->method('getDefinition')
280 ->will($this->returnValue(['mapping' => array_fill_keys(array_keys($expected), '')]));
282 $export = $field->toArray();
283 $this->assertEquals($expected, $export);
289 public function testGetType() {
290 // Ensure that FieldConfig::getType() is not delegated to
292 $this->entityManager->expects($this->never())
293 ->method('getFieldStorageDefinitions');
294 $this->fieldStorage->expects($this->never())
297 $field = new FieldConfig([
298 'field_name' => $this->fieldStorage->getName(),
299 'entity_type' => 'test_entity_type',
300 'bundle' => 'test_bundle',
301 'field_type' => 'test_field',
302 ], $this->entityTypeId);
304 $this->assertEquals('test_field', $field->getType());
312 * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
314 class DependencyFieldItem {
316 public static function calculateDependencies(FieldDefinitionInterface $definition) {
317 return ['module' => ['test_module3']];
320 public static function onDependencyRemoval($field_config, $dependencies) {