d1824cfcceeda7c9c86b694f75a467d26a8fd10c
[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\field\Entity\FieldConfig;
14 use Drupal\Tests\UnitTestCase;
15
16 /**
17  * @coversDefaultClass \Drupal\field\Entity\FieldConfig
18  * @group field
19  */
20 class FieldConfigEntityUnitTest extends UnitTestCase {
21
22   /**
23    * The entity type used for testing.
24    *
25    * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $entityType;
28
29   /**
30    * The entity manager used for testing.
31    *
32    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $entityManager;
35
36   /**
37    * The ID of the type of the entity under test.
38    *
39    * @var string
40    */
41   protected $entityTypeId;
42
43   /**
44    * The UUID generator used for testing.
45    *
46    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $uuid;
49
50   /**
51    * The mock field storage.
52    *
53    * @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
54    */
55   protected $fieldStorage;
56
57   /**
58    * The typed configuration manager used for testing.
59    *
60    * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
61    */
62   protected $typedConfigManager;
63
64   /**
65    * The mock field type plugin manager;
66    *
67    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
68    */
69   protected $fieldTypePluginManager;
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function setUp() {
75     $this->entityTypeId = $this->randomMachineName();
76     $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
77
78     $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
79
80     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
81
82     $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
83
84     $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
85
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);
92
93     // Create a mock FieldStorageConfig object.
94     $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
95     $this->fieldStorage->expects($this->any())
96       ->method('getType')
97       ->will($this->returnValue('test_field'));
98     $this->fieldStorage->expects($this->any())
99       ->method('getName')
100       ->will($this->returnValue('field_test'));
101     $this->fieldStorage->expects($this->any())
102       ->method('getSettings')
103       ->willReturn([]);
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,
110       ]));
111   }
112
113   /**
114    * @covers ::calculateDependencies
115    */
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']));
122
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);
139
140     $this->fieldTypePluginManager->expects($this->any())
141       ->method('getDefinition')
142       ->with('test_field')
143       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
144
145     $this->fieldStorage->expects($this->once())
146       ->method('getConfigDependencyName')
147       ->will($this->returnValue('field.storage.test_entity_type.test_field'));
148
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']);
159   }
160
161   /**
162    * Test that invalid bundles are handled.
163    */
164   public function testCalculateDependenciesIncorrectBundle() {
165     $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
166     $storage->expects($this->any())
167       ->method('load')
168       ->with('test_bundle_not_exists')
169       ->will($this->returnValue(NULL));
170
171     $this->entityManager->expects($this->any())
172       ->method('getStorage')
173       ->with('bundle_entity_type')
174       ->will($this->returnValue($storage));
175
176     $target_entity_type = new EntityType([
177       'id' => 'test_entity_type',
178       'bundle_entity_type' => 'bundle_entity_type',
179     ]);
180
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);
197
198     $this->fieldTypePluginManager->expects($this->any())
199       ->method('getDefinition')
200       ->with('test_field')
201       ->willReturn(['provider' => 'test_module', 'config_dependencies' => ['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
202
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();
211   }
212
213   /**
214    * @covers ::onDependencyRemoval
215    */
216   public function testOnDependencyRemoval() {
217     $this->fieldTypePluginManager->expects($this->any())
218       ->method('getDefinition')
219       ->with('test_field')
220       ->willReturn(['class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
221
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',
227       'dependencies' => [
228         'module' => [
229           'fruiter',
230         ]
231       ],
232       'third_party_settings' => [
233         'fruiter' => [
234           'fruit' => 'apple',
235         ]
236       ]
237     ]);
238     $changed = $field->onDependencyRemoval(['module' => ['fruiter']]);
239     $this->assertTrue($changed);
240   }
241
242   /**
243    * @covers ::toArray
244    */
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);
252
253     $expected = [
254       'id' => 'test_entity_type.test_bundle.field_test',
255       'uuid' => NULL,
256       'status' => TRUE,
257       'langcode' => 'en',
258       'field_name' => 'field_test',
259       'entity_type' => 'test_entity_type',
260       'bundle' => 'test_bundle',
261       'label' => '',
262       'description' => '',
263       'required' => FALSE,
264       'default_value' => [],
265       'default_value_callback' => '',
266       'settings' => [],
267       'dependencies' => [],
268       'field_type' => 'test_field',
269     ];
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())
275       ->method('getKey')
276       ->with('id')
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), '')]));
281
282     $export = $field->toArray();
283     $this->assertEquals($expected, $export);
284   }
285
286   /**
287    * @covers ::getType
288    */
289   public function testGetType() {
290     // Ensure that FieldConfig::getType() is not delegated to
291     // FieldStorage.
292     $this->entityManager->expects($this->never())
293       ->method('getFieldStorageDefinitions');
294     $this->fieldStorage->expects($this->never())
295       ->method('getType');
296
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);
303
304     $this->assertEquals('test_field', $field->getType());
305   }
306
307 }
308
309 /**
310  * A test class.
311  *
312  * @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
313  */
314 class DependencyFieldItem {
315
316   public static function calculateDependencies(FieldDefinitionInterface $definition) {
317     return ['module' => ['test_module3']];
318   }
319
320   public static function onDependencyRemoval($field_config, $dependencies) {
321   }
322
323 }