Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / tests / src / Kernel / FieldImportDeleteUninstallTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Delete field storages and fields during config synchronization and uninstall
11  * module that provides the field type.
12  *
13  * @group field
14  * @see \Drupal\field\ConfigImporterFieldPurger
15  * @see field_config_import_steps_alter()
16  */
17 class FieldImportDeleteUninstallTest extends FieldKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['telephone'];
25
26   protected function setUp() {
27     parent::setUp();
28     // Module uninstall requires users_data tables.
29     // @see drupal_flush_all_caches()
30     // @see user_modules_uninstalled()
31     $this->installSchema('user', ['users_data']);
32   }
33
34   /**
35    * Tests deleting field storages and fields as part of config import.
36    */
37   public function testImportDeleteUninstall() {
38     // Create a field to delete to prove that
39     // \Drupal\field\ConfigImporterFieldPurger does not purge fields that are
40     // not related to the configuration synchronization.
41     $unrelated_field_storage = FieldStorageConfig::create([
42       'field_name' => 'field_int',
43       'entity_type' => 'entity_test',
44       'type' => 'integer',
45     ]);
46     $unrelated_field_storage->save();
47     FieldConfig::create([
48       'field_storage' => $unrelated_field_storage,
49       'bundle' => 'entity_test',
50     ])->save();
51
52     // Create a telephone field for validation.
53     $field_storage = FieldStorageConfig::create([
54       'field_name' => 'field_test',
55       'entity_type' => 'entity_test',
56       'type' => 'telephone',
57     ]);
58     $field_storage->save();
59     FieldConfig::create([
60       'field_storage' => $field_storage,
61       'bundle' => 'entity_test',
62     ])->save();
63
64     $entity = EntityTest::create();
65     $value = '+0123456789';
66     $entity->field_test = $value;
67     $entity->field_int = '99';
68     $entity->name->value = $this->randomMachineName();
69     $entity->save();
70
71     // Verify entity has been created properly.
72     $id = $entity->id();
73     $entity = EntityTest::load($id);
74     $this->assertEqual($entity->field_test->value, $value);
75     $this->assertEqual($entity->field_test[0]->value, $value);
76     $this->assertEqual($entity->field_int->value, '99');
77
78     // Delete unrelated field before copying configuration and running the
79     // synchronization.
80     $unrelated_field_storage->delete();
81
82     $active = $this->container->get('config.storage');
83     $sync = $this->container->get('config.storage.sync');
84     $this->copyConfig($active, $sync);
85
86     // Stage uninstall of the Telephone module.
87     $core_extension = $this->config('core.extension')->get();
88     unset($core_extension['module']['telephone']);
89     $sync->write('core.extension', $core_extension);
90
91     // Stage the field deletion
92     $sync->delete('field.storage.entity_test.field_test');
93     $sync->delete('field.field.entity_test.entity_test.field_test');
94
95     $steps = $this->configImporter()->initialize();
96     $this->assertIdentical($steps[0], ['\Drupal\field\ConfigImporterFieldPurger', 'process'], 'The additional process configuration synchronization step has been added.');
97
98     // This will purge all the data, delete the field and uninstall the
99     // Telephone module.
100     $this->configImporter()->import();
101
102     $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
103     $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The test field has been deleted by the configuration synchronization');
104     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
105     $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
106     $this->assertTrue(isset($deleted_storages[$unrelated_field_storage->uuid()]), 'Unrelated field not purged by configuration synchronization.');
107   }
108
109   /**
110    * Tests purging already deleted field storages and fields during a config
111    * import.
112    */
113   public function testImportAlreadyDeletedUninstall() {
114     // Create a telephone field for validation.
115     $field_storage = FieldStorageConfig::create([
116       'field_name' => 'field_test',
117       'entity_type' => 'entity_test',
118       'type' => 'telephone',
119     ]);
120     $field_storage->save();
121     $field_storage_uuid = $field_storage->uuid();
122     FieldConfig::create([
123       'field_storage' => $field_storage,
124       'bundle' => 'entity_test',
125     ])->save();
126
127     // Create 12 entities to ensure that the purging works as expected.
128     for ($i = 0; $i < 12; $i++) {
129       $entity = EntityTest::create();
130       $value = '+0123456789';
131       $entity->field_test = $value;
132       $entity->name->value = $this->randomMachineName();
133       $entity->save();
134
135       // Verify entity has been created properly.
136       $id = $entity->id();
137       $entity = EntityTest::load($id);
138       $this->assertEqual($entity->field_test->value, $value);
139     }
140
141     // Delete the field.
142     $field_storage->delete();
143
144     $active = $this->container->get('config.storage');
145     $sync = $this->container->get('config.storage.sync');
146     $this->copyConfig($active, $sync);
147
148     // Stage uninstall of the Telephone module.
149     $core_extension = $this->config('core.extension')->get();
150     unset($core_extension['module']['telephone']);
151     $sync->write('core.extension', $core_extension);
152
153     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
154     $this->assertTrue(isset($deleted_storages[$field_storage_uuid]), 'Field has been deleted and needs purging before configuration synchronization.');
155
156     $steps = $this->configImporter()->initialize();
157     $this->assertIdentical($steps[0], ['\Drupal\field\ConfigImporterFieldPurger', 'process'], 'The additional process configuration synchronization step has been added.');
158
159     // This will purge all the data, delete the field and uninstall the
160     // Telephone module.
161     $this->configImporter()->import();
162
163     $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
164     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
165     $this->assertFalse(isset($deleted_storages[$field_storage_uuid]), 'Field has been completed removed from the system.');
166   }
167
168 }