-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2976 from mjansenDatabay/feature/7/mail/consumer-…
…subject-prefixes ILIAS 7: Group and Course Mail Prefix (Mail)
- Loading branch information
Showing
4 changed files
with
179 additions
and
17 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
Services/Mail/classes/Mime/Subject/class.ilMailMimeSubjectBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |