diff --git a/changelog/_unreleased/2024-04-15-add-language-to-send-mail-event.md b/changelog/_unreleased/2024-04-15-add-language-to-send-mail-event.md index d461591ed12..ac8dda40283 100644 --- a/changelog/_unreleased/2024-04-15-add-language-to-send-mail-event.md +++ b/changelog/_unreleased/2024-04-15-add-language-to-send-mail-event.md @@ -5,9 +5,6 @@ author: Jasper Peeters author_email: jasper.peeters@meteor.be author_github: JasperP98 --- - # Core - -* Add `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events - -* Add language id property to databag for mail events +* Added `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events +* Added language id property to databag for mail events diff --git a/tests/unit/Core/Content/Flow/Dispatching/Action/SendMailActionTest.php b/tests/unit/Core/Content/Flow/Dispatching/Action/SendMailActionTest.php index df47ae72eb9..84431197f34 100644 --- a/tests/unit/Core/Content/Flow/Dispatching/Action/SendMailActionTest.php +++ b/tests/unit/Core/Content/Flow/Dispatching/Action/SendMailActionTest.php @@ -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; @@ -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, @@ -423,6 +425,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void 'recipient' => ['type' => 'customer'], 'documentTypeIds' => null, ]); + $languageId = Uuid::randomHex(); $expected = [ 'data' => [ @@ -437,6 +440,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void 'subject' => null, 'mediaIds' => [], 'senderName' => null, + 'languageId' => $languageId, 'attachmentsConfig' => new MailAttachmentsConfig( Context::createDefaultContext(), $this->mailTemplate, @@ -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', @@ -506,6 +511,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void 'firstName' => 'Max', 'lastName' => 'Mustermann', ], + 'languageId' => $languageId, ] ); diff --git a/tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php b/tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php new file mode 100644 index 00000000000..3d8be9e14d1 --- /dev/null +++ b/tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php @@ -0,0 +1,122 @@ + $stored + * @param array $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 $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], + ]; + } +} diff --git a/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/LanguageAwareEvent.php b/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/LanguageAwareEvent.php new file mode 100644 index 00000000000..b671c9a7d8a --- /dev/null +++ b/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/LanguageAwareEvent.php @@ -0,0 +1,40 @@ +add('languageId', new ScalarValueType(ScalarValueType::TYPE_STRING)); + } + + public function getLanguageId(): ?string + { + return $this->languageId; + } +} diff --git a/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/NonLanguageAwareEvent.php b/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/NonLanguageAwareEvent.php new file mode 100644 index 00000000000..0f857476704 --- /dev/null +++ b/tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/NonLanguageAwareEvent.php @@ -0,0 +1,28 @@ +