Skip to content

Commit 61c16f5

Browse files
committed
MC-41618: Background image in description is removed when saving the category page.
- Fix page-builder config.media_url is resolved in admin scope instead of default store view
1 parent b958b56 commit 61c16f5

File tree

4 files changed

+142
-123
lines changed

4 files changed

+142
-123
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image/Upload.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\Framework\Filesystem;
1212

1313
/**
14-
* Class Upload
14+
* Image upload controller
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1517
*/
1618
class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
1719
{
@@ -115,7 +117,7 @@ public function execute()
115117
}
116118

117119
$result = $fileUploader->save($this->getUploadDir());
118-
$baseUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
120+
$baseUrl = $this->_backendUrl->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]);
119121
$result['id'] = $this->cmsWysiwygImages->idEncode($result['file']);
120122
$result['url'] = $baseUrl . $this->getFilePath(self::UPLOAD_DIR, $result['file']);
121123
} catch (\Exception $e) {

app/code/Magento/PageBuilder/Model/Stage/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function getConfig()
198198
'menu_sections' => $this->getMenuSections(),
199199
'content_types' => $this->getContentTypes(),
200200
'stage_config' => $this->data,
201-
'media_url' => $this->frontendUrlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]),
201+
'media_url' => $this->urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]),
202202
'preview_url' => $this->urlBuilder->getUrl('pagebuilder/stage/preview'),
203203
'render_url' => $this->urlBuilder->getUrl('pagebuilder/stage/render'),
204204
'template_save_url' => $this->urlBuilder->getUrl('pagebuilder/template/save'),

dev/tests/integration/testsuite/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image/UploadTest.php

Lines changed: 79 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -7,160 +7,125 @@
77

88
namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
99

10-
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\Controller\Result\Json;
12-
use Magento\Framework\Controller\Result\JsonFactory;
13-
use Magento\Framework\File\Mime;
14-
use Magento\Framework\File\Uploader;
15-
use Magento\Framework\File\UploaderFactory;
16-
use Magento\Framework\ObjectManagerInterface;
17-
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as UploadController;
18-
use PHPUnit\Framework\MockObject\MockObject;
19-
use PHPUnit\Framework\TestCase;
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\Framework\Exception\FileSystemException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\DirectoryList;
14+
use Magento\TestFramework\TestCase\AbstractBackendController;
2015

