TemplateTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Test\Unit\Model;
  7. use Magento\Framework\App\Area;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\App\TemplateTypesInterface;
  10. use Magento\Setup\Module\I18n\Locale;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * Covers \Magento\Email\Model\Template
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class TemplateTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $context;
  23. /**
  24. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $design;
  27. /**
  28. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  29. * @deprecated since 2.3.0 in favor of stateful global objects elimination.
  30. */
  31. private $registry;
  32. /**
  33. * @var \Magento\Store\Model\App\Emulation|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $appEmulation;
  36. /**
  37. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $storeManager;
  40. /**
  41. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $filesystem;
  44. /**
  45. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $assetRepo;
  48. /**
  49. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $scopeConfig;
  52. /**
  53. * @var \Magento\Email\Model\Template\FilterFactory|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $filterFactory;
  56. /**
  57. * @var \Magento\Framework\Filter\FilterManager|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $filterManager;
  60. /**
  61. * @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $urlModel;
  64. /**
  65. * @var \Magento\Email\Model\Template\Config|\PHPUnit_Framework_MockObject_MockObject
  66. */
  67. private $emailConfig;
  68. /**
  69. * @var \Magento\Email\Model\TemplateFactory|\PHPUnit_Framework_MockObject_MockObject
  70. */
  71. private $templateFactory;
  72. /**
  73. * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
  74. */
  75. private $serializerMock;
  76. protected function setUp()
  77. {
  78. $this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->appEmulation = $this->getMockBuilder(\Magento\Store\Model\App\Emulation::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->assetRepo = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->templateFactory = $this->getMockBuilder(\Magento\Email\Model\TemplateFactory::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->filterManager = $this->getMockBuilder(\Magento\Framework\Filter\FilterManager::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $this->urlModel = $this->getMockBuilder(\Magento\Framework\Url::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->filterFactory = $this->getMockBuilder(\Magento\Email\Model\Template\FilterFactory::class)
  115. ->setMethods(['create'])
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
  119. }
  120. /**
  121. * Return the model under test with additional methods mocked.
  122. *
  123. * @param array $mockedMethods
  124. * @return \Magento\Email\Model\Template|\PHPUnit_Framework_MockObject_MockObject
  125. */
  126. protected function getModelMock(array $mockedMethods = [])
  127. {
  128. return $this->getMockBuilder(\Magento\Email\Model\Template::class)
  129. ->setMethods(array_merge($mockedMethods, ['__wakeup', '__sleep', '_init']))
  130. ->setConstructorArgs([
  131. $this->context,
  132. $this->design,
  133. $this->registry,
  134. $this->appEmulation,
  135. $this->storeManager,
  136. $this->assetRepo,
  137. $this->filesystem,
  138. $this->scopeConfig,
  139. $this->emailConfig,
  140. $this->templateFactory,
  141. $this->filterManager,
  142. $this->urlModel,
  143. $this->filterFactory,
  144. [],
  145. $this->serializerMock
  146. ])
  147. ->getMock();
  148. }
  149. public function testSetAndGetIsChildTemplate()
  150. {
  151. $model = $this->getModelMock();
  152. $model->setIsChildTemplate(true);
  153. $this->assertSame(true, $model->isChildTemplate());
  154. $model->setIsChildTemplate(false);
  155. $this->assertSame(false, $model->isChildTemplate());
  156. }
  157. public function testSetAndGetTemplateFilter()
  158. {
  159. $model = $this->getModelMock();
  160. $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $model->setTemplateFilter($filterTemplate);
  164. $this->assertSame($filterTemplate, $model->getTemplateFilter());
  165. }
  166. public function testGetTemplateFilterWithEmptyValue()
  167. {
  168. $filterTemplate = $this->getMockBuilder(\Magento\Framework\Filter\Template::class)
  169. ->setMethods(['setUseAbsoluteLinks', 'setStoreId', 'setUrlModel'])
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $filterTemplate->expects($this->once())
  173. ->method('setUseAbsoluteLinks')
  174. ->will($this->returnSelf());
  175. $filterTemplate->expects($this->once())
  176. ->method('setStoreId')
  177. ->will($this->returnSelf());
  178. $this->filterFactory->method('create')
  179. ->will($this->returnValue($filterTemplate));
  180. $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  181. ->setMethods(['getStore'])
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $model = $this->getModelMock(['getUseAbsoluteLinks', 'getDesignConfig']);
  185. $model->expects($this->once())
  186. ->method('getDesignConfig')
  187. ->will($this->returnValue($designConfig));
  188. $this->assertSame($filterTemplate, $model->getTemplateFilter());
  189. }
  190. /**
  191. * @param $templateType string
  192. * @param $templateText string
  193. * @param $parsedTemplateText string
  194. * @param $expectedTemplateSubject string|null
  195. * @param $expectedOrigTemplateVariables array|null
  196. * @param $expectedTemplateStyles string|null
  197. * @dataProvider loadDefaultDataProvider
  198. */
  199. public function testLoadDefault(
  200. $templateType,
  201. $templateText,
  202. $parsedTemplateText,
  203. $expectedTemplateSubject,
  204. $expectedOrigTemplateVariables,
  205. $expectedTemplateStyles
  206. ) {
  207. $model = $this->getModelMock([
  208. 'getDesignParams'
  209. ]);
  210. $designParams = [
  211. 'area' => Area::AREA_FRONTEND,
  212. 'theme' => 'Magento/blank',
  213. 'locale' => Locale::DEFAULT_SYSTEM_LOCALE,
  214. ];
  215. $model->expects($this->once())
  216. ->method('getDesignParams')
  217. ->will($this->returnValue($designParams));
  218. $templateId = 'templateId';
  219. $templateFile = 'templateFile';
  220. $this->emailConfig->expects($this->once())
  221. ->method('getTemplateFilename')
  222. ->with($templateId)
  223. ->will($this->returnValue($templateFile));
  224. $this->emailConfig->expects($this->once())
  225. ->method('getTemplateType')
  226. ->with($templateId)
  227. ->will($this->returnValue($templateType));
  228. $modulesDir = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
  229. ->setMethods(['readFile', 'getRelativePath'])
  230. ->getMockForAbstractClass();
  231. $relativePath = 'relativePath';
  232. $modulesDir->expects($this->once())
  233. ->method('getRelativePath')
  234. ->with($templateFile)
  235. ->will($this->returnValue($relativePath));
  236. $modulesDir->expects($this->once())
  237. ->method('readFile')
  238. ->will($this->returnValue($templateText));
  239. $this->filesystem->expects($this->once())
  240. ->method('getDirectoryRead')
  241. ->with(DirectoryList::ROOT)
  242. ->will($this->returnValue($modulesDir));
  243. $model->loadDefault($templateId);
  244. if ($templateType === 'html') {
  245. $this->assertEquals(TemplateTypesInterface::TYPE_HTML, $model->getTemplateType());
  246. } else {
  247. $this->assertEquals(TemplateTypesInterface::TYPE_TEXT, $model->getTemplateType());
  248. }
  249. $this->assertEquals($templateId, $model->getId());
  250. $this->assertEquals($parsedTemplateText, $model->getTemplateText());
  251. $this->assertEquals($expectedTemplateSubject, $model->getTemplateSubject());
  252. $this->assertEquals($expectedOrigTemplateVariables, $model->getData('orig_template_variables'));
  253. $this->assertEquals($expectedTemplateStyles, $model->getTemplateStyles());
  254. }
  255. /**
  256. * @return array
  257. */
  258. public function loadDefaultDataProvider()
  259. {
  260. return [
  261. 'empty' => [
  262. 'templateType' => 'html',
  263. 'templateText' => '',
  264. 'parsedTemplateText' => '',
  265. 'expectedTemplateSubject' => null,
  266. 'expectedOrigTemplateVariables' => null,
  267. 'expectedTemplateStyles' => null,
  268. ],
  269. 'copyright in Plain Text Removed' => [
  270. 'templateType' => 'text',
  271. 'templateText' => '<!-- Copyright © Magento, Inc. All rights reserved. -->',
  272. 'parsedTemplateText' => '',
  273. 'expectedTemplateSubject' => null,
  274. 'expectedOrigTemplateVariables' => null,
  275. 'expectedTemplateStyles' => null,
  276. ],
  277. 'copyright in HTML Removed' => [
  278. 'templateType' => 'html',
  279. 'templateText' => '<!-- Copyright © Magento, Inc. All rights reserved. -->',
  280. 'parsedTemplateText' => '',
  281. 'expectedTemplateSubject' => null,
  282. 'expectedOrigTemplateVariables' => null,
  283. 'expectedTemplateStyles' => null,
  284. ],
  285. 'subject set' => [
  286. 'templateType' => 'html',
  287. 'templateText' => '<!--@subject Email Subject @-->',
  288. 'parsedTemplateText' => '',
  289. 'expectedTemplateSubject' => 'Email Subject',
  290. 'expectedOrigTemplateVariables' => null,
  291. 'expectedTemplateStyles' => null,
  292. ],
  293. 'orig_template_variables set' => [
  294. 'templateType' => 'html',
  295. 'templateText' => '<!--@vars {"store url=\"\"":"Store Url"} @-->Some Other Text',
  296. 'parsedTemplateText' => 'Some Other Text',
  297. 'expectedTemplateSubject' => null,
  298. 'expectedOrigTemplateVariables' => '{"store url=\"\"":"Store Url"}',
  299. 'expectedTemplateStyles' => null,
  300. ],
  301. 'styles' => [
  302. 'templateType' => 'html',
  303. 'templateText' => '<!--@styles p { color: #000; } @-->Some Other Text',
  304. 'parsedTemplateText' => 'Some Other Text',
  305. 'expectedTemplateSubject' => null,
  306. 'expectedOrigTemplateVariables' => null,
  307. 'expectedTemplateStyles' => 'p { color: #000; }',
  308. ],
  309. ];
  310. }
  311. /**
  312. * Test to ensure that this method handles loading templates from DB vs filesystem, based on whether template ID is
  313. * numeric.
  314. *
  315. * @param bool $loadFromDatabase
  316. * @dataProvider loadByConfigPathDataProvider
  317. */
  318. public function testLoadByConfigPath($loadFromDatabase)
  319. {
  320. $configPath = 'design/email/header_template';
  321. $model = $this->getModelMock([
  322. 'getDesignConfig',
  323. 'loadDefault',
  324. 'load',
  325. 'getTemplateText',
  326. 'setTemplateText',
  327. ]);
  328. $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  329. ->setMethods(['getStore'])
  330. ->disableOriginalConstructor()
  331. ->getMock();
  332. $storeId = 'storeId';
  333. $designConfig->expects($this->once())
  334. ->method('getStore')
  335. ->will($this->returnValue($storeId));
  336. $model->expects($this->once())
  337. ->method('getDesignConfig')
  338. ->will($this->returnValue($designConfig));
  339. if ($loadFromDatabase) {
  340. $templateId = '1';
  341. $model->expects($this->once())
  342. ->method('load')
  343. ->with($templateId)
  344. ->will($this->returnSelf());
  345. } else {
  346. $templateId = 'design_email_header_template';
  347. $model->expects($this->once())
  348. ->method('loadDefault')
  349. ->with($templateId)
  350. ->will($this->returnSelf());
  351. }
  352. $this->scopeConfig->expects($this->once())
  353. ->method('getValue')
  354. ->with($configPath, ScopeInterface::SCOPE_STORE, $storeId)
  355. ->will($this->returnValue($templateId));
  356. $model->loadByConfigPath($configPath);
  357. }
  358. /**
  359. * @return array
  360. */
  361. public function loadByConfigPathDataProvider()
  362. {
  363. return [
  364. 'Load from filesystem' => [
  365. false,
  366. 'Test template content',
  367. 'Test template content',
  368. ],
  369. 'Load from database' => [
  370. true,
  371. 'Test template content',
  372. 'Test template content',
  373. ],
  374. ];
  375. }
  376. public function testGetAndSetId()
  377. {
  378. $model = $this->getModelMock();
  379. $templateId = 'templateId';
  380. $this->assertEquals($model, $model->setId($templateId));
  381. $this->assertEquals($templateId, $model->getId());
  382. }
  383. /**
  384. * @param $senderName string
  385. * @param $senderEmail string
  386. * @param $templateSubject string
  387. * @dataProvider isValidForSendDataProvider
  388. */
  389. public function testIsValidForSend($senderName, $senderEmail, $templateSubject, $expectedValue)
  390. {
  391. $model = $this->getModelMock(['getSenderName', 'getSenderEmail', 'getTemplateSubject']);
  392. $model->expects($this->any())
  393. ->method('getSenderName')
  394. ->will($this->returnValue($senderName));
  395. $model->expects($this->any())
  396. ->method('getSenderEmail')
  397. ->will($this->returnValue($senderEmail));
  398. $model->expects($this->any())
  399. ->method('getTemplateSubject')
  400. ->will($this->returnValue($templateSubject));
  401. $this->assertEquals($expectedValue, $model->isValidForSend());
  402. }
  403. /**
  404. * @return array
  405. */
  406. public function isValidForSendDataProvider()
  407. {
  408. return [
  409. 'should be valid' => [
  410. 'senderName' => 'sender name',
  411. 'senderEmail' => 'email@example.com',
  412. 'templateSubject' => 'template subject',
  413. 'expectedValue' => true
  414. ],
  415. 'no sender name so not valid' => [
  416. 'senderName' => '',
  417. 'senderEmail' => 'email@example.com',
  418. 'templateSubject' => 'template subject',
  419. 'expectedValue' => false
  420. ],
  421. 'no sender email so not valid' => [
  422. 'senderName' => 'sender name',
  423. 'senderEmail' => '',
  424. 'templateSubject' => 'template subject',
  425. 'expectedValue' => false
  426. ],
  427. 'no subject so not valid' => [
  428. 'senderName' => 'sender name',
  429. 'senderEmail' => 'email@example.com',
  430. 'templateSubject' => '',
  431. 'expectedValue' => false
  432. ],
  433. ];
  434. }
  435. public function testGetProcessedTemplateSubject()
  436. {
  437. $model = $this->getModelMock(['getTemplateFilter', 'getDesignConfig', 'applyDesignConfig']);
  438. $templateSubject = 'templateSubject';
  439. $model->setTemplateSubject($templateSubject);
  440. $filterTemplate = $this->getMockBuilder(\Magento\Framework\Filter\Template::class)
  441. ->setMethods(['setVariables', 'setStoreId', 'filter'])
  442. ->disableOriginalConstructor()
  443. ->getMock();
  444. $model->expects($this->once())
  445. ->method('getTemplateFilter')
  446. ->will($this->returnValue($filterTemplate));
  447. $model->expects($this->once())
  448. ->method('applyDesignConfig');
  449. $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  450. ->setMethods(['getStore'])
  451. ->disableOriginalConstructor()
  452. ->getMock();
  453. $storeId = 'storeId';
  454. $designConfig->expects($this->once())
  455. ->method('getStore')
  456. ->will($this->returnValue($storeId));
  457. $model->expects($this->once())
  458. ->method('getDesignConfig')
  459. ->will($this->returnValue($designConfig));
  460. $filterTemplate->expects($this->once())
  461. ->method('setStoreId')
  462. ->with($storeId)
  463. ->will($this->returnSelf());
  464. $expectedResult = 'expected';
  465. $filterTemplate->expects($this->once())
  466. ->method('filter')
  467. ->with($templateSubject)
  468. ->will($this->returnValue($expectedResult));
  469. $variables = [ 'key' => 'value' ];
  470. $filterTemplate->expects($this->once())
  471. ->method('setVariables')
  472. ->with(array_merge($variables, ['this' => $model]));
  473. $this->assertEquals($expectedResult, $model->getProcessedTemplateSubject($variables));
  474. }
  475. /**
  476. * @param $withGroup bool
  477. * @param $templateVariables string
  478. * @param $expectedResult array
  479. * @dataProvider getVariablesOptionArrayDataProvider
  480. */
  481. public function testGetVariablesOptionArray($withGroup, $templateVariables, $expectedResult)
  482. {
  483. $model = $this->getModelMock();
  484. $model->setData('orig_template_variables', $templateVariables);
  485. $this->serializerMock->expects($this->any())->method('unserialize')
  486. ->willReturn(
  487. json_decode($templateVariables, true)
  488. );
  489. $this->assertEquals($expectedResult, $model->getVariablesOptionArray($withGroup));
  490. }
  491. /**
  492. * @return array
  493. */
  494. public function getVariablesOptionArrayDataProvider()
  495. {
  496. return [
  497. 'empty variables' => [
  498. 'withGroup' => false,
  499. 'templateVariables' => '',
  500. 'expectedResult' => [],
  501. ],
  502. 'empty variables with grouped option' => [
  503. 'withGroup' => true,
  504. 'templateVariables' => '',
  505. 'expectedResult' => [],
  506. ],
  507. 'customer account new variables' => [
  508. 'withGroup' => false,
  509. 'templateVariables' => '{"store url=\"\"":"Store Url","var logo_url":"Email Logo Image Url",'
  510. . '"var customer.name":"Customer Name"}',
  511. 'expectedResult' => [
  512. [
  513. 'value' => '{{store url=""}}',
  514. 'label' => __('%1', 'Store Url'),
  515. ],
  516. [
  517. 'value' => '{{var logo_url}}',
  518. 'label' => __('%1', 'Email Logo Image Url'),
  519. ],
  520. [
  521. 'value' => '{{var customer.name}}',
  522. 'label' => __('%1', 'Customer Name'),
  523. ],
  524. ],
  525. ],
  526. 'customer account new variables with grouped option' => [
  527. 'withGroup' => true,
  528. 'templateVariables' => '{"store url=\"\"":"Store Url","var logo_url":"Email Logo Image Url",'
  529. . '"var customer.name":"Customer Name"}',
  530. 'expectedResult' => [
  531. 'label' => __('Template Variables'),
  532. 'value' => [
  533. [
  534. 'value' => '{{store url=""}}',
  535. 'label' => __('%1', 'Store Url'),
  536. ],
  537. [
  538. 'value' => '{{var logo_url}}',
  539. 'label' => __('%1', 'Email Logo Image Url'),
  540. ],
  541. [
  542. 'value' => '{{var customer.name}}',
  543. 'label' => __('%1', 'Customer Name'),
  544. ],
  545. ],
  546. ],
  547. ],
  548. ];
  549. }
  550. /**
  551. * @param $templateId string|int
  552. * @param $expectedResult string
  553. * @dataProvider processTemplateVariable
  554. */
  555. public function testProcessTemplate($templateId, $expectedResult)
  556. {
  557. $model = $this->getModelMock([
  558. 'load',
  559. 'loadDefault',
  560. 'getProcessedTemplate',
  561. 'applyDesignConfig',
  562. 'cancelDesignConfig',
  563. ]);
  564. $model->setId($templateId);
  565. if (is_numeric($templateId)) {
  566. $model->expects($this->once())
  567. ->method('load')
  568. ->with($templateId);
  569. } else {
  570. $model->expects($this->once())
  571. ->method('loadDefault')
  572. ->with($templateId);
  573. }
  574. $model->expects($this->once())
  575. ->method('applyDesignConfig')
  576. ->will($this->returnValue(true));
  577. $model->expects($this->once())
  578. ->method('cancelDesignConfig')
  579. ->will($this->returnValue(true));
  580. $vars = [ 'key' => 'value' ];
  581. $model->setVars($vars);
  582. $model->expects($this->once())
  583. ->method('getProcessedTemplate')
  584. ->with($vars)
  585. ->will($this->returnValue($expectedResult));
  586. $this->assertEquals($expectedResult, $model->processTemplate());
  587. $this->assertTrue($model->getUseAbsoluteLinks());
  588. }
  589. /**
  590. * @return array
  591. */
  592. public function processTemplateVariable()
  593. {
  594. return [
  595. 'numeric id' => [
  596. 'templateId' => 1,
  597. 'expectedResult' => 'expected result',
  598. ],
  599. 'string id' => [
  600. 'templateId' => 'my id',
  601. 'expectedResult' => 'expected result',
  602. ],
  603. ];
  604. }
  605. /**
  606. * @expectedException \Magento\Framework\Exception\MailException
  607. */
  608. public function testProcessTemplateThrowsExceptionNonExistentTemplate()
  609. {
  610. $model = $this->getModelMock([
  611. 'loadDefault',
  612. 'applyDesignConfig',
  613. ]);
  614. $model->expects($this->once())
  615. ->method('loadDefault')
  616. ->will($this->returnValue(true));
  617. $model->expects($this->once())
  618. ->method('applyDesignConfig')
  619. ->will($this->returnValue(true));
  620. $model->processTemplate();
  621. }
  622. public function testGetSubject()
  623. {
  624. $variables = ['key' => 'value'];
  625. $model = $this->getModelMock(['getProcessedTemplateSubject']);
  626. $model->setVars($variables);
  627. $expectedResult = 'result';
  628. $model->expects($this->once())
  629. ->method('getProcessedTemplateSubject')
  630. ->with($variables)
  631. ->will($this->returnValue($expectedResult));
  632. $this->assertEquals($expectedResult, $model->getSubject());
  633. }
  634. public function testSetOptions()
  635. {
  636. $options = ['someOption' => 'someValue'];
  637. $model = $this->getModelMock(['setDesignConfig']);
  638. $model->expects($this->once())
  639. ->method('setDesignConfig')
  640. ->with($options);
  641. $model->setOptions($options);
  642. }
  643. /**
  644. * @dataProvider getTypeDataProvider
  645. * @param string $templateType
  646. * @param int $expectedResult
  647. */
  648. public function testGetType($templateType, $expectedResult)
  649. {
  650. $emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
  651. ->setMethods(['getTemplateType'])
  652. ->disableOriginalConstructor()
  653. ->getMock();
  654. $emailConfig->expects($this->once())->method('getTemplateType')->will($this->returnValue($templateType));
  655. /** @var \Magento\Email\Model\Template $model */
  656. $model = $this->getMockBuilder(\Magento\Email\Model\Template::class)
  657. ->setMethods(['_init'])
  658. ->setConstructorArgs([
  659. $this->createMock(\Magento\Framework\Model\Context::class),
  660. $this->createMock(\Magento\Theme\Model\View\Design::class),
  661. $this->createMock(\Magento\Framework\Registry::class),
  662. $this->createMock(\Magento\Store\Model\App\Emulation::class),
  663. $this->createMock(\Magento\Store\Model\StoreManager::class),
  664. $this->createMock(\Magento\Framework\View\Asset\Repository::class),
  665. $this->createMock(\Magento\Framework\Filesystem::class),
  666. $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class),
  667. $emailConfig,
  668. $this->createMock(\Magento\Email\Model\TemplateFactory::class),
  669. $this->createMock(\Magento\Framework\Filter\FilterManager::class),
  670. $this->createMock(\Magento\Framework\Url::class),
  671. $this->createMock(\Magento\Email\Model\Template\FilterFactory::class),
  672. ])
  673. ->getMock();
  674. $model->setTemplateId(10);
  675. $this->assertEquals($expectedResult, $model->getType());
  676. }
  677. /**
  678. * @return array
  679. */
  680. public function getTypeDataProvider()
  681. {
  682. return [['text', 1], ['html', 2]];
  683. }
  684. }