ConfigTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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\Template;
  7. use Magento\Email\Model\Template\Config;
  8. class ConfigTest extends \PHPUnit\Framework\TestCase
  9. {
  10. private $designParams = [
  11. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  12. 'theme' => 'Magento/blank',
  13. 'locale' => \Magento\Setup\Module\I18n\Locale::DEFAULT_SYSTEM_LOCALE,
  14. 'module' => 'Fixture_ModuleOne',
  15. ];
  16. /**
  17. * @var Config
  18. */
  19. private $model;
  20. /**
  21. * @var \Magento\Email\Model\Template\Config\Data|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_dataStorage;
  24. /**
  25. * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_moduleReader;
  28. /**
  29. * @var \Magento\Framework\View\FileSystem|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $viewFileSystem;
  32. /**
  33. * @var \Magento\Framework\View\Design\Theme\ThemePackageList|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $themePackages;
  36. /**
  37. * @var \Magento\Framework\Filesystem\Directory\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $readDirFactory;
  40. protected function setUp()
  41. {
  42. $this->_dataStorage = $this->createPartialMock(\Magento\Email\Model\Template\Config\Data::class, ['get']);
  43. $this->_dataStorage->expects(
  44. $this->any()
  45. )->method(
  46. 'get'
  47. )->will(
  48. $this->returnValue(require __DIR__ . '/Config/_files/email_templates_merged.php')
  49. );
  50. $this->_moduleReader = $this->createPartialMock(\Magento\Framework\Module\Dir\Reader::class, ['getModuleDir']);
  51. $this->viewFileSystem = $this->createPartialMock(
  52. \Magento\Framework\View\FileSystem::class,
  53. ['getEmailTemplateFileName']
  54. );
  55. $this->themePackages = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackageList::class);
  56. $this->readDirFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  57. $this->model = new Config(
  58. $this->_dataStorage,
  59. $this->_moduleReader,
  60. $this->viewFileSystem,
  61. $this->themePackages,
  62. $this->readDirFactory
  63. );
  64. }
  65. public function testGetAvailableTemplates()
  66. {
  67. $templates = require __DIR__ . '/Config/_files/email_templates_merged.php';
  68. $themes = [];
  69. $i = 1;
  70. foreach ($templates as $templateData) {
  71. $theme = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackage::class);
  72. $theme->expects($this->any())
  73. ->method('getArea')
  74. ->willReturn($templateData['area']);
  75. $theme->expects($this->any())
  76. ->method('getVendor')
  77. ->willReturn('Vendor');
  78. $theme->expects($this->any())
  79. ->method('getName')
  80. ->willReturn('custom_theme');
  81. $theme->expects($this->any())
  82. ->method('getPath')
  83. ->willReturn('/theme/path');
  84. $themes[] = $theme;
  85. $i++;
  86. }
  87. $this->themePackages->expects($this->exactly(count($templates)))
  88. ->method('getThemes')
  89. ->willReturn($themes);
  90. $dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  91. $this->readDirFactory->expects($this->any())
  92. ->method('create')
  93. ->willReturn($dir);
  94. $dir->expects($this->any())
  95. ->method('isExist')
  96. ->willReturn(true);
  97. $expected = [
  98. 'template_one' => ['label' => 'Template One', 'module' => 'Fixture_ModuleOne'],
  99. 'template_two' => ['label' => 'Template 2', 'module' => 'Fixture_ModuleTwo'],
  100. 'template_one/Vendor/custom_theme' => [
  101. 'label' => 'Template One (Vendor/custom_theme)',
  102. 'module' => 'Fixture_ModuleOne'
  103. ],
  104. 'template_two/Vendor/custom_theme' => [
  105. 'label' => 'Template 2 (Vendor/custom_theme)',
  106. 'module' => 'Fixture_ModuleTwo'
  107. ],
  108. ];
  109. $actualTemplates = $this->model->getAvailableTemplates();
  110. $this->assertCount(count($expected), $actualTemplates);
  111. foreach ($actualTemplates as $templateOptions) {
  112. $this->assertArrayHasKey($templateOptions['value'], $expected);
  113. $expectedOptions = $expected[$templateOptions['value']];
  114. $this->assertEquals($expectedOptions['label'], (string) $templateOptions['label']);
  115. $this->assertEquals($expectedOptions['module'], (string) $templateOptions['group']);
  116. }
  117. }
  118. public function testGetThemeTemplates()
  119. {
  120. $templates = require __DIR__ . '/Config/_files/email_templates_merged.php';
  121. $templateId = 'template_one';
  122. $template = $templates[$templateId];
  123. $foundThemePath = 'Vendor/custom_theme';
  124. $theme = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackage::class);
  125. $theme->expects($this->any())
  126. ->method('getArea')
  127. ->willReturn('frontend');
  128. $theme->expects($this->any())
  129. ->method('getVendor')
  130. ->willReturn('Vendor');
  131. $theme->expects($this->any())
  132. ->method('getName')
  133. ->willReturn('custom_theme');
  134. $theme->expects($this->any())
  135. ->method('getPath')
  136. ->willReturn('/theme/path');
  137. $this->themePackages->expects($this->once())
  138. ->method('getThemes')
  139. ->willReturn([$theme]);
  140. $dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  141. $this->readDirFactory->expects($this->once())
  142. ->method('create')
  143. ->with('/theme/path')
  144. ->willReturn($dir);
  145. $dir->expects($this->once())
  146. ->method('isExist')
  147. ->willReturn(true);
  148. $actualTemplates = $this->model->getThemeTemplates($templateId);
  149. $this->assertNotEmpty($actualTemplates);
  150. foreach ($actualTemplates as $templateOptions) {
  151. $this->assertEquals(
  152. sprintf(
  153. '%s (%s)',
  154. $template['label'],
  155. $foundThemePath
  156. ),
  157. $templateOptions['label']
  158. );
  159. $this->assertEquals(sprintf('%s/%s', $templateId, $foundThemePath), $templateOptions['value']);
  160. $this->assertEquals($template['module'], $templateOptions['group']);
  161. }
  162. }
  163. /**
  164. * @dataProvider parseTemplateCodePartsDataProvider
  165. *
  166. * @param string $input
  167. * @param array $expectedOutput
  168. */
  169. public function testParseTemplateIdParts($input, $expectedOutput)
  170. {
  171. $this->assertEquals($this->model->parseTemplateIdParts($input), $expectedOutput);
  172. }
  173. /**
  174. * @return array
  175. */
  176. public function parseTemplateCodePartsDataProvider()
  177. {
  178. return [
  179. 'Template ID with no theme' => [
  180. 'random_template_code',
  181. [
  182. 'templateId' => 'random_template_code',
  183. 'theme' => null,
  184. ],
  185. ],
  186. 'Template ID with theme' => [
  187. 'random_template_code/Vendor/CustomTheme',
  188. [
  189. 'templateId' => 'random_template_code',
  190. 'theme' => 'Vendor/CustomTheme',
  191. ],
  192. ],
  193. ];
  194. }
  195. public function testGetTemplateLabel()
  196. {
  197. $this->assertEquals('Template One', $this->model->getTemplateLabel('template_one'));
  198. }
  199. public function testGetTemplateType()
  200. {
  201. $this->assertEquals('html', $this->model->getTemplateType('template_one'));
  202. }
  203. public function testGetTemplateModule()
  204. {
  205. $this->assertEquals('Fixture_ModuleOne', $this->model->getTemplateModule('template_one'));
  206. }
  207. public function testGetTemplateArea()
  208. {
  209. $this->assertEquals('frontend', $this->model->getTemplateArea('template_one'));
  210. }
  211. public function testGetTemplateFilenameWithParams()
  212. {
  213. $this->viewFileSystem->expects(
  214. $this->once()
  215. )->method(
  216. 'getEmailTemplateFileName'
  217. )->with(
  218. 'one.html',
  219. $this->designParams,
  220. 'Fixture_ModuleOne'
  221. )->will(
  222. $this->returnValue('_files/Fixture/ModuleOne/view/frontend/email/one.html')
  223. );
  224. $actualResult = $this->model->getTemplateFilename('template_one', $this->designParams);
  225. $this->assertEquals('_files/Fixture/ModuleOne/view/frontend/email/one.html', $actualResult);
  226. }
  227. /**
  228. * Ensure that the getTemplateFilename method can be called without design params
  229. */
  230. public function testGetTemplateFilenameWithNoParams()
  231. {
  232. $this->viewFileSystem->expects(
  233. $this->once()
  234. )->method(
  235. 'getEmailTemplateFileName'
  236. )->with(
  237. 'one.html',
  238. [
  239. 'area' => $this->designParams['area'],
  240. 'module' => $this->designParams['module'],
  241. ],
  242. 'Fixture_ModuleOne'
  243. )->will(
  244. $this->returnValue('_files/Fixture/ModuleOne/view/frontend/email/one.html')
  245. );
  246. $actualResult = $this->model->getTemplateFilename('template_one');
  247. $this->assertEquals('_files/Fixture/ModuleOne/view/frontend/email/one.html', $actualResult);
  248. }
  249. /**
  250. * @expectedException \UnexpectedValueException
  251. * @expectedExceptionMessage Template file 'one.html' is not found
  252. * @return void
  253. */
  254. public function testGetTemplateFilenameWrongFileName(): void
  255. {
  256. $this->viewFileSystem->expects($this->once())->method('getEmailTemplateFileName')
  257. ->with('one.html', $this->designParams, 'Fixture_ModuleOne')
  258. ->willReturn(false);
  259. $this->model->getTemplateFilename('template_one', $this->designParams);
  260. }
  261. /**
  262. * @param string $getterMethod
  263. * @param $argument
  264. * @dataProvider getterMethodUnknownTemplateDataProvider
  265. * @expectedException \UnexpectedValueException
  266. * @expectedExceptionMessage Email template 'unknown' is not defined
  267. */
  268. public function testGetterMethodUnknownTemplate($getterMethod, $argument = null)
  269. {
  270. if (!$argument) {
  271. $this->model->{$getterMethod}('unknown');
  272. } else {
  273. $this->model->{$getterMethod}('unknown', $argument);
  274. }
  275. }
  276. /**
  277. * @return array
  278. */
  279. public function getterMethodUnknownTemplateDataProvider()
  280. {
  281. return [
  282. 'label getter' => ['getTemplateLabel'],
  283. 'type getter' => ['getTemplateType'],
  284. 'module getter' => ['getTemplateModule'],
  285. 'file getter' => ['getTemplateFilename', $this->designParams],
  286. ];
  287. }
  288. /**
  289. * @param string $getterMethod
  290. * @param string $expectedException
  291. * @param array $fixtureFields
  292. * @param $argument
  293. * @dataProvider getterMethodUnknownFieldDataProvider
  294. */
  295. public function testGetterMethodUnknownField(
  296. $getterMethod,
  297. $expectedException,
  298. array $fixtureFields = [],
  299. $argument = null
  300. ) {
  301. $this->expectException('UnexpectedValueException');
  302. $this->expectExceptionMessage($expectedException);
  303. $dataStorage = $this->createPartialMock(\Magento\Email\Model\Template\Config\Data::class, ['get']);
  304. $dataStorage->expects(
  305. $this->atLeastOnce()
  306. )->method(
  307. 'get'
  308. )->will(
  309. $this->returnValue(['fixture' => $fixtureFields])
  310. );
  311. $model = new Config(
  312. $dataStorage,
  313. $this->_moduleReader,
  314. $this->viewFileSystem,
  315. $this->themePackages,
  316. $this->readDirFactory
  317. );
  318. if (!$argument) {
  319. $model->{$getterMethod}('fixture');
  320. } else {
  321. $model->{$getterMethod}('fixture', $argument);
  322. }
  323. }
  324. /**
  325. * @return array
  326. */
  327. public function getterMethodUnknownFieldDataProvider()
  328. {
  329. return [
  330. 'label getter' => ['getTemplateLabel', "Field 'label' is not defined for email template 'fixture'."],
  331. 'type getter' => ['getTemplateType', "Field 'type' is not defined for email template 'fixture'."],
  332. 'module getter' => [
  333. 'getTemplateModule',
  334. "Field 'module' is not defined for email template 'fixture'.",
  335. ],
  336. 'file getter, unknown module' => [
  337. 'getTemplateFilename',
  338. "Field 'module' is not defined for email template 'fixture'.",
  339. [],
  340. $this->designParams,
  341. ],
  342. 'file getter, unknown file' => [
  343. 'getTemplateFilename',
  344. "Field 'file' is not defined for email template 'fixture'.",
  345. ['module' => 'Fixture_Module'],
  346. $this->designParams,
  347. ],
  348. ];
  349. }
  350. }