Skip to content

Commit

Permalink
Merge pull request #2976 from mjansenDatabay/feature/7/mail/consumer-…
Browse files Browse the repository at this point in the history
…subject-prefixes

ILIAS 7: Group and Course Mail Prefix (Mail)
  • Loading branch information
mjansenDatabay authored Nov 6, 2020
2 parents cf8d4e7 + 52e21a0 commit ba1bd80
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);
/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */

/**
* Class ilMailMimeSubjectBuilder
* @author Michael Jansen <[email protected]>
*/
class ilMailMimeSubjectBuilder
{
/** @var ilSetting */
private $settings;
/** @var string */
private $defaultPrefix;

/**
* ilMailMimeSubjectBuilder constructor.
* @param ilSetting $settings
* @param string $defaultPrefix
*/
public function __construct(ilSetting $settings, string $defaultPrefix)
{
$this->settings = $settings;
$this->defaultPrefix = $defaultPrefix;
}

/**
* @param string $subject
* @param bool $addPrefix
* @param string $contextPrefix
* @return string
*/
public function subject(string $subject, bool $addPrefix = false, string $contextPrefix = '') : string
{
$subject = trim($subject);
$contextPrefix = trim($contextPrefix);

if ($addPrefix) {
// #9096
$globalPrefix = $this->settings->get('mail_subject_prefix', false);
if (!is_string($globalPrefix)) {
$globalPrefix = $this->defaultPrefix;
}
$globalPrefix = trim($globalPrefix);

$prefix = $globalPrefix;
if ($contextPrefix !== '') {
$prefix = str_replace(['[', ']',], '', $prefix);
if ($prefix !== '') {
$prefix = '[' . $prefix . ' : ' . $contextPrefix . ']';
} else {
$prefix = '[' . $contextPrefix . ']';
}
}

if (strlen($prefix) > 0) {
$subject = $prefix . ' ' . $subject;
}
}

return $subject;
}
}
5 changes: 3 additions & 2 deletions Services/Mail/classes/class.ilMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*/
class ilMail
{
const ILIAS_HOST = 'ilias';
public const ILIAS_HOST = 'ilias';
public const PROP_CONTEXT_SUBJECT_PREFIX = 'subject_prefix';

/** @var ilLanguage */
protected $lng;
Expand Down Expand Up @@ -1442,7 +1443,7 @@ private function sendMimeMail(string $to, string $cc, string $bcc, $subject, $me
$mailer = new ilMimeMail();
$mailer->From($this->senderFactory->getSenderByUsrId((int) $this->user_id));
$mailer->To($to);
$mailer->Subject($subject, true);
$mailer->Subject($subject, true, (string) ($this->contextParameters[self::PROP_CONTEXT_SUBJECT_PREFIX] ?? ''));
$mailer->Body($message);

if ($cc) {
Expand Down
25 changes: 10 additions & 15 deletions Services/Mail/classes/class.ilMimeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class ilMimeMail
/** @var \ilSetting */
protected $settings;

/** @var ilMailMimeSubjectBuilder */
protected $subjectBuilder;

/**
* ilMimeMail constructor.
*/
Expand All @@ -84,6 +87,8 @@ public function __construct()
$factory = $DIC["mail.mime.transport.factory"];
self::setDefaultTransport($factory->getTransport());
}

$this->subjectBuilder = new ilMailMimeSubjectBuilder($this->settings, self::MAIL_SUBJECT_PREFIX);
}

/**
Expand Down Expand Up @@ -111,23 +116,13 @@ public static function getDefaultTransport()
}

/**
* @param string $subject Define the subject line of the email
* @param bool $a_add_prefix
* @param string $subject
* @param bool $addPrefix
* @param string $contextPrefix
*/
public function Subject($subject, $a_add_prefix = false)
public function Subject(string $subject, bool $addPrefix = false, string $contextPrefix = '') : void
{
if ($a_add_prefix) {
// #9096
$subjectPrefix = $this->settings->get('mail_subject_prefix');
if (false === $subjectPrefix) {
$subjectPrefix = self::MAIL_SUBJECT_PREFIX;
}
if (strlen($subjectPrefix) > 0) {
$subject = $subjectPrefix . ' ' . $subject;
}
}

$this->subject = $subject;
$this->subject = $this->subjectBuilder->subject($subject, $addPrefix, $contextPrefix);
}

/**
Expand Down
104 changes: 104 additions & 0 deletions Services/Mail/test/ilMailMimeSubjectBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php declare(strict_types=1);
/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */

/**
* @author Michael Jansen <[email protected]>
*/
class ilMailMimeSubjectBuilderTest extends ilMailBaseTest
{
private const DEFAULT_PREFIX = 'docu default';

/**
* @return array<string, array<int, string>>
*/
public function globalSubjectPrefixOnlyProvider() : array
{
return [
'Global Prefix without Brackets' => ['docu', 'docu %s'],
'Global Prefix with Brackets' => ['[docu]', '[docu] %s'],
];
}

/**
* @return array<string, array<int, string|false>>
*/
public function subjectPrefixesProvider() : array
{
return [
'Global Prefix without Brackets and Additional Context Prefix' => ['docu', 'Course', '[docu : Course] %s'],
'Global Prefix with Brackets and Additional Context Prefix' => ['[docu]', 'Course', '[docu : Course] %s'],
'Empty Global Prefix with Brackets and Additional Context Prefix' => [
'', // The administrator saved the global email settings form with an empty global subject prefix
'Course',
'[Course] %s'
],
'Absent Global Prefix with Brackets and Additional Context Prefix' => [
false, // The administrator did not save the global email settings form, yet
'Course',
'[' . self::DEFAULT_PREFIX . ' : Course] %s'
],
];
}

public function testSubjectMustNotBeChangedWhenNotPrefixShouldBeAdded() : void
{
$settings = $this->getMockBuilder(ilSetting::class)->onlyMethods(['get'])->disableOriginalConstructor()->getMock();
$subjectBuilder = new ilMailMimeSubjectBuilder($settings, self::DEFAULT_PREFIX);

$subject = 'phpunit';
$this->assertEquals($subject, $subjectBuilder->subject($subject, false));
$this->assertEquals($subject, $subjectBuilder->subject($subject, false, 'Course'));
}

/**
* @dataProvider globalSubjectPrefixOnlyProvider
* @param string $globalPrefix
* @param string $expectedSubject
*/
public function testGlobalPrefixMustBePrependedWhenDefinedAndPrefixShouldBeAppended(
string $globalPrefix,
string $expectedSubject
) : void {
$settings = $this->getMockBuilder(ilSetting::class)->onlyMethods(['get'])->disableOriginalConstructor()->getMock();
$settings->expects($this->once())->method('get')->with('mail_subject_prefix')->willReturn($globalPrefix);

$subjectBuilder = new ilMailMimeSubjectBuilder($settings, self::DEFAULT_PREFIX);

$subject = 'phpunit';
$expectedSubject = sprintf($expectedSubject, $subject);
$this->assertEquals($expectedSubject, $subjectBuilder->subject($subject, true));
}

public function testDefaultPrefixMustBePrependedWhenNoGlobalPrefixIsDefinedAndPrefixShouldBeAppended() : void
{
$settings = $this->getMockBuilder(ilSetting::class)->onlyMethods(['get'])->disableOriginalConstructor()->getMock();
$settings->expects($this->once())->method('get')->with('mail_subject_prefix')->willReturn(false);

$subjectBuilder = new ilMailMimeSubjectBuilder($settings, self::DEFAULT_PREFIX);

$subject = 'phpunit';
$expectedSubject = self::DEFAULT_PREFIX . ' ' . $subject;
$this->assertEquals($expectedSubject, $subjectBuilder->subject($subject, true));
}

/**
* @dataProvider subjectPrefixesProvider
* @param string|false $globalPrefix
* @param string $contextPrefix
* @param string $expectedSubject
*/
public function testContextPrefixMustBePrependedWhenGivenAndPrefixShouldBeAppended(
$globalPrefix,
string $contextPrefix,
string $expectedSubject
) : void {
$settings = $this->getMockBuilder(ilSetting::class)->onlyMethods(['get'])->disableOriginalConstructor()->getMock();
$settings->expects($this->once())->method('get')->with('mail_subject_prefix')->willReturn($globalPrefix);

$subjectBuilder = new ilMailMimeSubjectBuilder($settings, self::DEFAULT_PREFIX);

$subject = 'phpunit';
$expectedSubject = sprintf($expectedSubject, $subject);
$this->assertEquals($expectedSubject, $subjectBuilder->subject($subject, true, $contextPrefix));
}
}

0 comments on commit ba1bd80

Please sign in to comment.