From 7aff00a4f505b1baa56a71c05ecfef8a7b0ba485 Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Tue, 20 Aug 2024 22:03:05 +0200 Subject: [PATCH] Issue #3469184 by catch, smustgrave: Split up EntityDefinitionUpdateTest (cherry picked from commit 320be9e6581795f0b1ca01a2fc39be6632336e22) --- .../EntityDefinitionUpdateProviderTest.php | 365 ++++++++++++++++++ .../Entity/EntityDefinitionUpdateTest.php | 299 -------------- .../FieldableEntityDefinitionUpdateTest.php | 157 -------- 3 files changed, 365 insertions(+), 456 deletions(-) create mode 100644 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateProviderTest.php diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateProviderTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateProviderTest.php new file mode 100644 index 000000000000..c7cddd7bdd12 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateProviderTest.php @@ -0,0 +1,365 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\KernelTests\Core\Entity; + +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait; + +/** + * Tests EntityDefinitionUpdateManager functionality. + * + * @coversDefaultClass \Drupal\Core\Entity\EntityDefinitionUpdateManager + * + * @group Entity + * @group #slow + */ +class EntityDefinitionUpdateProviderTest extends EntityKernelTestBase { + + use EntityDefinitionTestTrait; + + /** + * The entity definition update manager. + * + * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface + */ + protected $entityDefinitionUpdateManager; + + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** + * Modules to enable. + * + * @var array + */ + protected static $modules = ['entity_test_update', 'language']; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager'); + $this->entityFieldManager = $this->container->get('entity_field.manager'); + $this->database = $this->container->get('database'); + + // Install every entity type's schema that wasn't installed in the parent + // method. + foreach (array_diff_key($this->entityTypeManager->getDefinitions(), array_flip(['user', 'entity_test'])) as $entity_type_id => $entity_type) { + $this->installEntitySchema($entity_type_id); + } + } + + /** + * Tests deleting a base field when it has existing data. + * + * @dataProvider baseFieldDeleteWithExistingDataTestCases + */ + public function testBaseFieldDeleteWithExistingData($entity_type_id, $create_entity_revision, $base_field_revisionable, $create_entity_translation): void { + // Enable an additional language. + ConfigurableLanguage::createFromLangcode('ro')->save(); + + /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */ + $storage = $this->entityTypeManager->getStorage($entity_type_id); + $schema_handler = $this->database->schema(); + + // Create an entity without the base field, to ensure NULL values are not + // added to the dedicated table storage to be purged. + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $storage->create(); + $entity->save(); + + // Add the base field and run the update. + $this->addBaseField('string', $entity_type_id, $base_field_revisionable, TRUE, $create_entity_translation); + $this->applyEntityUpdates(); + + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $storage->getTableMapping(); + $storage_definition = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id)['new_base_field']; + + // Save an entity with the base field populated. + $entity = $storage->create(['new_base_field' => 'foo']); + $entity->save(); + + if ($create_entity_translation) { + $translation = $entity->addTranslation('ro', ['new_base_field' => 'foo-ro']); + $translation->save(); + } + + if ($create_entity_revision) { + $entity->setNewRevision(TRUE); + $entity->isDefaultRevision(FALSE); + $entity->new_base_field = 'bar'; + $entity->save(); + + if ($create_entity_translation) { + $translation = $entity->getTranslation('ro'); + $translation->new_base_field = 'bar-ro'; + $translation->save(); + } + } + + // Remove the base field and apply updates. + $this->removeBaseField($entity_type_id); + $this->applyEntityUpdates(); + + // Check that the base field's column is deleted. + $this->assertFalse($schema_handler->fieldExists($entity_type_id, 'new_base_field'), 'Column deleted from shared table for new_base_field.'); + + // Check that a dedicated 'deleted' table was created for the deleted base + // field. + $dedicated_deleted_table_name = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE); + $this->assertTrue($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated table was created for the deleted new_base_field.'); + + $expected[] = [ + 'bundle' => $entity->bundle(), + 'deleted' => '1', + 'entity_id' => '2', + 'revision_id' => '2', + 'langcode' => 'en', + 'delta' => '0', + 'new_base_field_value' => 'foo', + ]; + + if ($create_entity_translation) { + $expected[] = [ + 'bundle' => $entity->bundle(), + 'deleted' => '1', + 'entity_id' => '2', + 'revision_id' => '2', + 'langcode' => 'ro', + 'delta' => '0', + 'new_base_field_value' => 'foo-ro', + ]; + } + + // Check that the deleted field's data is preserved in the dedicated + // 'deleted' table. + $result = $this->database->select($dedicated_deleted_table_name, 't') + ->fields('t') + ->orderBy('revision_id', 'ASC') + ->orderBy('langcode', 'ASC') + ->execute() + ->fetchAll(\PDO::FETCH_ASSOC); + $this->assertSameSize($expected, $result); + + // Use assertEquals and not assertSame here to prevent that a different + // sequence of the columns in the table will affect the check. + $this->assertEquals($expected, $result); + + if ($create_entity_revision) { + $dedicated_deleted_revision_table_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE); + $this->assertTrue($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated revision table was created for the deleted new_base_field.'); + + if ($base_field_revisionable) { + $expected[] = [ + 'bundle' => $entity->bundle(), + 'deleted' => '1', + 'entity_id' => '2', + 'revision_id' => '3', + 'langcode' => 'en', + 'delta' => '0', + 'new_base_field_value' => 'bar', + ]; + + if ($create_entity_translation) { + $expected[] = [ + 'bundle' => $entity->bundle(), + 'deleted' => '1', + 'entity_id' => '2', + 'revision_id' => '3', + 'langcode' => 'ro', + 'delta' => '0', + 'new_base_field_value' => 'bar-ro', + ]; + } + } + + $result = $this->database->select($dedicated_deleted_revision_table_name, 't') + ->fields('t') + ->orderBy('revision_id', 'ASC') + ->orderBy('langcode', 'ASC') + ->execute() + ->fetchAll(\PDO::FETCH_ASSOC); + $this->assertSameSize($expected, $result); + + // Use assertEquals and not assertSame here to prevent that a different + // sequence of the columns in the table will affect the check. + $this->assertEquals($expected, $result); + } + + // Check that the field storage definition is marked for purging. + $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions(); + $this->assertArrayHasKey($storage_definition->getUniqueStorageIdentifier(), $deleted_storage_definitions, 'The base field is marked for purging.'); + + // Purge field data, and check that the storage definition has been + // completely removed once the data is purged. + field_purge_batch(10); + $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions(); + $this->assertEmpty($deleted_storage_definitions, 'The base field has been deleted.'); + $this->assertFalse($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated field table was deleted after new_base_field was purged.'); + + if (isset($dedicated_deleted_revision_table_name)) { + $this->assertFalse($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated field revision table was deleted after new_base_field was purged.'); + } + } + + /** + * Test cases for ::testBaseFieldDeleteWithExistingData. + */ + public static function baseFieldDeleteWithExistingDataTestCases() { + return [ + 'Non-revisionable, non-translatable entity type' => [ + 'entity_test_update', + FALSE, + FALSE, + FALSE, + ], + 'Non-revisionable, non-translatable custom data table' => [ + 'entity_test_mul', + FALSE, + FALSE, + FALSE, + ], + 'Non-revisionable, non-translatable entity type, revisionable base field' => [ + 'entity_test_update', + FALSE, + TRUE, + FALSE, + ], + 'Non-revisionable, non-translatable custom data table, revisionable base field' => [ + 'entity_test_mul', + FALSE, + TRUE, + FALSE, + ], + 'Revisionable, translatable entity type, non revisionable and non-translatable base field' => [ + 'entity_test_mulrev', + TRUE, + FALSE, + FALSE, + ], + 'Revisionable, translatable entity type, revisionable and non-translatable base field' => [ + 'entity_test_mulrev', + TRUE, + TRUE, + FALSE, + ], + 'Revisionable and non-translatable entity type, revisionable and non-translatable base field' => [ + 'entity_test_rev', + TRUE, + TRUE, + FALSE, + ], + 'Revisionable and non-translatable entity type, non-revisionable and non-translatable base field' => [ + 'entity_test_rev', + TRUE, + FALSE, + FALSE, + ], + 'Revisionable and translatable entity type, non-revisionable and translatable base field' => [ + 'entity_test_mulrev', + TRUE, + FALSE, + TRUE, + ], + 'Revisionable and translatable entity type, revisionable and translatable base field' => [ + 'entity_test_mulrev', + TRUE, + TRUE, + TRUE, + ], + ]; + } + + /** + * Tests adding a base field with initial values inherited from another field. + * + * @dataProvider initialValueFromFieldTestCases + */ + public function testInitialValueFromField($default_initial_value, $expected_value): void { + $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); + $db_schema = $this->database->schema(); + + // Create two entities before adding the base field. + /** @var \Drupal\entity_test_update\Entity\EntityTestUpdate $entity */ + $storage->create([ + 'name' => 'First entity', + 'test_single_property' => 'test existing value', + ])->save(); + + // The second entity does not have any value for the 'test_single_property' + // field, allowing us to test the 'default_value' parameter of + // \Drupal\Core\Field\BaseFieldDefinition::setInitialValueFromField(). + $storage->create([ + 'name' => 'Second entity', + ])->save(); + + // Add a base field with an initial value inherited from another field. + $definitions['new_base_field'] = BaseFieldDefinition::create('string') + ->setName('new_base_field') + ->setLabel('A new base field') + ->setInitialValueFromField('name'); + $definitions['another_base_field'] = BaseFieldDefinition::create('string') + ->setName('another_base_field') + ->setLabel('Another base field') + ->setInitialValueFromField('test_single_property', $default_initial_value); + + $this->state->set('entity_test_update.additional_base_field_definitions', $definitions); + + $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update."); + $this->assertFalse($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' does not exist before applying the update."); + $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $definitions['new_base_field']); + $this->entityDefinitionUpdateManager->installFieldStorageDefinition('another_base_field', 'entity_test_update', 'entity_test', $definitions['another_base_field']); + $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table."); + $this->assertTrue($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' has been created on the 'entity_test_update' table."); + + // Check that the initial values have been applied. + $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); + $entities = $storage->loadMultiple(); + $this->assertEquals('First entity', $entities[1]->get('new_base_field')->value); + $this->assertEquals('Second entity', $entities[2]->get('new_base_field')->value); + + $this->assertEquals('test existing value', $entities[1]->get('another_base_field')->value); + $this->assertEquals($expected_value, $entities[2]->get('another_base_field')->value); + } + + /** + * Test cases for ::testInitialValueFromField. + */ + public static function initialValueFromFieldTestCases() { + return [ + 'literal value' => [ + 'test initial value', + 'test initial value', + ], + 'indexed array' => [ + ['value' => 'test initial value'], + 'test initial value', + ], + 'empty array' => [ + [], + NULL, + ], + 'null' => [ + NULL, + NULL, + ], + ]; + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 7d0ea21b1881..ec283d6a165b 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -18,7 +18,6 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\entity_test\FieldStorageDefinition; use Drupal\entity_test_update\Entity\EntityTestUpdate; -use Drupal\language\Entity\ConfigurableLanguage; use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait; /** @@ -514,228 +513,6 @@ public function testBundleFieldCreateDeleteWithExistingEntities(): void { } } - /** - * Tests deleting a base field when it has existing data. - * - * @dataProvider baseFieldDeleteWithExistingDataTestCases - */ - public function testBaseFieldDeleteWithExistingData($entity_type_id, $create_entity_revision, $base_field_revisionable, $create_entity_translation): void { - // Enable an additional language. - ConfigurableLanguage::createFromLangcode('ro')->save(); - - /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */ - $storage = $this->entityTypeManager->getStorage($entity_type_id); - $schema_handler = $this->database->schema(); - - // Create an entity without the base field, to ensure NULL values are not - // added to the dedicated table storage to be purged. - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $storage->create(); - $entity->save(); - - // Add the base field and run the update. - $this->addBaseField('string', $entity_type_id, $base_field_revisionable, TRUE, $create_entity_translation); - $this->applyEntityUpdates(); - - /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ - $table_mapping = $storage->getTableMapping(); - $storage_definition = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id)['new_base_field']; - - // Save an entity with the base field populated. - $entity = $storage->create(['new_base_field' => 'foo']); - $entity->save(); - - if ($create_entity_translation) { - $translation = $entity->addTranslation('ro', ['new_base_field' => 'foo-ro']); - $translation->save(); - } - - if ($create_entity_revision) { - $entity->setNewRevision(TRUE); - $entity->isDefaultRevision(FALSE); - $entity->new_base_field = 'bar'; - $entity->save(); - - if ($create_entity_translation) { - $translation = $entity->getTranslation('ro'); - $translation->new_base_field = 'bar-ro'; - $translation->save(); - } - } - - // Remove the base field and apply updates. - $this->removeBaseField($entity_type_id); - $this->applyEntityUpdates(); - - // Check that the base field's column is deleted. - $this->assertFalse($schema_handler->fieldExists($entity_type_id, 'new_base_field'), 'Column deleted from shared table for new_base_field.'); - - // Check that a dedicated 'deleted' table was created for the deleted base - // field. - $dedicated_deleted_table_name = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE); - $this->assertTrue($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated table was created for the deleted new_base_field.'); - - $expected[] = [ - 'bundle' => $entity->bundle(), - 'deleted' => '1', - 'entity_id' => '2', - 'revision_id' => '2', - 'langcode' => 'en', - 'delta' => '0', - 'new_base_field_value' => 'foo', - ]; - - if ($create_entity_translation) { - $expected[] = [ - 'bundle' => $entity->bundle(), - 'deleted' => '1', - 'entity_id' => '2', - 'revision_id' => '2', - 'langcode' => 'ro', - 'delta' => '0', - 'new_base_field_value' => 'foo-ro', - ]; - } - - // Check that the deleted field's data is preserved in the dedicated - // 'deleted' table. - $result = $this->database->select($dedicated_deleted_table_name, 't') - ->fields('t') - ->orderBy('revision_id', 'ASC') - ->orderBy('langcode', 'ASC') - ->execute() - ->fetchAll(\PDO::FETCH_ASSOC); - $this->assertSameSize($expected, $result); - - // Use assertEquals and not assertSame here to prevent that a different - // sequence of the columns in the table will affect the check. - $this->assertEquals($expected, $result); - - if ($create_entity_revision) { - $dedicated_deleted_revision_table_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE); - $this->assertTrue($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated revision table was created for the deleted new_base_field.'); - - if ($base_field_revisionable) { - $expected[] = [ - 'bundle' => $entity->bundle(), - 'deleted' => '1', - 'entity_id' => '2', - 'revision_id' => '3', - 'langcode' => 'en', - 'delta' => '0', - 'new_base_field_value' => 'bar', - ]; - - if ($create_entity_translation) { - $expected[] = [ - 'bundle' => $entity->bundle(), - 'deleted' => '1', - 'entity_id' => '2', - 'revision_id' => '3', - 'langcode' => 'ro', - 'delta' => '0', - 'new_base_field_value' => 'bar-ro', - ]; - } - } - - $result = $this->database->select($dedicated_deleted_revision_table_name, 't') - ->fields('t') - ->orderBy('revision_id', 'ASC') - ->orderBy('langcode', 'ASC') - ->execute() - ->fetchAll(\PDO::FETCH_ASSOC); - $this->assertSameSize($expected, $result); - - // Use assertEquals and not assertSame here to prevent that a different - // sequence of the columns in the table will affect the check. - $this->assertEquals($expected, $result); - } - - // Check that the field storage definition is marked for purging. - $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions(); - $this->assertArrayHasKey($storage_definition->getUniqueStorageIdentifier(), $deleted_storage_definitions, 'The base field is marked for purging.'); - - // Purge field data, and check that the storage definition has been - // completely removed once the data is purged. - field_purge_batch(10); - $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions(); - $this->assertEmpty($deleted_storage_definitions, 'The base field has been deleted.'); - $this->assertFalse($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated field table was deleted after new_base_field was purged.'); - - if (isset($dedicated_deleted_revision_table_name)) { - $this->assertFalse($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated field revision table was deleted after new_base_field was purged.'); - } - } - - /** - * Test cases for ::testBaseFieldDeleteWithExistingData. - */ - public static function baseFieldDeleteWithExistingDataTestCases() { - return [ - 'Non-revisionable, non-translatable entity type' => [ - 'entity_test_update', - FALSE, - FALSE, - FALSE, - ], - 'Non-revisionable, non-translatable custom data table' => [ - 'entity_test_mul', - FALSE, - FALSE, - FALSE, - ], - 'Non-revisionable, non-translatable entity type, revisionable base field' => [ - 'entity_test_update', - FALSE, - TRUE, - FALSE, - ], - 'Non-revisionable, non-translatable custom data table, revisionable base field' => [ - 'entity_test_mul', - FALSE, - TRUE, - FALSE, - ], - 'Revisionable, translatable entity type, non revisionable and non-translatable base field' => [ - 'entity_test_mulrev', - TRUE, - FALSE, - FALSE, - ], - 'Revisionable, translatable entity type, revisionable and non-translatable base field' => [ - 'entity_test_mulrev', - TRUE, - TRUE, - FALSE, - ], - 'Revisionable and non-translatable entity type, revisionable and non-translatable base field' => [ - 'entity_test_rev', - TRUE, - TRUE, - FALSE, - ], - 'Revisionable and non-translatable entity type, non-revisionable and non-translatable base field' => [ - 'entity_test_rev', - TRUE, - FALSE, - FALSE, - ], - 'Revisionable and translatable entity type, non-revisionable and translatable base field' => [ - 'entity_test_mulrev', - TRUE, - FALSE, - TRUE, - ], - 'Revisionable and translatable entity type, revisionable and translatable base field' => [ - 'entity_test_mulrev', - TRUE, - TRUE, - TRUE, - ], - ]; - } - /** * Tests deleting a bundle field when it has existing data. */ @@ -1243,82 +1020,6 @@ public function testInitialValue(): void { $this->assertEquals('test value', $entities[2]->get('new_base_field')->value); } - /** - * Tests adding a base field with initial values inherited from another field. - * - * @dataProvider initialValueFromFieldTestCases - */ - public function testInitialValueFromField($default_initial_value, $expected_value): void { - $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); - $db_schema = $this->database->schema(); - - // Create two entities before adding the base field. - /** @var \Drupal\entity_test_update\Entity\EntityTestUpdate $entity */ - $storage->create([ - 'name' => 'First entity', - 'test_single_property' => 'test existing value', - ])->save(); - - // The second entity does not have any value for the 'test_single_property' - // field, allowing us to test the 'default_value' parameter of - // \Drupal\Core\Field\BaseFieldDefinition::setInitialValueFromField(). - $storage->create([ - 'name' => 'Second entity', - ])->save(); - - // Add a base field with an initial value inherited from another field. - $definitions['new_base_field'] = BaseFieldDefinition::create('string') - ->setName('new_base_field') - ->setLabel('A new base field') - ->setInitialValueFromField('name'); - $definitions['another_base_field'] = BaseFieldDefinition::create('string') - ->setName('another_base_field') - ->setLabel('Another base field') - ->setInitialValueFromField('test_single_property', $default_initial_value); - - $this->state->set('entity_test_update.additional_base_field_definitions', $definitions); - - $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update."); - $this->assertFalse($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' does not exist before applying the update."); - $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $definitions['new_base_field']); - $this->entityDefinitionUpdateManager->installFieldStorageDefinition('another_base_field', 'entity_test_update', 'entity_test', $definitions['another_base_field']); - $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table."); - $this->assertTrue($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' has been created on the 'entity_test_update' table."); - - // Check that the initial values have been applied. - $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); - $entities = $storage->loadMultiple(); - $this->assertEquals('First entity', $entities[1]->get('new_base_field')->value); - $this->assertEquals('Second entity', $entities[2]->get('new_base_field')->value); - - $this->assertEquals('test existing value', $entities[1]->get('another_base_field')->value); - $this->assertEquals($expected_value, $entities[2]->get('another_base_field')->value); - } - - /** - * Test cases for ::testInitialValueFromField. - */ - public static function initialValueFromFieldTestCases() { - return [ - 'literal value' => [ - 'test initial value', - 'test initial value', - ], - 'indexed array' => [ - ['value' => 'test initial value'], - 'test initial value', - ], - 'empty array' => [ - [], - NULL, - ], - 'null' => [ - NULL, - NULL, - ], - ]; - } - /** * Tests the error handling when using initial values from another field. */ diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php index 3155142602f3..f6cb922fd35f 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php @@ -127,163 +127,6 @@ protected function setUp(): void { new Settings($settings); } - /** - * @covers ::updateFieldableEntityType - * @dataProvider providerTestFieldableEntityTypeUpdates - */ - public function testFieldableEntityTypeUpdates($initial_rev, $initial_mul, $new_rev, $new_mul, $data_migration_supported): void { - // The 'entity_test_update' entity type is neither revisionable nor - // translatable by default, so we need to get it into the initial testing - // state. This also covers the "no existing data" scenario for fieldable - // entity type updates. - if ($initial_rev || $initial_mul) { - $entity_type = $this->getUpdatedEntityTypeDefinition($initial_rev, $initial_mul); - $field_storage_definitions = $this->getUpdatedFieldStorageDefinitions($initial_rev, $initial_mul); - - $this->entityDefinitionUpdateManager->updateFieldableEntityType($entity_type, $field_storage_definitions); - $this->assertEntityTypeSchema($initial_rev, $initial_mul); - } - - // Add a few entities so we can test the data copying step. - $this->insertData($initial_rev, $initial_mul); - - $updated_entity_type = $this->getUpdatedEntityTypeDefinition($new_rev, $new_mul); - $updated_field_storage_definitions = $this->getUpdatedFieldStorageDefinitions($new_rev, $new_mul); - - if (!$data_migration_supported) { - $this->expectException(EntityStorageException::class); - $this->expectExceptionMessage('Converting an entity type from revisionable to non-revisionable or from translatable to non-translatable is not supported.'); - } - - // Check that existing data can be retrieved from the storage before the - // entity schema is updated. - if ($data_migration_supported) { - $this->assertEntityData($initial_rev, $initial_mul); - } - - // Enable the creation of a new base field during a fieldable entity type - // update. - $this->state->set('entity_test_update.install_new_base_field_during_update', TRUE); - - // Simulate a batch run since we are converting the entities one by one. - $sandbox = []; - do { - $this->entityDefinitionUpdateManager->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions, $sandbox); - } while ($sandbox['#finished'] != 1); - - $this->assertEntityTypeSchema($new_rev, $new_mul, TRUE); - $this->assertEntityData($initial_rev, $initial_mul); - - $change_list = $this->entityDefinitionUpdateManager->getChangeList(); - $this->assertArrayNotHasKey('entity_test_update', $change_list, "There are no remaining updates for the 'entity_test_update' entity type."); - - // Check that we can still save new entities after the schema has been - // updated. - $this->insertData($new_rev, $new_mul); - - // Check that the backup tables have been kept in place. - $this->assertBackupTables(); - } - - /** - * Data provider for testFieldableEntityTypeUpdates(). - */ - public static function providerTestFieldableEntityTypeUpdates() { - return [ - 'no change' => [ - 'initial_rev' => FALSE, - 'initial_mul' => FALSE, - 'new_rev' => FALSE, - 'new_mul' => FALSE, - 'data_migration_supported' => TRUE, - ], - 'non_rev non_mul to rev non_mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => FALSE, - 'new_rev' => TRUE, - 'new_mul' => FALSE, - 'data_migration_supported' => TRUE, - ], - 'non_rev non_mul to rev mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => FALSE, - 'new_rev' => TRUE, - 'new_mul' => TRUE, - 'data_migration_supported' => TRUE, - ], - 'non_rev non_mul to non_rev mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => FALSE, - 'new_rev' => FALSE, - 'new_mul' => TRUE, - 'data_migration_supported' => TRUE, - ], - 'rev non_mul to non_rev non_mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => FALSE, - 'new_rev' => FALSE, - 'new_mul' => FALSE, - 'data_migration_supported' => FALSE, - ], - 'rev non_mul to non_rev mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => FALSE, - 'new_rev' => FALSE, - 'new_mul' => TRUE, - 'data_migration_supported' => FALSE, - ], - 'rev non_mul to rev mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => FALSE, - 'new_rev' => TRUE, - 'new_mul' => TRUE, - 'data_migration_supported' => TRUE, - ], - 'non_rev mul to non_rev non_mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => TRUE, - 'new_rev' => FALSE, - 'new_mul' => FALSE, - 'data_migration_supported' => FALSE, - ], - 'non_rev mul to rev non_mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => TRUE, - 'new_rev' => TRUE, - 'new_mul' => FALSE, - 'data_migration_supported' => FALSE, - ], - 'non_rev mul to rev mul' => [ - 'initial_rev' => FALSE, - 'initial_mul' => TRUE, - 'new_rev' => TRUE, - 'new_mul' => TRUE, - 'data_migration_supported' => TRUE, - ], - 'rev mul to non_rev non_mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => TRUE, - 'new_rev' => FALSE, - 'new_mul' => FALSE, - 'data_migration_supported' => FALSE, - ], - 'rev mul to rev non_mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => TRUE, - 'new_rev' => TRUE, - 'new_mul' => FALSE, - 'data_migration_supported' => FALSE, - ], - 'rev mul to non_rev mul' => [ - 'initial_rev' => TRUE, - 'initial_mul' => TRUE, - 'new_rev' => FALSE, - 'new_mul' => TRUE, - 'data_migration_supported' => FALSE, - ], - ]; - } - /** * Generates test entities for the 'entity_test_update' entity type. * -- GitLab