email support
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Admin\EmailTemplateController;
|
||||
use App\Models\Document;
|
||||
use App\Models\EmailTemplate;
|
||||
|
||||
it('fills img src from template documents by matching alt', function () {
|
||||
$tpl = new EmailTemplate;
|
||||
// fake relation collection with a single image document
|
||||
$doc = new Document([
|
||||
'path' => 'email-images/logo-uuid.png',
|
||||
'file_name' => 'logo-uuid.png',
|
||||
'mime_type' => 'image/png',
|
||||
'name' => 'logo',
|
||||
]);
|
||||
$tpl->setRelation('documents', collect([$doc]));
|
||||
|
||||
$controller = new EmailTemplateController;
|
||||
$ref = new ReflectionClass($controller);
|
||||
$method = $ref->getMethod('attachSrcFromTemplateDocuments');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$input = '<div><img alt="Logo" width="300"></div>';
|
||||
$output = $method->invoke($controller, $tpl, $input);
|
||||
|
||||
expect($output)->toContain('src="/storage/email-images/logo-uuid.png"');
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use App\Services\EmailImageInliner;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
it('inlines public storage images to base64 data uris', function () {
|
||||
$dir = storage_path('app/public/email-images');
|
||||
if (! is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
$path = $dir.'/test-inline.png';
|
||||
// a tiny 1x1 PNG
|
||||
$png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO8m4y8AAAAASUVORK5CYII=');
|
||||
File::put($path, $png);
|
||||
|
||||
$html = '<p><img src="/storage/email-images/test-inline.png" alt="x"></p>';
|
||||
$service = new EmailImageInliner;
|
||||
$out = $service->inline($html);
|
||||
|
||||
expect($out)->toContain('src="data:image/png;base64,');
|
||||
});
|
||||
|
||||
it('inlines absolute URLs that point to /storage paths', function () {
|
||||
$dir = storage_path('app/public/email-images');
|
||||
if (! is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
$path = $dir.'/test-abs.png';
|
||||
// a tiny 1x1 PNG
|
||||
$png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO8m4y8AAAAASUVORK5CYII=');
|
||||
File::put($path, $png);
|
||||
|
||||
$html = '<p><img src="http://localhost/storage/email-images/test-abs.png" alt="x"></p>';
|
||||
$service = new EmailImageInliner;
|
||||
$out = $service->inline($html);
|
||||
|
||||
expect($out)->toContain('src="data:image/png;base64,');
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Services\EmailTemplateRenderer;
|
||||
|
||||
it('renders placeholders in subject, html and text', function () {
|
||||
$renderer = new EmailTemplateRenderer;
|
||||
|
||||
$template = [
|
||||
'subject' => 'Hello {{ person.full_name }} - {{ contract.reference }}',
|
||||
'html' => '<p>Case: {{ case.uuid }}</p><p>Meta: {{ contract.meta.foo }}</p>',
|
||||
'text' => 'Client: {{ client.uuid }} Extra: {{ extra.note }}',
|
||||
];
|
||||
|
||||
$ctx = [
|
||||
'person' => (object) ['first_name' => 'Jane', 'last_name' => 'Doe', 'email' => 'jane@example.test'],
|
||||
'client' => (object) ['uuid' => 'cl-123'],
|
||||
'client_case' => (object) ['uuid' => 'cc-456', 'reference' => 'REF-1'],
|
||||
'contract' => (object) ['uuid' => 'co-789', 'reference' => 'CON-42', 'meta' => ['foo' => 'bar']],
|
||||
'extra' => ['note' => 'hello'],
|
||||
];
|
||||
|
||||
$result = $renderer->render($template, $ctx);
|
||||
|
||||
expect($result['subject'])->toBe('Hello Jane Doe - CON-42');
|
||||
expect($result['html'])->toContain('Case: cc-456');
|
||||
expect($result['html'])->toContain('Meta: bar');
|
||||
expect($result['text'])->toBe('Client: cl-123 Extra: hello');
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Admin\EmailTemplateController;
|
||||
|
||||
it('adds src to img when a /storage URL appears shortly after the tag', function () {
|
||||
$controller = new EmailTemplateController;
|
||||
|
||||
$input = <<<HTML
|
||||
<div>
|
||||
<img alt="Logo" width="300" style="display:block;margin:0 auto 16px">
|
||||
\n\n
|
||||
https://localhost/storage/email-images/logo.png
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
$ref = new ReflectionClass($controller);
|
||||
$method = $ref->getMethod('repairImgWithoutSrc');
|
||||
$method->setAccessible(true);
|
||||
|
||||
/** @var string $output */
|
||||
$output = $method->invoke($controller, $input);
|
||||
|
||||
expect($output)->toContain('src="https://localhost/storage/email-images/logo.png"');
|
||||
});
|
||||
Reference in New Issue
Block a user