You are here

public function EntityDefinitionUpdateTest::testDefinitionEvents in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testDefinitionEvents()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testDefinitionEvents()

Tests entity type and field storage definition events.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php, line 932

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

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')
    ->setName('field_storage_test')
    ->setLabel(new TranslatableMarkup('Field storage test'))
    ->setTargetEntityTypeId('entity_test_rev');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create was not dispatched yet.');
  \Drupal::service('field_storage_definition.listener')
    ->onFieldStorageDefinitionCreate($storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::CREATE), 'Last installed field storage definition was created before the event was fired.');

  // Check that the newly added field can be retrieved from the live field
  // storage definitions.
  $field_storage_definitions = $this->entityFieldManager
    ->getFieldStorageDefinitions('entity_test_rev');
  $this
    ->assertArrayHasKey('field_storage_test', $field_storage_definitions);
  $updated_storage_definition = clone $storage_definition;
  $updated_storage_definition
    ->setLabel(new TranslatableMarkup('Updated field storage test'));
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update was not dispatched yet.');
  \Drupal::service('field_storage_definition.listener')
    ->onFieldStorageDefinitionUpdate($updated_storage_definition, $storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::UPDATE), 'Last installed field storage definition was updated before the event was fired.');

  // Check that the updated field can be retrieved from the live field storage
  // definitions.
  $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.');
  \Drupal::service('field_storage_definition.listener')
    ->onFieldStorageDefinitionDelete($storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::DELETE), 'Last installed field storage definition was deleted before the event was fired.');

  // Check that the deleted field can no longer be retrieved from the live
  // field storage definitions.
  $field_storage_definitions = $this->entityFieldManager
    ->getFieldStorageDefinitions('entity_test_rev');
  $this
    ->assertArrayNotHasKey('field_storage_test', $field_storage_definitions);

  // Test entity type events.
  $entity_type = $this->entityTypeManager
    ->getDefinition('entity_test_rev');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create was not dispatched yet.');
  \Drupal::service('entity_type.listener')
    ->onEntityTypeCreate($entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(EntityTypeEvents::CREATE), 'Last installed entity type definition was created before the event was fired.');
  $updated_entity_type = clone $entity_type;
  $updated_entity_type
    ->set('label', new TranslatableMarkup('Updated entity test rev'));
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update was not dispatched yet.');
  \Drupal::service('entity_type.listener')
    ->onEntityTypeUpdate($updated_entity_type, $entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(EntityTypeEvents::UPDATE), 'Last installed entity type definition was updated before the event was fired.');

  // Check that the updated definition can be retrieved from the live entity
  // type definitions.
  $entity_type = $this->entityTypeManager
    ->getDefinition('entity_test_rev');
  $this
    ->assertEquals(new TranslatableMarkup('Updated entity test rev'), $entity_type
    ->getLabel());
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete was not dispatched yet.');
  \Drupal::service('entity_type.listener')
    ->onEntityTypeDelete($entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete event successfully dispatched.');
  $this
    ->assertTrue($event_subscriber
    ->hasDefinitionBeenUpdated(EntityTypeEvents::DELETE), 'Last installed entity type definition was deleted before the event was fired.');

  // Check that the deleted entity type can no longer be retrieved from the
  // live entity type definitions.
  $this
    ->assertNull($this->entityTypeManager
    ->getDefinition('entity_test_rev', FALSE));
}