Skip to content

Commit ab4fa33

Browse files
committed
Refactor and unit test for issue 25893
1 parent 66bf8d1 commit ab4fa33

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Directive.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
use Magento\Cms\Model\Template\Filter;
1414
use Magento\Cms\Model\Wysiwyg\Config;
1515
use Magento\Framework\App\Action\HttpGetActionInterface;
16+
use Magento\Framework\Image\Adapter\AdapterInterface;
17+
use Magento\Framework\Image\AdapterFactory;
18+
use Psr\Log\LoggerInterface;
19+
use Magento\Framework\Url\DecoderInterface;
20+
use Magento\Framework\Controller\Result\Raw;
21+
use Magento\Framework\Controller\Result\RawFactory;
22+
use Magento\Backend\App\Action\Context;
23+
use Magento\Framework\App\ObjectManager;
1624

1725
/**
1826
* Process template text for wysiwyg editor.
@@ -30,61 +38,93 @@ class Directive extends Action implements HttpGetActionInterface
3038
const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';
3139

3240
/**
33-
* @var \Magento\Framework\Url\DecoderInterface
41+
* @var DecoderInterface
3442
*/
3543
protected $urlDecoder;
3644

3745
/**
38-
* @var \Magento\Framework\Controller\Result\RawFactory
46+
* @var RawFactory
3947
*/
4048
protected $resultRawFactory;
4149

4250
/**
43-
* @param Action\Context $context
44-
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
45-
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
51+
* @var LoggerInterface
52+
*/
53+
private $logger;
54+
55+
/**
56+
* @var AdapterFactory
57+
*/
58+
private $adapterFactory;
59+
60+
/**
61+
* @var Config
62+
*/
63+
private $config;
64+
65+
/**
66+
* @var Filter
67+
*/
68+
private $filter;
69+
70+
/**
71+
* Constructor
72+
*
73+
* @param Context $context
74+
* @param DecoderInterface $urlDecoder
75+
* @param RawFactory $resultRawFactory
76+
* @param AdapterFactory|null $adapterFactory
77+
* @param LoggerInterface|null $logger
78+
* @param Config|null $config
79+
* @param Filter|null $filter
4680
*/
4781
public function __construct(
48-
Action\Context $context,
49-
\Magento\Framework\Url\DecoderInterface $urlDecoder,
50-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
82+
Context $context,
83+
DecoderInterface $urlDecoder,
84+
RawFactory $resultRawFactory,
85+
AdapterFactory $adapterFactory = null,
86+
LoggerInterface $logger = null,
87+
Config $config = null,
88+
Filter $filter = null
5189
) {
5290
parent::__construct($context);
5391
$this->urlDecoder = $urlDecoder;
5492
$this->resultRawFactory = $resultRawFactory;
93+
$this->adapterFactory = $adapterFactory ?: ObjectManager::getInstance()->get(AdapterFactory::class);
94+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
95+
$this->config = $config ?: ObjectManager::getInstance()->get(Config::class);
96+
$this->filter = $filter ?: ObjectManager::getInstance()->get(Filter::class);
5597
}
5698

5799
/**
58100
* Template directives callback
59101
*
60-
* @return \Magento\Framework\Controller\Result\Raw
102+
* @return Raw
61103
*/
62104
public function execute()
63105
{
64106
$directive = $this->getRequest()->getParam('___directive');
65107
$directive = $this->urlDecoder->decode($directive);
66108
try {
67109
/** @var Filter $filter */
68-
$filter = $this->_objectManager->create(Filter::class);
69-
$imagePath = $filter->filter($directive);
70-
/** @var \Magento\Framework\Image\Adapter\AdapterInterface $image */
71-
$image = $this->_objectManager->get(\Magento\Framework\Image\AdapterFactory::class)->create();
72-
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
110+
$imagePath = $this->filter->filter($directive);
111+
/** @var AdapterInterface $image */
112+
$image = $this->adapterFactory->create();
113+
/** @var Raw $resultRaw */
73114
$resultRaw = $this->resultRawFactory->create();
74115
$image->open($imagePath);
75116
$resultRaw->setHeader('Content-Type', $image->getMimeType());
76117
$resultRaw->setContents($image->getImage());
77118
} catch (\Exception $e) {
78119
/** @var Config $config */
79-
$config = $this->_objectManager->get(Config::class);
80-
$imagePath = $config->getSkinImagePlaceholderPath();
120+
$imagePath = $this->config->getSkinImagePlaceholderPath();
81121
try {
82122
$image->open($imagePath);
83123
$resultRaw->setHeader('Content-Type', $image->getMimeType());
84124
$resultRaw->setContents($image->getImage());
85-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
125+
$this->logger->warning($e);
86126
} catch (\Exception $e) {
87-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
127+
$this->logger->warning($e);
88128
}
89129
}
90130
return $resultRaw;

app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Wysiwyg/DirectiveTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ protected function setUp()
151151
[
152152
'context' => $this->actionContextMock,
153153
'urlDecoder' => $this->urlDecoderMock,
154-
'resultRawFactory' => $this->rawFactoryMock
154+
'resultRawFactory' => $this->rawFactoryMock,
155+
'adapterFactory' => $this->imageAdapterFactoryMock,
156+
'logger' => $this->loggerMock,
157+
'config' => $this->wysiwygConfigMock,
158+
'filter' => $this->templateFilterMock
155159
]
156160
);
157161
}
@@ -228,7 +232,7 @@ public function testExecuteException()
228232
->method('getImage')
229233
->willReturn($imageBody);
230234
$this->loggerMock->expects($this->once())
231-
->method('critical')
235+
->method('warning')
232236
->with($exception);
233237
$this->rawFactoryMock->expects($this->any())
234238
->method('create')
@@ -253,23 +257,12 @@ protected function prepareExecuteTest()
253257
->method('decode')
254258
->with($directiveParam)
255259
->willReturn($directive);
256-
$this->objectManagerMock->expects($this->once())
257-
->method('create')
258-
->with(\Magento\Cms\Model\Template\Filter::class)
259-
->willReturn($this->templateFilterMock);
260+
260261
$this->templateFilterMock->expects($this->once())
261262
->method('filter')
262263
->with($directive)
263264
->willReturn(self::IMAGE_PATH);
264-
$this->objectManagerMock->expects($this->any())
265-
->method('get')
266-
->willReturnMap(
267-
[
268-
[\Magento\Framework\Image\AdapterFactory::class, $this->imageAdapterFactoryMock],
269-
[\Magento\Cms\Model\Wysiwyg\Config::class, $this->wysiwygConfigMock],
270-
[\Psr\Log\LoggerInterface::class, $this->loggerMock]
271-
]
272-
);
265+
273266
$this->imageAdapterFactoryMock->expects($this->once())
274267
->method('create')
275268
->willReturn($this->imageAdapterMock);
@@ -301,7 +294,7 @@ public function testExecuteWithDeletedImage()
301294
->willThrowException($exception);
302295

303296
$this->loggerMock->expects($this->once())
304-
->method('critical')
297+
->method('warning')
305298
->with($exception);
306299

307300
$this->wysiwygDirective->execute();

0 commit comments

Comments
 (0)