3 namespace Drupal\Tests\system\Functional\Entity\Update;
5 use Drupal\Core\Entity\Sql\TemporaryTableMapping;
8 * Tests converting a translatable entity type with data to revisionable.
14 class SqlContentEntityStorageSchemaConverterTranslatableTest extends SqlContentEntityStorageSchemaConverterTestBase {
19 protected function setDatabaseDumpFiles() {
20 $this->databaseDumpFiles = [
21 __DIR__ . '/../../../../fixtures/update/drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz',
22 __DIR__ . '/../../../../fixtures/update/drupal-8.entity-test-schema-converter-enabled.php',
27 * Tests that a failed "make revisionable" update preserves the existing data.
29 public function testMakeRevisionableErrorHandling() {
30 $original_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
31 $original_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
33 $original_entity_schema_data = $this->installedStorageSchema->get('entity_test_update.entity_schema_data', []);
34 foreach ($original_storage_definitions as $storage_definition) {
35 $original_field_schema_data[$storage_definition->getName()] = $this->installedStorageSchema->get('entity_test_update.field_schema_data.' . $storage_definition->getName(), []);
38 // Check that entity type is not revisionable prior to running the update
40 $this->assertFalse($original_entity_type->isRevisionable());
42 // Make the update throw an exception during the entity save process.
43 \Drupal::state()->set('entity_test_update.throw_exception', TRUE);
45 // Since the update process is interrupted by the exception thrown above,
46 // we can not do the full post update testing offered by UpdatePathTestBase.
47 $this->checkFailedUpdates = FALSE;
49 // Make the entity type revisionable and run the updates.
50 $this->updateEntityTypeToRevisionableAndTranslatable();
54 // Check that the update failed.
55 $this->assertRaw('<strong>' . t('Failed:') . '</strong>');
57 // Check that the last installed entity type definition is kept as
59 $new_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
60 $this->assertFalse($new_entity_type->isRevisionable(), 'The entity type is kept unchanged.');
62 // Check that the last installed field storage definitions did not change by
63 // looking at the 'langcode' field, which is updated automatically.
64 $new_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
65 $langcode_key = $original_entity_type->getKey('langcode');
66 $this->assertEqual($original_storage_definitions[$langcode_key]->isRevisionable(), $new_storage_definitions[$langcode_key]->isRevisionable(), "The 'langcode' field is kept unchanged.");
68 /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */
69 $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
71 // Check that installed storage schema did not change.
72 $new_entity_schema_data = $this->installedStorageSchema->get('entity_test_update.entity_schema_data', []);
73 $this->assertEqual($original_entity_schema_data, $new_entity_schema_data);
75 foreach ($new_storage_definitions as $storage_definition) {
76 $new_field_schema_data[$storage_definition->getName()] = $this->installedStorageSchema->get('entity_test_update.field_schema_data.' . $storage_definition->getName(), []);
78 $this->assertEqual($original_field_schema_data, $new_field_schema_data);
80 // Check that temporary tables have been removed.
81 $schema = \Drupal::database()->schema();
82 foreach ($storage->getTableMapping()->getTableNames() as $table_name) {
83 $this->assertFalse($schema->tableExists(TemporaryTableMapping::getTempTableName($table_name)));
86 // Check that the original tables still exist and their data is intact.
87 $this->assertTrue($schema->tableExists('entity_test_update'));
88 $this->assertTrue($schema->tableExists('entity_test_update_data'));
90 $base_table_count = \Drupal::database()->select('entity_test_update')
94 $this->assertEqual($base_table_count, 102);
96 $data_table_count = \Drupal::database()->select('entity_test_update_data')
100 // There are two records for each entity, one for English and one for
102 $this->assertEqual($data_table_count, 204);
104 $base_table_row = \Drupal::database()->select('entity_test_update')
105 ->fields('entity_test_update')
106 ->condition('id', 1, '=')
107 ->condition('langcode', 'en', '=')
109 ->fetchAllAssoc('id');
110 $this->assertEqual('843e9ac7-3351-4cc1-a202-2dbffffae21c', $base_table_row[1]->uuid);
112 $data_table_row = \Drupal::database()->select('entity_test_update_data')
113 ->fields('entity_test_update_data')
114 ->condition('id', 1, '=')
115 ->condition('langcode', 'en', '=')
117 ->fetchAllAssoc('id');
118 $this->assertEqual('1 - test single property', $data_table_row[1]->test_single_property);
119 $this->assertEqual('1 - test multiple properties - value1', $data_table_row[1]->test_multiple_properties__value1);
120 $this->assertEqual('1 - test multiple properties - value2', $data_table_row[1]->test_multiple_properties__value2);
121 $this->assertEqual('1 - test entity base field info', $data_table_row[1]->test_entity_base_field_info);