EditTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Test\Unit\Block\Adminhtml\Template;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class EditTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Email\Block\Adminhtml\Template\Edit
  15. */
  16. protected $_block;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_configStructureMock;
  21. /**
  22. * @var \Magento\Email\Model\Template\Config|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_emailConfigMock;
  25. /**
  26. * @var array
  27. */
  28. protected $_fixtureConfigPath = [
  29. ['scope' => 'scope_11', 'scope_id' => 'scope_id_1', 'path' => 'section1/group1/field1'],
  30. ['scope' => 'scope_11', 'scope_id' => 'scope_id_1', 'path' => 'section1/group1/group2/field1'],
  31. ['scope' => 'scope_11', 'scope_id' => 'scope_id_1', 'path' => 'section1/group1/group2/group3/field1'],
  32. ];
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $filesystemMock;
  37. protected function setUp()
  38. {
  39. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $layoutMock = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['helper']);
  41. $helperMock = $this->createMock(\Magento\Backend\Helper\Data::class);
  42. $menuConfigMock = $this->createMock(\Magento\Backend\Model\Menu\Config::class);
  43. $menuMock = $this->getMockBuilder(\Magento\Backend\Model\Menu::class)
  44. ->setConstructorArgs([$this->createMock(\Psr\Log\LoggerInterface::class)])
  45. ->getMock();
  46. $menuItemMock = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  47. $urlBuilder = $this->createMock(\Magento\Backend\Model\Url::class);
  48. $this->_configStructureMock = $this->createMock(\Magento\Config\Model\Config\Structure::class);
  49. $this->_emailConfigMock = $this->createMock(\Magento\Email\Model\Template\Config::class);
  50. $this->filesystemMock = $this->createPartialMock(
  51. \Magento\Framework\Filesystem::class,
  52. ['getFilesystem', '__wakeup', 'getPath', 'getDirectoryRead']
  53. );
  54. $viewFilesystem = $this->getMockBuilder(\Magento\Framework\View\FileSystem::class)
  55. ->setMethods(['getTemplateFileName'])
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $viewFilesystem->expects(
  59. $this->any()
  60. )->method(
  61. 'getTemplateFileName'
  62. )->will(
  63. $this->returnValue(DirectoryList::ROOT . '/custom/filename.phtml')
  64. );
  65. $params = [
  66. 'urlBuilder' => $urlBuilder,
  67. 'layout' => $layoutMock,
  68. 'menuConfig' => $menuConfigMock,
  69. 'configStructure' => $this->_configStructureMock,
  70. 'emailConfig' => $this->_emailConfigMock,
  71. 'filesystem' => $this->filesystemMock,
  72. 'viewFileSystem' => $viewFilesystem,
  73. ];
  74. $arguments = $objectManager->getConstructArguments(
  75. \Magento\Email\Block\Adminhtml\Template\Edit::class,
  76. $params
  77. );
  78. $urlBuilder->expects($this->any())->method('getUrl')->will($this->returnArgument(0));
  79. $menuConfigMock->expects($this->any())->method('getMenu')->will($this->returnValue($menuMock));
  80. $menuMock->expects($this->any())->method('get')->will($this->returnValue($menuItemMock));
  81. $menuItemMock->expects($this->any())->method('getTitle')->will($this->returnValue('Title'));
  82. $layoutMock->expects($this->any())->method('helper')->will($this->returnValue($helperMock));
  83. $this->_block = $objectManager->getObject(\Magento\Email\Block\Adminhtml\Template\Edit::class, $arguments);
  84. }
  85. /**
  86. * @return void
  87. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  88. */
  89. public function testGetCurrentlyUsedForPaths()
  90. {
  91. $sectionMock = $this->createPartialMock(
  92. \Magento\Config\Model\Config\Structure\Element\Section::class,
  93. ['getLabel']
  94. );
  95. $groupMock1 = $this->createPartialMock(
  96. \Magento\Config\Model\Config\Structure\Element\Group::class,
  97. ['getLabel']
  98. );
  99. $groupMock2 = $this->createPartialMock(
  100. \Magento\Config\Model\Config\Structure\Element\Group::class,
  101. ['getLabel']
  102. );
  103. $groupMock3 = $this->createPartialMock(
  104. \Magento\Config\Model\Config\Structure\Element\Group::class,
  105. ['getLabel']
  106. );
  107. $filedMock = $this->createPartialMock(
  108. \Magento\Config\Model\Config\Structure\Element\Field::class,
  109. ['getLabel']
  110. );
  111. $map = [
  112. [['section1', 'group1'], $groupMock1],
  113. [['section1', 'group1', 'group2'], $groupMock2],
  114. [['section1', 'group1', 'group2', 'group3'], $groupMock3],
  115. [['section1', 'group1', 'field1'], $filedMock],
  116. [['section1', 'group1', 'group2', 'field1'], $filedMock],
  117. [['section1', 'group1', 'group2', 'group3', 'field1'], $filedMock],
  118. ];
  119. $sectionMock->expects($this->any())->method('getLabel')->will($this->returnValue('Section_1_Label'));
  120. $groupMock1->expects($this->any())->method('getLabel')->will($this->returnValue('Group_1_Label'));
  121. $groupMock2->expects($this->any())->method('getLabel')->will($this->returnValue('Group_2_Label'));
  122. $groupMock3->expects($this->any())->method('getLabel')->will($this->returnValue('Group_3_Label'));
  123. $filedMock->expects($this->any())->method('getLabel')->will($this->returnValue('Field_1_Label'));
  124. $this->_configStructureMock->expects($this->any())
  125. ->method('getElement')
  126. ->with('section1')
  127. ->will($this->returnValue($sectionMock));
  128. $this->_configStructureMock->expects($this->any())
  129. ->method('getElementByPathParts')
  130. ->will($this->returnValueMap($map));
  131. $templateMock = $this->createMock(\Magento\Email\Model\BackendTemplate::class);
  132. $templateMock->expects($this->once())
  133. ->method('getSystemConfigPathsWhereCurrentlyUsed')
  134. ->will($this->returnValue($this->_fixtureConfigPath));
  135. $this->_block->setEmailTemplate($templateMock);
  136. $actual = $this->_block->getCurrentlyUsedForPaths(false);
  137. $expected = [
  138. [
  139. ['title' => __('Title')],
  140. ['title' => __('Title'), 'url' => 'adminhtml/system_config/'],
  141. ['title' => 'Section_1_Label', 'url' => 'adminhtml/system_config/edit'],
  142. ['title' => 'Group_1_Label'],
  143. ['title' => 'Field_1_Label', 'scope' => __('Default Config')],
  144. ],
  145. [
  146. ['title' => __('Title')],
  147. ['title' => __('Title'), 'url' => 'adminhtml/system_config/'],
  148. ['title' => 'Section_1_Label', 'url' => 'adminhtml/system_config/edit'],
  149. ['title' => 'Group_1_Label'],
  150. ['title' => 'Group_2_Label'],
  151. ['title' => 'Field_1_Label', 'scope' => __('Default Config')]
  152. ],
  153. [
  154. ['title' => __('Title')],
  155. ['title' => __('Title'), 'url' => 'adminhtml/system_config/'],
  156. ['title' => 'Section_1_Label', 'url' => 'adminhtml/system_config/edit'],
  157. ['title' => 'Group_1_Label'],
  158. ['title' => 'Group_2_Label'],
  159. ['title' => 'Group_3_Label'],
  160. ['title' => 'Field_1_Label', 'scope' => __('Default Config')]
  161. ],
  162. ];
  163. $this->assertEquals($expected, $actual);
  164. }
  165. public function testGetDefaultTemplatesAsOptionsArray()
  166. {
  167. $directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  168. $this->filesystemMock->expects($this->any())
  169. ->method('getDirectoryRead')
  170. ->will($this->returnValue($directoryMock));
  171. $this->_emailConfigMock
  172. ->expects($this->once())
  173. ->method('getAvailableTemplates')
  174. ->will($this->returnValue(
  175. [
  176. [
  177. 'value' => 'template_b2',
  178. 'label' => 'Template B2',
  179. 'group' => 'Fixture_ModuleB',
  180. ],
  181. [
  182. 'value' => 'template_a',
  183. 'label' => 'Template A',
  184. 'group' => 'Fixture_ModuleA',
  185. ],
  186. [
  187. 'value' => 'template_b1',
  188. 'label' => 'Template B1',
  189. 'group' => 'Fixture_ModuleB',
  190. ],
  191. ]
  192. ));
  193. $this->assertEmpty($this->_block->getData('template_options'));
  194. $this->_block->setTemplate('my/custom\template.phtml');
  195. $this->_block->toHtml();
  196. $expectedResult = [
  197. '' => [['value' => '', 'label' => '', 'group' => '']],
  198. 'Fixture_ModuleA' => [
  199. ['value' => 'template_a', 'label' => 'Template A', 'group' => 'Fixture_ModuleA'],
  200. ],
  201. 'Fixture_ModuleB' => [
  202. ['value' => 'template_b1', 'label' => 'Template B1', 'group' => 'Fixture_ModuleB'],
  203. ['value' => 'template_b2', 'label' => 'Template B2', 'group' => 'Fixture_ModuleB'],
  204. ],
  205. ];
  206. $this->assertEquals(
  207. $expectedResult,
  208. $this->_block->getData('template_options'),
  209. 'Options are expected to be sorted by modules and by labels of email templates within modules'
  210. );
  211. }
  212. }