Skip to content
Snippets Groups Projects
Commit b725d20e authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3414657 by eiriksm: Add method for getting environment variables

parent a48dd6cd
1 merge request!26Add method and tests
Pipeline #77294 passed
......@@ -19,6 +19,50 @@ class TeamNode extends Node {
const BILLING_ENABLED_FIELD = 'field_send_receipts';
const BILLING_EMAIL_FIELD = 'field_billing_email';
/**
* Get the env variables.
*
* Returns an array of env variables, with the key being the name of the
* variable, and the value being the value. This, despite the fact that it is
* stored as a single textfield, which in theory would make the user able to
* override their own variables within the same form. This would then be
* considered a feature.
*/
public function getEnvironmentVariables() : array {
$variables = [];
if (!$this->hasField('field_environment_variables') || $this->get('field_environment_variables')->isEmpty()) {
return $variables;
}
$variables_string = $this->get('field_environment_variables')->first()->getString();
$variables_string = trim($variables_string);
if (!$variables_string) {
return $variables;
}
$variables_array = preg_split('/\r\n|\r|\n/', $variables_string);
foreach ($variables_array as $variable) {
$variable = trim($variable);
if (!$variable) {
continue;
}
$variable_parts = explode('=', $variable);
if (count($variable_parts) < 2) {
// Not really looking like a good env var.
continue;
}
$key = trim($variable_parts[0]);
$value = trim($variable_parts[1]);
if (count($variable_parts) !== 2) {
// Join all the others so that the value can in fact contain an =.
array_shift($variable_parts);
$variable_parts = array_map('trim', $variable_parts);
$value = implode('=', $variable_parts);
}
$variables[$key] = $value;
}
$variables = array_filter($variables);
return $variables;
}
/**
* Currently this is only one email, but we return an array for consistency.
*
......
......@@ -104,6 +104,22 @@ abstract class KernelTestBase extends CoreKernelTestBase {
]);
$field->save();
// The field we use for env vars.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_environment_variables',
'entity_type' => 'node',
'type' => 'string_long',
'cardinality' => 1,
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $this->nodeType->id(),
'field_type' => 'string_long',
'required' => FALSE,
]);
$field->save();
// Slack notifications.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_slack_notifications',
......
......@@ -102,6 +102,68 @@ class TeamNodeTest extends KernelTestBase {
self::assertEquals($expected_result, $node->getBillingEmails());
}
/**
* Test the environment variables.
*
* @dataProvider getEnvironmentVariables
*/
public function testGetEnvironmentVariables($variable_value, $expected_result) {
/** @var \Drupal\violinist_teams\TeamNode $node */
$node = Node::create([
'type' => $this->nodeType->id(),
]);
$node->set('field_environment_variables', $variable_value);
self::assertEquals($expected_result, $node->getEnvironmentVariables());
}
/**
* Dataprovider for environment variables.
*/
public function getEnvironmentVariables() : array {
return [
[
'FOO=bar',
['FOO' => 'bar'],
],
[
'FOO=bar
FOO=baz',
['FOO' => 'baz'],
],
[
'FOO=bar
FOO=baz
FOO=',
[],
],
[
'FOO=
',
[],
],
[
NULL,
[],
],
[
'',
[],
],
[
'random_string = no value',
['random_string' => 'no value'],
],
[
'something stupid',
[],
],
[
'TEST=maybe_some_key_containing_the_equals_sign6622¤¤==!',
['TEST' => 'maybe_some_key_containing_the_equals_sign6622¤¤==!'],
],
];
}
/**
* Data provider for billing and billing email test.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment