Skip to content

Commit a61277c

Browse files
committed
MC-42160: Static CMS image urls when using a different (sub)domain for custom admin url
1 parent 0c14764 commit a61277c

File tree

3 files changed

+101
-63
lines changed

3 files changed

+101
-63
lines changed

app/code/Magento/Cms/Helper/Wysiwyg/Images.php

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Cms\Helper\Wysiwyg;
78

9+
use Exception;
10+
use InvalidArgumentException;
11+
use Magento\Backend\Helper\Data;
812
use Magento\Framework\App\Filesystem\DirectoryList;
9-
use Magento\Framework\Exception\ValidatorException;
13+
use Magento\Framework\App\Helper\AbstractHelper;
14+
use Magento\Framework\App\Helper\Context;
15+
use Magento\Framework\Escaper;
16+
use Magento\Framework\Exception\FileSystemException;
17+
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\Filesystem;
19+
use Magento\Framework\Filesystem\Directory\Read;
20+
use Magento\Framework\Filesystem\Directory\Write;
21+
use Magento\Framework\UrlInterface;
22+
use Magento\Store\Model\StoreManagerInterface;
23+
use Magento\Theme\Helper\Storage;
1024

1125
/**
1226
* Wysiwyg Images Helper.
1327
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1428
*/
15-
class Images extends \Magento\Framework\App\Helper\AbstractHelper
29+
class Images extends AbstractHelper
1630
{
1731
/**
1832
* Image directory subpath relative to media directory
@@ -23,12 +37,14 @@ class Images extends \Magento\Framework\App\Helper\AbstractHelper
2337

2438
/**
2539
* Current directory path
40+
*
2641
* @var string
2742
*/
2843
protected $_currentPath;
2944

3045
/**
3146
* Current directory URL
47+
*
3248
* @var string
3349
*/
3450
protected $_currentUrl;
@@ -41,51 +57,51 @@ class Images extends \Magento\Framework\App\Helper\AbstractHelper
4157
protected $_storeId;
4258

4359
/**
44-
* @var \Magento\Framework\Filesystem\Directory\Write
60+
* @var Write
4561
*/
4662
protected $_directory;
4763

4864
/**
4965
* Adminhtml data
5066
*
51-
* @var \Magento\Backend\Helper\Data
67+
* @var Data
5268
*/
5369
protected $_backendData;
5470

5571
/**
5672
* Store manager
5773
*
58-
* @var \Magento\Store\Model\StoreManagerInterface
74+
* @var StoreManagerInterface
5975
*/
6076
protected $_storeManager;
6177

6278
/**
6379
* String escaper
6480
*
65-
* @var \Magento\Framework\Escaper
81+
* @var Escaper
6682
*/
6783
protected $escaper;
6884

6985
/**
70-
* @var \Magento\Framework\Filesystem\Directory\Read
86+
* @var Read
7187
*/
7288
private $_readDirectory;
7389

7490
/**
7591
* Construct
7692
*
77-
* @param \Magento\Framework\App\Helper\Context $context
78-
* @param \Magento\Backend\Helper\Data $backendData
79-
* @param \Magento\Framework\Filesystem $filesystem
80-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
81-
* @param \Magento\Framework\Escaper $escaper
93+
* @param Context $context
94+
* @param Data $backendData
95+
* @param Filesystem $filesystem
96+
* @param StoreManagerInterface $storeManager
97+
* @param Escaper $escaper
8298
*/
8399
public function __construct(
84-
\Magento\Framework\App\Helper\Context $context,
85-
\Magento\Backend\Helper\Data $backendData,
86-
\Magento\Framework\Filesystem $filesystem,
87-
\Magento\Store\Model\StoreManagerInterface $storeManager,
88-
\Magento\Framework\Escaper $escaper
100+
Context $context,
101+
Data $backendData,
102+
Filesystem $filesystem,
103+
StoreManagerInterface $storeManager,
104+
Escaper $escaper
89105
) {
90106
parent::__construct($context);
91107
$this->_backendData = $backendData;
@@ -136,7 +152,7 @@ public function getStorageRootSubpath()
136152
*/
137153
public function getBaseUrl()
138154
{
139-
return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
155+
return $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
140156
}
141157

142158
/**
@@ -166,19 +182,19 @@ public function convertPathToId($path)
166182
*
167183
* @param string $id
168184
* @return string
169-
* @throws \InvalidArgumentException
185+
* @throws InvalidArgumentException
170186
*/
171187
public function convertIdToPath($id)
172188
{
173-
if ($id === \Magento\Theme\Helper\Storage::NODE_ROOT) {
189+
if ($id === Storage::NODE_ROOT) {
174190
return $this->getStorageRoot();
175191
} else {
176192
$path = $this->getStorageRoot() . $this->idDecode($id);
177193

178194
try {
179195
$this->_readDirectory->getAbsolutePath($path);
180-
} catch (\Exception $e) {
181-
throw new \InvalidArgumentException('Path is invalid');
196+
} catch (Exception $e) {
197+
throw new InvalidArgumentException('Path is invalid');
182198
}
183199

184200
return $path;
@@ -192,7 +208,7 @@ public function convertIdToPath($id)
192208
*/
193209
public function isUsingStaticUrlsAllowed()
194210
{
195-
$checkResult = (object) [];
211+
$checkResult = (object)[];
196212
$checkResult->isAllowed = false;
197213
$this->_eventManager->dispatch(
198214
'cms_wysiwyg_images_static_urls_allowed',
@@ -211,7 +227,7 @@ public function isUsingStaticUrlsAllowed()
211227
public function getImageHtmlDeclaration($filename, $renderAsTag = false)
212228
{
213229
$fileUrl = $this->getCurrentUrl() . $filename;
214-
$mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
230+
$mediaUrl = $this->_storeManager->getStore($this->_storeId)->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
215231
$mediaPath = str_replace($mediaUrl, '', $fileUrl);
216232
$directive = sprintf('{{media url="%s"}}', $mediaPath);
217233
if ($renderAsTag) {
@@ -238,7 +254,7 @@ public function getImageHtmlDeclaration($filename, $renderAsTag = false)
238254
* Return path of the root directory for startup. Also try to create target directory if it doesn't exist
239255
*
240256
* @return string
241-
* @throws \Magento\Framework\Exception\LocalizedException
257+
* @throws LocalizedException
242258
*/
243259
public function getCurrentPath()
244260
{
@@ -268,21 +284,21 @@ public function getCurrentPath()
268284
* Create subdirectory if doesn't exist
269285
*
270286
* @param string $absPath Path of subdirectory to create
271-
* @throws \Magento\Framework\Exception\LocalizedException
287+
* @throws LocalizedException
272288
*/
273289
private function createSubDirIfNotExist(string $absPath)
274290
{
275291
$relPath = $this->_directory->getRelativePath($absPath);
276292
if (!$this->_directory->isExist($relPath)) {
277293
try {
278294
$this->_directory->create($relPath);
279-
} catch (\Magento\Framework\Exception\FileSystemException $e) {
295+
} catch (FileSystemException $e) {
280296
$message = __(
281297
'Can\'t create %1 as subdirectory of %2, you might have some permission issue.',
282298
$relPath,
283299
$this->_directory->getAbsolutePath()
284300
);
285-
throw new \Magento\Framework\Exception\LocalizedException($message);
301+
throw new LocalizedException($message);
286302
}
287303
}
288304
}
@@ -299,7 +315,7 @@ public function getCurrentUrl()
299315
$mediaUrl = $this->_storeManager->getStore(
300316
$this->_storeId
301317
)->getBaseUrl(
302-
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
318+
UrlInterface::URL_TYPE_MEDIA
303319
);
304320
$this->_currentUrl = rtrim($mediaUrl . $this->_directory->getRelativePath($path), '/') . '/';
305321
}

dev/tests/integration/testsuite/Magento/Cms/Helper/Wysiwyg/ImagesTest.php

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Cms\Helper\Wysiwyg;
79

810
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\Helper\Context;
12+
use Magento\Framework\Event\ManagerInterface;
13+
use Magento\Framework\Filesystem;
14+
use Magento\TestFramework\Helper\Bootstrap;
915
use Magento\TestFramework\ObjectManager;
16+
use PHPUnit\Framework\TestCase;
17+
use stdClass;
1018

11-
class ImagesTest extends \PHPUnit\Framework\TestCase
19+
class ImagesTest extends TestCase
1220
{
1321
/**
1422
* @var ObjectManager
@@ -17,19 +25,17 @@ class ImagesTest extends \PHPUnit\Framework\TestCase
1725

1826
protected function setUp(): void
1927
{
20-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
28+
$this->objectManager = Bootstrap::getObjectManager();
2129
}
2230

2331
public function testGetStorageRoot()
2432
{
25-
/** @var \Magento\Framework\Filesystem $filesystem */
26-
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
27-
\Magento\Framework\Filesystem::class
28-
);
33+
/** @var Filesystem $filesystem */
34+
$filesystem = Bootstrap::getObjectManager()->get(Filesystem::class);
2935
$mediaPath = $filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
30-
/** @var \Magento\Cms\Helper\Wysiwyg\Images $helper */
36+
/** @var Images $helper */
3137
$helper = $this->objectManager->create(
32-
\Magento\Cms\Helper\Wysiwyg\Images::class
38+
Images::class
3339
);
3440
$this->assertStringStartsWith($mediaPath, $helper->getStorageRoot());
3541
}
@@ -39,10 +45,9 @@ public function testGetStorageRoot()
3945
*/
4046
public function testGetCurrentUrl()
4147
{
42-
/** @var \Magento\Cms\Helper\Wysiwyg\Images $helper */
43-
$helper = $this->objectManager->create(
44-
\Magento\Cms\Helper\Wysiwyg\Images::class
45-
);
48+
/** @var Images $helper */
49+
$helper = $this->objectManager->create(Images::class);
50+
4651
$this->assertStringStartsWith('http://example.com/', $helper->getCurrentUrl());
4752
}
4853

@@ -74,6 +79,20 @@ public function testGetImageHtmlDeclaration(
7479
}
7580
}
7681

82+
/**
83+
* Verify image html declaration renders correctly with custom admin url.
84+
*
85+
* @magentoConfigFixture default/admin/url/use_custom 1
86+
* @magentoConfigFixture admin_store web/unsecure/base_url http://example-admin.com/
87+
*/
88+
public function testGetImageHtmlDeclarationCustomBackendUrl()
89+
{
90+
$helper = $this->generateHelper(false, 0);
91+
$actualResult = $helper->getImageHtmlDeclaration('wysiwyg/hello.png', true);
92+
93+
$this->assertEquals('<img src="{{media url=&quot;wysiwyg/hello.png&quot;}}" alt="" />', $actualResult);
94+
}
95+
7796
/**
7897
* Data provider for testGetImageHtmlDeclaration
7998
*
@@ -94,7 +113,7 @@ function ($actualResult) {
94113
);
95114

96115
$this->assertStringContainsString($expectedResult, parse_url($actualResult, PHP_URL_PATH));
97-
}
116+
},
98117
],
99118
[true, 'wysiwyg/hello.png', false, 'http://example.com/media/wysiwyg/hello.png'],
100119
[false, 'wysiwyg/hello.png', true, '<img src="{{media url=&quot;wysiwyg/hello.png&quot;}}" alt="" />'],
@@ -105,24 +124,23 @@ function ($actualResult) {
105124
* Generate instance of Images Helper
106125
*
107126
* @param bool $isStaticUrlsAllowed - mock is created to override value of isUsingStaticUrlsAllowed method in class
108-
* @return \Magento\Cms\Helper\Wysiwyg\Images
127+
* @param int $storeId
128+
* @return Images
109129
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
110130
*/
111-
private function generateHelper($isStaticUrlsAllowed = false)
131+
private function generateHelper($isStaticUrlsAllowed = false, int $storeId = 1)
112132
{
113-
$storeId = 1;
114-
115-
$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
133+
$eventManagerMock = $this->createMock(ManagerInterface::class);
116134

117-
$contextMock = $this->objectManager->create(\Magento\Framework\App\Helper\Context::class, [
135+
$contextMock = $this->objectManager->create(Context::class, [
118136
'eventManager' => $eventManagerMock,
119137
]);
120138

121-
$helper = $this->objectManager->create(\Magento\Cms\Helper\Wysiwyg\Images::class, [
122-
'context' => $contextMock
139+
$helper = $this->objectManager->create(Images::class, [
140+
'context' => $contextMock,
123141
]);
124142

125-
$checkResult = new \stdClass();
143+
$checkResult = new stdClass();
126144
$checkResult->isAllowed = false;
127145

128146
$eventManagerMock->expects($this->any())

0 commit comments

Comments
 (0)