Skip to content

Commit 313f04a

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85300: 8437: Silent error when an email template is not found #970
2 parents 0462fee + 34a1e83 commit 313f04a

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
817817
} catch (MailException $e) {
818818
// If we are not able to send a new account email, this should be ignored
819819
$this->logger->critical($e);
820+
} catch (\UnexpectedValueException $e) {
821+
$this->logger->error($e);
820822
}
821823
}
822824

app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,4 +1721,102 @@ private function prepareDateTimeFactory()
17211721

17221722
return $dateTime;
17231723
}
1724+
1725+
public function testCreateAccountUnexpectedValueException()
1726+
{
1727+
$websiteId = 1;
1728+
$storeId = null;
1729+
$defaultStoreId = 1;
1730+
$customerId = 1;
1731+
$customerEmail = '[email protected]';
1732+
$newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
1733+
$exception = new \UnexpectedValueException('Template file was not found');
1734+
1735+
$datetime = $this->prepareDateTimeFactory();
1736+
1737+
$address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
1738+
$address->expects($this->once())
1739+
->method('setCustomerId')
1740+
->with($customerId);
1741+
$store = $this->createMock(\Magento\Store\Model\Store::class);
1742+
$store->expects($this->once())
1743+
->method('getId')
1744+
->willReturn($defaultStoreId);
1745+
$website = $this->createMock(\Magento\Store\Model\Website::class);
1746+
$website->expects($this->atLeastOnce())
1747+
->method('getStoreIds')
1748+
->willReturn([1, 2, 3]);
1749+
$website->expects($this->once())
1750+
->method('getDefaultStore')
1751+
->willReturn($store);
1752+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
1753+
$customer->expects($this->atLeastOnce())
1754+
->method('getId')
1755+
->willReturn($customerId);
1756+
$customer->expects($this->atLeastOnce())
1757+
->method('getEmail')
1758+
->willReturn($customerEmail);
1759+
$customer->expects($this->atLeastOnce())
1760+
->method('getWebsiteId')
1761+
->willReturn($websiteId);
1762+
$customer->expects($this->atLeastOnce())
1763+
->method('getStoreId')
1764+
->willReturn($storeId);
1765+
$customer->expects($this->once())
1766+
->method('setStoreId')
1767+
->with($defaultStoreId);
1768+
$customer->expects($this->once())
1769+
->method('getAddresses')
1770+
->willReturn([$address]);
1771+
$customer->expects($this->once())
1772+
->method('setAddresses')
1773+
->with(null);
1774+
$this->customerRepository->expects($this->once())
1775+
->method('get')
1776+
->with($customerEmail)
1777+
->willReturn($customer);
1778+
$this->share->expects($this->once())
1779+
->method('isWebsiteScope')
1780+
->willReturn(true);
1781+
$this->storeManager->expects($this->atLeastOnce())
1782+
->method('getWebsite')
1783+
->with($websiteId)
1784+
->willReturn($website);
1785+
$this->customerRepository->expects($this->atLeastOnce())
1786+
->method('save')
1787+
->willReturn($customer);
1788+
$this->addressRepository->expects($this->atLeastOnce())
1789+
->method('save')
1790+
->with($address);
1791+
$this->customerRepository->expects($this->once())
1792+
->method('getById')
1793+
->with($customerId)
1794+
->willReturn($customer);
1795+
$this->random->expects($this->once())
1796+
->method('getUniqueHash')
1797+
->willReturn($newLinkToken);
1798+
$customerSecure = $this->createPartialMock(
1799+
\Magento\Customer\Model\Data\CustomerSecure::class,
1800+
['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash']
1801+
);
1802+
$customerSecure->expects($this->any())
1803+
->method('setRpToken')
1804+
->with($newLinkToken);
1805+
$customerSecure->expects($this->any())
1806+
->method('setRpTokenCreatedAt')
1807+
->with($datetime)
1808+
->willReturnSelf();
1809+
$customerSecure->expects($this->any())
1810+
->method('getPasswordHash')
1811+
->willReturn(null);
1812+
$this->customerRegistry->expects($this->atLeastOnce())
1813+
->method('retrieveSecureData')
1814+
->willReturn($customerSecure);
1815+
$this->emailNotificationMock->expects($this->once())
1816+
->method('newAccount')
1817+
->willThrowException($exception);
1818+
$this->logger->expects($this->once())->method('error')->with($exception);
1819+
1820+
$this->accountManagement->createAccount($customer);
1821+
}
17241822
}

app/code/Magento/Email/Model/Template/Config.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ public function getTemplateFilename($templateId, $designParams = [])
205205
$designParams['module'] = $module;
206206

207207
$file = $this->_getInfo($templateId, 'file');
208+
$filename = $this->getFilename($file, $designParams, $module);
208209

209-
return $this->viewFileSystem->getEmailTemplateFileName($file, $designParams, $module);
210+
return $filename;
210211
}
211212

212213
/**
@@ -230,4 +231,24 @@ protected function _getInfo($templateId, $fieldName)
230231
}
231232
return $data[$templateId][$fieldName];
232233
}
234+
235+
/**
236+
* @param string $file
237+
* @param array $designParams
238+
* @param string $module
239+
*
240+
* @return string
241+
*
242+
* @throws \UnexpectedValueException
243+
*/
244+
private function getFilename($file, array $designParams, $module)
245+
{
246+
$filename = $this->viewFileSystem->getEmailTemplateFileName($file, $designParams, $module);
247+
248+
if ($filename === false) {
249+
throw new \UnexpectedValueException("Template file '{$file}' is not found.");
250+
}
251+
252+
return $filename;
253+
}
233254
}

app/code/Magento/Email/Test/Unit/Model/Template/ConfigTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ public function testGetTemplateFilenameWithNoParams()
272272
$this->assertEquals('_files/Fixture/ModuleOne/view/frontend/email/one.html', $actualResult);
273273
}
274274

275+
/**
276+
* @expectedException \UnexpectedValueException
277+
* @expectedExceptionMessage Template file 'one.html' is not found
278+
*/
279+
public function testGetTemplateFilenameWrongFileName()
280+
{
281+
$this->viewFileSystem->expects($this->once())->method('getEmailTemplateFileName')
282+
->with('one.html', $this->designParams, 'Fixture_ModuleOne')
283+
->willReturn(false);
284+
285+
$this->model->getTemplateFilename('template_one', $this->designParams);
286+
}
287+
275288
/**
276289
* @param string $getterMethod
277290
* @param $argument

0 commit comments

Comments
 (0)