AbstractTemplateTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Email\Model\AbstractTemplate.
  8. */
  9. namespace Magento\Email\Test\Unit\Model;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class AbstractTemplateTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $design;
  19. /**
  20. * @var \Magento\Store\Model\App\Emulation|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $appEmulation;
  23. /**
  24. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $storeManager;
  27. /**
  28. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $store;
  31. /**
  32. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $filesystem;
  35. /**
  36. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $assetRepo;
  39. /**
  40. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $scopeConfig;
  43. /**
  44. * @var \Magento\Email\Model\Template\FilterFactory|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $filterFactory;
  47. /**
  48. * @var \Magento\Email\Model\Template\Config|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $emailConfig;
  51. /**
  52. * @var \Magento\Email\Model\TemplateFactory|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. private $templateFactory;
  55. protected function setUp()
  56. {
  57. $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->appEmulation = $this->getMockBuilder(\Magento\Store\Model\App\Emulation::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  70. ->setMethods(['getFrontendName', 'getId'])
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->store->expects($this->any())
  74. ->method('getFrontendName')
  75. ->will($this->returnValue('frontendName'));
  76. $this->store->expects($this->any())
  77. ->method('getFrontendName')
  78. ->will($this->returnValue('storeId'));
  79. $this->storeManager->expects($this->any())
  80. ->method('getStore')
  81. ->will($this->returnValue($this->store));
  82. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->assetRepo = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $this->filterFactory = $this->getMockBuilder(\Magento\Email\Model\Template\FilterFactory::class)
  95. ->setMethods(['create'])
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->templateFactory = $this->getMockBuilder(\Magento\Email\Model\TemplateFactory::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. }
  102. /**
  103. * Return the model under test with additional methods mocked.
  104. *
  105. * @param array $mockedMethods
  106. * @param array $data
  107. * @return \Magento\Email\Model\Template|\PHPUnit_Framework_MockObject_MockObject
  108. */
  109. protected function getModelMock(array $mockedMethods = [], array $data = [])
  110. {
  111. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  112. return $this->getMockForAbstractClass(
  113. \Magento\Email\Model\AbstractTemplate::class,
  114. $helper->getConstructArguments(
  115. \Magento\Email\Model\AbstractTemplate::class,
  116. [
  117. 'design' => $this->design,
  118. 'appEmulation' => $this->appEmulation,
  119. 'storeManager' => $this->storeManager,
  120. 'filesystem' => $this->filesystem,
  121. 'assetRepo' => $this->assetRepo,
  122. 'scopeConfig' => $this->scopeConfig,
  123. 'emailConfig' => $this->emailConfig,
  124. 'filterFactory' => $this->filterFactory,
  125. 'templateFactory' => $this->templateFactory,
  126. 'data' => $data,
  127. ]
  128. ),
  129. '',
  130. true,
  131. true,
  132. true,
  133. array_merge($mockedMethods, ['__wakeup', '__sleep', '_init'])
  134. );
  135. }
  136. /**
  137. * @param $variables array
  138. * @param $templateType string
  139. * @param $storeId int
  140. * @param $expectedVariables array
  141. * @param $expectedResult string
  142. * @dataProvider getProcessedTemplateProvider
  143. */
  144. public function testGetProcessedTemplate($variables, $templateType, $storeId, $expectedVariables, $expectedResult)
  145. {
  146. $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
  147. ->setMethods([
  148. 'setUseSessionInUrl',
  149. 'setPlainTemplateMode',
  150. 'setIsChildTemplate',
  151. 'setDesignParams',
  152. 'setVariables',
  153. 'setStoreId',
  154. 'filter',
  155. 'getStoreId',
  156. 'getInlineCssFiles',
  157. ])
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $filterTemplate->expects($this->once())
  161. ->method('setUseSessionInUrl')
  162. ->with(false)
  163. ->will($this->returnSelf());
  164. $filterTemplate->expects($this->once())
  165. ->method('setPlainTemplateMode')
  166. ->with($templateType === \Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT)
  167. ->will($this->returnSelf());
  168. $filterTemplate->expects($this->once())
  169. ->method('setIsChildTemplate')
  170. ->will($this->returnSelf());
  171. $filterTemplate->expects($this->once())
  172. ->method('setDesignParams')
  173. ->will($this->returnSelf());
  174. $filterTemplate->expects($this->any())
  175. ->method('setStoreId')
  176. ->will($this->returnSelf());
  177. $filterTemplate->expects($this->any())
  178. ->method('getStoreId')
  179. ->will($this->returnValue($storeId));
  180. $expectedVariables['store'] = $this->store;
  181. $model = $this->getModelMock([
  182. 'getDesignParams',
  183. 'applyDesignConfig',
  184. 'getTemplateText',
  185. 'isPlain',
  186. ]);
  187. $filterTemplate->expects($this->any())
  188. ->method('setVariables')
  189. ->with(array_merge(['this' => $model], $expectedVariables));
  190. $model->setTemplateFilter($filterTemplate);
  191. $model->setTemplateType($templateType);
  192. $designParams = [
  193. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  194. 'theme' => 'themeId',
  195. 'locale' => 'localeId',
  196. ];
  197. $model->expects($this->any())
  198. ->method('getDesignParams')
  199. ->will($this->returnValue($designParams));
  200. $model->expects($this->atLeastOnce())
  201. ->method('isPlain')
  202. ->will($this->returnValue($templateType === \Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT));
  203. $preparedTemplateText = $expectedResult; //'prepared text';
  204. $model->expects($this->once())
  205. ->method('getTemplateText')
  206. ->will($this->returnValue($preparedTemplateText));
  207. $filterTemplate->expects($this->once())
  208. ->method('filter')
  209. ->with($preparedTemplateText)
  210. ->will($this->returnValue($expectedResult));
  211. $this->assertEquals($expectedResult, $model->getProcessedTemplate($variables));
  212. }
  213. /**
  214. * @expectedException \LogicException
  215. */
  216. public function testGetProcessedTemplateException()
  217. {
  218. $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
  219. ->setMethods([
  220. 'setUseSessionInUrl',
  221. 'setPlainTemplateMode',
  222. 'setIsChildTemplate',
  223. 'setDesignParams',
  224. 'setVariables',
  225. 'setStoreId',
  226. 'filter',
  227. 'getStoreId',
  228. 'getInlineCssFiles',
  229. ])
  230. ->disableOriginalConstructor()
  231. ->getMock();
  232. $filterTemplate->expects($this->once())
  233. ->method('setUseSessionInUrl')
  234. ->will($this->returnSelf());
  235. $filterTemplate->expects($this->once())
  236. ->method('setPlainTemplateMode')
  237. ->will($this->returnSelf());
  238. $filterTemplate->expects($this->once())
  239. ->method('setIsChildTemplate')
  240. ->will($this->returnSelf());
  241. $filterTemplate->expects($this->once())
  242. ->method('setDesignParams')
  243. ->will($this->returnSelf());
  244. $filterTemplate->expects($this->any())
  245. ->method('setStoreId')
  246. ->will($this->returnSelf());
  247. $filterTemplate->expects($this->any())
  248. ->method('getStoreId')
  249. ->will($this->returnValue(1));
  250. $model = $this->getModelMock([
  251. 'getDesignParams',
  252. 'applyDesignConfig',
  253. 'getTemplateText',
  254. 'isPlain',
  255. ]);
  256. $designParams = [
  257. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  258. 'theme' => 'themeId',
  259. 'locale' => 'localeId',
  260. ];
  261. $model->expects($this->any())
  262. ->method('getDesignParams')
  263. ->will($this->returnValue($designParams));
  264. $model->setTemplateFilter($filterTemplate);
  265. $model->setTemplateType(\Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT);
  266. $filterTemplate->expects($this->once())
  267. ->method('filter')
  268. ->will($this->throwException(new \Exception));
  269. $model->getProcessedTemplate([]);
  270. }
  271. /**
  272. * @return array
  273. */
  274. public function getProcessedTemplateProvider()
  275. {
  276. return [
  277. 'default' => [
  278. 'variables' => [],
  279. 'templateType' => \Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT,
  280. 'storeId' => 1,
  281. 'expectedVariables' => [
  282. 'logo_url' => null,
  283. 'logo_alt' => 'frontendName',
  284. 'store' => null,
  285. 'logo_width' => null,
  286. 'logo_height' => null,
  287. 'store_phone' => null,
  288. 'store_hours' => null,
  289. 'store_email' => null,
  290. ],
  291. 'expectedResult' => 'expected result',
  292. ],
  293. 'logo variables set' => [
  294. 'variables' => [
  295. 'logo_url' => 'http://example.com/logo',
  296. 'logo_alt' => 'Logo Alt',
  297. ],
  298. 'templateType' => \Magento\Framework\App\TemplateTypesInterface::TYPE_HTML,
  299. 'storeId' => 1,
  300. 'expectedVariables' => [
  301. 'logo_url' => 'http://example.com/logo',
  302. 'logo_alt' => 'Logo Alt',
  303. 'store' => null,
  304. 'logo_width' => null,
  305. 'logo_height' => null,
  306. 'store_phone' => null,
  307. 'store_hours' => null,
  308. 'store_email' => null,
  309. 'template_styles' => null,
  310. ],
  311. 'expectedResult' => 'expected result',
  312. ],
  313. ];
  314. }
  315. public function testGetDefaultEmailLogo()
  316. {
  317. $model = $this->getModelMock(['getDesignParams']);
  318. $value = 'urlWithParamsValue';
  319. $designParams = [
  320. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  321. 'theme' => 'themeId',
  322. 'locale' => 'localeId',
  323. ];
  324. $model->expects($this->once())
  325. ->method('getDesignParams')
  326. ->will($this->returnValue($designParams));
  327. $this->assetRepo->method('getUrlWithParams')
  328. ->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_LOGO_FILE_ID, $designParams)
  329. ->will($this->returnValue($value));
  330. $this->assertEquals($value, $model->getDefaultEmailLogo());
  331. }
  332. /**
  333. * @param array $config
  334. * @expectedException \Magento\Framework\Exception\LocalizedException
  335. * @dataProvider invalidInputParametersDataProvider
  336. */
  337. public function testSetDesignConfigWithInvalidInputParametersThrowsException($config)
  338. {
  339. $this->getModelMock()->setDesignConfig($config);
  340. }
  341. public function testSetDesignConfigWithValidInputParametersReturnsSuccess()
  342. {
  343. $config = ['area' => 'some_area', 'store' => 1];
  344. $model = $this->getModelMock();
  345. $model->setDesignConfig($config);
  346. $this->assertEquals($config, $model->getDesignConfig()->getData());
  347. }
  348. /**
  349. * @return array
  350. */
  351. public function invalidInputParametersDataProvider()
  352. {
  353. return [[[]], [['area' => 'some_area']], [['store' => 'any_store']]];
  354. }
  355. public function testEmulateDesignAndRevertDesign()
  356. {
  357. $model = $this->getModelMock();
  358. $originalConfig = ['area' => 'some_area', 'store' => 1];
  359. $model->setDesignConfig($originalConfig);
  360. $expectedConfigs = [
  361. ['in' => ['area' => 'frontend', 'store' => null], 'out' => $originalConfig],
  362. ['in' => ['area' => 'frontend', 'store' => false], 'out' => $originalConfig],
  363. ['in' => ['area' => 'frontend', 'store' => 0], 'out' => ['area' => 'frontend', 'store' => 0]],
  364. ['in' => ['area' => 'frontend', 'store' => 1], 'out' => ['area' => 'frontend', 'store' => 1]],
  365. ['in' => ['area' => 'frontend', 'store' => 2], 'out' => ['area' => 'frontend', 'store' => 2]],
  366. ];
  367. foreach ($expectedConfigs as $set) {
  368. $model->emulateDesign($set['in']['store'], $set['in']['area']);
  369. // assert config data has been emulated
  370. $this->assertEquals($set['out'], $model->getDesignConfig()->getData());
  371. $model->revertDesign();
  372. // assert config data has been reverted to the original state
  373. $this->assertEquals($originalConfig, $model->getDesignConfig()->getData());
  374. }
  375. }
  376. public function testGetDesignConfig()
  377. {
  378. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  379. $designMock = $this->createMock(\Magento\Framework\View\DesignInterface::class);
  380. $designMock->expects($this->any())->method('getArea')->willReturn('test_area');
  381. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  382. $storeMock->expects($this->any())->method('getId')->willReturn(2);
  383. $storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  384. $storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
  385. $model = $this->getMockForAbstractClass(
  386. \Magento\Email\Model\AbstractTemplate::class,
  387. $helper->getConstructArguments(
  388. \Magento\Email\Model\AbstractTemplate::class,
  389. [
  390. 'design' => $designMock,
  391. 'storeManager' => $storeManagerMock
  392. ]
  393. )
  394. );
  395. $expectedConfig = ['area' => 'test_area', 'store' => 2];
  396. $this->assertEquals($expectedConfig, $model->getDesignConfig()->getData());
  397. }
  398. /**
  399. * @return void
  400. */
  401. public function testSetForcedAreaWhenAreaIsNotSet(): void
  402. {
  403. $templateId = 'test_template';
  404. $model = $this->getModelMock([], ['area' => null]);
  405. $this->emailConfig->expects($this->once())
  406. ->method('getTemplateArea')
  407. ->with($templateId);
  408. $model->setForcedArea($templateId);
  409. }
  410. /**
  411. * @return void
  412. */
  413. public function testSetForcedAreaWhenAreaIsSet(): void
  414. {
  415. $templateId = 'test_template';
  416. $model = $this->getModelMock([], ['area' => 'frontend']);
  417. $this->emailConfig->expects($this->never())
  418. ->method('getTemplateArea');
  419. $model->setForcedArea($templateId);
  420. }
  421. }