3 namespace Drupal\Tests\system\Functional\Entity\Update;
5 use Drupal\Core\Entity\Sql\TemporaryTableMapping;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
10 * Defines a class for testing the conversion of entity types to revisionable.
12 abstract class SqlContentEntityStorageSchemaConverterTestBase extends UpdatePathTestBase {
14 use EntityDefinitionTestTrait;
17 * The entity manager service.
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityManager;
24 * The entity definition update manager.
26 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
28 protected $entityDefinitionUpdateManager;
31 * The last installed schema repository service.
33 * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
35 protected $lastInstalledSchemaRepository;
38 * The key-value collection for tracking installed storage schema.
40 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
42 protected $installedStorageSchema;
47 * @var \Drupal\Core\State\StateInterface
54 protected function setUp() {
57 $this->entityManager = \Drupal::entityManager();
58 $this->entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
59 $this->lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
60 $this->installedStorageSchema = \Drupal::keyValue('entity.storage_schema.sql');
61 $this->state = \Drupal::state();
65 * Tests the conversion of an entity type to revisionable.
67 public function testMakeRevisionable() {
68 // Check that entity type is not revisionable prior to running the update
70 $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
71 $this->assertFalse($entity_test_update->isRevisionable());
73 $translatable = $entity_test_update->isTranslatable();
75 // Make the entity type revisionable and run the updates.
77 $this->updateEntityTypeToRevisionableAndTranslatable();
80 $this->updateEntityTypeToRevisionable();
85 /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_test_update */
86 $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
87 $field_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
89 $this->assertTrue($entity_test_update->isRevisionable());
90 $this->assertEquals($translatable, isset($field_storage_definitions['revision_translation_affected']));
92 /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */
93 $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
94 $this->assertEqual(count($storage->loadMultiple()), 102, 'All test entities were found.');
96 // Check that each field value was copied correctly to the revision tables.
97 for ($i = 1; $i <= 102; $i++) {
98 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
99 $revision = $storage->loadRevision($i);
101 $this->assertEqual($i, $revision->id());
102 $this->assertEqual($i, $revision->getRevisionId());
104 $this->assertEqual($i . ' - test single property', $revision->test_single_property->value);
106 $this->assertEqual($i . ' - test multiple properties - value1', $revision->test_multiple_properties->value1);
107 $this->assertEqual($i . ' - test multiple properties - value2', $revision->test_multiple_properties->value2);
109 $this->assertEqual($i . ' - test single property multiple values 0', $revision->test_single_property_multiple_values->value);
110 $this->assertEqual($i . ' - test single property multiple values 1', $revision->test_single_property_multiple_values[1]->value);
112 $this->assertEqual($i . ' - test multiple properties multiple values - value1 0', $revision->test_multiple_properties_multiple_values[0]->value1);
113 $this->assertEqual($i . ' - test multiple properties multiple values - value2 0', $revision->test_multiple_properties_multiple_values[0]->value2);
114 $this->assertEqual($i . ' - test multiple properties multiple values - value1 1', $revision->test_multiple_properties_multiple_values[1]->value1);
115 $this->assertEqual($i . ' - test multiple properties multiple values - value2 1', $revision->test_multiple_properties_multiple_values[1]->value2);
117 $this->assertEqual($i . ' - field test configurable field - value1 0', $revision->field_test_configurable_field[0]->value1);
118 $this->assertEqual($i . ' - field test configurable field - value2 0', $revision->field_test_configurable_field[0]->value2);
119 $this->assertEqual($i . ' - field test configurable field - value1 1', $revision->field_test_configurable_field[1]->value1);
120 $this->assertEqual($i . ' - field test configurable field - value2 1', $revision->field_test_configurable_field[1]->value2);
122 $this->assertEqual($i . ' - test entity base field info', $revision->test_entity_base_field_info->value);
124 // Do the same checks for translated field values if the entity type is
126 if (!$translatable) {
130 // Check that the correct initial value was provided for the
131 // 'revision_translation_affected' field.
132 $this->assertTrue($revision->revision_translation_affected->value);
134 $translation = $revision->getTranslation('ro');
136 $this->assertEqual($i . ' - test single property - ro', $translation->test_single_property->value);
138 $this->assertEqual($i . ' - test multiple properties - value1 - ro', $translation->test_multiple_properties->value1);
139 $this->assertEqual($i . ' - test multiple properties - value2 - ro', $translation->test_multiple_properties->value2);
141 $this->assertEqual($i . ' - test single property multiple values 0 - ro', $translation->test_single_property_multiple_values[0]->value);
142 $this->assertEqual($i . ' - test single property multiple values 1 - ro', $translation->test_single_property_multiple_values[1]->value);
144 $this->assertEqual($i . ' - test multiple properties multiple values - value1 0 - ro', $translation->test_multiple_properties_multiple_values[0]->value1);
145 $this->assertEqual($i . ' - test multiple properties multiple values - value2 0 - ro', $translation->test_multiple_properties_multiple_values[0]->value2);
146 $this->assertEqual($i . ' - test multiple properties multiple values - value1 1 - ro', $translation->test_multiple_properties_multiple_values[1]->value1);
147 $this->assertEqual($i . ' - test multiple properties multiple values - value2 1 - ro', $translation->test_multiple_properties_multiple_values[1]->value2);
149 $this->assertEqual($i . ' - field test configurable field - value1 0 - ro', $translation->field_test_configurable_field[0]->value1);
150 $this->assertEqual($i . ' - field test configurable field - value2 0 - ro', $translation->field_test_configurable_field[0]->value2);
151 $this->assertEqual($i . ' - field test configurable field - value1 1 - ro', $translation->field_test_configurable_field[1]->value1);
152 $this->assertEqual($i . ' - field test configurable field - value2 1 - ro', $translation->field_test_configurable_field[1]->value2);
154 $this->assertEqual($i . ' - test entity base field info - ro', $translation->test_entity_base_field_info->value);
157 // Check that temporary tables have been removed at the end of the process.
158 $schema = \Drupal::database()->schema();
159 foreach ($storage->getTableMapping()->getTableNames() as $table_name) {
160 $this->assertFalse($schema->tableExists(TemporaryTableMapping::getTempTableName($table_name)));
163 // Check that backup tables have been removed at the end of the process.
164 $schema = \Drupal::database()->schema();
165 foreach ($storage->getTableMapping()->getTableNames() as $table_name) {
166 $this->assertFalse($schema->tableExists(TemporaryTableMapping::getTempTableName($table_name, 'old_')));