Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
8 namespace Drupal\Tests\field\Unit; | 8 namespace Drupal\Tests\field\Unit; |
9 | 9 |
10 use Drupal\Core\Entity\EntityType; | 10 use Drupal\Core\Entity\EntityType; |
11 use Drupal\Core\Field\FieldDefinitionInterface; | 11 use Drupal\Core\Field\FieldDefinitionInterface; |
12 use Drupal\Core\DependencyInjection\ContainerBuilder; | 12 use Drupal\Core\DependencyInjection\ContainerBuilder; |
13 use Drupal\Core\Entity\EntityFieldManagerInterface; | |
14 use Drupal\Core\Entity\EntityManager; | |
15 use Drupal\Core\Entity\EntityTypeManagerInterface; | |
13 use Drupal\field\Entity\FieldConfig; | 16 use Drupal\field\Entity\FieldConfig; |
14 use Drupal\Tests\UnitTestCase; | 17 use Drupal\Tests\UnitTestCase; |
15 | 18 |
16 /** | 19 /** |
17 * @coversDefaultClass \Drupal\field\Entity\FieldConfig | 20 * @coversDefaultClass \Drupal\field\Entity\FieldConfig |
32 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject | 35 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
33 */ | 36 */ |
34 protected $entityManager; | 37 protected $entityManager; |
35 | 38 |
36 /** | 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 /** | |
37 * The ID of the type of the entity under test. | 54 * The ID of the type of the entity under test. |
38 * | 55 * |
39 * @var string | 56 * @var string |
40 */ | 57 */ |
41 protected $entityTypeId; | 58 protected $entityTypeId; |
73 */ | 90 */ |
74 protected function setUp() { | 91 protected function setUp() { |
75 $this->entityTypeId = $this->randomMachineName(); | 92 $this->entityTypeId = $this->randomMachineName(); |
76 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface'); | 93 $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface'); |
77 | 94 |
78 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); | 95 $this->entityManager = new EntityManager(); |
96 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class); | |
97 $this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class); | |
79 | 98 |
80 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); | 99 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); |
81 | 100 |
82 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); | 101 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); |
83 | 102 |
84 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface'); | 103 $this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface'); |
85 | 104 |
86 $container = new ContainerBuilder(); | 105 $container = new ContainerBuilder(); |
87 $container->set('entity.manager', $this->entityManager); | 106 $container->set('entity.manager', $this->entityManager); |
107 $container->set('entity_field.manager', $this->entityFieldManager); | |
108 $container->set('entity_type.manager', $this->entityTypeManager); | |
88 $container->set('uuid', $this->uuid); | 109 $container->set('uuid', $this->uuid); |
89 $container->set('config.typed', $this->typedConfigManager); | 110 $container->set('config.typed', $this->typedConfigManager); |
90 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); | 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); | |
91 \Drupal::setContainer($container); | 115 \Drupal::setContainer($container); |
92 | 116 |
93 // Create a mock FieldStorageConfig object. | 117 // Create a mock FieldStorageConfig object. |
94 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface'); | 118 $this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface'); |
95 $this->fieldStorage->expects($this->any()) | 119 $this->fieldStorage->expects($this->any()) |
100 ->will($this->returnValue('field_test')); | 124 ->will($this->returnValue('field_test')); |
101 $this->fieldStorage->expects($this->any()) | 125 $this->fieldStorage->expects($this->any()) |
102 ->method('getSettings') | 126 ->method('getSettings') |
103 ->willReturn([]); | 127 ->willReturn([]); |
104 // Place the field in the mocked entity manager's field registry. | 128 // Place the field in the mocked entity manager's field registry. |
105 $this->entityManager->expects($this->any()) | 129 $this->entityFieldManager->expects($this->any()) |
106 ->method('getFieldStorageDefinitions') | 130 ->method('getFieldStorageDefinitions') |
107 ->with('test_entity_type') | 131 ->with('test_entity_type') |
108 ->will($this->returnValue([ | 132 ->will($this->returnValue([ |
109 $this->fieldStorage->getName() => $this->fieldStorage, | 133 $this->fieldStorage->getName() => $this->fieldStorage, |
110 ])); | 134 ])); |
118 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); | 142 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); |
119 $target_entity_type->expects($this->any()) | 143 $target_entity_type->expects($this->any()) |
120 ->method('getBundleConfigDependency') | 144 ->method('getBundleConfigDependency') |
121 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id'])); | 145 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id'])); |
122 | 146 |
123 $this->entityManager->expects($this->at(0)) | 147 $this->entityTypeManager->expects($this->at(0)) |
124 ->method('getDefinition') | 148 ->method('getDefinition') |
125 ->with($this->entityTypeId) | 149 ->with($this->entityTypeId) |
126 ->willReturn($this->entityType); | 150 ->willReturn($this->entityType); |
127 $this->entityManager->expects($this->at(1)) | 151 $this->entityTypeManager->expects($this->at(1)) |
128 ->method('getDefinition') | 152 ->method('getDefinition') |
129 ->with($this->entityTypeId) | 153 ->with($this->entityTypeId) |
130 ->willReturn($this->entityType); | 154 ->willReturn($this->entityType); |
131 $this->entityManager->expects($this->at(2)) | 155 $this->entityTypeManager->expects($this->at(2)) |
132 ->method('getDefinition') | 156 ->method('getDefinition') |
133 ->with($this->entityTypeId) | 157 ->with($this->entityTypeId) |
134 ->willReturn($this->entityType); | 158 ->willReturn($this->entityType); |
135 $this->entityManager->expects($this->at(3)) | 159 $this->entityTypeManager->expects($this->at(3)) |
136 ->method('getDefinition') | 160 ->method('getDefinition') |
137 ->with('test_entity_type') | 161 ->with('test_entity_type') |
138 ->willReturn($target_entity_type); | 162 ->willReturn($target_entity_type); |
139 | 163 |
140 $this->fieldTypePluginManager->expects($this->any()) | 164 $this->fieldTypePluginManager->expects($this->any()) |
166 $storage->expects($this->any()) | 190 $storage->expects($this->any()) |
167 ->method('load') | 191 ->method('load') |
168 ->with('test_bundle_not_exists') | 192 ->with('test_bundle_not_exists') |
169 ->will($this->returnValue(NULL)); | 193 ->will($this->returnValue(NULL)); |
170 | 194 |
171 $this->entityManager->expects($this->any()) | 195 $this->entityTypeManager->expects($this->any()) |
172 ->method('getStorage') | 196 ->method('getStorage') |
173 ->with('bundle_entity_type') | 197 ->with('bundle_entity_type') |
174 ->will($this->returnValue($storage)); | 198 ->will($this->returnValue($storage)); |
175 | 199 |
176 $target_entity_type = new EntityType([ | 200 $target_entity_type = new EntityType([ |
177 'id' => 'test_entity_type', | 201 'id' => 'test_entity_type', |
178 'bundle_entity_type' => 'bundle_entity_type', | 202 'bundle_entity_type' => 'bundle_entity_type', |
179 ]); | 203 ]); |
180 | 204 |
181 $this->entityManager->expects($this->at(0)) | 205 $this->entityTypeManager->expects($this->at(0)) |
182 ->method('getDefinition') | 206 ->method('getDefinition') |
183 ->with($this->entityTypeId) | 207 ->with($this->entityTypeId) |
184 ->willReturn($this->entityType); | 208 ->willReturn($this->entityType); |
185 $this->entityManager->expects($this->at(1)) | 209 $this->entityTypeManager->expects($this->at(1)) |
186 ->method('getDefinition') | 210 ->method('getDefinition') |
187 ->with($this->entityTypeId) | 211 ->with($this->entityTypeId) |
188 ->willReturn($this->entityType); | 212 ->willReturn($this->entityType); |
189 $this->entityManager->expects($this->at(2)) | 213 $this->entityTypeManager->expects($this->at(2)) |
190 ->method('getDefinition') | 214 ->method('getDefinition') |
191 ->with($this->entityTypeId) | 215 ->with($this->entityTypeId) |
192 ->willReturn($this->entityType); | 216 ->willReturn($this->entityType); |
193 $this->entityManager->expects($this->at(3)) | 217 $this->entityTypeManager->expects($this->at(3)) |
194 ->method('getDefinition') | 218 ->method('getDefinition') |
195 ->with('test_entity_type') | 219 ->with('test_entity_type') |
196 ->willReturn($target_entity_type); | 220 ->willReturn($target_entity_type); |
197 | 221 |
198 $this->fieldTypePluginManager->expects($this->any()) | 222 $this->fieldTypePluginManager->expects($this->any()) |
265 'default_value_callback' => '', | 289 'default_value_callback' => '', |
266 'settings' => [], | 290 'settings' => [], |
267 'dependencies' => [], | 291 'dependencies' => [], |
268 'field_type' => 'test_field', | 292 'field_type' => 'test_field', |
269 ]; | 293 ]; |
270 $this->entityManager->expects($this->any()) | 294 $this->entityTypeManager->expects($this->any()) |
271 ->method('getDefinition') | 295 ->method('getDefinition') |
272 ->with($this->entityTypeId) | 296 ->with($this->entityTypeId) |
273 ->will($this->returnValue($this->entityType)); | 297 ->will($this->returnValue($this->entityType)); |
274 $this->entityType->expects($this->once()) | 298 $this->entityType->expects($this->once()) |
275 ->method('getKey') | 299 ->method('getKey') |
287 * @covers ::getType | 311 * @covers ::getType |
288 */ | 312 */ |
289 public function testGetType() { | 313 public function testGetType() { |
290 // Ensure that FieldConfig::getType() is not delegated to | 314 // Ensure that FieldConfig::getType() is not delegated to |
291 // FieldStorage. | 315 // FieldStorage. |
292 $this->entityManager->expects($this->never()) | 316 $this->entityFieldManager->expects($this->never()) |
293 ->method('getFieldStorageDefinitions'); | 317 ->method('getFieldStorageDefinitions'); |
294 $this->fieldStorage->expects($this->never()) | 318 $this->fieldStorage->expects($this->never()) |
295 ->method('getType'); | 319 ->method('getType'); |
296 | 320 |
297 $field = new FieldConfig([ | 321 $field = new FieldConfig([ |