Skip to content

Commit 06453df

Browse files
committed
MAGETWO-93778: "Thank you" text is broken on storefront with enabled translate-inline
1 parent 54d065a commit 06453df

File tree

2 files changed

+130
-3
lines changed

2 files changed

+130
-3
lines changed

app/code/Magento/Theme/Controller/Result/MessagePlugin.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\Theme\Controller\Result;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Controller\Result\Json;
910
use Magento\Framework\Controller\ResultInterface;
1011
use Magento\Framework\Message\MessageInterface;
12+
use Magento\Framework\Translate\Inline\ParserInterface;
13+
use Magento\Framework\Translate\InlineInterface;
1114

1215
/**
1316
* Plugin for putting messages to cookies
@@ -44,26 +47,34 @@ class MessagePlugin
4447
*/
4548
private $serializer;
4649

50+
/**
51+
* @var InlineInterface
52+
*/
53+
private $inlineTranslate;
54+
4755
/**
4856
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
4957
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
5058
* @param \Magento\Framework\Message\ManagerInterface $messageManager
5159
* @param \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy
5260
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
61+
* @param InlineInterface|null $inlineTranslate
5362
*/
5463
public function __construct(
5564
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
5665
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
5766
\Magento\Framework\Message\ManagerInterface $messageManager,
5867
\Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
59-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
68+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
69+
InlineInterface $inlineTranslate = null
6070
) {
6171
$this->cookieManager = $cookieManager;
6272
$this->cookieMetadataFactory = $cookieMetadataFactory;
6373
$this->messageManager = $messageManager;
64-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
74+
$this->serializer = $serializer ?: ObjectManager::getInstance()
6575
->get(\Magento\Framework\Serialize\Serializer\Json::class);
6676
$this->interpretationStrategy = $interpretationStrategy;
77+
$this->inlineTranslate = $inlineTranslate ?: ObjectManager::getInstance()->get(InlineInterface::class);
6778
}
6879

6980
/**
@@ -112,6 +123,12 @@ public function afterRenderResult(
112123
private function setCookie(array $messages)
113124
{
114125
if (!empty($messages)) {
126+
if ($this->inlineTranslate->isAllowed()) {
127+
foreach ($messages as &$message) {
128+
$message['text'] = $this->convertMessageText($message['text']);
129+
}
130+
}
131+
115132
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
116133
$publicCookieMetadata->setDurationOneYear();
117134
$publicCookieMetadata->setPath('/');
@@ -125,6 +142,21 @@ private function setCookie(array $messages)
125142
}
126143
}
127144

145+
/**
146+
* Replace wrapping translation with html body.
147+
*
148+
* @param string $text
149+
* @return string
150+
*/
151+
private function convertMessageText(string $text): string
152+
{
153+
if (preg_match('#' . ParserInterface::REGEXP_TOKEN . '#', $text, $matches)) {
154+
$text = $matches[1];
155+
}
156+
157+
return $text;
158+
}
159+
128160
/**
129161
* Return messages array and clean message manager messages
130162
*

app/code/Magento/Theme/Test/Unit/Controller/Result/MessagePluginTest.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1515
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
1616
use Magento\Framework\Stdlib\CookieManagerInterface;
17+
use Magento\Framework\Translate\InlineInterface;
1718
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
1819
use Magento\Theme\Controller\Result\MessagePlugin;
1920

@@ -40,6 +41,9 @@ class MessagePluginTest extends \PHPUnit\Framework\TestCase
4041
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
4142
private $serializerMock;
4243

44+
/** @var InlineInterface|\PHPUnit_Framework_MockObject_MockObject */
45+
private $inlineTranslateMock;
46+
4347
protected function setUp()
4448
{
4549
$this->cookieManagerMock = $this->getMockBuilder(CookieManagerInterface::class)
@@ -53,13 +57,15 @@ protected function setUp()
5357
->getMockForAbstractClass();
5458
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
5559
->getMock();
60+
$this->inlineTranslateMock = $this->getMockBuilder(InlineInterface::class)->getMockForAbstractClass();
5661

5762
$this->model = new MessagePlugin(
5863
$this->cookieManagerMock,
5964
$this->cookieMetadataFactoryMock,
6065
$this->managerMock,
6166
$this->interpretationStrategyMock,
62-
$this->serializerMock
67+
$this->serializerMock,
68+
$this->inlineTranslateMock
6369
);
6470
}
6571

@@ -450,4 +456,93 @@ function ($data) {
450456

451457
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
452458
}
459+
460+
/**
461+
* @return void
462+
*/
463+
public function testAfterRenderResultWithAllowedInlineTranslate(): void
464+
{
465+
$messageType = 'message1type';
466+
$messageText = '{{{message1text}}{{message1text}}{{message1text}}{{theme/luma}}}';
467+
$expectedMessages = [
468+
[
469+
'type' => $messageType,
470+
'text' => 'message1text',
471+
],
472+
];
473+
474+
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $resultMock */
475+
$resultMock = $this->getMockBuilder(Redirect::class)
476+
->disableOriginalConstructor()
477+
->getMock();
478+
479+
/** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $cookieMetadataMock */
480+
$cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
481+
->disableOriginalConstructor()
482+
->getMock();
483+
484+
$this->cookieMetadataFactoryMock->expects($this->once())
485+
->method('createPublicCookieMetadata')
486+
->willReturn($cookieMetadataMock);
487+
488+
$this->cookieManagerMock->expects($this->once())
489+
->method('setPublicCookie')
490+
->with(
491+
MessagePlugin::MESSAGES_COOKIES_NAME,
492+
json_encode($expectedMessages),
493+
$cookieMetadataMock
494+
);
495+
$this->cookieManagerMock->expects($this->once())
496+
->method('getCookie')
497+
->with(
498+
MessagePlugin::MESSAGES_COOKIES_NAME
499+
)
500+
->willReturn(json_encode([]));
501+
502+
$this->serializerMock->expects($this->once())
503+
->method('unserialize')
504+
->willReturnCallback(
505+
function ($data) {
506+
return json_decode($data, true);
507+
}
508+
);
509+
$this->serializerMock->expects($this->once())
510+
->method('serialize')
511+
->willReturnCallback(
512+
function ($data) {
513+
return json_encode($data);
514+
}
515+
);
516+
517+
/** @var MessageInterface|\PHPUnit_Framework_MockObject_MockObject $messageMock */
518+
$messageMock = $this->getMockBuilder(MessageInterface::class)
519+
->getMock();
520+
$messageMock->expects($this->once())
521+
->method('getType')
522+
->willReturn($messageType);
523+
524+
$this->interpretationStrategyMock->expects($this->once())
525+
->method('interpret')
526+
->with($messageMock)
527+
->willReturn($messageText);
528+
529+
$this->inlineTranslateMock->expects($this->once())
530+
->method('isAllowed')
531+
->willReturn(true);
532+
533+
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
534+
$collectionMock = $this->getMockBuilder(Collection::class)
535+
->disableOriginalConstructor()
536+
->getMock();
537+
$collectionMock->expects($this->once())
538+
->method('getItems')
539+
->willReturn([$messageMock]);
540+
541+
$this->managerMock->expects($this->once())
542+
->method('getMessages')
543+
->with(true, null)
544+
->willReturn($collectionMock);
545+
546+
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
547+
}
453548
}

0 commit comments

Comments
 (0)