Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Contains \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Drupal\Tests\field\Unit;
|
Chris@0
|
9
|
Chris@0
|
10 use Drupal\Core\Entity\EntityType;
|
Chris@0
|
11 use Drupal\Core\Field\FieldDefinitionInterface;
|
Chris@0
|
12 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@14
|
13 use Drupal\Core\Entity\EntityFieldManagerInterface;
|
Chris@14
|
14 use Drupal\Core\Entity\EntityManager;
|
Chris@14
|
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
16 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
17 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * @coversDefaultClass \Drupal\field\Entity\FieldConfig
|
Chris@0
|
21 * @group field
|
Chris@0
|
22 */
|
Chris@0
|
23 class FieldConfigEntityUnitTest extends UnitTestCase {
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The entity type used for testing.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $entityType;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * The entity manager used for testing.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $entityManager;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@14
|
40 * The entity type manager used for testing.
|
Chris@14
|
41 *
|
Chris@14
|
42 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@14
|
43 */
|
Chris@14
|
44 protected $entityTypeManager;
|
Chris@14
|
45
|
Chris@14
|
46 /**
|
Chris@14
|
47 * The entity field manager used for testing.
|
Chris@14
|
48 *
|
Chris@14
|
49 * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@14
|
50 */
|
Chris@14
|
51 protected $entityFieldManager;
|
Chris@14
|
52
|
Chris@14
|
53 /**
|
Chris@0
|
54 * The ID of the type of the entity under test.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @var string
|
Chris@0
|
57 */
|
Chris@0
|
58 protected $entityTypeId;
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * The UUID generator used for testing.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
64 */
|
Chris@0
|
65 protected $uuid;
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * The mock field storage.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
71 */
|
Chris@0
|
72 protected $fieldStorage;
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * The typed configuration manager used for testing.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
78 */
|
Chris@0
|
79 protected $typedConfigManager;
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * The mock field type plugin manager;
|
Chris@0
|
83 *
|
Chris@0
|
84 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
85 */
|
Chris@0
|
86 protected $fieldTypePluginManager;
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * {@inheritdoc}
|
Chris@0
|
90 */
|
Chris@0
|
91 protected function setUp() {
|
Chris@0
|
92 $this->entityTypeId = $this->randomMachineName();
|
Chris@0
|
93 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
|
Chris@0
|
94
|
Chris@14
|
95 $this->entityManager = new EntityManager();
|
Chris@14
|
96 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
|
Chris@14
|
97 $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
|
Chris@0
|
98
|
Chris@0
|
99 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
Chris@0
|
100
|
Chris@0
|
101 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
|
Chris@0
|
102
|
Chris@0
|
103 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
|
Chris@0
|
104
|
Chris@0
|
105 $container = new ContainerBuilder();
|
Chris@0
|
106 $container->set('entity.manager', $this->entityManager);
|
Chris@14
|
107 $container->set('entity_field.manager', $this->entityFieldManager);
|
Chris@14
|
108 $container->set('entity_type.manager', $this->entityTypeManager);
|
Chris@0
|
109 $container->set('uuid', $this->uuid);
|
Chris@0
|
110 $container->set('config.typed', $this->typedConfigManager);
|
Chris@0
|
111 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
|
Chris@14
|
112 // Inject the container into entity.manager so it can defer to
|
Chris@14
|
113 // entity_type.manager, etc.
|
Chris@14
|
114 $this->entityManager->setContainer($container);
|
Chris@0
|
115 \Drupal::setContainer($container);
|
Chris@0
|
116
|
Chris@0
|
117 // Create a mock FieldStorageConfig object.
|
Chris@0
|
118 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
|
Chris@0
|
119 $this->fieldStorage->expects($this->any())
|
Chris@0
|
120 ->method('getType')
|
Chris@0
|
121 ->will($this->returnValue('test_field'));
|
Chris@0
|
122 $this->fieldStorage->expects($this->any())
|
Chris@0
|
123 ->method('getName')
|
Chris@0
|
124 ->will($this->returnValue('field_test'));
|
Chris@0
|
125 $this->fieldStorage->expects($this->any())
|
Chris@0
|
126 ->method('getSettings')
|
Chris@0
|
127 ->willReturn([]);
|
Chris@0
|
128 // Place the field in the mocked entity manager's field registry.
|
Chris@14
|
129 $this->entityFieldManager->expects($this->any())
|
Chris@0
|
130 ->method('getFieldStorageDefinitions')
|
Chris@0
|
131 ->with('test_entity_type')
|
Chris@0
|
132 ->will($this->returnValue([
|
Chris@0
|
133 $this->fieldStorage->getName() => $this->fieldStorage,
|
Chris@0
|
134 ]));
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * @covers ::calculateDependencies
|
Chris@0
|
139 */
|
Chris@0
|
140 public function testCalculateDependencies() {
|
Chris@0
|
141 // Mock the interfaces necessary to create a dependency on a bundle entity.
|
Chris@0
|
142 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
Chris@0
|
143 $target_entity_type->expects($this->any())
|
Chris@0
|
144 ->method('getBundleConfigDependency')
|
Chris@0
|
145 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
|
Chris@0
|
146
|
Chris@14
|
147 $this->entityTypeManager->expects($this->at(0))
|
Chris@0
|
148 ->method('getDefinition')
|
Chris@0
|
149 ->with($this->entityTypeId)
|
Chris@0
|
150 ->willReturn($this->entityType);
|
Chris@14
|
151 $this->entityTypeManager->expects($this->at(1))
|
Chris@0
|
152 ->method('getDefinition')
|
Chris@0
|
153 ->with($this->entityTypeId)
|
Chris@0
|
154 ->willReturn($this->entityType);
|
Chris@14
|
155 $this->entityTypeManager->expects($this->at(2))
|
Chris@0
|
156 ->method('getDefinition')
|
Chris@0
|
157 ->with($this->entityTypeId)
|
Chris@0
|
158 ->willReturn($this->entityType);
|
Chris@14
|
159 $this->entityTypeManager->expects($this->at(3))
|
Chris@0
|
160 ->method('getDefinition')
|
Chris@0
|
161 ->with('test_entity_type')
|
Chris@0
|
162 ->willReturn($target_entity_type);
|
Chris@0
|
163
|
Chris@0
|
164 $this->fieldTypePluginManager->expects($this->any())
|
Chris@0
|
165 ->method('getDefinition')
|
Chris@0
|
166 ->with('test_field')
|
Chris@0
|
167 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
|
Chris@0
|
168
|
Chris@0
|
169 $this->fieldStorage->expects($this->once())
|
Chris@0
|
170 ->method('getConfigDependencyName')
|
Chris@0
|
171 ->will($this->returnValue('field.storage.test_entity_type.test_field'));
|
Chris@0
|
172
|
Chris@0
|
173 $field = new FieldConfig([
|
Chris@0
|
174 'field_name' => $this->fieldStorage->getName(),
|
Chris@0
|
175 'entity_type' => 'test_entity_type',
|
Chris@0
|
176 'bundle' => 'test_bundle',
|
Chris@0
|
177 'field_type' => 'test_field',
|
Chris@0
|
178 ], $this->entityTypeId);
|
Chris@0
|
179 $dependencies = $field->calculateDependencies()->getDependencies();
|
Chris@0
|
180 $this->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
|
Chris@0
|
181 $this->assertContains('test.test_entity_type.id', $dependencies['config']);
|
Chris@0
|
182 $this->assertEquals(['test_module', 'test_module2', 'test_module3'], $dependencies['module']);
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * Test that invalid bundles are handled.
|
Chris@0
|
187 */
|
Chris@0
|
188 public function testCalculateDependenciesIncorrectBundle() {
|
Chris@0
|
189 $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
|
Chris@0
|
190 $storage->expects($this->any())
|
Chris@0
|
191 ->method('load')
|
Chris@0
|
192 ->with('test_bundle_not_exists')
|
Chris@0
|
193 ->will($this->returnValue(NULL));
|
Chris@0
|
194
|
Chris@14
|
195 $this->entityTypeManager->expects($this->any())
|
Chris@0
|
196 ->method('getStorage')
|
Chris@0
|
197 ->with('bundle_entity_type')
|
Chris@0
|
198 ->will($this->returnValue($storage));
|
Chris@0
|
199
|
Chris@0
|
200 $target_entity_type = new EntityType([
|
Chris@0
|
201 'id' => 'test_entity_type',
|
Chris@0
|
202 'bundle_entity_type' => 'bundle_entity_type',
|
Chris@0
|
203 ]);
|
Chris@0
|
204
|
Chris@14
|
205 $this->entityTypeManager->expects($this->at(0))
|
Chris@0
|
206 ->method('getDefinition')
|
Chris@0
|
207 ->with($this->entityTypeId)
|
Chris@0
|
208 ->willReturn($this->entityType);
|
Chris@14
|
209 $this->entityTypeManager->expects($this->at(1))
|
Chris@0
|
210 ->method('getDefinition')
|
Chris@0
|
211 ->with($this->entityTypeId)
|
Chris@0
|
212 ->willReturn($this->entityType);
|
Chris@14
|
213 $this->entityTypeManager->expects($this->at(2))
|
Chris@0
|
214 ->method('getDefinition')
|
Chris@0
|
215 ->with($this->entityTypeId)
|
Chris@0
|
216 ->willReturn($this->entityType);
|
Chris@14
|
217 $this->entityTypeManager->expects($this->at(3))
|
Chris@0
|
218 ->method('getDefinition')
|
Chris@0
|
219 ->with('test_entity_type')
|
Chris@0
|
220 ->willReturn($target_entity_type);
|
Chris@0
|
221
|
Chris@0
|
222 $this->fieldTypePluginManager->expects($this->any())
|
Chris@0
|
223 ->method('getDefinition')
|
Chris@0
|
224 ->with('test_field')
|
Chris@0
|
225 ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
|
Chris@0
|
226
|
Chris@0
|
227 $field = new FieldConfig([
|
Chris@0
|
228 'field_name' => $this->fieldStorage->getName(),
|
Chris@0
|
229 'entity_type' => 'test_entity_type',
|
Chris@0
|
230 'bundle' => 'test_bundle_not_exists',
|
Chris@0
|
231 'field_type' => 'test_field',
|
Chris@0
|
232 ], $this->entityTypeId);
|
Chris@0
|
233 $this->setExpectedException(\LogicException::class, 'Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists.');
|
Chris@0
|
234 $field->calculateDependencies();
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * @covers ::onDependencyRemoval
|
Chris@0
|
239 */
|
Chris@0
|
240 public function testOnDependencyRemoval() {
|
Chris@0
|
241 $this->fieldTypePluginManager->expects($this->any())
|
Chris@0
|
242 ->method('getDefinition')
|
Chris@0
|
243 ->with('test_field')
|
Chris@0
|
244 ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
|
Chris@0
|
245
|
Chris@0
|
246 $field = new FieldConfig([
|
Chris@0
|
247 'field_name' => $this->fieldStorage->getName(),
|
Chris@0
|
248 'entity_type' => 'test_entity_type',
|
Chris@0
|
249 'bundle' => 'test_bundle',
|
Chris@0
|
250 'field_type' => 'test_field',
|
Chris@0
|
251 'dependencies' => [
|
Chris@0
|
252 'module' => [
|
Chris@0
|
253 'fruiter',
|
Chris@0
|
254 ]
|
Chris@0
|
255 ],
|
Chris@0
|
256 'third_party_settings' => [
|
Chris@0
|
257 'fruiter' => [
|
Chris@0
|
258 'fruit' => 'apple',
|
Chris@0
|
259 ]
|
Chris@0
|
260 ]
|
Chris@0
|
261 ]);
|
Chris@0
|
262 $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
|
Chris@0
|
263 $this->assertTrue($changed);
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * @covers ::toArray
|
Chris@0
|
268 */
|
Chris@0
|
269 public function testToArray() {
|
Chris@0
|
270 $field = new FieldConfig([
|
Chris@0
|
271 'field_name' => $this->fieldStorage->getName(),
|
Chris@0
|
272 'entity_type' => 'test_entity_type',
|
Chris@0
|
273 'bundle' => 'test_bundle',
|
Chris@0
|
274 'field_type' => 'test_field',
|
Chris@0
|
275 ], $this->entityTypeId);
|
Chris@0
|
276
|
Chris@0
|
277 $expected = [
|
Chris@0
|
278 'id' => 'test_entity_type.test_bundle.field_test',
|
Chris@0
|
279 'uuid' => NULL,
|
Chris@0
|
280 'status' => TRUE,
|
Chris@0
|
281 'langcode' => 'en',
|
Chris@0
|
282 'field_name' => 'field_test',
|
Chris@0
|
283 'entity_type' => 'test_entity_type',
|
Chris@0
|
284 'bundle' => 'test_bundle',
|
Chris@0
|
285 'label' => '',
|
Chris@0
|
286 'description' => '',
|
Chris@0
|
287 'required' => FALSE,
|
Chris@0
|
288 'default_value' => [],
|
Chris@0
|
289 'default_value_callback' => '',
|
Chris@0
|
290 'settings' => [],
|
Chris@0
|
291 'dependencies' => [],
|
Chris@0
|
292 'field_type' => 'test_field',
|
Chris@0
|
293 ];
|
Chris@14
|
294 $this->entityTypeManager->expects($this->any())
|
Chris@0
|
295 ->method('getDefinition')
|
Chris@0
|
296 ->with($this->entityTypeId)
|
Chris@0
|
297 ->will($this->returnValue($this->entityType));
|
Chris@0
|
298 $this->entityType->expects($this->once())
|
Chris@0
|
299 ->method('getKey')
|
Chris@0
|
300 ->with('id')
|
Chris@0
|
301 ->will($this->returnValue('id'));
|
Chris@0
|
302 $this->typedConfigManager->expects($this->once())
|
Chris@0
|
303 ->method('getDefinition')
|
Chris@0
|
304 ->will($this->returnValue(['mapping' => array_fill_keys(array_keys($expected), '')]));
|
Chris@0
|
305
|
Chris@0
|
306 $export = $field->toArray();
|
Chris@0
|
307 $this->assertEquals($expected, $export);
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 /**
|
Chris@0
|
311 * @covers ::getType
|
Chris@0
|
312 */
|
Chris@0
|
313 public function testGetType() {
|
Chris@0
|
314 // Ensure that FieldConfig::getType() is not delegated to
|
Chris@0
|
315 // FieldStorage.
|
Chris@14
|
316 $this->entityFieldManager->expects($this->never())
|
Chris@0
|
317 ->method('getFieldStorageDefinitions');
|
Chris@0
|
318 $this->fieldStorage->expects($this->never())
|
Chris@0
|
319 ->method('getType');
|
Chris@0
|
320
|
Chris@0
|
321 $field = new FieldConfig([
|
Chris@0
|
322 'field_name' => $this->fieldStorage->getName(),
|
Chris@0
|
323 'entity_type' => 'test_entity_type',
|
Chris@0
|
324 'bundle' => 'test_bundle',
|
Chris@0
|
325 'field_type' => 'test_field',
|
Chris@0
|
326 ], $this->entityTypeId);
|
Chris@0
|
327
|
Chris@0
|
328 $this->assertEquals('test_field', $field->getType());
|
Chris@0
|
329 }
|
Chris@0
|
330
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 /**
|
Chris@0
|
334 * A test class.
|
Chris@0
|
335 *
|
Chris@0
|
336 * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
|
Chris@0
|
337 */
|
Chris@0
|
338 class DependencyFieldItem {
|
Chris@0
|
339
|
Chris@0
|
340 public static function calculateDependencies(FieldDefinitionInterface $definition) {
|
Chris@0
|
341 return ['module' => ['test_module3']];
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 public static function onDependencyRemoval($field_config, $dependencies) {
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 }
|