Skip to content

Commit b6c47a2

Browse files
author
Alexander Akimov
authored
Merge pull request #2949 from magento-tsg/2.3-develop-pr30
[TSG] Upporting for 2.3 (pr30) (2.3.0)
2 parents c4d2ef4 + e72436f commit b6c47a2

File tree

11 files changed

+339
-33
lines changed

11 files changed

+339
-33
lines changed

app/code/Magento/Signifyd/Controller/Webhooks/Handler.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Framework\App\Action\Action;
99
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Request\InvalidRequestException;
11+
use Magento\Framework\App\RequestInterface;
1012
use Magento\Framework\Exception\LocalizedException;
1113
use Magento\Signifyd\Api\CaseRepositoryInterface;
1214
use Magento\Signifyd\Model\CaseServices\UpdatingServiceFactory;
@@ -21,7 +23,7 @@
2123
*
2224
* @see https://www.signifyd.com/docs/api/#/reference/webhooks/
2325
*/
24-
class Handler extends Action
26+
class Handler extends Action implements \Magento\Framework\App\CsrfAwareActionInterface
2527
{
2628
/**
2729
* Event topic of test webhook request.
@@ -136,4 +138,20 @@ public function execute()
136138
$this->logger->critical($e);
137139
}
138140
}
141+
142+
/**
143+
* @inheritDoc
144+
*/
145+
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
146+
{
147+
return null;
148+
}
149+
150+
/**
151+
* @inheritDoc
152+
*/
153+
public function validateForCsrf(RequestInterface $request): ?bool
154+
{
155+
return true;
156+
}
139157
}

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
}

app/code/Magento/Weee/Model/Tax.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,20 @@ public function getProductWeeeAttributes(
248248
$round = true
249249
) {
250250
$result = [];
251-
252-
$websiteId = $this->_storeManager->getWebsite($website)->getId();
251+
$websiteId = null;
253252
/** @var \Magento\Store\Model\Store $store */
254-
$store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
253+
$store = null;
254+
if (!$website) {
255+
$store = $product->getStore();
256+
if ($store) {
257+
$websiteId = $store->getWebsiteId();
258+
}
259+
}
260+
if (!$websiteId) {
261+
$websiteObject = $this->_storeManager->getWebsite($website);
262+
$websiteId = $websiteObject->getId();
263+
$store = $websiteObject->getDefaultGroup()->getDefaultStore();
264+
}
255265

256266
$allWeee = $this->getWeeeTaxAttributeCodes($store);
257267
if (!$allWeee) {

app/code/Magento/Weee/Test/Unit/Model/TaxTest.php

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,17 @@ protected function setUp()
157157
}
158158

159159
/**
160-
* test GetProductWeeeAttributes
161160
* @dataProvider getProductWeeeAttributesDataProvider
162161
* @param array $weeeTaxCalculationsByEntity
163-
* @param array $expectedFptLabel
162+
* @param mixed $websitePassed
163+
* @param string $expectedFptLabel
164+
* @return void
164165
*/
165-
public function testGetProductWeeeAttributes($weeeTaxCalculationsByEntity, $expectedFptLabel)
166-
{
166+
public function testGetProductWeeeAttributes(
167+
array $weeeTaxCalculationsByEntity,
168+
$websitePassed,
169+
string $expectedFptLabel
170+
): void {
167171
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
168172
$website = $this->createMock(\Magento\Store\Model\Website::class);
169173
$store = $this->createMock(\Magento\Store\Model\Store::class);
@@ -187,28 +191,38 @@ public function testGetProductWeeeAttributes($weeeTaxCalculationsByEntity, $expe
187191
->method('getAttributeCodesByFrontendType')
188192
->willReturn(['0'=>'fpt']);
189193

190-
$store->expects($this->any())
191-
->method('getId')
192-
->willReturn(1);
193-
194-
$product->expects($this->any())
195-
->method('getId')
196-
->willReturn(1);
197-
194+
$this->storeManager->expects($this->any())
195+
->method('getWebsite')
196+
->willReturn($website);
198197
$website->expects($this->any())
199198
->method('getId')
200-
->willReturn(1);
199+
->willReturn($websitePassed);
201200
$website->expects($this->any())
202201
->method('getDefaultGroup')
203202
->willReturn($group);
204-
205203
$group->expects($this->any())
206204
->method('getDefaultStore')
207205
->willReturn($store);
206+
$store->expects($this->any())
207+
->method('getId')
208+
->willReturn(1);
208209

209-
$this->storeManager->expects($this->any())
210-
->method('getWebsite')
211-
->willReturn($website);
210+
if ($websitePassed) {
211+
$product->expects($this->never())
212+
->method('getStore')
213+
->willReturn($store);
214+
} else {
215+
$product->expects($this->once())
216+
->method('getStore')
217+
->willReturn($store);
218+
$store->expects($this->once())
219+
->method('getWebsiteId')
220+
->willReturn(1);
221+
}
222+
223+
$product->expects($this->any())
224+
->method('getId')
225+
->willReturn(1);
212226

213227
$this->weeeConfig->expects($this->any())
214228
->method('isEnabled')
@@ -237,7 +251,7 @@ public function testGetProductWeeeAttributes($weeeTaxCalculationsByEntity, $expe
237251
0 => $weeeTaxCalculationsByEntity
238252
]);
239253

240-
$result = $this->model->getProductWeeeAttributes($product, null, null, null, true);
254+
$result = $this->model->getProductWeeeAttributes($product, null, null, $websitePassed, true);
241255
$this->assertTrue(is_array($result));
242256
$this->assertArrayHasKey(0, $result);
243257
$obj = $result[0];
@@ -312,7 +326,8 @@ public function getProductWeeeAttributesDataProvider()
312326
'frontend_label' => 'fpt_label_frontend',
313327
'attribute_code' => 'fpt_code',
314328
],
315-
'expectedFptLabel' => 'fpt_label'
329+
'websitePassed' => 1,
330+
'expectedFptLabel' => 'fpt_label',
316331
],
317332
'store_label_not_defined' => [
318333
'weeeTaxCalculationsByEntity' => [
@@ -321,8 +336,19 @@ public function getProductWeeeAttributesDataProvider()
321336
'frontend_label' => 'fpt_label_frontend',
322337
'attribute_code' => 'fpt_code',
323338
],
324-
'expectedFptLabel' => 'fpt_label_frontend'
325-
]
339+
'websitePassed' => 1,
340+
'expectedFptLabel' => 'fpt_label_frontend',
341+
],
342+
'website_not_passed' => [
343+
'weeeTaxCalculationsByEntity' => [
344+
'weee_value' => 1,
345+
'label_value' => '',
346+
'frontend_label' => 'fpt_label_frontend',
347+
'attribute_code' => 'fpt_code',
348+
],
349+
'websitePassed' => null,
350+
'expectedFptLabel' => 'fpt_label_frontend',
351+
],
326352
];
327353
}
328354

0 commit comments

Comments
 (0)