Skip to content

Commit 81837a8

Browse files
committed
Merge remote-tracking branch 'origin/MC-31727' into 2.4-develop-pr15
2 parents 690500f + 8014f52 commit 81837a8

File tree

3 files changed

+110
-47
lines changed

3 files changed

+110
-47
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\Framework\Controller\Result\RawFactory;
2222
use Magento\Backend\App\Action\Context;
2323
use Magento\Framework\App\ObjectManager;
24+
use Magento\Framework\Filesystem\Driver\File;
2425

2526
/**
2627
* Process template text for wysiwyg editor.
@@ -67,6 +68,11 @@ class Directive extends Action implements HttpGetActionInterface
6768
*/
6869
private $filter;
6970

71+
/**
72+
* @var File
73+
*/
74+
private $file;
75+
7076
/**
7177
* Constructor
7278
*
@@ -77,6 +83,7 @@ class Directive extends Action implements HttpGetActionInterface
7783
* @param LoggerInterface|null $logger
7884
* @param Config|null $config
7985
* @param Filter|null $filter
86+
* @param File|null $file
8087
*/
8188
public function __construct(
8289
Context $context,
@@ -85,7 +92,8 @@ public function __construct(
8592
AdapterFactory $adapterFactory = null,
8693
LoggerInterface $logger = null,
8794
Config $config = null,
88-
Filter $filter = null
95+
Filter $filter = null,
96+
File $file = null
8997
) {
9098
parent::__construct($context);
9199
$this->urlDecoder = $urlDecoder;
@@ -94,6 +102,7 @@ public function __construct(
94102
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
95103
$this->config = $config ?: ObjectManager::getInstance()->get(Config::class);
96104
$this->filter = $filter ?: ObjectManager::getInstance()->get(Filter::class);
105+
$this->file = $file ?: ObjectManager::getInstance()->get(File::class);
97106
}
98107

99108
/**
@@ -127,6 +136,15 @@ public function execute()
127136
$this->logger->warning($e);
128137
}
129138
}
139+
$mimeType = $image->getMimeType();
140+
unset($image);
141+
// To avoid issues with PNG images with alpha blending we return raw file
142+
// after validation as an image source instead of generating the new PNG image
143+
// with image adapter
144+
$content = $this->file->fileGetContents($imagePath);
145+
$resultRaw->setHeader('Content-Type', $mimeType);
146+
$resultRaw->setContents($content);
147+
130148
return $resultRaw;
131149
}
132150
}

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

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,123 @@
55
*/
66
namespace Magento\Cms\Test\Unit\Controller\Adminhtml\Wysiwyg;
77

8+
use Magento\Backend\App\Action\Context;
9+
use Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive;
10+
use Magento\Cms\Model\Template\Filter;
11+
use Magento\Cms\Model\Wysiwyg\Config;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\ResponseInterface;
14+
use Magento\Framework\Controller\Result\Raw;
15+
use Magento\Framework\Controller\Result\RawFactory;
16+
use Magento\Framework\Filesystem\Driver\File;
17+
use Magento\Framework\Image\Adapter\AdapterInterface;
18+
use Magento\Framework\Image\AdapterFactory;
19+
use Magento\Framework\ObjectManagerInterface;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use Magento\Framework\Url\DecoderInterface;
22+
use PHPUnit\Framework\TestCase;
23+
use PHPUnit_Framework_MockObject_MockObject;
24+
use Psr\Log\LoggerInterface;
25+
826
/**
927
* @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive
1028
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1129
*/
12-
class DirectiveTest extends \PHPUnit\Framework\TestCase
30+
class DirectiveTest extends TestCase
1331
{
1432
const IMAGE_PATH = 'pub/media/wysiwyg/image.jpg';
1533

1634
/**
17-
* @var \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive
35+
* @var Directive
1836
*/
1937
protected $wysiwygDirective;
2038

2139
/**
22-
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
40+
* @var Context|PHPUnit_Framework_MockObject_MockObject
2341
*/
2442
protected $actionContextMock;
2543

2644
/**
27-
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
45+
* @var RequestInterface|PHPUnit_Framework_MockObject_MockObject
2846
*/
2947
protected $requestMock;
3048

3149
/**
32-
* @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
50+
* @var DecoderInterface|PHPUnit_Framework_MockObject_MockObject
3351
*/
3452
protected $urlDecoderMock;
3553

3654
/**
37-
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
55+
* @var ObjectManagerInterface|PHPUnit_Framework_MockObject_MockObject
3856
*/
3957
protected $objectManagerMock;
4058

4159
/**
42-
* @var \Magento\Cms\Model\Template\Filter|\PHPUnit_Framework_MockObject_MockObject
60+
* @var Filter|PHPUnit_Framework_MockObject_MockObject
4361
*/
4462
protected $templateFilterMock;
4563

4664
/**
47-
* @var \Magento\Framework\Image\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
65+
* @var AdapterFactory|PHPUnit_Framework_MockObject_MockObject
4866
*/
4967
protected $imageAdapterFactoryMock;
5068

5169
/**
52-
* @var \Magento\Framework\Image\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
70+
* @var AdapterInterface|PHPUnit_Framework_MockObject_MockObject
5371
*/
5472
protected $imageAdapterMock;
5573

5674
/**
57-
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
75+
* @var ResponseInterface|PHPUnit_Framework_MockObject_MockObject
5876
*/
5977
protected $responseMock;
6078

6179
/**
62-
* @var \Magento\Cms\Model\Wysiwyg\Config|\PHPUnit_Framework_MockObject_MockObject
80+
* @var File|PHPUnit_Framework_MockObject_MockObject
81+
*/
82+
protected $fileMock;
83+
84+
/**
85+
* @var Config|PHPUnit_Framework_MockObject_MockObject
6386
*/
6487
protected $wysiwygConfigMock;
6588

6689
/**
67-
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
90+
* @var LoggerInterface|PHPUnit_Framework_MockObject_MockObject
6891
*/
6992
protected $loggerMock;
7093

7194
/**
72-
* @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
95+
* @var RawFactory|PHPUnit_Framework_MockObject_MockObject
7396
*/
7497
protected $rawFactoryMock;
7598

7699
/**
77-
* @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
100+
* @var Raw|PHPUnit_Framework_MockObject_MockObject
78101
*/
79102
protected $rawMock;
80103

81104
protected function setUp()
82105
{
83-
$this->actionContextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
106+
$this->actionContextMock = $this->getMockBuilder(Context::class)
84107
->disableOriginalConstructor()
85108
->getMock();
86-
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
109+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
87110
->disableOriginalConstructor()
88111
->getMock();
89-
$this->urlDecoderMock = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)
112+
$this->urlDecoderMock = $this->getMockBuilder(DecoderInterface::class)
90113
->disableOriginalConstructor()
91114
->getMock();
92-
$this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
115+
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
93116
->disableOriginalConstructor()
94117
->getMock();
95-
$this->templateFilterMock = $this->getMockBuilder(\Magento\Cms\Model\Template\Filter::class)
118+
$this->templateFilterMock = $this->getMockBuilder(Filter::class)
96119
->disableOriginalConstructor()
97120
->getMock();
98-
$this->imageAdapterFactoryMock = $this->getMockBuilder(\Magento\Framework\Image\AdapterFactory::class)
121+
$this->imageAdapterFactoryMock = $this->getMockBuilder(AdapterFactory::class)
99122
->disableOriginalConstructor()
100123
->getMock();
101-
$this->imageAdapterMock = $this->getMockBuilder(\Magento\Framework\Image\Adapter\AdapterInterface::class)
124+
$this->imageAdapterMock = $this->getMockBuilder(AdapterInterface::class)
102125
->disableOriginalConstructor()
103126
->setMethods(
104127
[
@@ -117,21 +140,25 @@ protected function setUp()
117140
]
118141
)
119142
->getMock();
120-
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
143+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
121144
->disableOriginalConstructor()
122145
->setMethods(['setHeader', 'setBody', 'sendResponse'])
123146
->getMock();
124-
$this->wysiwygConfigMock = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
147+
$this->fileMock = $this->getMockBuilder(File::class)
125148
->disableOriginalConstructor()
149+
->setMethods(['fileGetContents'])
126150
->getMock();
127-
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
151+
$this->wysiwygConfigMock = $this->getMockBuilder(Config::class)
128152
->disableOriginalConstructor()
129153
->getMock();
130-
$this->rawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
154+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
155+
->disableOriginalConstructor()
156+
->getMock();
157+
$this->rawFactoryMock = $this->getMockBuilder(RawFactory::class)
131158
->setMethods(['create'])
132159
->disableOriginalConstructor()
133160
->getMock();
134-
$this->rawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
161+
$this->rawMock = $this->getMockBuilder(Raw::class)
135162
->disableOriginalConstructor()
136163
->getMock();
137164

@@ -145,17 +172,18 @@ protected function setUp()
145172
->method('getObjectManager')
146173
->willReturn($this->objectManagerMock);
147174

148-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
175+
$objectManager = new ObjectManager($this);
149176
$this->wysiwygDirective = $objectManager->getObject(
150-
\Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::class,
177+
Directive::class,
151178
[
152179
'context' => $this->actionContextMock,
153180
'urlDecoder' => $this->urlDecoderMock,
154181
'resultRawFactory' => $this->rawFactoryMock,
155182
'adapterFactory' => $this->imageAdapterFactoryMock,
156183
'logger' => $this->loggerMock,
157184
'config' => $this->wysiwygConfigMock,
158-
'filter' => $this->templateFilterMock
185+
'filter' => $this->templateFilterMock,
186+
'file' => $this->fileMock,
159187
]
160188
);
161189
}
@@ -172,23 +200,29 @@ public function testExecute()
172200
$this->imageAdapterMock->expects($this->once())
173201
->method('open')
174202
->with(self::IMAGE_PATH);
175-
$this->imageAdapterMock->expects($this->once())
203+
$this->imageAdapterMock->expects($this->atLeastOnce())
176204
->method('getMimeType')
177205
->willReturn($mimeType);
178-
$this->rawMock->expects($this->once())
206+
$this->rawMock->expects($this->atLeastOnce())
179207
->method('setHeader')
180208
->with('Content-Type', $mimeType)
181209
->willReturnSelf();
182-
$this->rawMock->expects($this->once())
210+
$this->rawMock->expects($this->atLeastOnce())
183211
->method('setContents')
184212
->with($imageBody)
185213
->willReturnSelf();
186214
$this->imageAdapterMock->expects($this->once())
187215
->method('getImage')
188216
->willReturn($imageBody);
217+
$this->fileMock->expects($this->once())
218+
->method('fileGetContents')
219+
->willReturn($imageBody);
189220
$this->rawFactoryMock->expects($this->any())
190221
->method('create')
191222
->willReturn($this->rawMock);
223+
$this->imageAdapterFactoryMock->expects($this->once())
224+
->method('create')
225+
->willReturn($this->imageAdapterMock);
192226

193227
$this->assertSame(
194228
$this->rawMock,
@@ -217,27 +251,34 @@ public function testExecuteException()
217251
$this->imageAdapterMock->expects($this->at(1))
218252
->method('open')
219253
->with($placeholderPath);
220-
$this->imageAdapterMock->expects($this->once())
254+
$this->imageAdapterMock->expects($this->atLeastOnce())
221255
->method('getMimeType')
222256
->willReturn($mimeType);
223-
$this->rawMock->expects($this->once())
257+
$this->rawMock->expects($this->atLeastOnce())
224258
->method('setHeader')
225259
->with('Content-Type', $mimeType)
226260
->willReturnSelf();
227-
$this->rawMock->expects($this->once())
261+
$this->rawMock->expects($this->atLeastOnce())
228262
->method('setContents')
229263
->with($imageBody)
230264
->willReturnSelf();
231-
$this->imageAdapterMock->expects($this->once())
265+
$this->imageAdapterMock->expects($this->any())
232266
->method('getImage')
233267
->willReturn($imageBody);
268+
$this->fileMock->expects($this->once())
269+
->method('fileGetContents')
270+
->willReturn($imageBody);
234271
$this->loggerMock->expects($this->once())
235272
->method('warning')
236273
->with($exception);
237274
$this->rawFactoryMock->expects($this->any())
238275
->method('create')
239276
->willReturn($this->rawMock);
240277

278+
$this->imageAdapterFactoryMock->expects($this->exactly(1))
279+
->method('create')
280+
->willReturn($this->imageAdapterMock);
281+
241282
$this->assertSame(
242283
$this->rawMock,
243284
$this->wysiwygDirective->execute()
@@ -297,6 +338,20 @@ public function testExecuteWithDeletedImage()
297338
->method('warning')
298339
->with($exception);
299340

341+
$this->rawMock->expects($this->once())
342+
->method('setHeader')
343+
->with('Content-Type', null)
344+
->willReturnSelf();
345+
346+
$this->rawMock->expects($this->once())
347+
->method('setContents')
348+
->with(null)
349+
->willReturnSelf();
350+
351+
$this->rawFactoryMock->expects($this->any())
352+
->method('create')
353+
->willReturn($this->rawMock);
354+
300355
$this->wysiwygDirective->execute();
301356
}
302357
}

lib/internal/Magento/Framework/Image/Adapter/Gd2.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ public function open($filename)
7575
$this->_getCallback('create', null, sprintf('Unsupported image format. File: %s', $this->_fileName)),
7676
$this->_fileName
7777
);
78-
$fileType = $this->getImageType();
79-
if (in_array($fileType, [IMAGETYPE_PNG, IMAGETYPE_GIF])) {
80-
$this->_keepTransparency = true;
81-
if ($this->_imageHandler) {
82-
$isAlpha = $this->checkAlpha($this->_fileName);
83-
if ($isAlpha) {
84-
$this->_fillBackgroundColor($this->_imageHandler);
85-
}
86-
}
87-
}
8878
}
8979

9080
/**

0 commit comments

Comments
 (0)