|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Magento\Email\Model\Template; |
| 11 | + |
| 12 | +use Magento\Email\Model\ResourceModel\Template\Collection as TemplateCollection; |
| 13 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\Exception\NotFoundException; |
| 16 | +use Magento\Framework\ObjectManagerInterface; |
| 17 | +use Magento\Store\Model\ScopeInterface; |
| 18 | +use Magento\Framework\Phrase; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 21 | +use Magento\TestFramework\Bootstrap as TestFrameworkBootstrap; |
| 22 | + |
| 23 | +/** |
| 24 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 25 | + */ |
| 26 | +class NewAccountEmailTemplateTest extends \PHPUnit\Framework\TestCase |
| 27 | +{ |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ObjectManagerInterface |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ScopeConfigInterface |
| 36 | + */ |
| 37 | + private $config; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + protected $storeData = []; |
| 43 | + |
| 44 | + /** |
| 45 | + * Set up |
| 46 | + */ |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + parent::setUp(); |
| 50 | + |
| 51 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 52 | + $this->config = $this->objectManager->get(ScopeConfigInterface::class); |
| 53 | + $this->storeData['name'] = $this->config->getValue( |
| 54 | + 'general/store_information/name', |
| 55 | + ScopeInterface::SCOPE_STORES |
| 56 | + ); |
| 57 | + $this->storeData['phone'] = $this->config->getValue( |
| 58 | + 'general/store_information/phone', |
| 59 | + ScopeInterface::SCOPE_STORES |
| 60 | + ); |
| 61 | + $this->storeData['city'] = $this->config->getValue( |
| 62 | + 'general/store_information/city', |
| 63 | + ScopeInterface::SCOPE_STORES |
| 64 | + ); |
| 65 | + $this->storeData['country'] = $this->config->getValue( |
| 66 | + 'general/store_information/country_id', |
| 67 | + ScopeInterface::SCOPE_STORES |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @magentoConfigFixture current_store general/store_information/name TestStore |
| 73 | + * @magentoConfigFixture default_store general/store_information/phone 5124666492 |
| 74 | + * @magentoConfigFixture default_store general/store_information/hours 10 to 2 |
| 75 | + * @magentoConfigFixture default_store general/store_information/street_line1 1 Test Dr |
| 76 | + * @magentoConfigFixture default_store general/store_information/street_line2 2nd Addr Line |
| 77 | + * @magentoConfigFixture default_store general/store_information/city Austin |
| 78 | + * @magentoConfigFixture default_store general/store_information/zip 78739 |
| 79 | + * @magentoConfigFixture default_store general/store_information/country_id US |
| 80 | + * @magentoConfigFixture default_store general/store_information/region_id 57 |
| 81 | + * @magentoDataFixture Magento/Email/Model/_files/email_template.php |
| 82 | + */ |
| 83 | + public function testNewAccountEmailTemplate(): void |
| 84 | + { |
| 85 | + |
| 86 | + /** @var MutableScopeConfigInterface $config */ |
| 87 | + $config = Bootstrap::getObjectManager() |
| 88 | + ->get(MutableScopeConfigInterface::class); |
| 89 | + $config->setValue( |
| 90 | + 'admin/emails/email_template', |
| 91 | + $this->getCustomEmailTemplateId( |
| 92 | + 'template_fixture' |
| 93 | + ) |
| 94 | + ); |
| 95 | + |
| 96 | + /** @var \Magento\User\Model\User $userModel */ |
| 97 | + $userModel = Bootstrap::getObjectManager()->get(\Magento\User\Model\User::class); |
| 98 | + $userModel->setFirstname( |
| 99 | + 'John' |
| 100 | + )->setLastname( |
| 101 | + 'Doe' |
| 102 | + )->setUsername( |
| 103 | + 'user1' |
| 104 | + )->setPassword( |
| 105 | + TestFrameworkBootstrap::ADMIN_PASSWORD |
| 106 | + )->setEmail( |
| 107 | + 'user1@magento.com' |
| 108 | + ); |
| 109 | + $userModel->save(); |
| 110 | + |
| 111 | + $userModel->sendNotificationEmailsIfRequired(); |
| 112 | + |
| 113 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 114 | + $transportBuilderMock = Bootstrap::getObjectManager() |
| 115 | + ->get(TransportBuilderMock::class); |
| 116 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 117 | + $sentMessage->getBodyText(); |
| 118 | + |
| 119 | + $storeText = implode(',', $this->storeData); |
| 120 | + |
| 121 | + $this->assertStringContainsString("John,", $sentMessage->getBodyText()); |
| 122 | + $this->assertStringContainsString("TestStore", $storeText); |
| 123 | + $this->assertStringContainsString("5124666492", $storeText); |
| 124 | + $this->assertStringContainsString("Austin", $storeText); |
| 125 | + $this->assertStringContainsString("US", $storeText); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Return email template id by origin template code |
| 130 | + * |
| 131 | + * @param string $origTemplateCode |
| 132 | + * @return int|null |
| 133 | + * @throws NotFoundException |
| 134 | + */ |
| 135 | + private function getCustomEmailTemplateId(string $origTemplateCode): ?int |
| 136 | + { |
| 137 | + $templateId = null; |
| 138 | + $templateCollection = Bootstrap::getObjectManager() |
| 139 | + ->create(TemplateCollection::class); |
| 140 | + foreach ($templateCollection as $template) { |
| 141 | + if ($template->getOrigTemplateCode() == $origTemplateCode) { |
| 142 | + $templateId = (int) $template->getId(); |
| 143 | + } |
| 144 | + } |
| 145 | + if ($templateId === null) { |
| 146 | + throw new NotFoundException(new Phrase( |
| 147 | + 'Customized %templateCode% email template not found', |
| 148 | + ['templateCode' => $origTemplateCode] |
| 149 | + )); |
| 150 | + } |
| 151 | + |
| 152 | + return $templateId; |
| 153 | + } |
| 154 | +} |
0 commit comments