-
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.
Merge remote-tracking branch 'origin/MC-32273' into 2.4-develop-pr17
- Loading branch information
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.