FieldModuleUninstallValidatorTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Field/FieldModuleUninstallValidatorTest.php
View source
<?php
namespace Drupal\system\Tests\Field;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\FieldStorageDefinition;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
class FieldModuleUninstallValidatorTest extends EntityUnitTestBase {
protected $entityDefinitionUpdateManager;
protected function setUp() {
parent::setUp();
$this
->installSchema('user', 'users_data');
$this->entityDefinitionUpdateManager = $this->container
->get('entity.definition_update_manager');
$definitions['extra_base_field'] = BaseFieldDefinition::create('string')
->setName('extra_base_field')
->setTargetEntityTypeId('entity_test')
->setTargetBundle('entity_test');
$this->state
->set('entity_test.additional_base_field_definitions', $definitions);
$definitions['extra_bundle_field'] = FieldStorageDefinition::create('string')
->setName('extra_bundle_field')
->setTargetEntityTypeId('entity_test')
->setTargetBundle('entity_test');
$this->state
->set('entity_test.additional_field_storage_definitions', $definitions);
$this->state
->set('entity_test.entity_test.additional_bundle_field_definitions', $definitions);
$this->entityManager
->clearCachedDefinitions();
}
public function testUninstallingModule() {
$this
->assertModuleInstallUninstall('entity_test_extra');
$entity = $this->entityManager
->getStorage('entity_test')
->create([
'name' => $this
->randomString(),
]);
$entity
->save();
$this
->assertModuleInstallUninstall('entity_test_extra');
$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_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');
}
$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.');
}
}
protected function assertModuleInstallUninstall($module_name) {
$this
->enableModules([
$module_name,
]);
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertTrue($this
->getModuleHandler()
->moduleExists($module_name), $module_name . ' module is enabled.');
$this
->getModuleInstaller()
->uninstall([
$module_name,
]);
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->assertFalse($this
->getModuleHandler()
->moduleExists($module_name), $module_name . ' module is disabled.');
}
protected function getModuleHandler() {
return $this->container
->get('module_handler');
}
protected function getModuleInstaller() {
return $this->container
->get('module_installer');
}
}