PreviewTest.php 5.7 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Email\Test\Unit\Block\Adminhtml\Template;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class PreviewTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
     */
    protected $objectManagerHelper;

    const MALICIOUS_TEXT = 'test malicious';

    /**
     * Init data
     */
    protected function setUp()
    {
        $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
    }

    /**
     * Check of processing email templates
     *
     * @param array $requestParamMap
     *
     * @dataProvider toHtmlDataProvider
     * @param $requestParamMap
     */
    public function testToHtml($requestParamMap)
    {
        $storeId = 1;
        $template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
            ->setMethods([
                'setDesignConfig',
                'getDesignConfig',
                '__wakeup',
                'getProcessedTemplate',
                'getAppState',
                'revertDesign'
            ])
            ->disableOriginalConstructor()
            ->getMock();
        $template->expects($this->once())
            ->method('getProcessedTemplate')
            ->with($this->equalTo([]))
            ->willReturn(self::MALICIOUS_TEXT);
        $designConfigData = [];
        $template->expects($this->atLeastOnce())
            ->method('getDesignConfig')
            ->willReturn(new \Magento\Framework\DataObject(
                $designConfigData
            ));
        $emailFactory = $this->createPartialMock(\Magento\Email\Model\TemplateFactory::class, ['create']);
        $emailFactory->expects($this->any())
            ->method('create')
            ->willReturn($template);

        $request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
        $request->expects($this->any())->method('getParam')->willReturnMap($requestParamMap);
        $eventManage = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
        $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
        $design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
        $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
        $store->expects($this->any())->method('getId')->willReturn($storeId);
        $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $storeManager->expects($this->atLeastOnce())
            ->method('getDefaultStoreView')
            ->willReturn($store);
        $storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
        $storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
        $appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
            ->setConstructorArgs([
                $scopeConfig
            ])
            ->setMethods(['emulateAreaCode'])
            ->disableOriginalConstructor()
            ->getMock();
        $appState->expects($this->any())
            ->method('emulateAreaCode')
            ->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA, [$template, 'getProcessedTemplate'])
            ->willReturn($template->getProcessedTemplate());

        $context = $this->createPartialMock(
            \Magento\Backend\Block\Template\Context::class,
            ['getRequest', 'getEventManager', 'getScopeConfig', 'getDesignPackage', 'getStoreManager', 'getAppState']
        );
        $context->expects($this->any())->method('getRequest')->willReturn($request);
        $context->expects($this->any())->method('getEventManager')->willReturn($eventManage);
        $context->expects($this->any())->method('getScopeConfig')->willReturn($scopeConfig);
        $context->expects($this->any())->method('getDesignPackage')->willReturn($design);
        $context->expects($this->any())->method('getStoreManager')->willReturn($storeManager);
        $context->expects($this->once())->method('getAppState')->willReturn($appState);

        $maliciousCode = $this->createPartialMock(\Magento\Framework\Filter\Input\MaliciousCode::class, ['filter']);
        $maliciousCode->expects($this->once())
            ->method('filter')
            ->with($this->equalTo($requestParamMap[1][2]))
            ->willReturn(self::MALICIOUS_TEXT);

        /** @var \Magento\Email\Block\Adminhtml\Template\Preview $preview */
        $preview = $this->objectManagerHelper->getObject(
            \Magento\Email\Block\Adminhtml\Template\Preview::class,
            [
                'context' => $context,
                'maliciousCode' => $maliciousCode,
                'emailFactory' => $emailFactory
            ]
        );
        $this->assertEquals(self::MALICIOUS_TEXT, $preview->toHtml());
    }

    /**
     * Data provider
     *
     * @return array
     */
    public function toHtmlDataProvider()
    {
        return [
            ['data 1' => [
                ['type', null, ''],
                ['text', null, sprintf('<javascript>%s</javascript>', self::MALICIOUS_TEXT)],
                ['styles', null, ''],
            ]],
            ['data 2' => [
                ['type', null, ''],
                ['text', null, sprintf('<iframe>%s</iframe>', self::MALICIOUS_TEXT)],
                ['styles', null, ''],
            ]],
            ['data 3' => [
                ['type', null, ''],
                ['text', null, self::MALICIOUS_TEXT],
                ['styles', null, ''],
            ]],
        ];
    }
}