You are here

public function EntityDefinitionUpdateTest::testDefinitionEvents in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest::testDefinitionEvents()

Tests entity type and field storage definition events.

File

core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php, line 546
Contains \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest.

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testDefinitionEvents() {

  /** @var \Drupal\entity_test\EntityTestDefinitionSubscriber $event_subscriber */
  $event_subscriber = $this->container
    ->get('entity_test.definition.subscriber');
  $event_subscriber
    ->enableEventTracking();

  // Test field storage definition events.
  $storage_definition = current($this->entityManager
    ->getFieldStorageDefinitions('entity_test_rev'));
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete was not dispatched yet.');
  $this->entityManager
    ->onFieldStorageDefinitionDelete($storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete event successfully dispatched.');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create was not dispatched yet.');
  $this->entityManager
    ->onFieldStorageDefinitionCreate($storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create event successfully dispatched.');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update was not dispatched yet.');
  $this->entityManager
    ->onFieldStorageDefinitionUpdate($storage_definition, $storage_definition);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update event successfully dispatched.');

  // Test entity type events.
  $entity_type = $this->entityManager
    ->getDefinition('entity_test_rev');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create was not dispatched yet.');
  $this->entityManager
    ->onEntityTypeCreate($entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::CREATE), 'Entity type create event successfully dispatched.');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update was not dispatched yet.');
  $this->entityManager
    ->onEntityTypeUpdate($entity_type, $entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update event successfully dispatched.');
  $this
    ->assertFalse($event_subscriber
    ->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete was not dispatched yet.');
  $this->entityManager
    ->onEntityTypeDelete($entity_type);
  $this
    ->assertTrue($event_subscriber
    ->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete event successfully dispatched.');
}