diff --git a/core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php b/core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php index 588c20111b..5467c07b19 100644 --- a/core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php @@ -59,6 +59,13 @@ class EntityTestDefinitionSubscriber implements EventSubscriberInterface, Entity */ protected $trackEvents = FALSE; + /** + * Determines whether the live definitions should be updated. + * + * @var bool + */ + protected $updateLiveDefinitions = FALSE; + /** * {@inheritdoc} */ @@ -88,7 +95,7 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { // Retrieve the live entity type definition in order to warm the static // cache and then insert the new entity type definition, so we can test that // the cache doesn't get stale after the event has fired. - if ($entity_type->id() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityTypeManager->getDefinition($entity_type->id()); $this->state->set('entity_test_rev.entity_type', $entity_type); } @@ -108,7 +115,7 @@ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeI // Retrieve the live entity type definition in order to warm the static // cache and then insert the new entity type definition, so we can test that // the cache doesn't get stale after the event has fired. - if ($entity_type->id() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityTypeManager->getDefinition($entity_type->id()); $this->state->set('entity_test_rev.entity_type', $entity_type); } @@ -133,7 +140,7 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { // Retrieve the live entity type definition in order to warm the static // cache and then delete the new entity type definition, so we can test that // the cache doesn't get stale after the event has fired. - if ($entity_type->id() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityTypeManager->getDefinition($entity_type->id()); $this->state->set('entity_test_rev.entity_type', ''); } @@ -151,7 +158,7 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $ // Retrieve the live field storage definitions in order to warm the static // cache and then insert the new storage definition, so we can test that the // cache doesn't get stale after the event has fired. - if ($storage_definition->getTargetEntityTypeId() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityFieldManager->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId()); $this->state->set('entity_test_rev.additional_base_field_definitions', [$storage_definition->getName() => $storage_definition]); } @@ -170,7 +177,7 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ // Retrieve the live field storage definitions in order to warm the static // cache and then insert the new storage definition, so we can test that the // cache doesn't get stale after the event has fired. - if ($storage_definition->getTargetEntityTypeId() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityFieldManager->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId()); $this->state->set('entity_test_rev.additional_base_field_definitions', [$storage_definition->getName() => $storage_definition]); } @@ -188,7 +195,7 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ // Retrieve the live field storage definitions in order to warm the static // cache and then remove the new storage definition, so we can test that the // cache doesn't get stale after the event has fired. - if ($storage_definition->getTargetEntityTypeId() === 'entity_test_rev') { + if ($this->updateLiveDefinitions) { $this->entityFieldManager->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId()); $this->state->set('entity_test_rev.additional_base_field_definitions', []); } @@ -201,6 +208,13 @@ public function enableEventTracking() { $this->trackEvents = TRUE; } + /** + * Enables live definition updates. + */ + public function enableLiveDefinitionUpdates() { + $this->updateLiveDefinitions = TRUE; + } + /** * Checks whether an event has been dispatched. * diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 4c2ca91716..af9a625264 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -37,6 +37,13 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { */ protected $entityDefinitionUpdateManager; + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + /** * The database connection. * @@ -57,6 +64,7 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { protected function setUp() { parent::setUp(); $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager'); + $this->entityFieldManager = $this->container->get('entity_field.manager'); $this->database = $this->container->get('database'); // Install every entity type's schema that wasn't installed in the parent @@ -167,7 +175,7 @@ public function testUpdateEntityTypeWithoutInCodeDefinition() { */ public function testUpdateFieldableEntityTypeWithoutInCodeDefinition() { $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update'); - $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update'); + $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_update'); // Remove the entity type definition. This is the same thing as removing the // code that defines it. @@ -835,6 +843,7 @@ public function testDefinitionEvents() { /** @var \Drupal\entity_test\EntityTestDefinitionSubscriber $event_subscriber */ $event_subscriber = $this->container->get('entity_test.definition.subscriber'); $event_subscriber->enableEventTracking(); + $event_subscriber->enableLiveDefinitionUpdates(); // Test field storage definition events. $storage_definition = FieldStorageDefinition::create('string') @@ -849,7 +858,7 @@ public function testDefinitionEvents() { // Check that the newly added field can be retrieved from the live field // storage definitions. - $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_rev'); + $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev'); $this->assertArrayHasKey('field_storage_test', $field_storage_definitions); $updated_storage_definition = clone $storage_definition; @@ -861,7 +870,7 @@ public function testDefinitionEvents() { // Check that the updated field can be retrieved from the live field storage // definitions. - $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_rev'); + $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev'); $this->assertEquals(new TranslatableMarkup('Updated field storage test'), $field_storage_definitions['field_storage_test']->getLabel()); $this->assertFalse($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete was not dispatched yet.'); @@ -871,7 +880,7 @@ public function testDefinitionEvents() { // Check that the deleted field can no longer be retrieved from the live // field storage definitions. - $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_rev'); + $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev'); $this->assertArrayNotHasKey('field_storage_test', $field_storage_definitions); // Test entity type events.