21-
class UploadTest extends TestCase
16+
/**
17+
* @magentoAppArea adminhtml
18+
*/
19+
class UploadTest extends AbstractBackendController
2220
{
23-
/**
24-
* @var UploadController
25-
*/
26-
private $controller;
27-
28-
/**
29-
* @var ObjectManagerInterface
30-
*/
31-
private $objectManager;
32-
33-
/**
34-
* @var UploaderFactory|MockObject
35-
*/
36-
private $uploaderFactory;
37-
38-
/**
39-
* @var Json|MockObject
40-
*/
41-
private $resultJson;
42-
43-
/**
44-
* @var JsonFactory|MockObject
45-
*/
46-
private $resultJsonFactory;
47-
48-
/**
49-
* @inheritdoc
50-
*/
51-
protected function setUp(): void
52-
{
53-
$this->objectManager = ObjectManager::getInstance();
54-
55-
$this->uploaderFactory = $this->createPartialMock(UploaderFactory::class, ['create']);
56-
57-
$this->resultJson = $this->getMockBuilder(Json::class)
58-
->setMethods(['setData'])
59-
->disableOriginalConstructor()
60-
->getMock();
61-
62-
$this->resultJsonFactory = $this->getMockBuilder(JsonFactory::class)
63-
->setMethods(['create'])
64-
->disableOriginalConstructor()
65-
->getMock();
66-
67-
$this->resultJsonFactory->expects($this->once())
68-
->method('create')
69-
->willReturn($this->resultJson);
70-
71-
$this->controller = $this->objectManager->create(UploadController::class, [
72-
'resultJsonFactory' => $this->resultJsonFactory,
73-
'uploaderFactory' => $this->uploaderFactory
74-
]);
75-
}
21+
private const URI = 'backend/pagebuilder/contentType/image_upload';
7622

7723
/**
7824
* @inheritdoc
7925
*/
8026
protected function tearDown(): void
8127
{
8228
$_FILES = [];
29+
parent::tearDown();
8330
}
8431

8532
/**
8633
* Assert that file validation passes when uploaded file has correct extension and valid mime type
87-
* @magentoAppArea adminhtml
8834
*/
8935
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
9036
{
91-
$valid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/a.png');
92-
93-
$this->setFilesGlobalMock($valid_file_pathname);
94-
$this->setUploaderMockForField('background_image');
95-
96-
$this->resultJson->expects($this->once())
97-
->method('setData')
98-
->willReturnCallback(function ($result) {
99-
$this->assertNotEquals([
100-
'error' => 'File validation failed.',
101-
'errorcode' => 0
102-
], $result);
103-
});
104-
105-
$this->controller->execute();
37+
$filePath = realpath(__DIR__ . '/../../../../_files/uploader/a.png');
38+
39+
$this->createUploadFixture($filePath);
40+
$this->makeRequest();
41+
$this->assertEquals(200, $this->getResponse()->getStatusCode());
42+
$result = json_decode($this->getResponse()->getBody(), true);
43+
$this->assertEquals(0, $result['error']);
44+
$this->assertNotEmpty($result['url']);
10645
}
10746

10847
/**
10948
* Assert that file validation fails when uploaded file has correct extension but invalid mime type
110-
* @magentoAppArea adminhtml
11149
*/
11250
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
11351
{
114-
$invalid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/not-a.png');
115-
116-
$this->setFilesGlobalMock($invalid_file_pathname);
117-
$this->setUploaderMockForField('background_image');
118-
119-
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
120-
$this->assertEquals([
52+
$filePath = realpath(__DIR__ . '/../../../../_files/uploader/not-a.png');
53+
54+
$this->createUploadFixture($filePath);
55+
$this->makeRequest();
56+
$this->assertEquals(200, $this->getResponse()->getStatusCode());
57+
$result = json_decode($this->getResponse()->getBody(), true);
58+
$this->assertEquals(
59+
[
12160
'error' => 'File validation failed.',
12261
'errorcode' => 0
123-
], $result);
124-
});
125-
126-
$this->controller->execute();
62+
],
63+
$result
64+
);
12765
}
12866

12967
/**
130-
* Initiates Uploader object for `$fieldId` and returns as a result of `UploaderFactory::create()`
68+
* Assert that file url should be based on backend base url
13169
*
132-
* @param string $fieldId
133-
* @return void
70+
* @magentoConfigFixture default_store web/unsecure/base_url http://storefront.magento.test/
71+
* @magentoConfigFixture default_store web/secure/base_url https://storefront.magento.test/
13472
*/
135-
private function setUploaderMockForField(string $fieldId): void
73+
public function testFileUrlShouldBeBaseOnBackendBaseUrl()
13674
{
137-
$uploader = $this->objectManager->create(Uploader::class, [
138-
'fileId' => $fieldId,
139-
'fileMime' => $this->objectManager->create(Mime::class),
140-
]);
75+
$filePath = realpath(__DIR__ . '/../../../../_files/uploader/a.png');
14176

142-
$this->uploaderFactory
143-
->expects($this->once())
144-
->method('create')
145-
->will($this->returnValue($uploader));
77+
$this->createUploadFixture($filePath);
78+
$this->makeRequest();
79+
$this->assertEquals(200, $this->getResponse()->getStatusCode());
80+
$result = json_decode($this->getResponse()->getBody(), true);
81+
$this->assertStringStartsWith('http://localhost/media/', $result['url']);
14682
}
14783

