diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index 9e525c314f..5d32538ee4 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -80,6 +80,13 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage */ protected $installedStorageSchema; + /** + * The entity last installed schema repository. + * + * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface + */ + protected $entityLastInstalledSchemaRepository; + /** * The deleted fields repository. * @@ -108,8 +115,8 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage */ public function __construct(EntityManagerInterface $entity_manager, ContentEntityTypeInterface $entity_type, SqlContentEntityStorage $storage, Connection $database) { $this->entityManager = $entity_manager; - $this->entityType = $this->entityManager->getLastInstalledDefinition($entity_type->id()); - $this->fieldStorageDefinitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type->id()); + $this->entityType = $this->lastInstalledSchemaRepository()->getLastInstalledDefinition($entity_type->id()); + $this->fieldStorageDefinitions = $this->lastInstalledSchemaRepository()->getLastInstalledFieldStorageDefinitions($entity_type->id()); $this->storage = $storage; $this->database = $database; } @@ -130,6 +137,23 @@ protected function installedStorageSchema() { return $this->installedStorageSchema; } + /** + * Gets the last installed schema repository service. + * + * @return \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface + * The last installed schema repository service. + * + * @todo Inject this dependency in the constructor once this class can be + * instantiated as a regular entity handler: + * https://www.drupal.org/node/2332857. + */ + protected function lastInstalledSchemaRepository() { + if (!isset($this->entityLastInstalledSchemaRepository)) { + $this->entityLastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository'); + } + return $this->entityLastInstalledSchemaRepository; + } + /** * Gets the deleted fields repository. * @@ -478,7 +502,7 @@ protected function preUpdateEntityTypeSchema(EntityTypeInterface $entity_type, E // Create dedicated field tables. foreach ($field_storage_definitions as $field_storage_definition) { if ($temporary_table_mapping->requiresDedicatedTableStorage($field_storage_definition)) { - $schema = $this->getDedicatedTableSchema($field_storage_definition); + $schema = $this->getDedicatedTableSchema($field_storage_definition, $entity_type); // Filter out tables which are not part of the table mapping. $schema = array_intersect_key($schema, $temporary_table_names); @@ -870,7 +894,7 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res if (!isset($this->schema[$entity_type_id]) || $reset) { // Prepare basic information about the entity type. /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ - $table_mapping = $this->storage->getCustomTableMapping($entity_type, $this->fieldStorageDefinitions); + $table_mapping = $this->getTableMapping($entity_type, $this->fieldStorageDefinitions); $tables = $this->getEntitySchemaTables($table_mapping); // Initialize the table schema. diff --git a/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module index 540f9c145c..f243113f57 100644 --- a/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module +++ b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module @@ -18,10 +18,10 @@ function entity_schema_test_entity_type_alter(array &$entity_types) { // Allow a test to tell us whether or not to alter the entity type. if (\Drupal::state()->get('entity_schema_update')) { - $entity_type = $entity_types['entity_test']; + $entity_type = $entity_types['entity_test_update']; if ($entity_type instanceof ContentEntityTypeInterface) { $entity_type->set('translatable', TRUE); - $entity_type->set('data_table', 'entity_test_field_data'); + $entity_type->set('data_table', 'entity_test_update_data'); // Update the keys with a revision ID. $keys = $entity_type->getKeys(); $keys['revision'] = 'revision_id'; @@ -40,7 +40,7 @@ function entity_schema_test_entity_type_alter(array &$entity_types) { * Implements hook_entity_base_field_info(). */ function entity_schema_test_entity_base_field_info(EntityTypeInterface $entity_type) { - if ($entity_type->id() == 'entity_test') { + if ($entity_type->id() == 'entity_test_update') { $definitions['custom_base_field'] = BaseFieldDefinition::create('string') ->setName('custom_base_field') ->setLabel(t('A custom base field')); @@ -60,10 +60,11 @@ function entity_schema_test_entity_base_field_info(EntityTypeInterface $entity_t * Implements hook_entity_field_storage_info(). */ function entity_schema_test_entity_field_storage_info(EntityTypeInterface $entity_type) { - if ($entity_type->id() == 'entity_test') { + if ($entity_type->id() == 'entity_test_update') { $definitions['custom_bundle_field'] = FieldStorageDefinition::create('string') ->setName('custom_bundle_field') ->setLabel(t('A custom bundle field')) + ->setRevisionable(TRUE) ->setTargetEntityTypeId($entity_type->id()); return $definitions; } @@ -73,7 +74,7 @@ function entity_schema_test_entity_field_storage_info(EntityTypeInterface $entit * Implements hook_entity_bundle_field_info(). */ function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) { - if ($entity_type->id() == 'entity_test' && $bundle == 'custom') { + if ($entity_type->id() == 'entity_test_update' && $bundle == 'custom') { /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $custom_bundle_field_storage */ $custom_bundle_field_storage = entity_schema_test_entity_field_storage_info($entity_type)['custom_bundle_field']; $definitions[$custom_bundle_field_storage->getName()] = FieldDefinition::createFromFieldStorageDefinition($custom_bundle_field_storage); @@ -85,7 +86,7 @@ function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity * Implements hook_entity_bundle_create(). */ function entity_schema_test_entity_bundle_create($entity_type_id, $bundle) { - if ($entity_type_id == 'entity_test' && $bundle == 'custom') { + if ($entity_type_id == 'entity_test_update' && $bundle == 'custom') { $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle); // Notify the entity storage that we just created a new field. \Drupal::entityManager()->onFieldDefinitionCreate($field_definitions['custom_bundle_field']); @@ -96,7 +97,7 @@ function entity_schema_test_entity_bundle_create($entity_type_id, $bundle) { * Implements hook_entity_bundle_delete(). */ function entity_schema_test_entity_bundle_delete($entity_type_id, $bundle) { - if ($entity_type_id == 'entity_test' && $bundle == 'custom') { + if ($entity_type_id == 'entity_test_update' && $bundle == 'custom') { $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle); // Notify the entity storage that our field is gone. \Drupal::entityManager()->onFieldDefinitionDelete($field_definitions['custom_bundle_field']); diff --git a/core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php b/core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php index 2633beb929..d5258eb0bd 100644 --- a/core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php +++ b/core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php @@ -24,87 +24,98 @@ protected function enableNewEntityType() { * Resets the entity type definition. */ protected function resetEntityType() { - $this->state->set('entity_test_update.entity_type', NULL); - $this->entityManager->clearCachedDefinitions(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition(FALSE, FALSE); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions(FALSE, FALSE); + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); } /** * Updates the 'entity_test_update' entity type to revisionable. + * + * @param bool $perform_update + * (optional) Whether the change should be performed by the entity + * definition update manager. */ - protected function updateEntityTypeToRevisionable() { - $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); + protected function updateEntityTypeToRevisionable($perform_update = FALSE) { + $translatable = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update')->isTranslatable(); - $keys = $entity_type->getKeys(); - $keys['revision'] = 'revision_id'; - $entity_type->set('entity_keys', $keys); - $entity_type->set('revision_table', 'entity_test_update_revision'); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition(TRUE, $translatable); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions(TRUE, $translatable); - $this->state->set('entity_test_update.entity_type', $entity_type); + if ($perform_update) { + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); + } } /** * Updates the 'entity_test_update' entity type not revisionable. + * + * @param bool $perform_update + * (optional) Whether the change should be performed by the entity + * definition update manager. */ - protected function updateEntityTypeToNotRevisionable() { - $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); + protected function updateEntityTypeToNotRevisionable($perform_update = FALSE) { + $translatable = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update')->isTranslatable(); - $keys = $entity_type->getKeys(); - unset($keys['revision']); - $entity_type->set('entity_keys', $keys); - $entity_type->set('revision_table', NULL); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition(FALSE, $translatable); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions(FALSE, $translatable); - $this->state->set('entity_test_update.entity_type', $entity_type); + if ($perform_update) { + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); + } } /** * Updates the 'entity_test_update' entity type to translatable. + * + * @param bool $perform_update + * (optional) Whether the change should be performed by the entity + * definition update manager. */ - protected function updateEntityTypeToTranslatable() { - $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); + protected function updateEntityTypeToTranslatable($perform_update = FALSE) { + $revisionable = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update')->isRevisionable(); - $entity_type->set('translatable', TRUE); - $entity_type->set('data_table', 'entity_test_update_data'); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition($revisionable, TRUE); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions($revisionable, TRUE); - if ($entity_type->isRevisionable()) { - $entity_type->set('revision_data_table', 'entity_test_update_revision_data'); + if ($perform_update) { + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); } - - $this->state->set('entity_test_update.entity_type', $entity_type); } /** * Updates the 'entity_test_update' entity type to not translatable. + * + * @param bool $perform_update + * (optional) Whether the change should be performed by the entity + * definition update manager. */ - protected function updateEntityTypeToNotTranslatable() { - $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); + protected function updateEntityTypeToNotTranslatable($perform_update = FALSE) { + $revisionable = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update')->isRevisionable(); - $entity_type->set('translatable', FALSE); - $entity_type->set('data_table', NULL); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition($revisionable, FALSE); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions($revisionable, FALSE); - if ($entity_type->isRevisionable()) { - $entity_type->set('revision_data_table', NULL); + if ($perform_update) { + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); } - - $this->state->set('entity_test_update.entity_type', $entity_type); } /** * Updates the 'entity_test_update' entity type to revisionable and * translatable. + * + * @param bool $perform_update + * (optional) Whether the change should be performed by the entity + * definition update manager. */ - protected function updateEntityTypeToRevisionableAndTranslatable() { - $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); + protected function updateEntityTypeToRevisionableAndTranslatable($perform_update = FALSE) { + $updated_entity_type = $this->getUpdatedEntityTypeDefinition(TRUE, TRUE); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions(TRUE, TRUE); - $keys = $entity_type->getKeys(); - $keys['revision'] = 'revision_id'; - $entity_type->set('entity_keys', $keys); - $entity_type->set('translatable', TRUE); - $entity_type->set('data_table', 'entity_test_update_data'); - $entity_type->set('revision_table', 'entity_test_update_revision'); - $entity_type->set('revision_data_table', 'entity_test_update_revision_data'); - - $this->state->set('entity_test_update.entity_type', $entity_type); + if ($perform_update) { + $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); + } } /** diff --git a/core/modules/views/tests/src/Kernel/EventSubscriber/ViewsEntitySchemaSubscriberIntegrationTest.php b/core/modules/views/tests/src/Kernel/EventSubscriber/ViewsEntitySchemaSubscriberIntegrationTest.php index 24e4e6af51..35aa6e282c 100644 --- a/core/modules/views/tests/src/Kernel/EventSubscriber/ViewsEntitySchemaSubscriberIntegrationTest.php +++ b/core/modules/views/tests/src/Kernel/EventSubscriber/ViewsEntitySchemaSubscriberIntegrationTest.php @@ -91,8 +91,7 @@ public function testDeleteEntityType() { $entity_storage = $this->entityManager->getStorage('view'); // Make the test entity type revisionable. - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); $views = $entity_storage->loadMultiple(); @@ -145,8 +144,7 @@ public function testBaseTableRename() { * Tests that renaming data tables adapts the views. */ public function testDataTableRename() { - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); $entity_storage = $this->entityManager->getStorage('view'); $view = $entity_storage->load('test_view_entity_test_data'); @@ -174,8 +172,7 @@ public function testDataTableRename() { * Tests that renaming revision tables adapts the views. */ public function testRevisionBaseTableRename() { - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); /** @var \Drupal\views\Entity\View $view */ $entity_storage = $this->entityManager->getStorage('view'); @@ -203,12 +200,7 @@ public function testRevisionBaseTableRename() { * Tests that renaming revision tables adapts the views. */ public function testRevisionDataTableRename() { - $this->updateEntityTypeToRevisionable(); - // Multiple changes, so we have to invalidate the caches, otherwise - // the second update will revert the first. - $this->entityManager->clearCachedDefinitions(); - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionableAndTranslatable(TRUE); /** @var \Drupal\views\Entity\View $view */ $entity_storage = $this->entityManager->getStorage('view'); @@ -236,8 +228,7 @@ public function testRevisionDataTableRename() { * Tests that adding data tables adapts the views. */ public function testDataTableAddition() { - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); /** @var \Drupal\views\Entity\View $view */ $entity_storage = $this->entityManager->getStorage('view'); @@ -254,8 +245,7 @@ public function testDataTableAddition() { * Tests that enabling revisions doesn't do anything. */ public function testRevisionEnabling() { - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); /** @var \Drupal\views\Entity\View $view */ $entity_storage = $this->entityManager->getStorage('view'); @@ -272,11 +262,8 @@ public function testRevisionEnabling() { * Tests that removing revision support disables the view. */ public function testRevisionDisabling() { - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); - - $this->updateEntityTypeToNotRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); + $this->updateEntityTypeToNotRevisionable(TRUE); /** @var \Drupal\views\Entity\View $view */ $entity_storage = $this->entityManager->getStorage('view'); @@ -297,16 +284,14 @@ public function testVariousTableUpdates() { // base <-> base + translation + revision // base <-> base + translation - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); @@ -316,24 +301,21 @@ public function testVariousTableUpdates() { $this->resetEntityType(); // base + translation <-> base + translation + revision - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotRevisionable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); @@ -350,16 +332,14 @@ public function testVariousTableUpdates() { $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); @@ -369,16 +349,14 @@ public function testVariousTableUpdates() { $this->resetEntityType(); // base <-> base + revision - $this->updateEntityTypeToRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotRevisionable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotRevisionable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); @@ -388,18 +366,16 @@ public function testVariousTableUpdates() { $this->resetEntityType(); // base <-> base + translation + revision - $this->updateEntityTypeToRevisionable(); - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); + $this->updateEntityTypeToTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); $this->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotRevisionable(); - $this->updateEntityTypeToNotTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotRevisionable(TRUE); + $this->updateEntityTypeToNotTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(); $this->assertEqual('entity_test_update', $view->get('base_table')); @@ -412,10 +388,7 @@ public function testVariousTableUpdates() { */ public function testVariousTableUpdatesForRevisionView() { // base + revision <-> base + translation + revision - $this->updateEntityTypeToRevisionable(); - // Multiple changes, so we have to invalidate the caches, otherwise - // the second update will revert the first. - $this->entityManager->clearCachedDefinitions(); + $this->updateEntityTypeToRevisionable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE); @@ -423,23 +396,19 @@ public function testVariousTableUpdatesForRevisionView() { $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE); $this->assertEqual('entity_test_update_revision', $view->get('base_table')); $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_revision_data', $display['display_options']['fields']['name']['table']); - $this->updateEntityTypeToNotTranslatable(); - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToNotTranslatable(TRUE); list($view, $display) = $this->getUpdatedViewAndDisplay(TRUE); $this->assertEqual('entity_test_update_revision', $view->get('base_table')); $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']); $this->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']); - - $this->resetEntityType(); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 56fc5ded14..da41e9f112 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -121,7 +121,7 @@ public function testEntityTypeUpdateWithoutData() { $this->assertEqual($this->entityDefinitionUpdateManager->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.'); // Run the update and ensure the revision table is created. - $this->entityDefinitionUpdateManager->applyUpdates(); + $this->updateEntityTypeToRevisionable(TRUE); $this->assertTrue($this->database->schema()->tableExists('entity_test_update_revision'), 'Revision table created for entity_test_update.'); } @@ -849,11 +849,8 @@ public function testSingleActionCalls() { // Make the entity type revisionable. $this->updateEntityTypeToRevisionable(); $this->assertFalse($db_schema->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' does not exist before applying the update."); - $entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update'); - $keys = $entity_type->getKeys(); - $keys['revision'] = 'revision_id'; - $entity_type->set('entity_keys', $keys); - $this->entityDefinitionUpdateManager->updateEntityType($entity_type); + + $this->updateEntityTypeToRevisionable(TRUE); $this->assertTrue($db_schema->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' table has been created."); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php index 4dd9ede70c..194bab82ad 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php @@ -5,6 +5,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait; /** * Tests the default entity storage schema handler. @@ -13,6 +14,15 @@ */ class EntitySchemaTest extends EntityKernelTestBase { + use EntityDefinitionTestTrait; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = ['entity_test_update']; + /** * The database connection used. * @@ -26,6 +36,7 @@ class EntitySchemaTest extends EntityKernelTestBase { protected function setUp() { parent::setUp(); $this->installSchema('user', ['users_data']); + $this->installEntitySchema('entity_test_update'); $this->database = $this->container->get('database'); } @@ -36,7 +47,7 @@ public function testCustomFieldCreateDelete() { // Install the module which adds the field. $this->installModule('entity_schema_test'); $this->entityManager->clearCachedDefinitions(); - $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test'); + $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test_update'); $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.'); $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.'); @@ -44,7 +55,7 @@ public function testCustomFieldCreateDelete() { $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']); $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']); /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ - $table_mapping = $this->entityManager->getStorage('entity_test')->getTableMapping(); + $table_mapping = $this->entityManager->getStorage('entity_test_update')->getTableMapping(); $base_table = current($table_mapping->getTableNames()); $base_column = current($table_mapping->getColumnNames('custom_base_field')); $this->assertTrue($this->database->schema()->fieldExists($base_table, $base_column), 'Table column created'); @@ -65,12 +76,10 @@ public function testCustomFieldCreateDelete() { * Whether the original definition should be altered or not. */ protected function updateEntityType($alter) { - $entity_test_id = 'entity_test'; - $original = $this->entityManager->getDefinition($entity_test_id); - $this->entityManager->clearCachedDefinitions(); $this->state->set('entity_schema_update', $alter); - $entity_type = $this->entityManager->getDefinition($entity_test_id); - $this->entityManager->onEntityTypeUpdate($entity_type, $original); + $updated_entity_type = $this->getUpdatedEntityTypeDefinition($alter, $alter); + $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions($alter, $alter); + $this->container->get('entity.definition_update_manager')->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions); } /** @@ -78,12 +87,12 @@ protected function updateEntityType($alter) { */ public function testEntitySchemaUpdate() { $this->installModule('entity_schema_test'); - $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test'); + $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test_update'); $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']); $this->entityManager->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']); $schema_handler = $this->database->schema(); - $tables = ['entity_test', 'entity_test_revision', 'entity_test_field_data', 'entity_test_field_revision']; - $dedicated_tables = ['entity_test__custom_bundle_field', 'entity_test_revision__custom_bundle_field']; + $tables = ['entity_test_update', 'entity_test_update_revision', 'entity_test_update_data', 'entity_test_update_revision_data']; + $dedicated_tables = ['entity_test_update__custom_bundle_field', 'entity_test_update_revision__custom_bundle_field']; // Initially only the base table and the dedicated field data table should // exist. @@ -299,10 +308,10 @@ public function testModifyingTranslatableColumnSchema() { $this->updateEntityType(TRUE); $fields = ['revision_log', 'uuid']; foreach ($fields as $field_name) { - $original_definition = $this->entityManager->getBaseFieldDefinitions('entity_test')[$field_name]; + $original_definition = $this->entityManager->getBaseFieldDefinitions('entity_test_update')[$field_name]; $new_definition = clone $original_definition; $new_definition->setLabel($original_definition->getLabel() . ', the other one'); - $this->assertTrue($this->entityManager->getStorage('entity_test') + $this->assertTrue($this->entityManager->getStorage('entity_test_update') ->requiresFieldDataMigration($new_definition, $original_definition)); } } diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index 06697dd778..37296dc7df 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\ContentEntityTypeInterface; +use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; use Drupal\Core\Entity\Sql\DefaultTableMapping; use Drupal\Tests\UnitTestCase; @@ -1204,15 +1205,30 @@ public function testRequiresEntityDataMigration($updated_entity_type_definition, ->method('createHandlerInstance') ->willReturn($original_storage); + $last_installed_schema_repository = $this->getMock(EntityLastInstalledSchemaRepositoryInterface::class); + $last_installed_schema_repository + ->expects($this->any()) + ->method('getLastInstalledDefinition') + ->willReturn($this->entityType); + $last_installed_schema_repository + ->expects($this->any()) + ->method('getLastInstalledFieldStorageDefinitions') + ->willReturn($this->storageDefinitions); + $this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema') - ->setConstructorArgs([$this->entityManager, $this->entityType, $this->storage, $connection]) - ->setMethods(['installedStorageSchema', 'hasSharedTableStructureChange']) + ->disableOriginalConstructor() + ->setMethods(['installedStorageSchema', 'lastInstalledSchemaRepository', 'hasSharedTableStructureChange']) ->getMock(); - - $this->storageSchema->expects($this->any()) + $this->storageSchema + ->expects($this->any()) ->method('hasSharedTableStructureChange') ->with($updated_entity_type_definition, $original_entity_type_definition) ->willReturn($shared_table_structure_changed); + $this->storageSchema + ->expects($this->any()) + ->method('lastInstalledSchemaRepository') + ->will($this->returnValue($last_installed_schema_repository)); + $this->storageSchema->__construct($this->entityManager, $this->entityType, $this->storage, $connection); $this->assertEquals($migration_required, $this->storageSchema->requiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition)); } @@ -1339,15 +1355,6 @@ public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterfac * be created. Defaults to expecting nothing. */ protected function setUpStorageSchema(array $expected = []) { - $this->entityManager->expects($this->any()) - ->method('getLastInstalledDefinition') - ->with($this->entityType->id()) - ->will($this->returnValue($this->entityType)); - - $this->entityManager->expects($this->any()) - ->method('getLastInstalledFieldStorageDefinitions') - ->with($this->entityType->id()) - ->will($this->returnValue($this->storageDefinitions)); $this->entityManager->expects($this->any()) ->method('getFieldStorageDefinitions') ->with($this->entityType->id()) @@ -1385,18 +1392,33 @@ protected function setUpStorageSchema(array $expected = []) { ->will($this->returnValue($this->dbSchemaHandler)); $key_value = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface'); + $last_installed_schema_repository = $this->getMock(EntityLastInstalledSchemaRepositoryInterface::class); + $last_installed_schema_repository + ->expects($this->any()) + ->method('getLastInstalledDefinition') + ->willReturn($this->entityType); + $last_installed_schema_repository + ->expects($this->any()) + ->method('getLastInstalledFieldStorageDefinitions') + ->willReturn($this->storageDefinitions); + $this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema') - ->setConstructorArgs([$this->entityManager, $this->entityType, $this->storage, $connection]) - ->setMethods(['installedStorageSchema', 'loadEntitySchemaData', 'hasSharedTableNameChanges', 'isTableEmpty', 'getTableMapping']) + ->disableOriginalConstructor() + ->setMethods(['installedStorageSchema', 'lastInstalledSchemaRepository', 'loadEntitySchemaData', 'hasSharedTableNameChanges', 'isTableEmpty', 'getTableMapping']) ->getMock(); $this->storageSchema ->expects($this->any()) ->method('installedStorageSchema') ->will($this->returnValue($key_value)); + $this->storageSchema + ->expects($this->any()) + ->method('lastInstalledSchemaRepository') + ->will($this->returnValue($last_installed_schema_repository)); $this->storageSchema ->expects($this->any()) ->method('isTableEmpty') ->willReturn(FALSE); + $this->storageSchema->__construct($this->entityManager, $this->entityType, $this->storage, $connection); } /**