Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve A "500 (Internal Server Error)" appears in Developer Console if Delete the image that is added to Page Content issue25893 #25924

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 69 additions & 20 deletions app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Cms\Controller\Adminhtml\Wysiwyg;

use Magento\Backend\App\Action;
use Magento\Cms\Model\Template\Filter;
use Magento\Cms\Model\Wysiwyg\Config;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Image\Adapter\AdapterInterface;
use Magento\Framework\Image\AdapterFactory;
use Psr\Log\LoggerInterface;
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ObjectManager;

/**
* Process template text for wysiwyg editor.
*
* Class Directive
*/
class Directive extends Action implements HttpGetActionInterface
{
Expand All @@ -25,58 +38,94 @@ class Directive extends Action implements HttpGetActionInterface
const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';

/**
* @var \Magento\Framework\Url\DecoderInterface
* @var DecoderInterface
*/
protected $urlDecoder;

/**
* @var \Magento\Framework\Controller\Result\RawFactory
* @var RawFactory
*/
protected $resultRawFactory;

/**
* @param Action\Context $context
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @var LoggerInterface
*/
private $logger;

/**
* @var AdapterFactory
*/
private $adapterFactory;

/**
* @var Config
*/
private $config;

/**
* @var Filter
*/
private $filter;

/**
* Constructor
*
* @param Context $context
* @param DecoderInterface $urlDecoder
* @param RawFactory $resultRawFactory
* @param AdapterFactory|null $adapterFactory
* @param LoggerInterface|null $logger
* @param Config|null $config
* @param Filter|null $filter
*/
public function __construct(
Action\Context $context,
\Magento\Framework\Url\DecoderInterface $urlDecoder,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
Context $context,
DecoderInterface $urlDecoder,
RawFactory $resultRawFactory,
AdapterFactory $adapterFactory = null,
LoggerInterface $logger = null,
Config $config = null,
Filter $filter = null
) {
parent::__construct($context);
$this->urlDecoder = $urlDecoder;
$this->resultRawFactory = $resultRawFactory;
$this->adapterFactory = $adapterFactory ?: ObjectManager::getInstance()->get(AdapterFactory::class);
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
$this->config = $config ?: ObjectManager::getInstance()->get(Config::class);
$this->filter = $filter ?: ObjectManager::getInstance()->get(Filter::class);
}

/**
* Template directives callback
*
* @return \Magento\Framework\Controller\Result\Raw
* @return Raw
*/
public function execute()
{
$directive = $this->getRequest()->getParam('___directive');
$directive = $this->urlDecoder->decode($directive);
try {
/** @var Filter $filter */
$filter = $this->_objectManager->create(Filter::class);
$imagePath = $filter->filter($directive);
/** @var \Magento\Framework\Image\Adapter\AdapterInterface $image */
$image = $this->_objectManager->get(\Magento\Framework\Image\AdapterFactory::class)->create();
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$imagePath = $this->filter->filter($directive);
/** @var AdapterInterface $image */
$image = $this->adapterFactory->create();
/** @var Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
$image->open($imagePath);
$resultRaw->setHeader('Content-Type', $image->getMimeType());
$resultRaw->setContents($image->getImage());
} catch (\Exception $e) {
/** @var Config $config */
$config = $this->_objectManager->get(Config::class);
$imagePath = $config->getSkinImagePlaceholderPath();
$image->open($imagePath);
$resultRaw->setHeader('Content-Type', $image->getMimeType());
$resultRaw->setContents($image->getImage());
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$imagePath = $this->config->getSkinImagePlaceholderPath();
try {
$image->open($imagePath);
$resultRaw->setHeader('Content-Type', $image->getMimeType());
$resultRaw->setContents($image->getImage());
$this->logger->warning($e);
} catch (\Exception $e) {
$this->logger->warning($e);
}
}
return $resultRaw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ protected function setUp()
[
'context' => $this->actionContextMock,
'urlDecoder' => $this->urlDecoderMock,
'resultRawFactory' => $this->rawFactoryMock
'resultRawFactory' => $this->rawFactoryMock,
'adapterFactory' => $this->imageAdapterFactoryMock,
'logger' => $this->loggerMock,
'config' => $this->wysiwygConfigMock,
'filter' => $this->templateFilterMock
]
);
}
Expand Down Expand Up @@ -228,7 +232,7 @@ public function testExecuteException()
->method('getImage')
->willReturn($imageBody);
$this->loggerMock->expects($this->once())
->method('critical')
->method('warning')
->with($exception);
$this->rawFactoryMock->expects($this->any())
->method('create')
Expand All @@ -253,25 +257,46 @@ protected function prepareExecuteTest()
->method('decode')
->with($directiveParam)
->willReturn($directive);
$this->objectManagerMock->expects($this->once())
->method('create')
->with(\Magento\Cms\Model\Template\Filter::class)
->willReturn($this->templateFilterMock);

$this->templateFilterMock->expects($this->once())
->method('filter')
->with($directive)
->willReturn(self::IMAGE_PATH);
$this->objectManagerMock->expects($this->any())
->method('get')
->willReturnMap(
[
[\Magento\Framework\Image\AdapterFactory::class, $this->imageAdapterFactoryMock],
[\Magento\Cms\Model\Wysiwyg\Config::class, $this->wysiwygConfigMock],
[\Psr\Log\LoggerInterface::class, $this->loggerMock]
]
);

$this->imageAdapterFactoryMock->expects($this->once())
->method('create')
->willReturn($this->imageAdapterMock);
}

/**
* Test Execute With Deleted Image
*
* @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::execute
*/
public function testExecuteWithDeletedImage()
{
$exception = new \Exception('epic fail');
$placeholderPath = 'pub/static/adminhtml/Magento/backend/en_US/Magento_Cms/images/wysiwyg_skin_image.png';
$this->prepareExecuteTest();

$this->imageAdapterMock->expects($this->any())
->method('open')
->with(self::IMAGE_PATH)
->willThrowException($exception);

$this->wysiwygConfigMock->expects($this->once())
->method('getSkinImagePlaceholderPath')
->willReturn($placeholderPath);

$this->imageAdapterMock->expects($this->any())
->method('open')
->with($placeholderPath)
->willThrowException($exception);

$this->loggerMock->expects($this->once())
->method('warning')
->with($exception);

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