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

Issue #3412567 by eiriksm: Add method for getting the team notification emails

parent d17d5b53
No related branches found
No related tags found
1 merge request!22Resolve #3412567 "Add method for"
Pipeline #72324 passed
......@@ -16,6 +16,27 @@ class TeamNode extends Node {
const PLAN_FIELD = 'field_plan';
const PROJECT_LIMIT_FIELD = 'field_project_limit';
/**
* Get the emails from the field.
*/
public function getNotificationEmails() {
$mails = [];
if (!$this->hasField('field_team_notification_emails') || $this->get('field_team_notification_emails')->isEmpty()) {
return $mails;
}
$mails_string = $this->get('field_team_notification_emails')->first()->getString();
return self::createMailArrayFromString($mails_string);
}
/**
* Helper to make sure we only get somehow valid strings at least.
*/
protected static function createMailArrayFromString(string $mails_string) : array {
$mails = explode("\n", $mails_string);
$mails = array_map('trim', $mails);
return array_filter($mails);
}
/**
* Get the max private repos a team can have, based on the field value.
*/
......
......@@ -87,6 +87,22 @@ abstract class KernelTestBase extends CoreKernelTestBase {
],
]);
$field->save();
// The field we use for notifications.
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_team_notification_emails',
'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();
$this->installSchema('user', ['users_data']);
}
......
......@@ -59,4 +59,38 @@ class TeamNodeTest extends KernelTestBase {
self::assertEquals([$user->id()], $node->getAdministratorIds());
}
/**
* Test the notification variations.
*
* @dataProvider notificationProvider
*/
public function testGetNotifications(string $field_value, array $expexted) {
/** @var \Drupal\violinist_teams\TeamNode $node */
$node = Node::create([
'type' => $this->nodeType->id(),
]);
$node->set('field_team_notification_emails', $field_value);
self::assertEquals($expexted, $node->getNotificationEmails());
}
/**
* Data provider for mail notification thing.
*/
public function notificationProvider() {
return [
[
'test@test.com',
['test@test.com'],
],
[
' test@test.com ',
['test@test.com'],
],
[
"test@test.com\ntesttest@test.com ",
['test@test.com', 'testtest@test.com'],
],
];
}
}
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