You are here

public function FieldModuleUninstallValidatorTest::testUninstallingModule in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Field/FieldModuleUninstallValidatorTest.php \Drupal\system\Tests\Field\FieldModuleUninstallValidatorTest::testUninstallingModule()

Tests uninstall entity_test module with and without content for the field.

File

core/modules/system/src/Tests/Field/FieldModuleUninstallValidatorTest.php, line 55
Contains \Drupal\system\Tests\Field\FieldModuleUninstallValidatorTest.

Class

FieldModuleUninstallValidatorTest
Tests FieldModuleUninstallValidator functionality.

Namespace

Drupal\system\Tests\Field

Code

public function testUninstallingModule() {

  // Test uninstall works fine without content.
  $this
    ->assertModuleInstallUninstall('entity_test_extra');

  // Test uninstalling works fine with content having no field values.
  $entity = $this->entityManager
    ->getStorage('entity_test')
    ->create([
    'name' => $this
      ->randomString(),
  ]);
  $entity
    ->save();
  $this
    ->assertModuleInstallUninstall('entity_test_extra');
  $entity
    ->delete();

  // Verify uninstall works fine without content again.
  $this
    ->assertModuleInstallUninstall('entity_test_extra');

  // Verify uninstalling entity_test is not possible when there is content for
  // the base field.
  $this
    ->enableModules([
    'entity_test_extra',
  ]);
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $entity = $this->entityManager
    ->getStorage('entity_test')
    ->create([
    'name' => $this
      ->randomString(),
    'extra_base_field' => $this
      ->randomString(),
  ]);
  $entity
    ->save();
  try {
    $message = 'Module uninstallation fails as the module provides a base field which has content.';
    $this
      ->getModuleInstaller()
      ->uninstall(array(
      'entity_test_extra',
    ));
    $this
      ->fail($message);
  } catch (ModuleUninstallValidatorException $e) {
    $this
      ->pass($message);
    $this
      ->assertEqual($e
      ->getMessage(), 'The following reasons prevents the modules from being uninstalled: There is data for the field extra_base_field on entity type Test entity');
  }

  // Verify uninstalling entity_test is not possible when there is content for
  // the bundle field.
  $entity
    ->delete();
  $this
    ->assertModuleInstallUninstall('entity_test_extra');
  $this
    ->enableModules([
    'entity_test_extra',
  ]);
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $entity = $this->entityManager
    ->getStorage('entity_test')
    ->create([
    'name' => $this
      ->randomString(),
    'extra_bundle_field' => $this
      ->randomString(),
  ]);
  $entity
    ->save();
  try {
    $this
      ->getModuleInstaller()
      ->uninstall(array(
      'entity_test_extra',
    ));
    $this
      ->fail('Module uninstallation fails as the module provides a bundle field which has content.');
  } catch (ModuleUninstallValidatorException $e) {
    $this
      ->pass('Module uninstallation fails as the module provides a bundle field which has content.');
  }
}