annotate core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php @ 19:fa3358dc1485 tip

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