diff --git a/core/lib/Drupal/Core/Entity/EntityGenerator.php b/core/lib/Drupal/Core/Entity/EntityGenerator.php index 62c3791576..7c2bfd9aec 100644 --- a/core/lib/Drupal/Core/Entity/EntityGenerator.php +++ b/core/lib/Drupal/Core/Entity/EntityGenerator.php @@ -2,7 +2,7 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Config\Entity\ConfigEntityType; +use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\Exception\EntityGeneratorMismatchException; use Drupal\Core\Entity\Exception\MissingEntityGeneratorHandlerException; @@ -39,26 +39,27 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) { * {@inheritdoc} */ public function generateFieldableEntity($entity_type_id, $bundle, array $values = []) { - $entityType = $this->entityTypeManager->getDefinition($entity_type_id); - if (!$entityType->entityClassImplements(FieldableEntityInterface::class)) { + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { throw new EntityGeneratorMismatchException("Provided entity type \"%s\" is not fieldable. Use \Drupal\Core\Entity\EntityGenerator::generateConfigurationEntity() instead", $entity_type_id); } - $bundle_key = $entityType->getKey('bundle'); + $bundle_key = $entity_type->getKey('bundle'); $values[$bundle_key] = $bundle; // Allow custom entity generation per entity type. - if ($entityType->hasHandlerClass('generator')) { + if ($entity_type->hasHandlerClass('generator')) { /** @var \Drupal\Core\Entity\GeneratorHandlerInterface $generator */ $generator = $this->entityTypeManager->getHandler($entity_type_id, 'generator'); $entity = $generator->generate($values); } else { - // Bundle is already set, and id and revision should never be set. + // Bundle is already set, and ID and revision should never be set. $forbidden_keys = [ - $entityType->getKey('id'), - $entityType->getKey('revision'), + $entity_type->getKey('id'), + $entity_type->getKey('revision'), $bundle_key ]; + // Forbid sample generation on any keys whose values were submitted. $forbidden_keys = array_merge($forbidden_keys, array_keys($values)); // Fallback entity generation in case no custom generator is supplied. /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ @@ -76,13 +77,13 @@ public function generateFieldableEntity($entity_type_id, $bundle, array $values * {@inheritdoc} */ public function generateConfigurationEntity($entity_type_id, array $values = []) { - $definition = $this->entityTypeManager->getDefinition($entity_type_id); - if (!$definition instanceof ConfigEntityType) { + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + if (!$entity_type->entityClassImplements(ConfigEntityInterface::class)) { throw new EntityGeneratorMismatchException("Provided entity type \"%s\" is not a configuration entity. Use \Drupal\Core\Entity\EntityGenerator::generateFieldableEntity() instead", $entity_type_id); } // Allow custom entity generation per entity type. /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - if ($definition->hasHandlerClass('generator')) { + if ($entity_type->hasHandlerClass('generator')) { /** @var \Drupal\Core\Entity\GeneratorHandlerInterface $generator */ $generator = $this->entityTypeManager->getHandler($entity_type_id, 'generator'); return $generator->generate($values); diff --git a/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php b/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php index 7fea3bec3e..2b199a578c 100644 --- a/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php @@ -22,7 +22,7 @@ * @param string $bundle * The entity bundle. * @param array $values - * Any default values to use during generation. + * (optional) Any default values to use during generation. * * @return \Drupal\Core\Entity\FieldableEntityInterface * A fieldable content entity. @@ -38,7 +38,7 @@ public function generateFieldableEntity($entity_type_id, $bundle, array $values * @param string $entity_type_id * The entity type ID. * @param array $values - * Any default values to use during generation. + * (optional) Any default values to use during generation. * * @return \Drupal\Core\Entity\EntityInterface * The generated entity. diff --git a/core/lib/Drupal/Core/Entity/Exception/MissingEntityGeneratorHandlerException.php b/core/lib/Drupal/Core/Entity/Exception/MissingEntityGeneratorHandlerException.php index 59540dedf9..475a5eff12 100644 --- a/core/lib/Drupal/Core/Entity/Exception/MissingEntityGeneratorHandlerException.php +++ b/core/lib/Drupal/Core/Entity/Exception/MissingEntityGeneratorHandlerException.php @@ -17,11 +17,18 @@ class MissingEntityGeneratorHandlerException extends \Exception { /** * Constructs an MissingEntityGeneratorHandlerException. * + * For the remaining parameters see \Exception. + * * @param string $entity_type_id * The entity ID. + * + * @see \Exception */ - public function __construct($entity_type_id) { - parent::__construct(sprintf("Missing generator handler. The \"%s\" entity type cannot be generated", $entity_type_id)); + public function __construct($entity_type_id, $message = '', $code = 0, \Exception $previous = NULL) { + if (empty($message)) { + $message = sprintf("Missing generator handler. The \"%s\" entity type cannot be generated", $entity_type_id); + } + parent::__construct($message, $code, $previous); } } diff --git a/core/lib/Drupal/Core/Entity/GeneratorHandlerInterface.php b/core/lib/Drupal/Core/Entity/GeneratorHandlerInterface.php index 140be68491..4d7a41c3a0 100644 --- a/core/lib/Drupal/Core/Entity/GeneratorHandlerInterface.php +++ b/core/lib/Drupal/Core/Entity/GeneratorHandlerInterface.php @@ -19,7 +19,7 @@ * Generate an entity of the type to which the handler is attached. * * @param array $values - * An array of values to include as part of the entity. + * (optional) An array of values to include as part of the entity. * * @return \Drupal\Core\Entity\EntityInterface */ diff --git a/core/modules/user/tests/src/Kernel/UserEntityTest.php b/core/modules/user/tests/src/Kernel/UserEntityTest.php index 1c501f049e..73fe869ec3 100644 --- a/core/modules/user/tests/src/Kernel/UserEntityTest.php +++ b/core/modules/user/tests/src/Kernel/UserEntityTest.php @@ -22,6 +22,14 @@ class UserEntityTest extends KernelTestBase { public static $modules = ['system', 'user', 'field']; /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $this->installEntitySchema('user'); + } + + /** * Tests some of the methods. * * @see \Drupal\user\Entity\User::getRoles() @@ -65,4 +73,22 @@ public function testUserMethods() { $this->assertEqual([RoleInterface::AUTHENTICATED_ID, 'test_role_two'], $user->getRoles()); } + /** + * Tests that all user fields validate properly. + * + * @see \Drupal\Core\Field\FieldItemListInterface::generateSampleItems + * @see \Drupal\Core\Field\FieldItemInterface::generateSampleValue() + * @see \Drupal\Core\Entity\FieldableEntityInterface::validate() + */ + public function testUserValidation() { + $user = User::create([]); + foreach ($user as $field_name => $field) { + if (!in_array($field_name, ['uid'])) { + $user->$field_name->generateSampleItems(); + } + } + $violations = $user->validate(); + $this->assertFalse((bool) $violations->count()); + } + }