-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MC-32273: Part of URL is missed after saving category image
- Loading branch information
1 parent
a66a2d5
commit 6fc1e3c
Showing
9 changed files
with
357 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Category; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\UrlInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Category Image Service | ||
*/ | ||
class Image | ||
{ | ||
private const ATTRIBUTE_NAME = 'image'; | ||
/** | ||
* @var FileInfo | ||
*/ | ||
private $fileInfo; | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* Initialize dependencies. | ||
* | ||
* @param FileInfo $fileInfo | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
FileInfo $fileInfo, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->fileInfo = $fileInfo; | ||
$this->storeManager = $storeManager; | ||
} | ||
/** | ||
* Resolve category image URL | ||
* | ||
* @param Category $category | ||
* @param string $attributeCode | ||
* @return string | ||
* @throws LocalizedException | ||
*/ | ||
public function getUrl(Category $category, string $attributeCode = self::ATTRIBUTE_NAME): string | ||
{ | ||
$url = ''; | ||
$image = $category->getData($attributeCode); | ||
if ($image) { | ||
if (is_string($image)) { | ||
$store = $this->storeManager->getStore(); | ||
$mediaBaseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); | ||
if ($this->fileInfo->isBeginsWithMediaDirectoryPath($image)) { | ||
$relativePath = $this->fileInfo->getRelativePathToMediaDirectory($image); | ||
$url = rtrim($mediaBaseUrl, '/') . '/' . ltrim($relativePath, '/'); | ||
} elseif (substr($image, 0, 1) !== '/') { | ||
$url = rtrim($mediaBaseUrl, '/') . '/' . ltrim(FileInfo::ENTITY_MEDIA_PATH, '/') . '/' . $image; | ||
} else { | ||
$url = $image; | ||
} | ||
} else { | ||
throw new LocalizedException( | ||
__('Something went wrong while getting the image url.') | ||
); | ||
} | ||
} | ||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
app/code/Magento/Catalog/Test/Unit/Model/Category/ImageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Test\Unit\Model\Category; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\Catalog\Model\Category\FileInfo; | ||
use Magento\Catalog\Model\Category\Image; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Framework\UrlInterface; | ||
use Magento\Store\Model\Store; | ||
use Magento\Store\Model\StoreManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test category image resolver | ||
*/ | ||
class ImageTest extends TestCase | ||
{ | ||
/** | ||
* @var Store|MockObject | ||
*/ | ||
private $store; | ||
/** | ||
* @var Category | ||
*/ | ||
private $category; | ||
/** | ||
* @var Image | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$storeManager = $this->createPartialMock(StoreManager::class, ['getStore']); | ||
$this->store = $this->createPartialMock(Store::class, ['getBaseUrl']); | ||
$storeManager->method('getStore')->willReturn($this->store); | ||
$objectManager = new ObjectManager($this); | ||
$this->category = $objectManager->getObject(Category::class); | ||
$this->model = $objectManager->getObject( | ||
Image::class, | ||
[ | ||
'storeManager' => $storeManager, | ||
'fileInfo' => $this->getFileInfo() | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test that image URL resolver works correctly with different base URL format | ||
* | ||
* @param string $baseUrl | ||
* @param string $imagePath | ||
* @param string $url | ||
* @dataProvider getUrlDataProvider | ||
*/ | ||
public function testGetUrl(string $imagePath, string $baseUrl, string $url) | ||
{ | ||
$this->store->method('getBaseUrl') | ||
->with(UrlInterface::URL_TYPE_MEDIA) | ||
->willReturn($baseUrl); | ||
$this->category->setData('image_attr_code', $imagePath); | ||
$this->assertEquals($url, $this->model->getUrl($this->category, 'image_attr_code')); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getUrlDataProvider() | ||
{ | ||
return [ | ||
[ | ||
'testimage', | ||
'http://www.example.com/', | ||
'http://www.example.com/catalog/category/testimage' | ||
], | ||
[ | ||
'testimage', | ||
'http://www.example.com/pub/media/', | ||
'http://www.example.com/pub/media/catalog/category/testimage' | ||
], | ||
[ | ||
'testimage', | ||
'http://www.example.com/base/path/pub/media/', | ||
'http://www.example.com/base/path/pub/media/catalog/category/testimage' | ||
], | ||
[ | ||
'/pub/media/catalog/category/testimage', | ||
'http://www.example.com/pub/media/', | ||
'http://www.example.com/pub/media/catalog/category/testimage' | ||
], | ||
[ | ||
'/pub/media/catalog/category/testimage', | ||
'http://www.example.com/base/path/pub/media/', | ||
'http://www.example.com/base/path/pub/media/catalog/category/testimage' | ||
], | ||
[ | ||
'/pub/media/posters/testimage', | ||
'http://www.example.com/pub/media/', | ||
'http://www.example.com/pub/media/posters/testimage' | ||
], | ||
[ | ||
'/pub/media/posters/testimage', | ||
'http://www.example.com/base/path/pub/media/', | ||
'http://www.example.com/base/path/pub/media/posters/testimage' | ||
], | ||
[ | ||
'', | ||
'http://www.example.com/', | ||
'' | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Get FileInfo mock | ||
* | ||
* @return MockObject | ||
*/ | ||
private function getFileInfo(): MockObject | ||
{ | ||
$mediaDir = 'pub/media'; | ||
$fileInfo = $this->createMock(FileInfo::class); | ||
$fileInfo->method('isBeginsWithMediaDirectoryPath') | ||
->willReturnCallback( | ||
function ($path) use ($mediaDir) { | ||
return strpos(ltrim($path, '/'), $mediaDir) === 0; | ||
} | ||
); | ||
$fileInfo->method('getRelativePathToMediaDirectory') | ||
->willReturnCallback( | ||
function ($path) use ($mediaDir) { | ||
return str_replace($mediaDir, '', $path); | ||
} | ||
); | ||
return $fileInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\ViewModel\Category; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\Catalog\Model\Category\Image as CategoryImage; | ||
use Magento\Framework\View\Element\Block\ArgumentInterface; | ||
|
||
/** | ||
* Category image view model | ||
*/ | ||
class Image implements ArgumentInterface | ||
{ | ||
private const ATTRIBUTE_NAME = 'image'; | ||
/** | ||
* @var CategoryImage | ||
*/ | ||
private $image; | ||
|
||
/** | ||
* Initialize dependencies. | ||
* | ||
* @param CategoryImage $image | ||
*/ | ||
public function __construct(CategoryImage $image) | ||
{ | ||
$this->image = $image; | ||
} | ||
|
||
/** | ||
* Resolve category image URL | ||
* | ||
* @param Category $category | ||
* @param string $attributeCode | ||
* @return string | ||
*/ | ||
public function getUrl(Category $category, string $attributeCode = self::ATTRIBUTE_NAME): string | ||
{ | ||
return $this->image->getUrl($category, $attributeCode); | ||
} | ||
} |
Oops, something went wrong.
6fc1e3c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
We are upgrade magento 2.3 into magento 2.4 , in the category page getting same error.
Please help on this issue anyone.
Thanks.
6fc1e3c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File Contents:
The above changes update core changes which was not available in Magento 2.3.5 (early versions of it at least), but later on applied around the time of Magento 2.3.7. I upgraded from 2.3.5 and got this error.
The solution is to make sure that you have these changes (all of them), as mentioned if you have current Magento 2.3.7 or higher," you will have all the files.
Possible Cause:
Most of the time is that you have overridden
catalog_category_view.xml
which replaces core layout functionality.Solution if you have overridden xml file
Now all that remains is to make sure that your theme/overridden
catalog_category_view.xml
file only applies changes you need, not everything.In other words, your
vendor/magento/module-catalog/*/catalog_category_view.xml
file should be the same as this file applied above, as this core file has the image etc the object needs.**