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 / SqlContentEntityStorageSchemaConverterTestBase.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Entity\Update;
4
5 use Drupal\Core\Entity\Sql\TemporaryTableMapping;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
8
9 /**
10  * Defines a class for testing the conversion of entity types to revisionable.
11  */
12 abstract class SqlContentEntityStorageSchemaConverterTestBase extends UpdatePathTestBase {
13
14   use EntityDefinitionTestTrait;
15
16   /**
17    * The entity manager service.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * The entity definition update manager.
25    *
26    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
27    */
28   protected $entityDefinitionUpdateManager;
29
30   /**
31    * The last installed schema repository service.
32    *
33    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
34    */
35   protected $lastInstalledSchemaRepository;
36
37   /**
38    * The key-value collection for tracking installed storage schema.
39    *
40    * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
41    */
42   protected $installedStorageSchema;
43
44   /**
45    * The state service.
46    *
47    * @var \Drupal\Core\State\StateInterface
48    */
49   protected $state;
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function setUp() {
55     parent::setUp();
56
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();
62   }
63
64   /**
65    * Tests the conversion of an entity type to revisionable.
66    */
67   public function testMakeRevisionable() {
68     // Check that entity type is not revisionable prior to running the update
69     // process.
70     $entity_test_update = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
71     $this->assertFalse($entity_test_update->isRevisionable());
72
73     $translatable = $entity_test_update->isTranslatable();
74
75     // Make the entity type revisionable and run the updates.
76     if ($translatable) {
77       $this->updateEntityTypeToRevisionableAndTranslatable();
78     }
79     else {
80       $this->updateEntityTypeToRevisionable();
81     }
82
83     $this->runUpdates();
84
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');
88
89     $this->assertTrue($entity_test_update->isRevisionable());
90     $this->assertEquals($translatable, isset($field_storage_definitions['revision_translation_affected']));
91
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.');
95
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);
100
101       $this->assertEqual($i, $revision->id());
102       $this->assertEqual($i, $revision->getRevisionId());
103
104       $this->assertEqual($i . ' - test single property', $revision->test_single_property->value);
105
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);
108
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);
111
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);
116
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);
121
122       $this->assertEqual($i . ' - test entity base field info', $revision->test_entity_base_field_info->value);
123
124       // Do the same checks for translated field values if the entity type is
125       // translatable.
126       if (!$translatable) {
127         continue;
128       }
129
130       // Check that the correct initial value was provided for the
131       // 'revision_translation_affected' field.
132       $this->assertTrue($revision->revision_translation_affected->value);
133
134       $translation = $revision->getTranslation('ro');
135
136       $this->assertEqual($i . ' - test single property - ro', $translation->test_single_property->value);
137
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);
140
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);
143
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);
148
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);
153
154       $this->assertEqual($i . ' - test entity base field info - ro', $translation->test_entity_base_field_info->value);
155     }
156
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)));
161     }
162
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_')));
167     }
168   }
169
170 }