feat: admin editable email templates
addresses #869eg7ptr
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Config;
|
||||
|
||||
use App\Config\EmailTextCatalog;
|
||||
use App\Config\EmailTextKey;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* The delivered wording is what every mail falls back to, and it is also the text an admin
|
||||
* starts editing from. A default that references a placeholder nobody declared would print
|
||||
* a dash forever without anyone noticing, which is what these tests are here to prevent.
|
||||
*
|
||||
* Since the wording moved to config/email_texts.yaml, building every definition here is
|
||||
* also what proves that file is complete: a missing entry or a placeholder without a
|
||||
* description fails in build() before any assertion runs.
|
||||
*/
|
||||
class EmailTextCatalogTest extends KernelTestCase
|
||||
{
|
||||
public function testEveryKeyHasADefinition(): void
|
||||
{
|
||||
$catalog = $this->catalog();
|
||||
|
||||
foreach (EmailTextKey::cases() as $key) {
|
||||
$definition = $catalog->get($key);
|
||||
|
||||
$this->assertSame($key, $definition->key);
|
||||
$this->assertNotSame('', $definition->label);
|
||||
$this->assertNotSame('', $definition->defaultSubject);
|
||||
$this->assertNotSame('', $definition->defaultBody);
|
||||
}
|
||||
}
|
||||
|
||||
public function testDefaultsOnlyUseDeclaredPlaceholders(): void
|
||||
{
|
||||
$catalog = $this->catalog();
|
||||
|
||||
foreach (EmailTextKey::cases() as $key) {
|
||||
$definition = $catalog->get($key);
|
||||
$declared = $definition->getPlaceholderNames();
|
||||
|
||||
preg_match_all(
|
||||
'/\{(\w+)\}/',
|
||||
$definition->defaultSubject.' '.$definition->defaultHeadline.' '.$definition->defaultBody,
|
||||
$matches
|
||||
);
|
||||
$used = array_unique($matches[1]);
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
array_diff($used, $declared),
|
||||
sprintf('Default wording of "%s" uses undeclared placeholders', $key->value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A placeholder nobody prints is either a leftover or a hint that the wording lost a
|
||||
* detail it used to carry.
|
||||
*/
|
||||
public function testEveryDeclaredPlaceholderIsUsedByTheDefaults(): void
|
||||
{
|
||||
$catalog = $this->catalog();
|
||||
|
||||
foreach (EmailTextKey::cases() as $key) {
|
||||
$definition = $catalog->get($key);
|
||||
$text = $definition->defaultSubject.' '.$definition->defaultHeadline.' '.$definition->defaultBody;
|
||||
|
||||
foreach ($definition->getPlaceholderNames() as $name) {
|
||||
$this->assertStringContainsString(
|
||||
'{'.$name.'}',
|
||||
$text,
|
||||
sprintf('Placeholder "%s" of "%s" is declared but never used', $name, $key->value)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testEveryPlaceholderIsDescribedForTheAdmin(): void
|
||||
{
|
||||
$catalog = $this->catalog();
|
||||
|
||||
foreach (EmailTextKey::cases() as $key) {
|
||||
foreach ($catalog->get($key)->placeholders as $name => $description) {
|
||||
$this->assertNotSame('', trim($description), sprintf('Placeholder "%s" has no description', $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function catalog(): EmailTextCatalog
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
return self::getContainer()->get(EmailTextCatalog::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user