10c0a7d9f5a6c77ccea109cf0b307b65ac83f5ea
[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 mock field type plugin manager;
76    *
77    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
78    */
79   protected $fieldTypePluginManager;
80
81   /**
82    * {@inheritdoc}
83    */
84   protected function setUp() {
85     $this->entityTypeId = $this->randomMachineName();
86     $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
87
88     $this->entityManager = new EntityManager();
89     $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
90     $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
91
92     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
93
94     $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
95
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);
106
107     // Create a mock FieldStorageConfig object.
108     $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
109     $this->fieldStorage->expects($this->any())
110       ->method('getType')
111       ->will($this->returnValue('test_field'));
112     $this->fieldStorage->expects($this->any())
113       ->method('getName')
114       ->will($this->returnValue('field_test'));
115     $this->fieldStorage->expects($this->any())
116       ->method('getSettings')
117       ->willReturn([]);
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,
124       ]));
125   }
126
127   /**
128    * @covers ::calculateDependencies
129    */
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']));
136
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);
153
154     $this->fieldTypePluginManager->expects($this->any())
155       ->method('getDefinition')
156       ->with('test_field')
157       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
158
159     $this->fieldStorage->expects($this->once())
160       ->method('getConfigDependencyName')
161       ->will($this->returnValue('field.storage.test_entity_type.test_field'));
162
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']);
173   }
174
175   /**
176    * Test that invalid bundles are handled.
177    */
178   public function testCalculateDependenciesIncorrectBundle() {
179     $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
180     $storage->expects($this->any())
181       ->method('load')
182       ->with('test_bundle_not_exists')
183       ->will($this->returnValue(NULL));
184
185     $this->entityTypeManager->expects($this->any())
186       ->method('getStorage')
187       ->with('bundle_entity_type')
188       ->will($this->returnValue($storage));
189
190     $target_entity_type = new EntityType([
191       'id' => 'test_entity_type',
192       'bundle_entity_type' => 'bundle_entity_type',
193     ]);
194
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);
211
212     $this->fieldTypePluginManager->expects($this->any())
213       ->method('getDefinition')
214       ->with('test_field')
215       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
216
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();
225   }
226
227   /**
228    * @covers ::onDependencyRemoval
229    */
230   public function testOnDependencyRemoval() {
231     $this->fieldTypePluginManager->expects($this->any())
232       ->method('getDefinition')
233       ->with('test_field')
234       ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
235
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',
241       'dependencies' => [
242         'module' => [
243           'fruiter',
244         ],
245       ],
246       'third_party_settings' => [
247         'fruiter' => [
248           'fruit' => 'apple',
249         ],
250       ],
251     ]);
252     $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
253     $this->assertTrue($changed);
254   }
255
256   /**
257    * @covers ::toArray
258    */
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);
266
267     $expected = [
268       'id' => 'test_entity_type.test_bundle.field_test',
269       'uuid' => NULL,
270       'status' => TRUE,
271       'langcode' => 'en',
272       'field_name' => 'field_test',
273       'entity_type' => 'test_entity_type',
274       'bundle' => 'test_bundle',
275       'label' => '',
276       'description' => '',
277       'required' => FALSE,
278       'default_value' => [],
279       'default_value_callback' => '',
280       'settings' => [],
281       'dependencies' => [],
282       'field_type' => 'test_field',
283     ];
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())
289       ->method('getKey')
290       ->with('id')
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))));
296
297     $export = $field->toArray();
298     $this->assertEquals($expected, $export);
299   }
300
301   /**
302    * @covers ::getType
303    */
304   public function testGetType() {
305     // Ensure that FieldConfig::getType() is not delegated to
306     // FieldStorage.
307     $this->entityFieldManager->expects($this->never())
308       ->method('getFieldStorageDefinitions');
309     $this->fieldStorage->expects($this->never())
310       ->method('getType');
311
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);
318
319     $this->assertEquals('test_field', $field->getType());
320   }
321
322 }
323
324 /**
325  * A test class.
326  *
327  * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
328  */
329 class DependencyFieldItem {
330
331   public static function calculateDependencies(FieldDefinitionInterface $definition) {
332     return ['module' => ['test_module3']];
333   }
334
335   public static function onDependencyRemoval($field_config, $dependencies) {
336   }
337
338 }