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