Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Entity / Update / SqlContentEntityStorageSchemaConverterTranslatableTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Entity\Update;
4
5 use Drupal\Core\Entity\Sql\TemporaryTableMapping;
6
7 /**
8  * Tests converting a translatable entity type with data to revisionable.
9  *
10  * @group Entity
11  * @group Update
12  * @group legacy
13  */
14 class SqlContentEntityStorageSchemaConverterTranslatableTest extends SqlContentEntityStorageSchemaConverterTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
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',
23     ];
24   }
25
26   /**
27    * Tests that a failed "make revisionable" update preserves the existing data.
28    */
29   public function testMakeRevisionableErrorHandling() {
30     $original_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
31     $original_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');
32
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(), []);
36     }
37
38     // Check that entity type is not revisionable prior to running the update
39     // process.
40     $this->assertFalse($original_entity_type->isRevisionable());
41
42     // Make the update throw an exception during the entity save process.
43     \Drupal::state()->set('entity_test_update.throw_exception', TRUE);
44
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;
48
49     // Make the entity type revisionable and run the updates.
50     $this->updateEntityTypeToRevisionableAndTranslatable();
51
52     $this->runUpdates();
53
54     // Check that the update failed.
55     $this->assertRaw('<strong>' . t('Failed:') . '</strong>');
56
57     // Check that the last installed entity type definition is kept as
58     // non-revisionable.
59     $new_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
60     $this->assertFalse($new_entity_type->isRevisionable(), 'The entity type is kept unchanged.');
61
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.");
67
68     /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage */
69     $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
70
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);
74
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(), []);
77     }
78     $this->assertEqual($original_field_schema_data, $new_field_schema_data);
79
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)));
84     }
85
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'));
89
90     $base_table_count = \Drupal::database()->select('entity_test_update')
91       ->countQuery()
92       ->execute()
93       ->fetchField();
94     $this->assertEqual($base_table_count, 102);
95
96     $data_table_count = \Drupal::database()->select('entity_test_update_data')
97       ->countQuery()
98       ->execute()
99       ->fetchField();
100     // There are two records for each entity, one for English and one for
101     // Romanian.
102     $this->assertEqual($data_table_count, 204);
103
104     $base_table_row = \Drupal::database()->select('entity_test_update')
105       ->fields('entity_test_update')
106       ->condition('id', 1, '=')
107       ->condition('langcode', 'en', '=')
108       ->execute()
109       ->fetchAllAssoc('id');
110     $this->assertEqual('843e9ac7-3351-4cc1-a202-2dbffffae21c', $base_table_row[1]->uuid);
111
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', '=')
116       ->execute()
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);
122   }
123
124 }