PreviewTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class PreviewTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. protected $objectManagerHelper;
  16. const MALICIOUS_TEXT = 'test malicious';
  17. /**
  18. * Init data
  19. */
  20. protected function setUp()
  21. {
  22. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. }
  24. /**
  25. * Check of processing email templates
  26. *
  27. * @param array $requestParamMap
  28. *
  29. * @dataProvider toHtmlDataProvider
  30. * @param $requestParamMap
  31. */
  32. public function testToHtml($requestParamMap)
  33. {
  34. $storeId = 1;
  35. $template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
  36. ->setMethods([
  37. 'setDesignConfig',
  38. 'getDesignConfig',
  39. '__wakeup',
  40. 'getProcessedTemplate',
  41. 'getAppState',
  42. 'revertDesign'
  43. ])
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $template->expects($this->once())
  47. ->method('getProcessedTemplate')
  48. ->with($this->equalTo([]))
  49. ->willReturn(self::MALICIOUS_TEXT);
  50. $designConfigData = [];
  51. $template->expects($this->atLeastOnce())
  52. ->method('getDesignConfig')
  53. ->willReturn(new \Magento\Framework\DataObject(
  54. $designConfigData
  55. ));
  56. $emailFactory = $this->createPartialMock(\Magento\Email\Model\TemplateFactory::class, ['create']);
  57. $emailFactory->expects($this->any())
  58. ->method('create')
  59. ->willReturn($template);
  60. $request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  61. $request->expects($this->any())->method('getParam')->willReturnMap($requestParamMap);
  62. $eventManage = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  63. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  64. $design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
  65. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
  66. $store->expects($this->any())->method('getId')->willReturn($storeId);
  67. $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $storeManager->expects($this->atLeastOnce())
  71. ->method('getDefaultStoreView')
  72. ->willReturn($store);
  73. $storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
  74. $storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
  75. $appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
  76. ->setConstructorArgs([
  77. $scopeConfig
  78. ])
  79. ->setMethods(['emulateAreaCode'])
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $appState->expects($this->any())
  83. ->method('emulateAreaCode')
  84. ->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA, [$template, 'getProcessedTemplate'])
  85. ->willReturn($template->getProcessedTemplate());
  86. $context = $this->createPartialMock(
  87. \Magento\Backend\Block\Template\Context::class,
  88. ['getRequest', 'getEventManager', 'getScopeConfig', 'getDesignPackage', 'getStoreManager', 'getAppState']
  89. );
  90. $context->expects($this->any())->method('getRequest')->willReturn($request);
  91. $context->expects($this->any())->method('getEventManager')->willReturn($eventManage);
  92. $context->expects($this->any())->method('getScopeConfig')->willReturn($scopeConfig);
  93. $context->expects($this->any())->method('getDesignPackage')->willReturn($design);
  94. $context->expects($this->any())->method('getStoreManager')->willReturn($storeManager);
  95. $context->expects($this->once())->method('getAppState')->willReturn($appState);
  96. $maliciousCode = $this->createPartialMock(\Magento\Framework\Filter\Input\MaliciousCode::class, ['filter']);
  97. $maliciousCode->expects($this->once())
  98. ->method('filter')
  99. ->with($this->equalTo($requestParamMap[1][2]))
  100. ->willReturn(self::MALICIOUS_TEXT);
  101. /** @var \Magento\Email\Block\Adminhtml\Template\Preview $preview */
  102. $preview = $this->objectManagerHelper->getObject(
  103. \Magento\Email\Block\Adminhtml\Template\Preview::class,
  104. [
  105. 'context' => $context,
  106. 'maliciousCode' => $maliciousCode,
  107. 'emailFactory' => $emailFactory
  108. ]
  109. );
  110. $this->assertEquals(self::MALICIOUS_TEXT, $preview->toHtml());
  111. }
  112. /**
  113. * Data provider
  114. *
  115. * @return array
  116. */
  117. public function toHtmlDataProvider()
  118. {
  119. return [
  120. ['data 1' => [
  121. ['type', null, ''],
  122. ['text', null, sprintf('<javascript>%s</javascript>', self::MALICIOUS_TEXT)],
  123. ['styles', null, ''],
  124. ]],
  125. ['data 2' => [
  126. ['type', null, ''],
  127. ['text', null, sprintf('<iframe>%s</iframe>', self::MALICIOUS_TEXT)],
  128. ['styles', null, ''],
  129. ]],
  130. ['data 3' => [
  131. ['type', null, ''],
  132. ['text', null, self::MALICIOUS_TEXT],
  133. ['styles', null, ''],
  134. ]],
  135. ];
  136. }
  137. }