Skip to content

Commit 02d0233

Browse files
akaashakaash
authored andcommitted
ACQE-4823 | Integration Testing - Validate that for multi-store, The customer receives an email with a store-specific email in From header
1 parent c766d6c commit 02d0233

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
namespace Magento\Store\Model;
9+
10+
use Magento\Customer\Test\Fixture\Customer;
11+
use Magento\Framework\App\Area;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Mail\Template\TransportBuilder;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Newsletter\Model\Subscriber;
16+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
17+
use Magento\Store\Test\Fixture\Store as StoreFixture;
18+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
19+
use Magento\TestFramework\Fixture\Config as ConfigFixture;
20+
use Magento\TestFramework\Fixture\DataFixture;
21+
use Magento\TestFramework\Fixture\DataFixtureStorage;
22+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
23+
use Magento\TestFramework\Helper\Bootstrap;
24+
25+
/**
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* phpcs:disable Magento2.Security.Superglobal
28+
*/
29+
class MultiStoreTest extends \PHPUnit\Framework\TestCase
30+
{
31+
/**
32+
* @var ObjectManagerInterface
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var DataFixtureStorage
38+
*/
39+
private $fixtures;
40+
41+
/**
42+
* @inheridoc
43+
* @throws LocalizedException
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
51+
}
52+
53+
/**
54+
* @return void
55+
* @throws LocalizedException
56+
* @throws \Magento\Framework\Exception\MailException
57+
*/
58+
#[
59+
ConfigFixture('system/smtp/transport', 'smtp', 'store'),
60+
DataFixture(WebsiteFixture::class, as: 'website2'),
61+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
62+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
63+
DataFixture(
64+
Customer::class,
65+
[
66+
'store_id' => '$store2.id$',
67+
'website_id' => '$website2.id$',
68+
'addresses' => [[]]
69+
],
70+
as: 'customer1'
71+
),
72+
DataFixture(WebsiteFixture::class, as: 'website3'),
73+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website3.id$'], 'store_group3'),
74+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group3.id$'], 'store3'),
75+
DataFixture(
76+
Customer::class,
77+
[
78+
'store_id' => '$store3.id$',
79+
'website_id' => '$website3.id$',
80+
'addresses' => [[]]
81+
],
82+
as: 'customer2'
83+
),
84+
]
85+
public function testStoreSpecificEmailInFromHeader()
86+
{
87+
$customerOne = $this->fixtures->get('customer1');
88+
$storeOne = $this->fixtures->get('store2');
89+
$customerOneData = [
90+
'email' => $customerOne->getEmail(),
91+
'storeId' => $storeOne->getData('store_id'),
92+
'storeEmail' => 'store_one@example.com'
93+
];
94+
95+
$this->subscribeNewsLetterAndAssertFromHeader($customerOneData);
96+
97+
$customerTwo = $this->fixtures->get('customer2');
98+
$storeTwo = $this->fixtures->get('store3');
99+
$customerTwoData = [
100+
'email' => $customerTwo->getEmail(),
101+
'storeId' => $storeTwo->getData('store_id'),
102+
'storeEmail' => 'store_two@example.com'
103+
];
104+
105+
$this->subscribeNewsLetterAndAssertFromHeader($customerTwoData);
106+
}
107+
108+
/**
109+
* @param $customerData
110+
* @return void
111+
* @throws LocalizedException
112+
* @throws \Magento\Framework\Exception\MailException
113+
*/
114+
private function subscribeNewsLetterAndAssertFromHeader(
115+
$customerData
116+
) :void {
117+
/** @var Subscriber $subscriber */
118+
$subscriber = $this->objectManager->create(Subscriber::class);
119+
$subscriber->subscribe($customerData['email']);
120+
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
121+
122+
/** @var TransportBuilder $transportBuilder */
123+
$transportBuilder = $this->objectManager->get(TransportBuilder::class);
124+
$transport = $transportBuilder->setTemplateIdentifier('newsletter_subscription_confirm_email_template')
125+
->setTemplateOptions(
126+
[
127+
'area' => Area::AREA_FRONTEND,
128+
'store' => (int) $customerData['storeId']
129+
]
130+
)
131+
->setFromByScope(
132+
[
133+
'email' => $customerData['storeEmail'],
134+
'name' => 'Store Email Name'
135+
],
136+
(int) $customerData['storeId']
137+
)
138+
->setTemplateVars(
139+
[
140+
'subscriber_data' => [
141+
'confirmation_link' => $subscriber->getConfirmationLink(),
142+
],
143+
]
144+
)
145+
->addTo($customerData['email'])
146+
->getTransport();
147+
$transport->sendMessage();
148+
$headers = $transport->getMessage()->getHeaders();
149+
$sendMessage = $transport->getMessage();
150+
$this->assertNotNull($sendMessage);
151+
$this->assertStringContainsString($customerData['storeEmail'], $headers['From']);
152+
}
153+
}

0 commit comments

Comments
 (0)