Skip to content

Commit a11a8c2

Browse files
author
Viktor Sevch
committed
MC-38254: Minor changes in downloadcss controller
1 parent 46822f2 commit a11a8c2

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

app/code/Magento/Theme/Controller/Adminhtml/System/Design/Theme/DownloadCss.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
*/
77
namespace Magento\Theme\Controller\Adminhtml\System\Design\Theme;
88

9+
use Magento\Backend\App\Action\Context;
910
use Magento\Framework\App\Action\HttpGetActionInterface;
11+
use Magento\Framework\App\Response\Http\FileFactory;
1012
use Magento\Framework\App\ResponseInterface;
1113
use Magento\Framework\App\Filesystem\DirectoryList;
12-
use Magento\Theme\Controller\Adminhtml\System\Design\Theme;
14+
use Magento\Framework\Escaper;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Registry;
1317
use Magento\Framework\Url\DecoderInterface;
18+
use Magento\Framework\View\Asset\Repository;
1419
use Magento\Framework\View\Design\ThemeInterface;
20+
use Magento\Theme\Controller\Adminhtml\System\Design\Theme;
1521
use Psr\Log\LoggerInterface;
1622

1723
/**
@@ -22,6 +28,32 @@
2228
*/
2329
class DownloadCss extends Theme implements HttpGetActionInterface
2430
{
31+
/**
32+
* @var Escaper
33+
*/
34+
private $escaper;
35+
36+
/**
37+
* DownloadCss constructor.
38+
* @param Context $context
39+
* @param Registry $coreRegistry
40+
* @param FileFactory $fileFactory
41+
* @param Repository $assetRepo
42+
* @param Filesystem $appFileSystem
43+
* @param Escaper|null $escaper
44+
*/
45+
public function __construct(
46+
Context $context,
47+
Registry $coreRegistry,
48+
FileFactory $fileFactory,
49+
Repository $assetRepo,
50+
Filesystem $appFileSystem,
51+
Escaper $escaper = null
52+
) {
53+
$this->escaper = $escaper ?? $context->getObjectManager()->get(Escaper::class);
54+
parent::__construct($context, $coreRegistry, $fileFactory, $assetRepo, $appFileSystem);
55+
}
56+
2557
/**
2658
* Download css file
2759
*
@@ -54,11 +86,11 @@ public function execute()
5486
DirectoryList::ROOT
5587
);
5688
} catch (\InvalidArgumentException $e) {
57-
$this->messageManager->addException($e, sprintf('Theme not found: "%d".', $themeId));
89+
$this->messageManager->addException($e, __('Theme not found: "%1".', $this->escaper->escapeHtml($themeId)));
5890
$this->getResponse()->setRedirect($this->_redirect->getRefererUrl());
5991
$this->_objectManager->get(LoggerInterface::class)->critical($e);
6092
} catch (\Exception $e) {
61-
$this->messageManager->addException($e, sprintf('File not found: "%d".', $fileId));
93+
$this->messageManager->addException($e, __('File not found: "%1".', $this->escaper->escapeHtml($fileId)));
6294
$this->getResponse()->setRedirect($this->_redirect->getRefererUrl());
6395
$this->_objectManager->get(LoggerInterface::class)->critical($e);
6496
}

app/code/Magento/Theme/Test/Unit/Controller/Adminhtml/System/Design/Theme/DownloadCssTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\App\Response\RedirectInterface;
1515
use Magento\Framework\App\ResponseInterface;
1616
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Escaper;
1718
use Magento\Framework\Filesystem;
1819
use Magento\Framework\Filesystem\Directory\ReadInterface;
1920
use Magento\Framework\Message\ManagerInterface;
@@ -88,6 +89,11 @@ class DownloadCssTest extends TestCase
8889
*/
8990
protected $controller;
9091

92+
/**
93+
* @var Escaper|MockObject
94+
*/
95+
private $escaperMock;
96+
9197
protected function setUp(): void
9298
{
9399
$context = $this->getMockBuilder(Context::class)
@@ -123,14 +129,18 @@ protected function setUp(): void
123129
$this->filesystem = $this->getMockBuilder(Filesystem::class)
124130
->disableOriginalConstructor()
125131
->getMock();
132+
$this->escaperMock = $this->getMockBuilder(Escaper::class)
133+
->disableOriginalConstructor()
134+
->getMock();
126135

127136
/** @var Context $context */
128137
$this->controller = new DownloadCss(
129138
$context,
130139
$this->registry,
131140
$this->fileFactory,
132141
$this->repository,
133-
$this->filesystem
142+
$this->filesystem,
143+
$this->escaperMock
134144
);
135145
}
136146

@@ -184,7 +194,7 @@ public function testExecute()
184194
->method('create')
185195
->with($relPath, ['type' => 'filename', 'value' => $relPath], DirectoryList::ROOT)
186196
->willReturn($this->getMockBuilder(ResponseInterface::class)
187-
->getMock());
197+
->getMock());
188198

189199
$this->assertInstanceOf(ResponseInterface::class, $this->controller->execute());
190200
}
@@ -231,6 +241,7 @@ public function testExecuteInvalidArgument()
231241
$logger->expects($this->once())->method('critical');
232242
$this->redirect->expects($this->once())->method('getRefererUrl')->willReturn($refererUrl);
233243
$this->response->expects($this->once())->method('setRedirect')->with($refererUrl);
244+
$this->escaperMock->expects($this->once())->method('escapeHtml')->with($themeId)->willReturn($themeId);
234245

235246
$this->controller->execute();
236247
}

0 commit comments

Comments
 (0)