Skip to content

Commit 15c920e

Browse files
committed
MAGETWO-90349: Template file 'header.html' is not found.
1 parent d849f99 commit 15c920e

File tree

8 files changed

+272
-7
lines changed

8 files changed

+272
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
873873
} catch (MailException $e) {
874874
// If we are not able to send a new account email, this should be ignored
875875
$this->logger->critical($e);
876+
} catch (\UnexpectedValueException $e) {
877+
$this->logger->error($e);
876878
}
877879
}
878880

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,4 +1875,105 @@ private function prepareDateTimeFactory()
18751875

18761876
return $dateTime;
18771877
}
1878+
1879+
/**
1880+
* @return void
1881+
*/
1882+
public function testCreateAccountUnexpectedValueException(): void
1883+
{
1884+
$websiteId = 1;
1885+
$storeId = null;
1886+
$defaultStoreId = 1;
1887+
$customerId = 1;
1888+
$customerEmail = 'email@email.com';
1889+
$newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
1890+
$exception = new \UnexpectedValueException('Template file was not found');
1891+
1892+
$datetime = $this->prepareDateTimeFactory();
1893+
1894+
$address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
1895+
$address->expects($this->once())
1896+
->method('setCustomerId')
1897+
->with($customerId);
1898+
$store = $this->createMock(\Magento\Store\Model\Store::class);
1899+
$store->expects($this->once())
1900+
->method('getId')
1901+
->willReturn($defaultStoreId);
1902+
$website = $this->createMock(\Magento\Store\Model\Website::class);
1903+
$website->expects($this->atLeastOnce())
1904+
->method('getStoreIds')
1905+
->willReturn([1, 2, 3]);
1906+
$website->expects($this->once())
1907+
->method('getDefaultStore')
1908+
->willReturn($store);
1909+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
1910+
$customer->expects($this->atLeastOnce())
1911+
->method('getId')
1912+
->willReturn($customerId);
1913+
$customer->expects($this->atLeastOnce())
1914+
->method('getEmail')
1915+
->willReturn($customerEmail);
1916+
$customer->expects($this->atLeastOnce())
1917+
->method('getWebsiteId')
1918+
->willReturn($websiteId);
1919+
$customer->expects($this->atLeastOnce())
1920+
->method('getStoreId')
1921+
->willReturn($storeId);
1922+
$customer->expects($this->once())
1923+
->method('setStoreId')
1924+
->with($defaultStoreId);
1925+
$customer->expects($this->once())
1926+
->method('getAddresses')
1927+
->willReturn([$address]);
1928+
$customer->expects($this->once())
1929+
->method('setAddresses')
1930+
->with(null);
1931+
$this->customerRepository->expects($this->once())
1932+
->method('get')
1933+
->with($customerEmail)
1934+
->willReturn($customer);
1935+
$this->share->expects($this->once())
1936+
->method('isWebsiteScope')
1937+
->willReturn(true);
1938+
$this->storeManager->expects($this->atLeastOnce())
1939+
->method('getWebsite')
1940+
->with($websiteId)
1941+
->willReturn($website);
1942+
$this->customerRepository->expects($this->atLeastOnce())
1943+
->method('save')
1944+
->willReturn($customer);
1945+
$this->addressRepository->expects($this->atLeastOnce())
1946+
->method('save')
1947+
->with($address);
1948+
$this->customerRepository->expects($this->once())
1949+
->method('getById')
1950+
->with($customerId)
1951+
->willReturn($customer);
1952+
$this->random->expects($this->once())
1953+
->method('getUniqueHash')
1954+
->willReturn($newLinkToken);
1955+
$customerSecure = $this->createPartialMock(
1956+
\Magento\Customer\Model\Data\CustomerSecure::class,
1957+
['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash']
1958+
);
1959+
$customerSecure->expects($this->any())
1960+
->method('setRpToken')
1961+
->with($newLinkToken);
1962+
$customerSecure->expects($this->any())
1963+
->method('setRpTokenCreatedAt')
1964+
->with($datetime)
1965+
->willReturnSelf();
1966+
$customerSecure->expects($this->any())
1967+
->method('getPasswordHash')
1968+
->willReturn(null);
1969+
$this->customerRegistry->expects($this->atLeastOnce())
1970+
->method('retrieveSecureData')
1971+
->willReturn($customerSecure);
1972+
$this->emailNotificationMock->expects($this->once())
1973+
->method('newAccount')
1974+
->willThrowException($exception);
1975+
$this->logger->expects($this->once())->method('error')->with($exception);
1976+
1977+
$this->accountManagement->createAccount($customer);
1978+
}
18781979
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,13 @@ protected function cancelDesignConfig()
531531
*
532532
* @param string $templateId
533533
* @return $this
534-
* @throws \Magento\Framework\Exception\MailException
535534
*/
536535
public function setForcedArea($templateId)
537536
{
538-
if ($this->area) {
539-
throw new \LogicException(__('The area is already set.'));
537+
if ($this->area === null) {
538+
$this->area = $this->emailConfig->getTemplateArea($templateId);
540539
}
541-
$this->area = $this->emailConfig->getTemplateArea($templateId);
540+
542541
return $this;
543542
}
544543

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

Lines changed: 24 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,26 @@ protected function _getInfo($templateId, $fieldName)
230231
}
231232
return $data[$templateId][$fieldName];
232233
}
234+
235+
/**
236+
* Retrieve template file path.
237+
*
238+
* @param string $file
239+
* @param array $designParams
240+
* @param string $module
241+
*
242+
* @return string
243+
*
244+
* @throws \UnexpectedValueException
245+
*/
246+
private function getFilename(string $file, array $designParams, string $module): string
247+
{
248+
$filename = $this->viewFileSystem->getEmailTemplateFileName($file, $designParams, $module);
249+
250+
if ($filename === false) {
251+
throw new \UnexpectedValueException("Template file '{$file}' is not found.");
252+
}
253+
254+
return $filename;
255+
}
233256
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,20 @@ 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+
* @return void
279+
*/
280+
public function testGetTemplateFilenameWrongFileName(): void
281+
{
282+
$this->viewFileSystem->expects($this->once())->method('getEmailTemplateFileName')
283+
->with('one.html', $this->designParams, 'Fixture_ModuleOne')
284+
->willReturn(false);
285+
286+
$this->model->getTemplateFilename('template_one', $this->designParams);
287+
}
288+
275289
/**
276290
* @param string $getterMethod
277291
* @param $argument

app/code/Magento/Theme/Model/Design/Config/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private function getTemplateText($templateId, DesignConfigInterface $designConfi
114114
if (is_numeric($templateId)) {
115115
$template->load($templateId);
116116
} else {
117+
$template->setForcedArea($templateId);
117118
$template->loadDefault($templateId);
118119
}
119120
$text = $template->getTemplateText();

app/code/Magento/Theme/Test/Unit/Model/Config/ValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testValidateHasRecursiveReference()
7070
$designElementMock->expects($this->once())->method('getValue')->willReturn($fieldConfig['field']);
7171

7272
$templateMock = $this->getMockBuilder(\Magento\Email\Model\TemplateInterface::class)
73-
->setMethods(['getTemplateText', 'emulateDesign', 'loadDefault', 'revertDesign'])
73+
->setMethods(['getTemplateText', 'emulateDesign', 'loadDefault', 'revertDesign', 'setForcedArea'])
7474
->getMock();
7575

7676
$this->templateFactoryMock->expects($this->once())->method('create')->willReturn($templateMock);
@@ -115,7 +115,7 @@ public function testValidateNoRecursiveReference()
115115
$designElementMock->expects($this->once())->method('getValue')->willReturn($fieldConfig['field']);
116116

117117
$templateMock = $this->getMockBuilder(\Magento\Email\Model\TemplateInterface::class)
118-
->setMethods(['getTemplateText', 'emulateDesign', 'loadDefault', 'revertDesign'])
118+
->setMethods(['getTemplateText', 'emulateDesign', 'loadDefault', 'revertDesign', 'setForcedArea'])
119119
->getMock();
120120

121121
$this->templateFactoryMock->expects($this->once())->method('create')->willReturn($templateMock);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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\Theme\Test\Unit\Model\Design\Config;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Theme\Model\Design\Config\Validator;
12+
13+
/**
14+
* Unit tests for Magento\Theme\Test\Unit\Model\Design\Config\Validator.
15+
*/
16+
class ValidatorTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var \Magento\Framework\Mail\TemplateInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $templateFactory;
22+
23+
/**
24+
* @var \Magento\Framework\Mail\TemplateInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $template;
27+
28+
/**
29+
* @var \Magento\Theme\Api\Data\DesignConfigInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $designConfig;
32+
33+
/**
34+
* @var ObjectManager
35+
*/
36+
private $objectManager;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
$this->objectManager = new ObjectManager($this);
44+
$this->templateFactory = $this->getMockBuilder(\Magento\Framework\Mail\TemplateInterfaceFactory::class)
45+
->disableOriginalConstructor()
46+
->setMethods(['create'])
47+
->getMockForAbstractClass();
48+
$this->template = $this->getMockBuilder(\Magento\Framework\Mail\TemplateInterface::class)
49+
->disableOriginalConstructor()
50+
->setMethods(
51+
[
52+
'emulateDesign',
53+
'setForcedArea',
54+
'loadDefault',
55+
'getTemplateText',
56+
'revertDesign',
57+
]
58+
)
59+
->getMockForAbstractClass();
60+
$this->templateFactory->expects($this->any())->method('create')->willReturn($this->template);
61+
$this->designConfig = $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigInterface::class)
62+
->disableOriginalConstructor()
63+
->setMethods(['getExtensionAttributes'])
64+
->getMockForAbstractClass();
65+
}
66+
67+
/**
68+
* @return void
69+
*/
70+
public function testGetDefaultTemplateTextDefaultScope(): void
71+
{
72+
$templateId = 'email_template';
73+
$designData = [
74+
'field_config' => ['field' => 'fieldValue'],
75+
'value' => $templateId,
76+
];
77+
78+
$this->templateFactory->expects($this->once())->method('create');
79+
$this->designConfig->expects($this->any())->method('getScope')->willReturn('default');
80+
$this->template->expects($this->once())->method('emulateDesign');
81+
$this->template->expects($this->once())->method('setForcedArea')->with($templateId);
82+
$this->template->expects($this->once())->method('loadDefault')->with($templateId);
83+
$this->template->expects($this->once())->method('getTemplateText');
84+
$this->template->expects($this->once())->method('revertDesign');
85+
86+
$extensionAttributes = $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigExtensionInterface::class)
87+
->disableOriginalConstructor()
88+
->setMethods(['getDesignConfigData'])
89+
->getMockForAbstractClass();
90+
91+
$extensionAttributes->expects($this->any())->method('getDesignConfigData')->willReturn(
92+
[
93+
$this->getDesignConfigData($designData),
94+
]
95+
);
96+
97+
$this->designConfig->expects($this->any())->method('getExtensionAttributes')->willReturn($extensionAttributes);
98+
99+
/** @var Validator $validator */
100+
$validator = $this->objectManager->getObject(
101+
Validator::class,
102+
[
103+
'templateFactory' => $this->templateFactory,
104+
'fields' => ['field' => 'fieldValue'],
105+
]
106+
);
107+
$validator->validate($this->designConfig);
108+
}
109+
110+
/**
111+
* Returns design config data object.
112+
*
113+
* @param array $data
114+
* @return \Magento\Theme\Model\Data\Design\Config\Data
115+
*/
116+
private function getDesignConfigData(array $data = []): \Magento\Theme\Model\Data\Design\Config\Data
117+
{
118+
return $this->objectManager->getObject(
119+
\Magento\Theme\Model\Data\Design\Config\Data::class,
120+
[
121+
'data' => $data,
122+
]
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)