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