Skip to content

Commit 679f9b4

Browse files
committed
ACP2E-3771: Product restock email alert with multiple store not working corretly on theme level
- Initial solution with test
1 parent 9e7230b commit 679f9b4

File tree

3 files changed

+182
-4
lines changed

3 files changed

+182
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductAlert\Model;
9+
10+
use Magento\Framework\App\Area;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\View\DesignInterface;
13+
use Magento\Framework\View\Element\Template\File\Resolver;
14+
use Magento\ProductAlert\Block\Email\Stock;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
17+
/**
18+
* Check before get template file name consists of themeId, if not add the themeId to get proper theme
19+
*/
20+
class UpdateThemeParams
21+
{
22+
/**
23+
* UpdateThemeParams constructor
24+
*
25+
* @param DesignInterface $design
26+
* @param StoreManagerInterface $storeManager
27+
* @param Stock $stock
28+
*/
29+
public function __construct(
30+
private readonly DesignInterface $design,
31+
private readonly StoreManagerInterface $storeManager,
32+
private readonly Stock $stock
33+
) {
34+
}
35+
36+
/**
37+
* Update theme params for multi store email templates
38+
*
39+
* @param Resolver $subject
40+
* @param string $template
41+
* @param array $params
42+
* @return array
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
44+
* @throws NoSuchEntityException
45+
*/
46+
public function beforeGetTemplateFileName(
47+
Resolver $subject,
48+
string $template,
49+
array $params = []
50+
): array {
51+
if ($template === $this->stock->getTemplate() && !isset($params['themeId'])) {
52+
$params['themeId'] = $this->design->getConfigurationDesignTheme(
53+
Area::AREA_FRONTEND,
54+
['store' => $this->storeManager->getStore()->getId()]
55+
);
56+
}
57+
return [$template, $params];
58+
}
59+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductAlert\Test\Unit\Model;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\View\DesignInterface;
12+
use Magento\ProductAlert\Block\Email\Stock;
13+
use Magento\Framework\View\Element\Template\File\Resolver;
14+
use Magento\ProductAlert\Model\UpdateThemeParams;
15+
use Magento\Store\Model\Store;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use PHPUnit\Framework\MockObject\Exception;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Class UpdateThemeParamsTest to check valid
23+
* template file returns for multi store setup
24+
*/
25+
class UpdateThemeParamsTest extends TestCase
26+
{
27+
/**
28+
* @var UpdateThemeParams
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var DesignInterface|MockObject
34+
*/
35+
private $designMock;
36+
37+
/**
38+
* @var StoreManagerInterface|MockObject
39+
*/
40+
private $storeManagerMock;
41+
42+
/**
43+
* @var Stock|MockObject
44+
*/
45+
private $stockMock;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp(): void
51+
{
52+
$this->designMock = $this->createMock(DesignInterface::class);
53+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
54+
$this->stockMock = $this->createMock(Stock::class);
55+
56+
$this->model = new UpdateThemeParams(
57+
$this->designMock,
58+
$this->storeManagerMock,
59+
$this->stockMock
60+
);
61+
}
62+
63+
/**
64+
* Test cases to check template file returns for multi store setup
65+
*
66+
* @param string $templateFileName
67+
* @param string $stockTemplateFileName
68+
* @param int $storeId
69+
* @param array $params
70+
* @return void
71+
* @throws Exception
72+
* @throws NoSuchEntityException
73+
* @dataProvider getTemplateFileDataProvider
74+
*/
75+
public function testBeforeGetTemplateFileName(
76+
string $templateFileName,
77+
string $stockTemplateFileName,
78+
int $storeId,
79+
array $params
80+
): void {
81+
$resolverMock = $this->createMock(Resolver::class);
82+
$storeMock = $this->createMock(Store::class);
83+
$this->stockMock->method('getTemplate')->willReturn($stockTemplateFileName);
84+
$this->storeManagerMock->method('getStore')->willReturn($storeMock);
85+
$storeMock->method('getId')->willReturn($storeId);
86+
$this->designMock
87+
->method('getConfigurationDesignTheme')
88+
->willReturn($params['themeId']);
89+
90+
$expectedArr = $this->model->beforeGetTemplateFileName($resolverMock, $templateFileName, $params);
91+
$this->assertEquals([$templateFileName, $params], $expectedArr);
92+
}
93+
94+
/**
95+
* Data provider for testBeforeGetTemplateFileName
96+
*
97+
* @return array
98+
*/
99+
public static function getTemplateFileDataProvider(): array
100+
{
101+
return [
102+
'test cases with valid template file name' => [
103+
'Magento_ProductAlert::email/stock.phtml',
104+
'Magento_ProductAlert::email/stock.phtml',
105+
1,
106+
['themeId' => 1]
107+
],
108+
'test cases with invalid template file name' => [
109+
'test.phtml',
110+
'Magento_ProductAlert::email/stock.phtml',
111+
1,
112+
['themeId' => 1]
113+
]
114+
];
115+
}
116+
}

app/code/Magento/ProductAlert/etc/di.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0"?>
22
<!--
3-
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
6-
*/
3+
/**
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
6+
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<type name="Magento\Framework\Module\Setup\Migration">
@@ -20,4 +20,7 @@
2020
</argument>
2121
</arguments>
2222
</type>
23+
<type name="Magento\Framework\View\Element\Template\File\Resolver">
24+
<plugin name="updateThemeParams" type="Magento\ProductAlert\Model\UpdateThemeParams"/>
25+
</type>
2326
</config>

0 commit comments

Comments
 (0)