Skip to content

Commit

Permalink
Merge branch 'next-35521/auto-imported-from-github' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-35521 - Add language to flow events

See merge request shopware/6/product/platform!13672
  • Loading branch information
jozsefdamokos committed Apr 26, 2024
2 parents 879d50f + 65c5c54 commit e90e79b
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
@@ -0,0 +1,10 @@
---
issue: NEXT-35521
title: add-language-to-send-mail-event
author: Jasper Peeters
author_email: jasper.peeters@meteor.be
author_github: JasperP98
---
# Core
* Added `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events
* Added language id property to databag for mail events
4 changes: 4 additions & 0 deletions src/Core/Content/DependencyInjection/flow.xml
Expand Up @@ -249,6 +249,10 @@
<tag name="flow.storer" priority="999"/>
</service>

<service id="Shopware\Core\Content\Flow\Dispatching\Storer\LanguageStorer">
<tag name="flow.storer"/>
</service>

<service id="Shopware\Core\Content\Flow\Dispatching\FlowFactory" public="true">
<argument type="tagged_iterator" tag="flow.storer"/>
</service>
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Content/Flow/Dispatching/Action/SendMailAction.php
Expand Up @@ -20,6 +20,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Event\MailAware;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Log\Package;
Expand Down Expand Up @@ -125,6 +126,7 @@ public function handleFlow(StorableFlow $flow): void
$data->set('recipients', $recipients);
$data->set('senderName', $mailTemplate->getTranslation('senderName'));
$data->set('salesChannelId', $flow->getData(MailAware::SALES_CHANNEL_ID));
$data->set('languageId', $flow->getData(LanguageAware::LANGUAGE_ID));

$data->set('templateId', $mailTemplate->getId());
$data->set('customFields', $mailTemplate->getCustomFields());
Expand Down
37 changes: 37 additions & 0 deletions src/Core/Content/Flow/Dispatching/Storer/LanguageStorer.php
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Content\Flow\Dispatching\Storer;

use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Log\Package;

#[Package('services-settings')]
class LanguageStorer extends FlowStorer
{
/**
* @param array<string, mixed> $stored
*
* @return array<string, mixed>
*/
public function store(FlowEventAware $event, array $stored): array
{
if (!$event instanceof LanguageAware) {
return $stored;
}

$stored[LanguageAware::LANGUAGE_ID] = $event->getLanguageId();

return $stored;
}

public function restore(StorableFlow $storable): void
{
if (!$storable->hasStore(LanguageAware::LANGUAGE_ID)) {
return;
}

$storable->setData(LanguageAware::LANGUAGE_ID, $storable->getStore(LanguageAware::LANGUAGE_ID));
}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/Event/LanguageAware.php
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Event;

use Shopware\Core\Framework\Log\Package;

#[Package('services-settings')]
#[IsFlowEventAware]
interface LanguageAware
{
public const LANGUAGE_ID = 'languageId';

public function getLanguageId(): ?string;
}
Expand Up @@ -25,6 +25,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Event\MailAware;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Log\Package;
Expand Down Expand Up @@ -265,6 +266,7 @@ public function testActionExecuted(?string $replyTo, array $exptectedReplyTo = [
'subject' => null,
'mediaIds' => [],
'senderName' => null,
'languageId' => null,
'attachmentsConfig' => new MailAttachmentsConfig(
Context::createDefaultContext(),
$this->mailTemplate,
Expand Down Expand Up @@ -423,6 +425,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'recipient' => ['type' => 'customer'],
'documentTypeIds' => null,
]);
$languageId = Uuid::randomHex();

$expected = [
'data' => [
Expand All @@ -437,6 +440,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'subject' => null,
'mediaIds' => [],
'senderName' => null,
'languageId' => $languageId,
'attachmentsConfig' => new MailAttachmentsConfig(
Context::createDefaultContext(),
$this->mailTemplate,
Expand Down Expand Up @@ -467,6 +471,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
$flow->setData(MailAware::MAIL_STRUCT, $templateData);
$flow->setData(MailAware::SALES_CHANNEL_ID, TestDefaults::SALES_CHANNEL);
$flow->setData(OrderAware::ORDER_ID, $orderId);
$flow->setData(LanguageAware::LANGUAGE_ID, $languageId);
$flow->setData(FlowMailVariables::CONTACT_FORM_DATA, [
'email' => 'customer@example.com',
'firstName' => 'Max',
Expand Down Expand Up @@ -506,6 +511,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'firstName' => 'Max',
'lastName' => 'Mustermann',
],
'languageId' => $languageId,
]
);

Expand Down
122 changes: 122 additions & 0 deletions tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php
@@ -0,0 +1,122 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Content\Flow\Dispatching\Storer\LanguageStorer;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub\LanguageAwareEvent;
use Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub\NonLanguageAwareEvent;

/**
* @internal
*/
#[Package('services-settings')]
#[CoversClass(LanguageStorer::class)]
class LanguageStorerTest extends TestCase
{
/**
* @param array<string, mixed> $stored
* @param array<string, mixed> $expected
*/
#[DataProvider('storeDataProvider')]
public function testStore(FlowEventAware $event, array $stored, array $expected): void
{
$storer = new LanguageStorer();
$stored = $storer->store($event, $stored);

static::assertSame($expected, $stored);
}

public static function storeDataProvider(): \Generator
{
$languageId = Uuid::randomHex();

yield 'store null' => [
'event' => new LanguageAwareEvent(null),
'stored' => [],
'expected' => [LanguageAware::LANGUAGE_ID => null],
];

yield 'store id' => [
'event' => new LanguageAwareEvent($languageId),
'stored' => [],
'expected' => [LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'store existing' => [
'event' => new LanguageAwareEvent($languageId),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'store null with existing' => [
'event' => new LanguageAwareEvent(null),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => null],
];

yield 'store non language aware' => [
'event' => new NonLanguageAwareEvent(),
'stored' => [],
'expected' => [],
];

yield 'store non language aware with existing' => [
'event' => new NonLanguageAwareEvent(),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi'],
];

$languageId2 = Uuid::randomHex();

yield 'store overwrite' => [
'event' => new LanguageAwareEvent($languageId2),
'stored' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId2],
];
}

/**
* @param array<string, mixed> $expected
*/
#[DataProvider('restoreDataProvider')]
public function testRestore(StorableFlow $flow, array $expected): void
{
$storer = new LanguageStorer();
$storer->restore($flow);

static::assertSame($expected, $flow->data());
}

public static function restoreDataProvider(): \Generator
{
$languageId = Uuid::randomHex();

yield 'restore empty' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), []),
'expected' => [],
];

yield 'restore id' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), [
LanguageAware::LANGUAGE_ID => $languageId,
]),
'expected' => [LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'restore null' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), [
LanguageAware::LANGUAGE_ID => null,
]),
'expected' => [LanguageAware::LANGUAGE_ID => null],
];
}
}
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\EventData\ScalarValueType;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\LanguageAware;

/**
* @internal
*/
class LanguageAwareEvent implements FlowEventAware, LanguageAware
{
public function __construct(private readonly ?string $languageId)
{
}

public function getName(): string
{
return 'test';
}

public function getContext(): Context
{
return Context::createDefaultContext();
}

public static function getAvailableData(): EventDataCollection
{
return (new EventDataCollection())
->add('languageId', new ScalarValueType(ScalarValueType::TYPE_STRING));
}

public function getLanguageId(): ?string
{
return $this->languageId;
}
}
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\FlowEventAware;

/**
* @internal
*/
class NonLanguageAwareEvent implements FlowEventAware
{
public function getName(): string
{
return 'test';
}

public function getContext(): Context
{
return Context::createDefaultContext();
}

public static function getAvailableData(): EventDataCollection
{
return new EventDataCollection();
}
}

0 comments on commit e90e79b

Please sign in to comment.