You are here

public function BaseFieldTest::testEntityTypeDependencies in Field Encryption 3.0.x

Tests that uninstalling a module providing an entity type works.

File

tests/src/Functional/BaseFieldTest.php, line 279

Class

BaseFieldTest
Tests base field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEntityTypeDependencies() {
  $this
    ->setFieldStorageSettings(TRUE);

  /** @var \Drupal\field_encrypt\Entity\FieldEncryptEntityType $config_entity */
  $config_entity = \Drupal::entityTypeManager()
    ->getStorage('field_encrypt_entity_type')
    ->load('node');
  $this
    ->assertNotNull($config_entity);
  $old_config = $config_entity
    ->getBaseFields();

  // Simulate config data to import.
  $active = $this->container
    ->get('config.storage');
  $sync = $this->container
    ->get('config.storage.sync');
  $this
    ->copyConfig($active, $sync);
  \Drupal::service('module_installer')
    ->uninstall([
    'node',
  ]);
  $config_entity = \Drupal::entityTypeManager()
    ->getStorage('field_encrypt_entity_type')
    ->load('node');
  $this
    ->assertNull($config_entity);
  \Drupal::service('module_installer')
    ->uninstall([
    'field_encrypt',
    'field_encrypt_test',
  ]);
  $this
    ->configImporter()
    ->import();
  $config_entity = \Drupal::entityTypeManager()
    ->getStorage('field_encrypt_entity_type')
    ->load('node');
  $this
    ->assertSame($old_config, $config_entity
    ->getBaseFields());
  $this
    ->assertFalse($this
    ->configImporter()
    ->reset()
    ->hasUnprocessedConfigurationChanges());

  // Save test entity.
  $this
    ->createNode([
    'title' => 'Test title en',
  ]);

  // Add a base field to the node entity.
  // @see field_encrypt_test_entity_base_field_info()
  $this
    ->assertSame([], \Drupal::entityDefinitionUpdateManager()
    ->getChangeList());
  $this
    ->assertFalse(\Drupal::entityDefinitionUpdateManager()
    ->needsUpdates());
  \Drupal::state()
    ->set('field_encrypt.create_base_field', TRUE);
  $this
    ->assertTrue(\Drupal::entityDefinitionUpdateManager()
    ->needsUpdates());
  $this
    ->applyEntityUpdates();

  // Remove title encryption and set up encryption on the test base field.
  $config_entity = \Drupal::entityTypeManager()
    ->getStorage('field_encrypt_entity_type')
    ->load('node');
  $config_entity
    ->setBaseFields([
    'field_encrypt_test_base_field' => [
      'value',
    ],
  ])
    ->save();

  /** @var \Drupal\Core\Queue\QueueInterface $queue */
  $queue = \Drupal::service('queue')
    ->get('field_encrypt_update_entity_encryption');
  $this
    ->assertSame(1, $queue
    ->numberOfItems());
  $this
    ->cronRun();

  // Cause the base field to be removed.
  \Drupal::state()
    ->set('field_encrypt.create_base_field', FALSE);
  $this
    ->applyEntityUpdates();
  $config_entity = \Drupal::entityTypeManager()
    ->getStorage('field_encrypt_entity_type')
    ->load('node');
  $this
    ->assertNull($config_entity);
  $queue = \Drupal::service('queue')
    ->get('field_encrypt_update_entity_encryption');
  $this
    ->assertSame(1, $queue
    ->numberOfItems());
}