diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 0a7fa593a3..e9c8473453 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -116,7 +116,7 @@ public function createWithSampleValues($bundle = FALSE, array $values = []) { /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ $entity = $this->create($values); foreach ($entity as $field_name => $value) { - if (!in_array($field_name, $forbidden_keys)) { + if (!in_array($field_name, $forbidden_keys, TRUE)) { $entity->get($field_name)->generateSampleItems(); } } diff --git a/core/modules/user/src/TimeZoneItem.php b/core/modules/user/src/TimeZoneItem.php index 693d1cc2ac..d550644e68 100644 --- a/core/modules/user/src/TimeZoneItem.php +++ b/core/modules/user/src/TimeZoneItem.php @@ -17,7 +17,7 @@ class TimeZoneItem extends StringItem { public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $timezones = User::getAllowedTimezones(); // We need to vary the selected timezones since we're generating a sample. - $key = rand(0, count($timezones)); + $key = rand(0, count($timezones)-1); return $timezones[$key]; } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php b/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php index b52659e5bb..b5bcd487f2 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php @@ -51,24 +51,36 @@ protected function setUp() { /** * Tests sample value content entity creation of all types. * - * * @covers ::createWithSampleValues + * @covers ::createWithSampleValues */ public function testSampleValueContentEntity() { foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) { if ($definition->entityClassImplements(FieldableEntityInterface::class)) { + $label = $definition->getKey('label'); + $values = []; + if ($label) { + $title = $this->randomString(); + $values[$label] = $title; + } // 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()); + $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()); + } } } // Create sample entities without bundles. else { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(); + $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(FALSE, $values); $violations = $entity->validate(); $this->assertCount(0, $violations); + if ($label) { + $this->assertEquals($title, $entity->label()); + } } } }