Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / src / Tests / FieldImportDeleteUninstallUiTest.php
1 <?php
2
3 namespace Drupal\field\Tests;
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 through the UI.
12  *
13  * @group field
14  * @see \Drupal\field\ConfigImporterFieldPurger
15  * @see field_config_import_steps_alter()
16  * @see field_form_config_admin_import_form_alter()
17  */
18 class FieldImportDeleteUninstallUiTest extends FieldTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['entity_test', 'telephone', 'config', 'filter', 'datetime'];
26
27   protected function setUp() {
28     parent::setUp();
29
30     $this->drupalLogin($this->drupalCreateUser(['synchronize configuration']));
31   }
32
33   /**
34    * Tests deleting field storages and fields as part of config import.
35    */
36   public function testImportDeleteUninstall() {
37     // Create a telephone field.
38     $field_storage = FieldStorageConfig::create([
39       'field_name' => 'field_tel',
40       'entity_type' => 'entity_test',
41       'type' => 'telephone',
42     ]);
43     $field_storage->save();
44     FieldConfig::create([
45       'field_storage' => $field_storage,
46       'bundle' => 'entity_test',
47     ])->save();
48
49     // Create a text field.
50     $date_field_storage = FieldStorageConfig::create([
51       'field_name' => 'field_date',
52       'entity_type' => 'entity_test',
53       'type' => 'datetime',
54     ]);
55     $date_field_storage->save();
56     FieldConfig::create([
57       'field_storage' => $date_field_storage,
58       'bundle' => 'entity_test',
59     ])->save();
60
61     // Create an entity which has values for the telephone and text field.
62     $entity = EntityTest::create();
63     $value = '+0123456789';
64     $entity->field_tel = $value;
65     $entity->field_date = time();
66     $entity->name->value = $this->randomMachineName();
67     $entity->save();
68
69     // Delete the text field before exporting configuration so that we can test
70     // that deleted fields that are provided by modules that will be uninstalled
71     // are also purged and that the UI message includes such fields.
72     $date_field_storage->delete();
73
74     // Verify entity has been created properly.
75     $id = $entity->id();
76     $entity = EntityTest::load($id);
77     $this->assertEqual($entity->field_tel->value, $value);
78     $this->assertEqual($entity->field_tel[0]->value, $value);
79
80     $active = $this->container->get('config.storage');
81     $sync = $this->container->get('config.storage.sync');
82     $this->copyConfig($active, $sync);
83
84     // Stage uninstall of the Telephone module.
85     $core_extension = $this->config('core.extension')->get();
86     unset($core_extension['module']['telephone']);
87     $sync->write('core.extension', $core_extension);
88
89     // Stage the field deletion
90     $sync->delete('field.storage.entity_test.field_tel');
91     $sync->delete('field.field.entity_test.entity_test.field_tel');
92     $this->drupalGet('admin/config/development/configuration');
93     // Test that the message for one field being purged during a configuration
94     // synchronization is correct.
95     $this->assertText('This synchronization will delete data from the field entity_test.field_tel.');
96
97     // Stage an uninstall of the datetime module to test the message for
98     // multiple fields.
99     unset($core_extension['module']['datetime']);
100     $sync->write('core.extension', $core_extension);
101
102     $this->drupalGet('admin/config/development/configuration');
103     $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.');
104
105     // This will purge all the data, delete the field and uninstall the
106     // Telephone and Text modules.
107     $this->drupalPostForm(NULL, [], t('Import all'));
108     $this->assertNoText('Field data will be deleted by this synchronization.');
109     $this->rebuildContainer();
110     $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
111     $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
112     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
113     $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
114     $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Text field has been completed removed from the system.');
115   }
116
117 }