b7dadf4926b11fccaaa9ea0ea17fa98362bd5cc0
[yaffs-website] / web / core / modules / field / tests / src / Unit / FieldConfigEntityUnitTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest.
6  */
7
8 namespace Drupal\Tests\field\Unit;
9
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;
18
19 /**
20  * @coversDefaultClass \Drupal\field\Entity\FieldConfig
21  * @group field
22  */
23 class FieldConfigEntityUnitTest extends UnitTestCase {
24
25   /**
26    * The entity type used for testing.
27    *
28    * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
29    */
30   protected $entityType;
31
32   /**
33    * The entity manager used for testing.
34    *
35    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
36    */
37   protected $entityManager;
38
39   /**
40    * The entity type manager used for testing.
41    *
42    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
43    */
44   protected $entityTypeManager;
45
46   /**
47    * The entity field manager used for testing.
48    *
49    * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50    */
51   protected $entityFieldManager;
52
53   /**
54    * The ID of the type of the entity under test.
55    *
56    * @var string
57    */
58   protected $entityTypeId;
59
60   /**
61    * The UUID generator used for testing.
62    *
63    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
64    */
65   protected $uuid;
66
67   /**
68    * The mock field storage.
69    *
70    * @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
71    */
72   protected $fieldStorage;
73
74   /**
75    * The typed configuration manager used for testing.
76    *
77    * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
78    */
79   protected $typedConfigManager;
80
81   /**
82    * The mock field type plugin manager;
83    *
84    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
85    */
86   protected $fieldTypePluginManager;
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function setUp() {
92     $this->entityTypeId = $this->randomMachineName();
93     $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
94
95     $this->entityManager = new EntityManager();
96     $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
97     $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
98
99     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
100
101     $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
102
103     $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
104
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);
116
117     // Create a mock FieldStorageConfig object.
118     $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
119     $this->fieldStorage->expects($this->any())
120       ->method('getType')
121       ->will($this->returnValue('test_field'));
122     $this->fieldStorage->expects($this->any())
123       ->method('getName')
124       ->will($this->returnValue('field_test'));
125     $this->fieldStorage->expects($this->any())
126       ->method('getSettings')
127       ->willReturn([]);
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,
134       ]));
135   }
136
137   /**
138    * @covers ::calculateDependencies
139    */
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']));
146
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);
163
164     $this->fieldTypePluginManager->expects($this->any())
165       ->method('getDefinition')
166       ->with('test_field')
167       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
168
169     $this->fieldStorage->expects($this->once())
170       ->method('getConfigDependencyName')
171       ->will($this->returnValue('field.storage.test_entity_type.test_field'));
172
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']);
183   }
184
185   /**
186    * Test that invalid bundles are handled.
187    */
188   public function testCalculateDependenciesIncorrectBundle() {
189     $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
190     $storage->expects($this->any())
191       ->method('load')
192       ->with('test_bundle_not_exists')
193       ->will($this->returnValue(NULL));
194
195     $this->entityTypeManager->expects($this->any())
196       ->method('getStorage')
197       ->with('bundle_entity_type')
198       ->will($this->returnValue($storage));
199
200     $target_entity_type = new EntityType([
201       'id' => 'test_entity_type',
202       'bundle_entity_type' => 'bundle_entity_type',
203     ]);
204
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);
221
222     $this->fieldTypePluginManager->expects($this->any())
223       ->method('getDefinition')
224       ->with('test_field')
225       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
226
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();
235   }
236
237   /**
238    * @covers ::onDependencyRemoval
239    */
240   public function testOnDependencyRemoval() {
241     $this->fieldTypePluginManager->expects($this->any())
242       ->method('getDefinition')
243       ->with('test_field')
244       ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
245
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       'dependencies' => [
252         'module' => [
253           'fruiter',
254         ]
255       ],
256       'third_party_settings' => [
257         'fruiter' => [
258           'fruit' => 'apple',
259         ]
260       ]
261     ]);
262     $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
263     $this->assertTrue($changed);
264   }
265
266   /**
267    * @covers ::toArray
268    */
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);
276
277     $expected = [
278       'id' => 'test_entity_type.test_bundle.field_test',
279       'uuid' => NULL,
280       'status' => TRUE,
281       'langcode' => 'en',
282       'field_name' => 'field_test',
283       'entity_type' => 'test_entity_type',
284       'bundle' => 'test_bundle',
285       'label' => '',
286       'description' => '',
287       'required' => FALSE,
288       'default_value' => [],
289       'default_value_callback' => '',
290       'settings' => [],
291       'dependencies' => [],
292       'field_type' => 'test_field',
293     ];
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())
299       ->method('getKey')
300       ->with('id')
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), '')]));
305
306     $export = $field->toArray();
307     $this->assertEquals($expected, $export);
308   }
309
310   /**
311    * @covers ::getType
312    */
313   public function testGetType() {
314     // Ensure that FieldConfig::getType() is not delegated to
315     // FieldStorage.
316     $this->entityFieldManager->expects($this->never())
317       ->method('getFieldStorageDefinitions');
318     $this->fieldStorage->expects($this->never())
319       ->method('getType');
320
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);
327
328     $this->assertEquals('test_field', $field->getType());
329   }
330
331 }
332
333 /**
334  * A test class.
335  *
336  * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
337  */
338 class DependencyFieldItem {
339
340   public static function calculateDependencies(FieldDefinitionInterface $definition) {
341     return ['module' => ['test_module3']];
342   }
343
344   public static function onDependencyRemoval($field_config, $dependencies) {
345   }
346
347 }