Skip to content

Commit

Permalink
NEXT-32265 - Fix download link mail
Browse files Browse the repository at this point in the history
  • Loading branch information
lacknere authored and phuoccx committed Dec 21, 2023
1 parent ab1cecf commit 476e026
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 4 deletions.
10 changes: 10 additions & 0 deletions changelog/_unreleased/2023-12-04-fix-download-link-mail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Fix download link mail
issue: NEXT-00000
author: Elias Lackner
author_email: [email protected]
author_github: @lacknere
---
# Core
* Changed `downloads_delivery` mail fixtures to use `rawUrl` instead of `url` for product download link to use the correct sales channel.
* Added new migration `Migration1701688920FixDownloadLinkMail` to update `downloads_delivery` mail templates.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td>
{% for download in lineItem.downloads %}
{% if download.accessGranted %}
{% set downloadLink = url('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}) %}
{% set downloadLink = rawUrl('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}, salesChannel.domains|first.url) %}
<a href="{{ downloadLink }}" target="_blank">
{{ download.media.fileName }}.{{ download.media.fileExtension }}
</a><br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Mit dieser E-Mail erhalten Sie die Dateien zu der Bestellung: {{ order.orderNumb

-------------------------------------
{% for download in lineItem.downloads %}{% if download.accessGranted %}
{{ download.media.fileName }}.{{ download.media.fileExtension }} - {% set downloadLink = url('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}) %}{{ downloadLink }}
{{ download.media.fileName }}.{{ download.media.fileExtension }} - {% set downloadLink = rawUrl('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}, salesChannel.domains|first.url) %}{{ downloadLink }}
{% endif %}{% endfor %}

{% endif %}{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td>
{% for download in lineItem.downloads %}
{% if download.accessGranted %}
{% set downloadLink = url('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}) %}
{% set downloadLink = rawUrl('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}, salesChannel.domains|first.url) %}
<a href="{{ downloadLink }}" target="_blank">
{{ download.media.fileName }}.{{ download.media.fileExtension }}
</a><br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Attached to this email you will find the files to the order: {{ order.orderNumbe

-------------------------------------
{% for download in lineItem.downloads %}{% if download.accessGranted %}
{{ download.media.fileName }}.{{ download.media.fileExtension }} - {% set downloadLink = url('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}) %}{{ downloadLink }}
{{ download.media.fileName }}.{{ download.media.fileExtension }} - {% set downloadLink = rawUrl('frontend.account.order.single.download', {'orderId': order.id, 'downloadId': download.id, 'deepLinkCode': order.deepLinkCode}, salesChannel.domains|first.url) %}{{ downloadLink }}
{% endif %}{% endfor %}

{% endif %}{% endfor %}
42 changes: 42 additions & 0 deletions src/Core/Migration/V6_6/Migration1701688920FixDownloadLinkMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\V6_6;

use Doctrine\DBAL\Connection;
use Shopware\Core\Content\MailTemplate\MailTemplateTypes;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Migration\MigrationStep;
use Shopware\Core\Migration\Traits\MailUpdate;
use Shopware\Core\Migration\Traits\UpdateMailTrait;

/**
* @internal
*/
#[Package('core')]
class Migration1701688920FixDownloadLinkMail extends MigrationStep
{
use UpdateMailTrait;

public function getCreationTimestamp(): int
{
return 1701688920;
}

public function update(Connection $connection): void
{
$update = new MailUpdate(
MailTemplateTypes::MAILTYPE_DOWNLOADS_DELIVERY,
(string) file_get_contents(__DIR__ . '/../Fixtures/mails/downloads_delivery/en-plain.html.twig'),
(string) file_get_contents(__DIR__ . '/../Fixtures/mails/downloads_delivery/en-html.html.twig'),
(string) file_get_contents(__DIR__ . '/../Fixtures/mails/downloads_delivery/de-plain.html.twig'),
(string) file_get_contents(__DIR__ . '/../Fixtures/mails/downloads_delivery/de-html.html.twig')
);

$this->updateMail($update, $connection);
}

public function updateDestructive(Connection $connection): void
{
// implement update destructive
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Migration\Core\V6_6;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\MailTemplate\MailTemplateTypes;
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use Shopware\Core\Migration\V6_6\Migration1701688920FixDownloadLinkMail;
use Shopware\Tests\Migration\MigrationTestTrait;

/**
* @internal
*
* @covers \Shopware\Core\Migration\V6_6\Migration1701688920FixDownloadLinkMail
*/
class Migration1701688920FixDownloadLinkMailTest extends TestCase
{
use MigrationTestTrait;

public function testCreationTimestamp(): void
{
$migration = new Migration1701688920FixDownloadLinkMail();
static::assertSame(1701688920, $migration->getCreationTimestamp());
}

public function testMailTemplateUpdated(): void
{
$connection = KernelLifecycleManager::getConnection();

$migration = new Migration1701688920FixDownloadLinkMail();
$migration->update($connection);

$deLangId = $this->fetchLanguageId($connection, 'de-DE');
$enLangId = $this->fetchLanguageId($connection, 'en-GB');
static::assertNotNull($deLangId);
static::assertNotNull($enLangId);

$template = [
'id' => $this->fetchSystemMailTemplateIdFromType(
$connection,
MailTemplateTypes::MAILTYPE_DOWNLOADS_DELIVERY
),
'htmlDe' => (string) file_get_contents(__DIR__ . '/../../../../src/Core/Migration/Fixtures/mails/downloads_delivery/de-html.html.twig'),
'plainDe' => (string) file_get_contents(__DIR__ . '/../../../../src/Core/Migration/Fixtures/mails/downloads_delivery/de-plain.html.twig'),
'htmlEn' => (string) file_get_contents(__DIR__ . '/../../../../src/Core/Migration/Fixtures/mails/downloads_delivery/en-html.html.twig'),
'plainEn' => (string) file_get_contents(__DIR__ . '/../../../../src/Core/Migration/Fixtures/mails/downloads_delivery/en-plain.html.twig'),
];

$mailTemplateTranslationDe = $this->getMailTemplateTranslation(
$connection,
$template,
$deLangId
);

static::assertEquals($mailTemplateTranslationDe['htmlDe'], $mailTemplateTranslationDe['content_html']);
static::assertEquals($mailTemplateTranslationDe['plainDe'], $mailTemplateTranslationDe['content_plain']);

$mailTemplateTranslationEn = $this->getMailTemplateTranslation(
$connection,
$template,
$enLangId
);

static::assertEquals($mailTemplateTranslationEn['htmlEn'], $mailTemplateTranslationEn['content_html']);
static::assertEquals($mailTemplateTranslationEn['plainEn'], $mailTemplateTranslationEn['content_plain']);
}

/**
* @param array<string, string|null> $template
*
* @throws Exception
*
* @return array<string, string>
*/
private function getMailTemplateTranslation(Connection $connection, array $template, string $langId): array
{
$sqlString = 'SELECT `content_plain`, `content_html` from `mail_template_translation` WHERE `mail_template_id`= :templateId AND `language_id` = :langId AND `updated_at` IS NULL';

static::assertNotNull($template['id']);
$translation = $connection->fetchAssociative($sqlString, [
'langId' => $langId,
'templateId' => $template['id'],
]);

if (!$translation) {
static::fail('mail template content empty');
}

return array_merge($template, ['content_html' => $translation['content_html'], 'content_plain' => $translation['content_plain']]);
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws Exception
*/
private function fetchSystemMailTemplateIdFromType(Connection $connection, string $type): ?string
{
$templateTypeId = $connection->executeQuery('
SELECT `id` from `mail_template_type` WHERE `technical_name` = :type
', ['type' => $type])->fetchOne();

$templateId = $connection->executeQuery('
SELECT `id` from `mail_template` WHERE `mail_template_type_id` = :typeId AND `system_default` = 1 AND `updated_at` IS NULL
', ['typeId' => $templateTypeId])->fetchOne();

if ($templateId === false || !\is_string($templateId)) {
return null;
}

return $templateId;
}

/**
* @throws Exception
*/
private function fetchLanguageId(Connection $connection, string $code): ?string
{
return $connection->fetchOne('
SELECT `language`.`id` FROM `language` INNER JOIN `locale` ON `language`.`locale_id` = `locale`.`id` WHERE `code` = :code LIMIT 1
', ['code' => $code]) ?: null;
}
}

0 comments on commit 476e026

Please sign in to comment.