You are here

public function UninstallTest::testDisableWithExistingContent in Multiversion 8

Tests uninstalling the module when there is existing content.

File

tests/src/Functional/UninstallTest.php, line 81

Class

UninstallTest
Test the UninstallTest class.

Namespace

Drupal\Tests\multiversion\Functional

Code

public function testDisableWithExistingContent() {
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  foreach ($this->entityTypes as $entity_type_id => $values) {
    $storage = $entity_type_manager
      ->getStorage($entity_type_id);
    $count = 2;
    for ($i = 0; $i < $count; $i++) {
      $storage
        ->create($values)
        ->save();
    }
    $count_before[$entity_type_id] = $count;
  }

  /** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
  $manager = $this->container
    ->get('multiversion.manager');

  // Disable entity types.
  $manager
    ->disableEntityTypes();

  // Delete workspace entities before uninstall.
  $storage = $entity_type_manager
    ->getStorage('workspace');
  $entities = $storage
    ->loadMultiple();
  $storage
    ->delete($entities);

  // Uninstall Multiversion.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'multiversion',
  ]);

  // Check that applying updates worked.
  $this
    ->assertFalse(\Drupal::entityDefinitionUpdateManager()
    ->needsUpdates(), 'There are no new updates to apply.');
  $ids_after = [];

  // Now check that the previously created entities still exist, have the
  // right IDs and are multiversion enabled.
  foreach ($this->entityTypes as $entity_type_id => $values) {
    $storage = $entity_type_manager
      ->getStorage($entity_type_id);
    $storage_class = $storage
      ->getEntityType($entity_type_id)
      ->getStorageClass();
    $this
      ->assertFalse(is_subclass_of($storage_class, ContentEntityStorageInterface::class), "{$entity_type_id} got the correct storage handler assigned.");
    $this
      ->assertTrue($storage
      ->getQuery() instanceof QueryInterface, "{$entity_type_id} got the correct query handler assigned.");
    $ids_after[$entity_type_id] = $storage
      ->getQuery()
      ->execute();
    $this
      ->assertCount($count_before[$entity_type_id], $ids_after[$entity_type_id], "All {$entity_type_id}s were migrated.");
  }
}