-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ConfigTest.php
186 lines (158 loc) · 8.23 KB
/
ConfigTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Unish;
use Composer\Semver\Comparator;
use Drupal\Core\Serialization\Yaml;
use Webmozart\PathUtil\Path;
/**
* Tests for Configuration Management commands for D8+.
* @group commands
* @group config
*/
class ConfigTest extends CommandUnishTestCase
{
use TestModuleHelperTrait;
public function setup(): void
{
if (!$this->getSites()) {
$this->setUpDrupal(1, true);
// Field module is needed for now for --existing-config. It is not actually
// enabled after testing profile is installed. Its required by file and update though.
$this->drush('pm:enable', ['config, field']);
}
}
/**
* @todo If this becomes an integration test, add test for stdin handling.
*/
public function testConfigGetSet()
{
// Simple value
$this->drush('config:set', ['system.site', 'name', 'config_test']);
$this->drush('config:get', ['system.site', 'name']);
$this->assertEquals("'system.site:name': config_test", $this->getOutput());
// Nested value
$this->drush('config:set', ['system.site', 'page.front', 'llama']);
$this->drush('config:get', ['system.site', 'page.front']);
$this->assertEquals("'system.site:page.front': llama", $this->getOutput());
// Simple sequence value
$this->drush('config:set', ['user.role.authenticated', 'permissions', '[foo,bar]'], ['input-format' => 'yaml']);
$this->drush('config:get', ['user.role.authenticated', 'permissions'], ['format' => 'json']);
$output = $this->getOutputFromJSON('user.role.authenticated:permissions');
// Mapping value
$this->drush('config:set', ['system.site', 'page', "{403: '403', front: home}"], ['input-format' => 'yaml']);
$this->drush('config:get', ['system.site', 'page'], ['format' => 'json']);
$output = $this->getOutputFromJSON('system.site:page');
$this->assertSame(['403' => '403', 'front' => 'home'], $output);
// Multiple top-level keys
$this->drush('config:set', ['user.role.authenticated', '?', "{label: 'Auth user', weight: 5}"], ['input-format' => 'yaml']);
$this->drush('config:get', ['user.role.authenticated'], ['format' => 'json']);
$output = $this->getOutputFromJSON();
$this->assertSame('Auth user', $output['label']);
$this->assertSame(5, $output['weight']);
}
public function testConfigExportImportStatusExistingConfig()
{
$system_site_yml = $this->getConfigSyncDir() . '/system.site.yml';
// Test export.
$this->drush('config-export');
$this->assertFileExists($system_site_yml);
// Test import and status by finishing the round trip.
$contents = file_get_contents($system_site_yml);
$contents = preg_replace('/front: .*/', 'front: unish', $contents);
file_put_contents($system_site_yml, $contents);
// Test status of changed configuration.
$this->drush('config:status');
$this->assertStringContainsString('system.site', $this->getOutput(), 'config:status correctly reports changes.');
// Test import.
$this->drush('config-import');
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
$page = $this->getOutputFromJSON('system.site:page');
$this->assertStringContainsString('unish', $page['front'], 'Config was successfully imported.');
// Test status of identical configuration, in different formatters.
$expected_output = [
'list' => '',
'table' => '',
'json' => '[]',
'xml' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document/>
XML
];
foreach ($expected_output as $formatter => $output) {
$this->drush('config:status', [], ['format' => $formatter]);
$this->assertEquals($output, $this->getOutput(), 'config:status correctly reports identical config.');
}
// Test the --existing-config option for site:install.
$this->drush('core:status', [], ['field' => 'drupal-version']);
$drupal_version = $this->getOutputRaw();
$contents = file_get_contents($system_site_yml);
$contents = preg_replace('/front: .*/', 'front: unish existing', $contents);
file_put_contents($system_site_yml, $contents);
$this->installDrupal('dev', true, ['existing-config' => true], false);
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
$page = $this->getOutputFromJSON('system.site:page');
$this->assertStringContainsString('unish existing', $page['front'], 'Existing config was successfully imported during site:install.');
// Similar, but this time via --partial option.
$contents = file_get_contents($system_site_yml);
$contents = preg_replace('/front: .*/', 'front: unish partial', $contents);
$partial_path = self::getSandbox() . '/partial';
$this->mkdir($partial_path);
$contents = file_put_contents($partial_path . '/system.site.yml', $contents);
$this->drush('config-import', [], ['partial' => null, 'source' => $partial_path]);
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
$page = $this->getOutputFromJSON('system.site:page');
$this->assertStringContainsString('unish partial', $page['front'], '--partial was successfully imported.');
}
public function testConfigImport()
{
$options = [
'include' => __DIR__,
];
$this->setupModulesForTests(['woot'], Path::join(__DIR__, '/../fixtures/modules'));
$this->drush('pm-enable', ['woot'], $options);
// Export the configuration.
$this->drush('config:export');
$root = $this->webroot();
// Introduce a new service in the Woot module that depends on a service
// in the Devel module (which is not yet enabled).
$filename = Path::join($root, 'modules/unish/woot/woot.services.yml');
$serviceDefinition = <<<YAML_FRAGMENT
woot.depending_service:
class: Drupal\woot\DependingService
arguments: ['@drush_empty_module.service']
YAML_FRAGMENT;
file_put_contents($filename, $serviceDefinition, FILE_APPEND);
$filename = Path::join($root, 'modules/unish/woot/woot.info.yml');
$moduleDependency = <<<YAML_FRAGMENT
dependencies:
- drush_empty_module
YAML_FRAGMENT;
file_put_contents($filename, $moduleDependency, FILE_APPEND);
// Add the 'drush_empty_module' module in core.extension.yml.
$extensionFile = $this->getConfigSyncDir() . '/core.extension.yml';
$this->assertFileExists($extensionFile);
$extension = Yaml::decode(file_get_contents($extensionFile));
$extension['module']['drush_empty_module'] = 0;
require_once $root . "/core/includes/module.inc";
$extension['module'] = module_config_sort($extension['module']);
file_put_contents($extensionFile, Yaml::encode($extension));
// When importing config, the 'woot' module should warn about a validation error.
$this->drush('config:import', [], [], null, null, CommandUnishTestCase::EXIT_ERROR);
$this->assertStringContainsString("woot config error", $this->getErrorOutput(), 'Woot returned an expected config validation error.');
// Now we disable the error, and retry the config import.
$this->drush('state:set', ['woot.shoud_not_fail_on_cim', 'true']);
$this->drush('config:import');
$this->drush('php:eval', ["return Drupal::getContainer()->getParameter('container.modules')"], ['format' => 'json']);
// Assure that new modules are fully enabled.
$out = $this->getOutputFromJSON();
$this->assertArrayHasKey('woot', $out);
$this->assertArrayHasKey('drush_empty_module', $out);
// We make sure that the service inside the newly enabled module exists now. A fatal
// error will be thrown by Drupal if the service does not exist.
$this->drush('php:eval', ['Drupal::service("drush_empty_module.service");']);
}
protected function getConfigSyncDir()
{
$this->drush('core:status', [], ['format' => 'json', 'fields' => 'config-sync']);
return $this->webroot() . '/' . $this->getOutputFromJSON('config-sync');
}
}