Skip to content

Commit a35e311

Browse files
ENGCOM-6442: Resolve A "500 (Internal Server Error)" appears in Developer Console if Delete the image that is added to Page Content issue25893 #25924
2 parents 593a9e2 + f6ad757 commit a35e311

File tree

2 files changed

+109
-35
lines changed

2 files changed

+109
-35
lines changed

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

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg;
811

912
use Magento\Backend\App\Action;
1013
use Magento\Cms\Model\Template\Filter;
1114
use Magento\Cms\Model\Wysiwyg\Config;
1215
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;
1324

1425
/**
1526
* Process template text for wysiwyg editor.
27+
*
28+
* Class Directive
1629
*/
1730
class Directive extends Action implements HttpGetActionInterface
1831
{
@@ -25,58 +38,94 @@ class Directive extends Action implements HttpGetActionInterface
2538
const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';
2639

2740
/**
28-
* @var \Magento\Framework\Url\DecoderInterface
41+
* @var DecoderInterface
2942
*/
3043
protected $urlDecoder;
3144

3245
/**
33-
* @var \Magento\Framework\Controller\Result\RawFactory
46+
* @var RawFactory
3447
*/
3548
protected $resultRawFactory;
3649

3750
/**
38-
* @param Action\Context $context
39-
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
40-
* @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
4180
*/
4281
public function __construct(
43-
Action\Context $context,
44-
\Magento\Framework\Url\DecoderInterface $urlDecoder,
45-
\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
4689
) {
4790
parent::__construct($context);
4891
$this->urlDecoder = $urlDecoder;
4992
$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);
5097
}
5198

5299
/**
53100
* Template directives callback
54101
*
55-
* @return \Magento\Framework\Controller\Result\Raw
102+
* @return Raw
56103
*/
57104
public function execute()
58105
{
59106
$directive = $this->getRequest()->getParam('___directive');
60107
$directive = $this->urlDecoder->decode($directive);
61108
try {
62109
/** @var Filter $filter */
63-
$filter = $this->_objectManager->create(Filter::class);
64-
$imagePath = $filter->filter($directive);
65-
/** @var \Magento\Framework\Image\Adapter\AdapterInterface $image */
66-
$image = $this->_objectManager->get(\Magento\Framework\Image\AdapterFactory::class)->create();
67-
/** @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 */
68114
$resultRaw = $this->resultRawFactory->create();
69115
$image->open($imagePath);
70116
$resultRaw->setHeader('Content-Type', $image->getMimeType());
71117
$resultRaw->setContents($image->getImage());
72118
} catch (\Exception $e) {
73119
/** @var Config $config */
74-
$config = $this->_objectManager->get(Config::class);
75-
$imagePath = $config->getSkinImagePlaceholderPath();
76-
$image->open($imagePath);
77-
$resultRaw->setHeader('Content-Type', $image->getMimeType());
78-
$resultRaw->setContents($image->getImage());
79-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
120+
$imagePath = $this->config->getSkinImagePlaceholderPath();
121+
try {
122+
$image->open($imagePath);
123+
$resultRaw->setHeader('Content-Type', $image->getMimeType());
124+
$resultRaw->setContents($image->getImage());
125+
$this->logger->warning($e);
126+
} catch (\Exception $e) {
127+
$this->logger->warning($e);
128+
}
80129
}
81130
return $resultRaw;
82131
}

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

Lines changed: 40 additions & 15 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,25 +257,46 @@ 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);
276269
}
270+
271+
/**
272+
* Test Execute With Deleted Image
273+
*
274+
* @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::execute
275+
*/
276+
public function testExecuteWithDeletedImage()
277+
{
278+
$exception = new \Exception('epic fail');
279+
$placeholderPath = 'pub/static/adminhtml/Magento/backend/en_US/Magento_Cms/images/wysiwyg_skin_image.png';
280+
$this->prepareExecuteTest();
281+
282+
$this->imageAdapterMock->expects($this->any())
283+
->method('open')
284+
->with(self::IMAGE_PATH)
285+
->willThrowException($exception);
286+
287+
$this->wysiwygConfigMock->expects($this->once())
288+
->method('getSkinImagePlaceholderPath')
289+
->willReturn($placeholderPath);
290+
291+
$this->imageAdapterMock->expects($this->any())
292+
->method('open')
293+
->with($placeholderPath)
294+
->willThrowException($exception);
295+
296+
$this->loggerMock->expects($this->once())
297+
->method('warning')
298+
->with($exception);
299+
300+
$this->wysiwygDirective->execute();
301+
}
277302
}

0 commit comments

Comments
 (0)