14884
/**
149-
* Mock that `$pathname` was uploaded (mock of `$_FILES` array)
85+
* Initiates request
15086
*
151-
* @param string $pathname
152-
* @return void
87+
* @param string $fileParam
15388
*/
154-
private function setFilesGlobalMock(string $pathname): void
89+
private function makeRequest(string $fileParam = 'image'): void
15590
{
91+
$this->getRequest()
92+
->setParams(['param_name' => $fileParam])
93+
->setMethod(HttpRequest::METHOD_POST);
94+
$this->dispatch(self::URI);
95+
}
96+
97+
/**
98+
* Creates a fixture for testing uploaded file
99+
*
100+
* @param string $filePath
101+
* @param string $fileType
102+
* @param string $fileParam
103+
* @return void
104+
* @throws FileSystemException
105+
*/
106+
private function createUploadFixture(
107+
string $filePath,
108+
string $fileType = 'image/png',
109+
string $fileParam = 'image'
110+
): void {
111+
$filename = basename($filePath);
112+
$filesize = filesize($filePath);
113+
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
114+
$filesystem = $this->_objectManager->get(Filesystem::class);
115+
$tmpDir = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
116+
// phpcs:ignore
117+
$subDir = md5(get_class($this));
118+
$tmpDir->create($subDir);
119+
$tmpPath = $tmpDir->getAbsolutePath("{$subDir}/{$filename}");
120+
copy($filePath, $tmpPath);
156121
$_FILES = [
157-
'background_image' => [
158-
'type' => 'image/png',
159-
'name' => basename($pathname),
160-
'tmp_name' => $pathname,
161-
'size' => filesize($pathname),
122+
$fileParam => [
123+
'type' => $fileType,
124+
'name' => $filename,
125+
'tmp_name' => $tmpPath,
126+
'size' => $filesize,
162127
'error' => UPLOAD_ERR_OK,
163-
]
128+
],
164129
];
165130
}
166131
}

dev/tests/integration/testsuite/Magento/PageBuilder/Model/Stage/ConfigTest.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Magento\PageBuilder\Model\Stage;
1010

1111
use Magento\Framework\Session\SessionManagerInterface;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
1214
use Magento\TestFramework\Helper\Bootstrap;
1315
use PHPUnit\Framework\TestCase;
1416

@@ -25,28 +27,78 @@ class ConfigTest extends TestCase
2527
*/
2628
private $model;
2729

30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
private $storeManager;
34+
35+
/**
36+
* @var int
37+
*/
38+
private $currentStoreId;
39+
2840
/**
2941
* @inheritDoc
3042
*/
3143
protected function setUp(): void
3244
{
3345
$objectManager = Bootstrap::getObjectManager();
3446
$this->model = $objectManager->get(Config::class);
47+
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
48+
$this->currentStoreId = $this->storeManager->getStore()->getId();
3549
}
3650

3751
/**
38-
* Test that "media_url" should be the same as storefront media URL
52+
* @inheritdoc
53+
*/
54+
protected function tearDown(): void
55+
{
56+
$this->storeManager->setCurrentStore($this->currentStoreId);
57+
58+
parent::tearDown();
59+
}
60+
61+
/**
62+
* Test that "media_url" should be the same as backend media URL
3963
*
64+
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
65+
* @magentoConfigFixture admin/url/use_custom_path 1
66+
* @magentoConfigFixture admin/url/custom_path secret
4067
* @magentoConfigFixture admin/url/use_custom 1
4168
* @magentoConfigFixture admin/url/custom https://backend.magento.test/
4269
* @magentoConfigFixture admin_store web/secure/base_url https://backend.magento.test/
43-
* @magentoConfigFixture admin_store web/unsecure/base_url https://backend.magento.test/
44-
* @magentoConfigFixture admin/url/use_custom_path 1
45-
* @magentoConfigFixture admin/url/custom_path secret
70+
* @magentoConfigFixture admin_store web/unsecure/base_url http://backend.magento.test/
71+
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://website2.magento.test/
72+
* @magentoConfigFixture fixture_second_store_store web/secure/base_url https://website2.magento.test/
73+
* @param string $store
74+
* @param string $mediaUrl
75+
* @dataProvider storeDataProvider
76+
*/
77+
public function testMediaUrlShouldBeTheSameAsBackendMediaURL(string $store, string $mediaUrl): void
78+
{
79+
$this->storeManager->setCurrentStore($store);
80+
$this->assertEquals($mediaUrl, $this->model->getConfig()['media_url']);
81+
}
82+
83+
/**
84+
* @return array
4685
*/
47-
public function testMediaUrlShouldBeTheSameAsStorefrontMediaURL(): void
86+
public function storeDataProvider(): array
4887
{
49-
$this->assertEquals('http://localhost/media/', $this->model->getConfig()['media_url']);
88+
return [
89+
[
90+
'admin',
91+
'http://backend.magento.test/media/'
92+
],
93+
[
94+
'default',
95+
'http://backend.magento.test/media/'
96+
],
97+
[
98+
'fixture_second_store',
99+
'http://backend.magento.test/media/'
100+
],
101+
];
50102
}
51103

52104
/**

0 commit comments

Comments
 (0)