-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSettingsTestCase.php
More file actions
368 lines (320 loc) Β· 10.1 KB
/
Copy pathSettingsTestCase.php
File metadata and controls
368 lines (320 loc) Β· 10.1 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
declare(strict_types=1);
namespace Drupal;
use PHPUnit\Framework\TestCase;
/**
* Class SettingsTestCase.
*
* Base class for testing Drupal settings.
*
* phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName
*/
abstract class SettingsTestCase extends TestCase {
/**
* Defines a constant for the name of the 'testing' environment.
*
* This is used to differentiate between the environment names set in
* settings.php and the environment used to test configs and settings
* in environment-less way.
*/
final const ENVIRONMENT_SUT = 'env-testing';
/**
* Defines a constant for the name of the 'local' environment.
*/
final const ENVIRONMENT_LOCAL = 'local';
/**
* Defines a constant for the name of the 'ci' environment.
*/
final const ENVIRONMENT_CI = 'ci';
/**
* Defines a constant for the name of the 'stage' environment.
*/
final const ENVIRONMENT_STAGE = 'stage';
/**
* Defines a constant for the name of the 'dev' environment.
*/
final const ENVIRONMENT_DEV = 'dev';
/**
* Defines a constant for the name of the 'prod' environment.
*/
final const ENVIRONMENT_PROD = 'prod';
/**
* Defines a constant for the allowed environment variables.
*
* These variables are used to filter the environment variables that are set
* during the test setup. This is to ensure that only relevant variables are
* set and to avoid conflicts with other environment variables.
*
* Consumer sites should update this list if they need to add additional
* environment variables that are not part of the default set.
*/
const ALLOWED_ENV_VARS = [
// Service variables.
'DATABASE_',
// phpcs:ignore #;< MIGRATION
'DATABASE2_',
// phpcs:ignore #;> MIGRATION
'REDIS_',
'COMPOSE_',
'GITHUB_',
'PACKAGE_',
'DOCKER_',
// Vortex and Drupal variables.
'VORTEX_',
'DRUPAL_',
];
/**
* Application root.
*
* @var string
*/
protected $app_root;
/**
* Site path.
*
* @var string
*/
protected $site_path;
/**
* Array of configuration.
*
* @var array
*/
protected $config;
/**
* Array of settings.
*
* @var array
*/
protected $settings;
/**
* Array of databases.
*
* @var array
*/
protected $databases;
/**
* Array of environment variables.
*
* @var array
*/
protected $envVars = [];
/**
* {@inheritdoc}
*/
protected function tearDown(): void {
$this->unsetEnvVars();
parent::tearDown();
}
/**
* Set environment variables.
*
* @param array $vars
* Array of environment variables.
*
* @SuppressWarnings("PHPMD.ElseExpression")
*/
protected function setEnvVars(array $vars): void {
// Unset the existing environment variable if not set in the test.
if (!isset($vars['TMP'])) {
$vars['TMP'] = NULL;
}
// Unset the existing environment variable if not set in the test.
if (!isset($vars['DRUPAL_CONFIG_PATH'])) {
$vars['DRUPAL_CONFIG_PATH'] = NULL;
}
// Do not enforce the CI environment unless it is explicitly set.
if (!isset($vars['CI'])) {
$vars['CI'] = FALSE;
}
// Filtered real vars without a value to unset them in the lines below.
$vars_real = self::getRealEnvVarsFilteredNoValues(static::ALLOWED_ENV_VARS);
// Passed vars + existing vars + filtered real vars.
$this->envVars = $vars + $this->envVars + $vars_real;
foreach ($this->envVars as $name => $value) {
// Unset the variable if it has a value of NULL.
if (is_null($value)) {
putenv($name);
}
else {
putenv(sprintf('%s=%s', $name, $value));
}
}
}
/**
* Get real environment variables with no values.
*
* @param array $prefixes
* Array of prefixes to filter the variables by.
*
* @return array
* Array of environment variables.
*/
protected static function getRealEnvVarsFilteredNoValues(array $prefixes = []): array {
$vars = getenv();
$vars = array_filter(array_keys($vars), static fn(string $key): bool => array_any($prefixes, fn($prefix): bool => str_starts_with($key, (string) $prefix)));
return array_fill_keys($vars, NULL);
}
/**
* Set environment variables.
*/
protected function unsetEnvVars(): void {
foreach (array_keys($this->envVars) as $name) {
putenv($name);
}
}
/**
* Require settings file.
*/
protected function requireSettingsFile(array $pre_settings = [], array $pre_config = []): void {
$app_root = getcwd() . '/web';
if (!file_exists($app_root)) {
throw new \RuntimeException('Could not determine application root.');
}
$site_path = 'sites/default';
$config = $pre_config;
$settings = $pre_settings;
$databases = [];
putenv('DRUPAL_SETTINGS_LOCAL_SKIP=1');
require $app_root . DIRECTORY_SEPARATOR . $site_path . DIRECTORY_SEPARATOR . 'settings.php';
putenv('DRUPAL_SETTINGS_LOCAL_SKIP');
$this->app_root = $app_root;
$this->site_path = $site_path;
$this->config = $config;
$this->settings = $settings;
$this->databases = $databases;
}
/**
* Assert that config retrieved from the real settings file match test data.
*
* @param array $expected
* Array of expected configs.
* @param array $expected_keys_only
* Array of expected configs that will be asserted by keys and data type
* only. This is used for cases when the data should exist but the value
* is static. Supports only top-level keys.
*/
protected function assertConfig(array $expected, array $expected_keys_only = []): void {
$actual_keys_only = array_intersect_key($this->config, $expected_keys_only);
$actual = array_diff_key($this->config, $expected_keys_only);
$this->assertEquals($expected, $actual, 'Configs');
$this->assertArrayContainsKeysTypes($expected_keys_only, $actual_keys_only, 'Config');
}
/**
* Assert that config contains partial data.
*
* @param array $expected
* Array of expected configs.
*/
protected function assertConfigContains(array $expected): void {
$this->assertArraySubset($expected, $this->config, 'Config array contains');
}
/**
* Assert that config does not contain partial data.
*
* @param array $expected
* Array of expected configs.
*/
protected function assertConfigNotContains(array $expected): void {
$this->assertArrayNotSubset($expected, $this->config, 'Config array does not contain');
}
/**
* Assert that settings retrieved from the real settings file match test data.
*
* @param array $expected
* Array of expected setting.
* @param array $expected_keys_only
* Array of expected setting that will be asserted by keys and data type
* only. This is used for cases when the data should exist but the value
* is static. Supports only top-level keys.
*/
protected function assertSettings(array $expected, array $expected_keys_only = []): void {
$actual_keys_only = array_intersect_key($this->settings, $expected_keys_only);
$actual = array_diff_key($this->settings, $expected_keys_only);
$this->assertEquals($expected, $actual, 'Settings');
$this->assertArrayContainsKeysTypes($expected_keys_only, $actual_keys_only, 'Settings');
}
/**
* Assert that settings contain partial data.
*
* @param array $expected
* Array of expected settings.
*/
protected function assertSettingsContains(array $expected): void {
$this->assertArraySubset($expected, $this->settings, 'Settings array contains');
}
/**
* Assert that settings do not contain partial data.
*
* @param array $expected
* Array of expected settings.
*/
protected function assertSettingsNotContains(array $expected): void {
$this->assertArrayNotSubset($expected, $this->settings, 'Settings array does not contain');
}
/**
* Assert that an array contains a subset.
*
* @param array $subset
* Array of subset to search for.
* @param array $haystack
* Array to search in.
* @param string $message
* Message to display on failure.
*
* @SuppressWarnings("PHPMD.ElseExpression")
*/
protected function assertArraySubset(array $subset, array $haystack, string $message = ''): void {
foreach ($subset as $key => $value) {
$this->assertTrue(array_key_exists($key, $haystack), $message . sprintf(': Key %s does not exist.', $key));
if (is_array($value)) {
$this->assertArraySubset($value, $haystack[$key], $message);
}
else {
$this->assertEquals($value, $haystack[$key], $message);
}
}
}
/**
* Assert that an array does not contain a subset.
*
* This is not a mirror of the assertArraySubset: it does not check the value
* of the key, only the key itself.
*
* @param array $subset
* Array of subset to search for.
* @param array $haystack
* Array to search in.
* @param string $message
* Message to display on failure.
*/
protected function assertArrayNotSubset(array $subset, array $haystack, string $message = ''): void {
foreach ($subset as $key => $value) {
if (is_array($value)) {
$this->assertArrayNotSubset($value, $haystack[$key] ?? [], $message);
continue;
}
$this->assertFalse(array_key_exists($key, $haystack), $message . sprintf(': Key %s exists at the deepest level.', $key));
}
}
/**
* Assert that an array contains a subset by keys and value data types.
*
* Used for cases when the data in array path should exist but the value
* itself does not matter.
*
* @param array $subset
* Array to search for.
* @param array $haystack
* Array to search in.
* @param string $message
* Message to display on failure.
*/
protected function assertArrayContainsKeysTypes(array $subset, array $haystack, string $message = ''): void {
$message = empty($message) ? $message : $message . ': ';
foreach ($subset as $key => $value) {
$this->assertArrayHasKey($key, $haystack, $message . 'Keys of key-only values match');
$this->assertEquals(gettype($value), gettype($haystack[$key]), $message . 'Types of key-only values match');
}
}
}