diff --git a/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php b/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php index 31088e9e88..37dac651e2 100644 --- a/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php +++ b/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeListenerInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\State\StateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -55,11 +56,16 @@ public static function getSubscribedEvents() { * {@inheritdoc} */ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) { + // Only add the new base field when a test needs it. + if (!$this->state->get('entity_test_update.install_new_base_field_during_update', FALSE)) { + return; + } + // Add a new base field when the entity type is updated. $definitions = $this->state->get('entity_test_update.additional_base_field_definitions', []); $definitions['new_base_field'] = BaseFieldDefinition::create('string') ->setName('new_base_field') - ->setLabel(t('A new base field')); + ->setLabel(new TranslatableMarkup('A new base field')); $this->state->set('entity_test_update.additional_base_field_definitions', $definitions); $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test_update', $definitions['new_base_field']); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php index 937a2ab5a4..a525f650eb 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php @@ -151,13 +151,17 @@ public function testFieldableEntityTypeUpdates($initial_rev, $initial_mul, $new_ $this->assertEntityData($initial_rev, $initial_mul); } + // Enable the creation of a new base field during a fieldable entity type + // update. + $this->state->set('entity_test_update.install_new_base_field_during_update', TRUE); + // Simulate a batch run since we are converting the entities one by one. $sandbox = []; do { $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions, $sandbox); } while ($sandbox['#finished'] != 1); - $this->assertEntityTypeSchema($new_rev, $new_mul); + $this->assertEntityTypeSchema($new_rev, $new_mul, TRUE); $this->assertEntityData($initial_rev, $initial_mul); $change_list = $this->entityDefinitionUpdateManager->getChangeList(); @@ -427,8 +431,20 @@ protected function assertEntityData($revisionable, $translatable) { * Whether the entity type is revisionable or not. * @param bool $translatable * Whether the entity type is translatable or not. + * @param bool $new_base_field + * (optional) Whether a new base field was added as part of the update. + * Defaults to FALSE. */ - protected function assertEntityTypeSchema($revisionable, $translatable) { + protected function assertEntityTypeSchema($revisionable, $translatable, $new_base_field = FALSE) { + // Check whether the 'new_base_field' field has been installed correctly. + $field_storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('new_base_field', $this->entityTypeId); + if ($new_base_field) { + $this->assertNotNull($field_storage_definition); + } + else { + $this->assertNull($field_storage_definition); + } + if ($revisionable && $translatable) { $this->assertRevisionableAndTranslatable(); }