Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / field / tests / src / Kernel / FieldImportDeleteTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11  * Delete field storages and fields during config delete method invocation.
12  *
13  * @group field
14  */
15 class FieldImportDeleteTest extends FieldKernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * The default configuration provided by field_test_config is imported by
21    * \Drupal\Tests\field\Kernel\FieldKernelTestBase::setUp() when it installs
22    * field configuration.
23    *
24    * @var array
25    */
26   public static $modules = ['field_test_config'];
27
28   /**
29    * Tests deleting field storages and fields as part of config import.
30    */
31   public function testImportDelete() {
32     $this->installConfig(['field_test_config']);
33     // At this point there are 5 field configuration objects in the active
34     // storage.
35     // - field.storage.entity_test.field_test_import
36     // - field.storage.entity_test.field_test_import_2
37     // - field.field.entity_test.entity_test.field_test_import
38     // - field.field.entity_test.entity_test.field_test_import_2
39     // - field.field.entity_test.test_bundle.field_test_import_2
40
41     $field_name = 'field_test_import';
42     $field_storage_id = "entity_test.$field_name";
43     $field_name_2 = 'field_test_import_2';
44     $field_storage_id_2 = "entity_test.$field_name_2";
45     $field_id = "entity_test.entity_test.$field_name";
46     $field_id_2a = "entity_test.entity_test.$field_name_2";
47     $field_id_2b = "entity_test.test_bundle.$field_name_2";
48     $field_storage_config_name = "field.storage.$field_storage_id";
49     $field_storage_config_name_2 = "field.storage.$field_storage_id_2";
50     $field_config_name = "field.field.$field_id";
51     $field_config_name_2a = "field.field.$field_id_2a";
52     $field_config_name_2b = "field.field.$field_id_2b";
53
54     // Create an entity with data in the first field to make sure that field
55     // needs to be purged.
56     $entity_test = EntityTest::create([
57       'type' => 'entity_test',
58     ]);
59     $entity_test->set($field_name, 'test data');
60     $entity_test->save();
61
62     // Create a second bundle for the 'Entity test' entity type.
63     entity_test_create_bundle('test_bundle');
64
65     // Get the uuid's for the field storages.
66     $field_storage_uuid = FieldStorageConfig::load($field_storage_id)->uuid();
67     $field_storage_uuid_2 = FieldStorageConfig::load($field_storage_id_2)->uuid();
68
69     $active = $this->container->get('config.storage');
70     $sync = $this->container->get('config.storage.sync');
71     $this->copyConfig($active, $sync);
72     $this->assertTrue($sync->delete($field_storage_config_name), SafeMarkup::format('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name]));
73     $this->assertTrue($sync->delete($field_storage_config_name_2), SafeMarkup::format('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name_2]));
74     $this->assertTrue($sync->delete($field_config_name), SafeMarkup::format('Deleted field: @field', ['@field' => $field_config_name]));
75     $this->assertTrue($sync->delete($field_config_name_2a), SafeMarkup::format('Deleted field: @field', ['@field' => $field_config_name_2a]));
76     $this->assertTrue($sync->delete($field_config_name_2b), SafeMarkup::format('Deleted field: @field', ['@field' => $field_config_name_2b]));
77
78     $deletes = $this->configImporter()->getUnprocessedConfiguration('delete');
79     $this->assertEqual(count($deletes), 5, 'Importing configuration will delete 3 fields and 2 field storages.');
80
81     // Import the content of the sync directory.
82     $this->configImporter()->import();
83
84     // Check that the field storages and fields are gone.
85     \Drupal::entityManager()->getStorage('field_storage_config')->resetCache([$field_storage_id]);
86     $field_storage = FieldStorageConfig::load($field_storage_id);
87     $this->assertFalse($field_storage, 'The field storage was deleted.');
88     \Drupal::entityManager()->getStorage('field_storage_config')->resetCache([$field_storage_id_2]);
89     $field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
90     $this->assertFalse($field_storage_2, 'The second field storage was deleted.');
91     \Drupal::entityManager()->getStorage('field_config')->resetCache([$field_id]);
92     $field = FieldConfig::load($field_id);
93     $this->assertFalse($field, 'The field was deleted.');
94     \Drupal::entityManager()->getStorage('field_config')->resetCache([$field_id_2a]);
95     $field_2a = FieldConfig::load($field_id_2a);
96     $this->assertFalse($field_2a, 'The second field on test bundle was deleted.');
97     \Drupal::entityManager()->getStorage('field_config')->resetCache([$field_id_2b]);
98     $field_2b = FieldConfig::load($field_id_2b);
99     $this->assertFalse($field_2b, 'The second field on test bundle 2 was deleted.');
100
101     // Check that all config files are gone.
102     $active = $this->container->get('config.storage');
103     $this->assertIdentical($active->listAll($field_storage_config_name), []);
104     $this->assertIdentical($active->listAll($field_storage_config_name_2), []);
105     $this->assertIdentical($active->listAll($field_config_name), []);
106     $this->assertIdentical($active->listAll($field_config_name_2a), []);
107     $this->assertIdentical($active->listAll($field_config_name_2b), []);
108
109     // Check that only the first storage definition is preserved in state.
110     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
111     $this->assertTrue(isset($deleted_storages[$field_storage_uuid]));
112     $this->assertFalse(isset($deleted_storages[$field_storage_uuid_2]));
113
114     // Purge field data, and check that the storage definition has been
115     // completely removed once the data is purged.
116     field_purge_batch(10);
117     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
118     $this->assertTrue(empty($deleted_storages), 'Fields are deleted');
119   }
120
121 }