diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 610cda4..ae6574f 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -82,7 +82,7 @@ protected function doCreate(array $values) { /** * {@inheritdoc} */ - public function createWithSampleValues($bundle = FALSE, array $values = [], $required_only = FALSE) { + public function createWithSampleValues($bundle = FALSE, array $values = []) { // ID and revision should never have sample values generated for them. $forbidden_keys = [ $this->entityType->getKey('id'), @@ -90,22 +90,28 @@ public function createWithSampleValues($bundle = FALSE, array $values = [], $req if ($revision_key = $this->entityType->getKey('revision')) { $forbidden_keys[] = $revision_key; } - if ($bundle && $bundle_key = $this->entityType->getKey('bundle')) { - $values[$bundle_key] = $bundle; + if ($bundle_key = $this->entityType->getKey('bundle')) { + if ($bundle) { + if (!array_key_exists($bundle, $this->entityManager->getBundleInfo($this->entityTypeId))) { + throw new EntityStorageException(sprintf("Missing entity bundle. The \"%s\" bundle does not exist", $bundle)); + } + $values[$bundle_key] = $bundle; + } + // Bundle should not be autogenerated. + $forbidden_keys[] = $bundle_key; } - if ($published_key = $this->entityType->getKey('published')) { - $values[$published_key] = TRUE; - } - if ($status_key = $this->entityType->getKey('status')) { - $values[$status_key] = TRUE; + + // Ensure that default_langcode is TRUE. + if ($default_langcode_key = $this->entityType->getKey('default_langcode')) { + $values[$default_langcode_key] = TRUE; } + // Forbid sample generation on any keys whose values were submitted. $forbidden_keys = array_merge($forbidden_keys, array_keys($values)); /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ $entity = $this->create($values); foreach ($entity as $field_name => $value) { - $definition = $entity->getFieldDefinition($field_name); - if (!in_array($field_name, $forbidden_keys, TRUE) && (!$required_only || ($definition && $definition->isRequired()))) { + if (!in_array($field_name, $forbidden_keys, TRUE)) { $entity->get($field_name)->generateSampleItems(); } } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php index cd79530..eb979c7 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php @@ -23,6 +23,7 @@ */ public function createTranslation(ContentEntityInterface $entity, $langcode, array $values = []); + /** * Creates an entity with sample field values. * @@ -30,9 +31,6 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr * (optional) The entity bundle. * @param array $values * (optional) Any default values to use during generation. - * @param bool $required_only - * (optional) If TRUE, only create sample values for required fields. - * Defaults to FALSE. * * @return \Drupal\Core\Entity\FieldableEntityInterface * A fieldable content entity. @@ -40,6 +38,6 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr * @throws \Drupal\Core\Entity\EntityStorageException * Thrown if the bundle does not exist or was needed but not specified. */ - public function createWithSampleValues($bundle = FALSE, array $values = [], $required_only = FALSE); + public function createWithSampleValues($bundle = FALSE, array $values = []); } diff --git a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueContentEntityStorage.php b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueContentEntityStorage.php index 9510963..b3fc12d 100644 --- a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueContentEntityStorage.php @@ -21,7 +21,6 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr /** * {@inheritdoc} */ - public function createWithSampleValues($bundle = FALSE, array $values = [], $required_only = FALSE) { - } + public function createWithSampleValues($bundle = FALSE, array $values = []) {} } diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index 5d13a5e..4bfcbf9 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -260,8 +260,11 @@ public function view($display_options = []) { public function generateSampleItems($count = 1) { $field_definition = $this->getFieldDefinition(); $field_type_class = $field_definition->getItemDefinition()->getClass(); + $values = $this->getValue(); for ($delta = 0; $delta < $count; $delta++) { - $values[$delta] = $field_type_class::generateSampleValue($field_definition); + if ($value = $field_type_class::generateSampleValue($field_definition)) { + $values[$delta] = $value; + } } $this->setValue($values); } diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 4270a1f..b526363 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -153,7 +153,7 @@ protected function updateModeratedEntity($moderation_state_id) { // Change the entity's default revision flag and the publishing status only // if the new workflow state is a valid one. - if ($workflow->getTypePlugin()->hasState($moderation_state_id)) { + if ($workflow && $workflow->getTypePlugin()->hasState($moderation_state_id)) { /** @var \Drupal\content_moderation\ContentModerationState $current_state */ $current_state = $workflow->getTypePlugin()->getState($moderation_state_id); diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 9b5b67d..63c7a0d 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -149,7 +149,7 @@ public function postSave($update) { */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $random = new Random(); - $values['alias'] = str_replace(' ', '-', strtolower($random->sentences(3))); + $values['alias'] = '/' . str_replace(' ', '-', strtolower($random->sentences(3))); return $values; } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php b/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php index b5bcd48..5bbce65 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php @@ -2,7 +2,8 @@ namespace Drupal\KernelTests\Core\Entity; -use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Entity\ContentEntityStorageInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\NodeType; use Drupal\taxonomy\Entity\Vocabulary; @@ -25,7 +26,21 @@ class CreateSampleEntityTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['system', 'field', 'filter', 'text', 'file', 'user', 'node', 'comment', 'taxonomy']; + public static $modules = [ + 'system', + 'field', + 'filter', + 'text', + 'file', + 'user', + 'node', + 'comment', + 'taxonomy', + 'menu_link_content', + 'link', + 'content_translation', + 'language', + ]; /** * {@inheritdoc} @@ -33,6 +48,8 @@ class CreateSampleEntityTest extends KernelTestBase { protected function setUp() { parent::setup(); + $this->installSchema('system', ['sequences']); + $this->installEntitySchema('file'); $this->installEntitySchema('user'); $this->installEntitySchema('node'); @@ -42,6 +59,7 @@ protected function setUp() { $this->installEntitySchema('comment_type'); $this->installEntitySchema('taxonomy_vocabulary'); $this->installEntitySchema('taxonomy_term'); + $this->installEntitySchema('menu_link_content'); $this->entityTypeManager = $this->container->get('entity_type.manager'); NodeType::create(['type' => 'article', 'name' => 'Article'])->save(); NodeType::create(['type' => 'page', 'name' => 'Page'])->save(); @@ -55,31 +73,32 @@ protected function setUp() { */ public function testSampleValueContentEntity() { foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) { - if ($definition->entityClassImplements(FieldableEntityInterface::class)) { - $label = $definition->getKey('label'); + $entity_storage = $this->entityTypeManager->getStorage($entity_type_id); + if ($entity_storage instanceof ContentEntityStorageInterface) { $values = []; - if ($label) { - $title = $this->randomString(); - $values[$label] = $title; + if ($label = $definition->getKey('label')) { + $values[$label] = $this->randomString(); } + // Create sample entities with bundles. if ($bundle_type = $definition->getBundleEntityType()) { - foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id(), $values); - $violations = $entity->validate(); - $this->assertCount(0, $violations); - if ($label) { - $this->assertEquals($title, $entity->label()); - } - } + $bundles = array_map(function (EntityInterface $entity_type) { + return $entity_type->id(); + }, $this->entityTypeManager->getStorage($bundle_type)->loadMultiple()); } // Create sample entities without bundles. else { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(FALSE, $values); + $bundles = [FALSE]; + } + foreach ($bundles as $bundle) { + $entity = $entity_storage->createWithSampleValues($bundle, $values); + $entity->save(); + $entity_storage->resetCache([$entity->id()]); + $entity = $entity_storage->load($entity->id()); $violations = $entity->validate(); - $this->assertCount(0, $violations); + $this->assertCount(0, $violations, (string) $violations); if ($label) { - $this->assertEquals($title, $entity->label()); + $this->assertEquals($values[$label], $entity->label()); } } } diff --git a/core/tests/Drupal/KernelTests/Core/Form/FormElementTitleTest.php b/core/tests/Drupal/KernelTests/Core/Form/FormElementTitleTest.php index f291e0b..4ffa6a4 100644 --- a/core/tests/Drupal/KernelTests/Core/Form/FormElementTitleTest.php +++ b/core/tests/Drupal/KernelTests/Core/Form/FormElementTitleTest.php @@ -345,7 +345,7 @@ protected function createEntities() { if ($entity_storage instanceof ContentEntityStorageInterface) { $bundle_entity_type_id = $entity_type->getBundleEntityType(); $bundle = $bundle_entity_type_id ? $entities[$bundle_entity_type_id]->id() : FALSE; - $entity = $entity_storage->createWithSampleValues($bundle, $entity_values[$entity_type_id], TRUE); + $entity = $entity_storage->createWithSampleValues($bundle, $entity_values[$entity_type_id]); } else { $values = $entity_values[$entity_type_id];