Skip to content

Commit 1827f09

Browse files
author
Anna Bukatar
committed
MC-41887: Validation Messages - CustomerData messages not showing up
1 parent 74ff24b commit 1827f09

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Message\MessageInterface;
1212
use Magento\Framework\Translate\Inline\ParserInterface;
1313
use Magento\Framework\Translate\InlineInterface;
14+
use Magento\Framework\Session\Config\ConfigInterface;
1415

1516
/**
1617
* Plugin for putting messages to cookies
@@ -54,21 +55,28 @@ class MessagePlugin
5455
*/
5556
private $inlineTranslate;
5657

58+
/**
59+
* @var ConfigInterface
60+
*/
61+
protected $sessionConfig;
62+
5763
/**
5864
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
5965
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
6066
* @param \Magento\Framework\Message\ManagerInterface $messageManager
6167
* @param \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy
6268
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
6369
* @param InlineInterface|null $inlineTranslate
70+
* @param ConfigInterface $sessionConfig
6471
*/
6572
public function __construct(
6673
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
6774
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
6875
\Magento\Framework\Message\ManagerInterface $messageManager,
6976
\Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
7077
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
71-
InlineInterface $inlineTranslate = null
78+
InlineInterface $inlineTranslate = null,
79+
ConfigInterface $sessionConfig
7280
) {
7381
$this->cookieManager = $cookieManager;
7482
$this->cookieMetadataFactory = $cookieMetadataFactory;
@@ -77,6 +85,7 @@ public function __construct(
7785
->get(\Magento\Framework\Serialize\Serializer\Json::class);
7886
$this->interpretationStrategy = $interpretationStrategy;
7987
$this->inlineTranslate = $inlineTranslate ?: ObjectManager::getInstance()->get(InlineInterface::class);
88+
$this->sessionConfig = $sessionConfig ?: ObjectManager::getInstance()->get(ConfigInterface::class);
8089
}
8190

8291
/**
@@ -132,7 +141,7 @@ private function setCookie(array $messages)
132141

133142
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
134143
$publicCookieMetadata->setDurationOneYear();
135-
$publicCookieMetadata->setPath('/');
144+
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());
136145
$publicCookieMetadata->setHttpOnly(false);
137146
$publicCookieMetadata->setSameSite('Strict');
138147

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
1717
use Magento\Framework\Stdlib\CookieManagerInterface;
1818
use Magento\Framework\Translate\InlineInterface;
19+
use Magento\Framework\Session\Config\ConfigInterface;
1920
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
2021
use Magento\Theme\Controller\Result\MessagePlugin;
2122
use PHPUnit\Framework\MockObject\MockObject;
@@ -47,6 +48,11 @@ class MessagePluginTest extends TestCase
4748
/** @var InlineInterface|MockObject */
4849
private $inlineTranslateMock;
4950

51+
/**
52+
* @var ConfigInterface|MockObject
53+
*/
54+
protected $sessionConfigMock;
55+
5056
protected function setUp(): void
5157
{
5258
$this->cookieManagerMock = $this->getMockBuilder(CookieManagerInterface::class)
@@ -62,14 +68,17 @@ protected function setUp(): void
6268
->getMock();
6369
$this->inlineTranslateMock = $this->getMockBuilder(InlineInterface::class)
6470
->getMockForAbstractClass();
71+
$this->sessionConfigMock = $this->getMockBuilder(ConfigInterface::class)
72+
->getMockForAbstractClass();
6573

6674
$this->model = new MessagePlugin(
6775
$this->cookieManagerMock,
6876
$this->cookieMetadataFactoryMock,
6977
$this->managerMock,
7078
$this->interpretationStrategyMock,
7179
$this->serializerMock,
72-
$this->inlineTranslateMock
80+
$this->inlineTranslateMock,
81+
$this->sessionConfigMock
7382
);
7483
}
7584

@@ -549,4 +558,49 @@ function ($data) {
549558

550559
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
551560
}
561+
562+
public function testSetCookieWithCookiePath()
563+
{
564+
/** @var Redirect|MockObject $resultMock */
565+
$resultMock = $this->getMockBuilder(Redirect::class)
566+
->disableOriginalConstructor()
567+
->getMock();
568+
569+
/** @var PublicCookieMetadata|MockObject $cookieMetadataMock */
570+
$cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
571+
->disableOriginalConstructor()
572+
->getMock();
573+
574+
$this->cookieMetadataFactoryMock->expects($this->once())
575+
->method('createPublicCookieMetadata')
576+
->willReturn($cookieMetadataMock);
577+
578+
/** @var MessageInterface|MockObject $messageMock */
579+
$messageMock = $this->getMockBuilder(MessageInterface::class)
580+
->getMock();
581+
582+
/** @var Collection|MockObject $collectionMock */
583+
$collectionMock = $this->getMockBuilder(Collection::class)
584+
->disableOriginalConstructor()
585+
->getMock();
586+
$collectionMock->expects($this->once())
587+
->method('getItems')
588+
->willReturn([$messageMock]);
589+
590+
$this->managerMock->expects($this->once())
591+
->method('getMessages')
592+
->with(true, null)
593+
->willReturn($collectionMock);
594+
595+
/* Test that getCookiePath is called during cookie setup */
596+
$this->sessionConfigMock->expects($this->once())
597+
->method('getCookiePath')
598+
->willReturn('/pub');
599+
$cookieMetadataMock->expects($this->once())
600+
->method('setPath')
601+
->with('/pub')
602+
->willReturn($cookieMetadataMock);
603+
604+
$this->model->afterRenderResult($resultMock, $resultMock);
605+
}
552606
}

0 commit comments

Comments
 (0)