-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for friendly admin emails (#336)
- Loading branch information
Showing
5 changed files
with
88 additions
and
43 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -7,7 +7,9 @@ | |
use Contao\Config; | ||
use Contao\CoreBundle\Framework\ContaoFramework; | ||
use Contao\PageModel; | ||
use Contao\StringUtil; | ||
use Contao\TestCase\ContaoTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Terminal42\NotificationCenterBundle\Config\MessageConfig; | ||
|
@@ -20,10 +22,11 @@ | |
|
||
class AdminEmailTokenSubscriberTest extends ContaoTestCase | ||
{ | ||
public function testAddsTokenFromPageModel(): void | ||
#[DataProvider('adminEmailProvider')] | ||
public function testAddsAdminTokens(string $configFriendlyEmail, string $pageFriendlyEmail, string $expectedName, string $expectedEmail): void | ||
{ | ||
$pageModel = $this->mockClassWithProperties(PageModel::class, [ | ||
'adminEmail' => '[email protected]', | ||
'adminEmail' => $pageFriendlyEmail, | ||
]); | ||
|
||
$stack = $this->buildRequestStack($pageModel); | ||
|
@@ -36,34 +39,43 @@ public function testAddsTokenFromPageModel(): void | |
$listener = new AdminEmailTokenListener( | ||
$stack, | ||
$tokenDefinitionFactory, | ||
$this->mockFrameworkWithAdminEmail('[email protected]'), | ||
$this->mockFrameworkWithAdminEmail($configFriendlyEmail), | ||
); | ||
$listener->onCreateParcel($event); | ||
|
||
$this->assertSame('[email protected]', $tokenCollection->getByName('admin_email')->getValue()); | ||
$this->assertSame($expectedName, $tokenCollection->getByName('admin_name')->getValue()); | ||
$this->assertSame($expectedEmail, $tokenCollection->getByName('admin_email')->getValue()); | ||
} | ||
|
||
public function testAddsTokenFromConfig(): void | ||
public static function adminEmailProvider(): \Generator | ||
{ | ||
$pageModel = $this->mockClassWithProperties(PageModel::class, [ | ||
'adminEmail' => '', | ||
]); | ||
|
||
$stack = $this->buildRequestStack($pageModel); | ||
$tokenCollection = new TokenCollection(); | ||
|
||
$event = $this->buildCreateParcelEvent($tokenCollection); | ||
|
||
$tokenDefinitionFactory = new CoreTokenDefinitionFactory(); | ||
|
||
$listener = new AdminEmailTokenListener( | ||
$stack, | ||
$tokenDefinitionFactory, | ||
$this->mockFrameworkWithAdminEmail('[email protected]'), | ||
); | ||
$listener->onCreateParcel($event); | ||
|
||
$this->assertSame('[email protected]', $tokenCollection->getByName('admin_email')->getValue()); | ||
yield 'Basic admin email in config' => [ | ||
'[email protected]', | ||
'', | ||
'', | ||
'[email protected]', | ||
]; | ||
|
||
yield 'Friendly admin email in config' => [ | ||
'Lorem Ipsum [[email protected]]', | ||
'', | ||
'Lorem Ipsum', | ||
'[email protected]', | ||
]; | ||
|
||
yield 'Basic admin email in page' => [ | ||
'Lorem Ipsum [[email protected]]', | ||
'[email protected]', | ||
'', | ||
'[email protected]', | ||
]; | ||
|
||
yield 'Friendly admin email in page' => [ | ||
'Lorem Ipsum [[email protected]]', | ||
'Dolor Sitamet [[email protected]]', | ||
'Dolor Sitamet', | ||
'[email protected]', | ||
]; | ||
} | ||
|
||
private function buildRequestStack(PageModel|null $pageModel = null): RequestStack | ||
|
@@ -86,20 +98,33 @@ private function buildCreateParcelEvent(TokenCollection $tokenCollection): Creat | |
|
||
private function mockFrameworkWithAdminEmail(string|null $adminEmail = null): ContaoFramework | ||
{ | ||
$adapter = $this->mockAdapter(['isComplete', 'get']); | ||
$adapter | ||
$configAdapter = $this->mockAdapter(['isComplete', 'get']); | ||
$configAdapter | ||
->method('isComplete') | ||
->willReturn(true) | ||
; | ||
|
||
$adapter | ||
$configAdapter | ||
->method('get') | ||
->with('adminEmail') | ||
->willReturn($adminEmail) | ||
; | ||
|
||
$stringUtilAdapter = $this->mockAdapter(['splitFriendlyEmail']); | ||
$stringUtilAdapter | ||
->method('splitFriendlyEmail') | ||
->willReturnCallback( | ||
static fn (string $email): array => match ($email) { | ||
'Lorem Ipsum [[email protected]]' => ['Lorem Ipsum', '[email protected]'], | ||
'Dolor Sitamet [[email protected]]' => ['Dolor Sitamet', '[email protected]'], | ||
default => ['', $email], | ||
}, | ||
) | ||
; | ||
|
||
return $this->mockContaoFramework([ | ||
Config::class => $adapter, | ||
Config::class => $configAdapter, | ||
StringUtil::class => $stringUtilAdapter, | ||
]); | ||
} | ||
} |