3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Component\Utility\SafeMarkup;
8 * Tests adding a custom bundle field.
12 class EntitySchemaTest extends EntityKernelTestBase {
15 * The database connection used.
17 * @var \Drupal\Core\Database\Connection
24 protected function setUp() {
26 $this->installSchema('user', ['users_data']);
27 $this->database = $this->container->get('database');
31 * Tests the custom bundle field creation and deletion.
33 public function testCustomFieldCreateDelete() {
34 // Install the module which adds the field.
35 $this->installModule('entity_schema_test');
36 $this->entityManager->clearCachedDefinitions();
37 $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test');
38 $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.');
39 $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');
41 // Make sure the field schema can be created.
42 $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
43 $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
44 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
45 $table_mapping = $this->entityManager->getStorage('entity_test')->getTableMapping();
46 $base_table = current($table_mapping->getTableNames());
47 $base_column = current($table_mapping->getColumnNames('custom_base_field'));
48 $this->assertTrue($this->database->schema()->fieldExists($base_table, $base_column), 'Table column created');
49 $table = $table_mapping->getDedicatedDataTableName($storage_definitions['custom_bundle_field']);
50 $this->assertTrue($this->database->schema()->tableExists($table), 'Table created');
52 // Make sure the field schema can be deleted.
53 $this->entityManager->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
54 $this->entityManager->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
55 $this->assertFalse($this->database->schema()->fieldExists($base_table, $base_column), 'Table column dropped');
56 $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
60 * Updates the entity type definition.
63 * Whether the original definition should be altered or not.
65 protected function updateEntityType($alter) {
66 $entity_test_id = 'entity_test';
67 $original = $this->entityManager->getDefinition($entity_test_id);
68 $this->entityManager->clearCachedDefinitions();
69 $this->state->set('entity_schema_update', $alter);
70 $entity_type = $this->entityManager->getDefinition($entity_test_id);
71 $this->entityManager->onEntityTypeUpdate($entity_type, $original);
75 * Tests that entity schema responds to changes in the entity type definition.
77 public function testEntitySchemaUpdate() {
78 $this->installModule('entity_schema_test');
79 $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test');
80 $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
81 $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
82 $schema_handler = $this->database->schema();
83 $tables = ['entity_test', 'entity_test_revision', 'entity_test_field_data', 'entity_test_field_revision'];
84 $dedicated_tables = ['entity_test__custom_bundle_field', 'entity_test_revision__custom_bundle_field'];
86 // Initially only the base table and the dedicated field data table should
88 foreach ($tables as $index => $table) {
89 $this->assertEqual($schema_handler->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
91 $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
93 // Update the entity type definition and check that the entity schema now
94 // supports translations and revisions.
95 $this->updateEntityType(TRUE);
96 foreach ($tables as $table) {
97 $this->assertTrue($schema_handler->tableExists($table), SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
99 foreach ($dedicated_tables as $table) {
100 $this->assertTrue($schema_handler->tableExists($table), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
103 // Revert changes and check that the entity schema now does not support
104 // neither translations nor revisions.
105 $this->updateEntityType(FALSE);
106 foreach ($tables as $index => $table) {
107 $this->assertEqual($schema_handler->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
109 $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
115 protected function refreshServices() {
116 parent::refreshServices();
117 $this->database = $this->container->get('database');
121 * Tests that modifying the UUID field for a translatable entity works.
123 public function testModifyingTranslatableColumnSchema() {
124 $this->installModule('entity_schema_test');
125 $this->updateEntityType(TRUE);
126 $fields = ['revision_log', 'uuid'];
127 foreach ($fields as $field_name) {
128 $original_definition = $this->entityManager->getBaseFieldDefinitions('entity_test')[$field_name];
129 $new_definition = clone $original_definition;
130 $new_definition->setLabel($original_definition->getLabel() . ', the other one');
131 $this->assertTrue($this->entityManager->getStorage('entity_test')
132 ->requiresFieldDataMigration($new_definition, $original_definition));
137 * Tests fields from an uninstalled module are removed from the schema.
139 public function testCleanUpStorageDefinition() {
140 // Find all the entity types provided by the entity_test module and install
141 // the schema for them.
142 $entity_type_ids = [];
143 $entities = \Drupal::entityManager()->getDefinitions();
144 foreach ($entities as $entity_type_id => $definition) {
145 if ($definition->getProvider() == 'entity_test') {
146 $this->installEntitySchema($entity_type_id);
147 $entity_type_ids[] = $entity_type_id;
151 // Get a list of all the entities in the schema.
152 $key_value_store = \Drupal::keyValue('entity.storage_schema.sql');
153 $schema = $key_value_store->getAll();
155 // Count the storage definitions provided by the entity_test module, so that
156 // after uninstall we can be sure there were some to be deleted.
157 $entity_type_id_count = 0;
159 foreach (array_keys($schema) as $storage_definition_name) {
160 list($entity_type_id, ,) = explode('.', $storage_definition_name);
161 if (in_array($entity_type_id, $entity_type_ids)) {
162 $entity_type_id_count++;
166 // Ensure that there are storage definitions from the entity_test module.
167 $this->assertNotEqual($entity_type_id_count, 0, 'There are storage definitions provided by the entity_test module in the schema.');
169 // Uninstall the entity_test module.
170 $this->container->get('module_installer')->uninstall(['entity_test']);
172 // Get a list of all the entities in the schema.
173 $key_value_store = \Drupal::keyValue('entity.storage_schema.sql');
174 $schema = $key_value_store->getAll();
176 // Count the storage definitions that come from entity types provided by
177 // the entity_test module.
178 $entity_type_id_count = 0;
180 foreach (array_keys($schema) as $storage_definition_name) {
181 list($entity_type_id, ,) = explode('.', $storage_definition_name);
182 if (in_array($entity_type_id, $entity_type_ids)) {
183 $entity_type_id_count++;
187 // Ensure that all storage definitions have been removed from the schema.
188 $this->assertEqual($entity_type_id_count, 0, 'After uninstalling entity_test module the schema should not contains fields from entities provided by the module.');