PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-email/Test/Unit/Model/Template/ConfigTest.php

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