Skip to content

Commit 03eff10

Browse files
ENGCOM-7506: Scoped identity at product alert email #26534
- Merge Pull Request #26534 from maqlec/magento2:alert-sender-scope-fix - Merged commits: 1. da04422 2. d88c8d8 3. 5f3b07d 4. 87dde61 5. 9c79208 6. 06e2117 7. 2b1e727 8. d9b4c5a 9. 667e51c 10. 3c6b66a 11. bba5b16 12. f823fc2 13. a50dcf5
2 parents 735579d + a50dcf5 commit 03eff10

File tree

4 files changed

+126
-14
lines changed

4 files changed

+126
-14
lines changed

app/code/Magento/ProductAlert/Model/Email.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
2-
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\ProductAlert\Model;
89

910
use Magento\Catalog\Model\Product;
@@ -40,7 +41,7 @@
4041
* @api
4142
* @since 100.0.2
4243
* @method int getStoreId()
43-
* @method $this setStoreId()
44+
* @method $this setStoreId(int $storeId)
4445
*/
4546
class Email extends AbstractModel
4647
{
@@ -206,7 +207,7 @@ public function getType()
206207
*
207208
* @return $this
208209
*/
209-
public function setWebsite(\Magento\Store\Model\Website $website)
210+
public function setWebsite(Website $website)
210211
{
211212
$this->_website = $website;
212213
return $this;
@@ -275,7 +276,7 @@ public function clean()
275276
*
276277
* @return $this
277278
*/
278-
public function addPriceProduct(\Magento\Catalog\Model\Product $product)
279+
public function addPriceProduct(Product $product)
279280
{
280281
$this->_priceProducts[$product->getId()] = $product;
281282
return $this;
@@ -288,7 +289,7 @@ public function addPriceProduct(\Magento\Catalog\Model\Product $product)
288289
*
289290
* @return $this
290291
*/
291-
public function addStockProduct(\Magento\Catalog\Model\Product $product)
292+
public function addStockProduct(Product $product)
292293
{
293294
$this->_stockProducts[$product->getId()] = $product;
294295
return $this;
@@ -342,7 +343,7 @@ public function send()
342343
return false;
343344
}
344345

345-
$storeId = $this->getStoreId() ?: (int) $this->_customer->getStoreId();
346+
$storeId = (int) $this->getStoreId() ?: (int) $this->_customer->getStoreId();
346347
$store = $this->getStore($storeId);
347348

348349
$this->_appEmulation->startEnvironmentEmulation($storeId);
@@ -378,12 +379,13 @@ public function send()
378379
'customerName' => $customerName,
379380
'alertGrid' => $alertGrid,
380381
]
381-
)->setFrom(
382+
)->setFromByScope(
382383
$this->_scopeConfig->getValue(
383384
self::XML_PATH_EMAIL_IDENTITY,
384385
ScopeInterface::SCOPE_STORE,
385386
$storeId
386-
)
387+
),
388+
$storeId
387389
)->addTo(
388390
$this->_customer->getEmail(),
389391
$customerName

dev/tests/integration/testsuite/Magento/ProductAlert/Model/EmailTest.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\ProductAlert\Model;
89

910
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
1012
use Magento\Customer\Api\AccountManagementInterface;
1113
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
use Magento\Customer\Helper\View;
1215
use Magento\Framework\Exception\LocalizedException;
1316
use Magento\Framework\Exception\MailException;
1417
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Magento\Store\Model\StoreManagerInterface;
1519
use Magento\Store\Model\Website;
20+
use Magento\TestFramework\Helper\Bootstrap;
1621
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
22+
use Magento\TestFramework\ObjectManager;
23+
use PHPUnit\Framework\TestCase;
1724

1825
/**
1926
* Test for Magento\ProductAlert\Model\Email class.
2027
*
2128
* @magentoAppIsolation enabled
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2230
*/
23-
class EmailTest extends \PHPUnit\Framework\TestCase
31+
class EmailTest extends TestCase
2432
{
2533
/**
2634
* @var Email
2735
*/
2836
protected $_emailModel;
2937

3038
/**
31-
* @var \Magento\TestFramework\ObjectManager
39+
* @var ObjectManager
3240
*/
3341
protected $_objectManager;
3442

@@ -38,7 +46,7 @@ class EmailTest extends \PHPUnit\Framework\TestCase
3846
protected $customerAccountManagement;
3947

4048
/**
41-
* @var \Magento\Customer\Helper\View
49+
* @var View
4250
*/
4351
protected $_customerViewHelper;
4452

@@ -62,11 +70,11 @@ class EmailTest extends \PHPUnit\Framework\TestCase
6270
*/
6371
protected function setUp(): void
6472
{
65-
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
73+
$this->_objectManager = Bootstrap::getObjectManager();
6674
$this->customerAccountManagement = $this->_objectManager->create(
6775
AccountManagementInterface::class
6876
);
69-
$this->_customerViewHelper = $this->_objectManager->create(\Magento\Customer\Helper\View::class);
77+
$this->_customerViewHelper = $this->_objectManager->create(View::class);
7078
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
7179
$this->customerRepository = $this->_objectManager->create(CustomerRepositoryInterface::class);
7280
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
@@ -100,7 +108,7 @@ public function testSend($isCustomerIdUsed)
100108
$this->_emailModel->setCustomerData($customer);
101109
}
102110

103-
/** @var \Magento\Catalog\Model\Product $product */
111+
/** @var Product $product */
104112
$product = $this->productRepository->getById(1);
105113

106114
$this->_emailModel->addPriceProduct($product);
@@ -165,4 +173,36 @@ public function testEmailForDifferentCustomers(): void
165173
);
166174
}
167175
}
176+
177+
/**
178+
* @magentoAppArea frontend
179+
* @magentoDataFixture Magento/Customer/_files/customer.php
180+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
181+
* @magentoDataFixture Magento/Store/_files/second_store_with_second_identity.php
182+
*/
183+
public function testScopedMessageIdentity()
184+
{
185+
/** @var Website $website */
186+
$website = $this->_objectManager->create(Website::class);
187+
$website->load(1);
188+
$this->_emailModel->setWebsite($website);
189+
190+
/** @var StoreManagerInterface $storeManager */
191+
$storeManager = $this->_objectManager->create(StoreManagerInterface::class);
192+
$store = $storeManager->getStore('fixture_second_store');
193+
$this->_emailModel->setStoreId($store->getId());
194+
195+
$customer = $this->customerRepository->getById(1);
196+
$this->_emailModel->setCustomerData($customer);
197+
198+
/** @var Product $product */
199+
$product = $this->productRepository->getById(1);
200+
201+
$this->_emailModel->addPriceProduct($product);
202+
$this->_emailModel->send();
203+
204+
$from = $this->transportBuilder->getSentMessage()->getFrom()[0];
205+
$this->assertEquals('Fixture Store Owner', $from->getName());
206+
$this->assertEquals('fixture.store.owner@example.com', $from->getEmail());
207+
}
168208
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Create fixture store with second identity
4+
*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
declare(strict_types=1);
9+
10+
use Magento\Config\Model\ResourceModel\Config;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\Store;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
16+
17+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_store.php');
18+
19+
$objectManager = Bootstrap::getObjectManager();
20+
$store = $objectManager->create(Store::class);
21+
if ($storeId = $store->load('fixture_second_store', 'code')->getId()) {
22+
/** @var Config $configResource */
23+
$configResource = $objectManager->get(Config::class);
24+
$configResource->saveConfig(
25+
'trans_email/ident_general/name',
26+
'Fixture Store Owner',
27+
ScopeInterface::SCOPE_STORES,
28+
$storeId
29+
);
30+
$configResource->saveConfig(
31+
'trans_email/ident_general/email',
32+
'fixture.store.owner@example.com',
33+
ScopeInterface::SCOPE_STORES,
34+
$storeId
35+
);
36+
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
37+
$scopeConfig->clean();
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Config\Model\ResourceModel\Config;
9+
use Magento\Store\Model\ScopeInterface;
10+
use Magento\Store\Model\Store;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
$store = $objectManager->create(Store::class);
16+
$storeId = $store->load('fixture_second_store', 'code')->getId();
17+
18+
if ($storeId) {
19+
$configResource = $objectManager->get(Config::class);
20+
$configResource->deleteConfig(
21+
'trans_email/ident_general/name',
22+
ScopeInterface::SCOPE_STORES,
23+
$storeId
24+
);
25+
$configResource->deleteConfig(
26+
'trans_email/ident_general/email',
27+
ScopeInterface::SCOPE_STORES,
28+
$storeId
29+
);
30+
}
31+
32+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_store_rollback.php');

0 commit comments

Comments
 (0)