Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigImportAllTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Config\StorageComparer;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\shortcut\Entity\Shortcut;
8 use Drupal\taxonomy\Entity\Term;
9 use Drupal\Tests\SchemaCheckTestTrait;
10 use Drupal\Tests\system\Functional\Module\ModuleTestBase;
11
12 /**
13  * Tests the largest configuration import possible with all available modules.
14  *
15  * @group config
16  */
17 class ConfigImportAllTest extends ModuleTestBase {
18
19   use SchemaCheckTestTrait;
20
21   /**
22    * A user with the 'synchronize configuration' permission.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $webUser;
27
28   /**
29    * The profile to install as a basis for testing.
30    *
31    * Using the standard profile as this has a lot of additional configuration.
32    *
33    * @var string
34    */
35   protected $profile = 'standard';
36
37   protected function setUp() {
38     parent::setUp();
39
40     $this->webUser = $this->drupalCreateUser(['synchronize configuration']);
41     $this->drupalLogin($this->webUser);
42   }
43
44   /**
45    * Tests that a fixed set of modules can be installed and uninstalled.
46    */
47   public function testInstallUninstall() {
48
49     // Get a list of modules to enable.
50     $all_modules = system_rebuild_module_data();
51     $all_modules = array_filter($all_modules, function ($module) {
52       // Filter contrib, hidden, already enabled modules and modules in the
53       // Testing package.
54       if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
55         return FALSE;
56       }
57       return TRUE;
58     });
59
60     // Install every module possible.
61     \Drupal::service('module_installer')->install(array_keys($all_modules));
62
63     $this->assertModules(array_keys($all_modules), TRUE);
64     foreach ($all_modules as $module => $info) {
65       $this->assertModuleConfig($module);
66       $this->assertModuleTablesExist($module);
67     }
68
69     // Export active config to sync.
70     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
71
72     system_list_reset();
73     $this->resetAll();
74
75     // Delete every field on the site so all modules can be uninstalled. For
76     // example, if a comment field exists then module becomes required and can
77     // not be uninstalled.
78
79     $field_storages = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple();
80     \Drupal::entityManager()->getStorage('field_storage_config')->delete($field_storages);
81     // Purge the data.
82     field_purge_batch(1000);
83
84     // Delete all terms.
85     $terms = Term::loadMultiple();
86     entity_delete_multiple('taxonomy_term', array_keys($terms));
87
88     // Delete all filter formats.
89     $filters = FilterFormat::loadMultiple();
90     entity_delete_multiple('filter_format', array_keys($filters));
91
92     // Delete any shortcuts so the shortcut module can be uninstalled.
93     $shortcuts = Shortcut::loadMultiple();
94     entity_delete_multiple('shortcut', array_keys($shortcuts));
95
96     system_list_reset();
97     $all_modules = system_rebuild_module_data();
98
99     // Ensure that only core required modules and the install profile can not be uninstalled.
100     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(array_keys($all_modules));
101     $this->assertEqual(['standard', 'system', 'user'], array_keys($validation_reasons));
102
103     $modules_to_uninstall = array_filter($all_modules, function ($module) use ($validation_reasons) {
104       // Filter required and not enabled modules.
105       if (!empty($module->info['required']) || $module->status == FALSE) {
106         return FALSE;
107       }
108       return TRUE;
109     });
110
111     // Can not uninstall config and use admin/config/development/configuration!
112     unset($modules_to_uninstall['config']);
113
114     $this->assertTrue(isset($modules_to_uninstall['comment']), 'The comment module will be disabled');
115     $this->assertTrue(isset($modules_to_uninstall['file']), 'The File module will be disabled');
116     $this->assertTrue(isset($modules_to_uninstall['editor']), 'The Editor module will be disabled');
117
118     // Uninstall all modules that can be uninstalled.
119     \Drupal::service('module_installer')->uninstall(array_keys($modules_to_uninstall));
120
121     $this->assertModules(array_keys($modules_to_uninstall), FALSE);
122     foreach ($modules_to_uninstall as $module => $info) {
123       $this->assertNoModuleConfig($module);
124       $this->assertModuleTablesDoNotExist($module);
125     }
126
127     // Import the configuration thereby re-installing all the modules.
128     $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
129     // Modules have been installed that have services.
130     $this->rebuildContainer();
131
132     // Check that there are no errors.
133     $this->assertIdentical($this->configImporter()->getErrors(), []);
134
135     // Check that all modules that were uninstalled are now reinstalled.
136     $this->assertModules(array_keys($modules_to_uninstall), TRUE);
137     foreach ($modules_to_uninstall as $module => $info) {
138       $this->assertModuleConfig($module);
139       $this->assertModuleTablesExist($module);
140     }
141
142     // Ensure that we have no configuration changes to import.
143     $storage_comparer = new StorageComparer(
144       $this->container->get('config.storage.sync'),
145       $this->container->get('config.storage'),
146       $this->container->get('config.manager')
147     );
148     $this->assertIdentical($storage_comparer->createChangelist()->getChangelist(), $storage_comparer->getEmptyChangelist());
149
150     // Now we have all configuration imported, test all of them for schema
151     // conformance. Ensures all imported default configuration is valid when
152     // all modules are enabled.
153     $names = $this->container->get('config.storage')->listAll();
154     /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
155     $typed_config = $this->container->get('config.typed');
156     foreach ($names as $name) {
157       $config = $this->config($name);
158       $this->assertConfigSchema($typed_config, $name, $config->get());
159     }
160   }
161
162 }