Skip to content

Commit

Permalink
issue #13497 - Method getUrl in Magento\Catalog\Model\Product\Attribu…
Browse files Browse the repository at this point in the history
…te\Frontend returns image url with double slash
  • Loading branch information
igortregub authored and nmalevanec committed Feb 19, 2018
1 parent fa906ed commit 8a2e547
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@
*
* @author Magento Core Team <core@magentocommerce.com>
*/

namespace Magento\Catalog\Model\Product\Attribute\Frontend;

class Image extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;

class Image extends AbstractFrontend
{
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $_storeManager;

/**
* Construct
*
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param StoreManagerInterface $storeManager
*/
public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
public function __construct(StoreManagerInterface $storeManager)
{
$this->_storeManager = $storeManager;
}
Expand All @@ -42,9 +47,9 @@ public function getUrl($product)
$image = $product->getData($this->getAttribute()->getAttributeCode());
$url = false;
if (!empty($image)) {
$url = $this->_storeManager->getStore($product->getStore())
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
. 'catalog/product/' . $image;
$url = $this->_storeManager
->getStore($product->getStore())
->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . 'catalog/product/' . ltrim($image, '/');
}
return $url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,71 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Frontend;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Frontend\Image;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;

class ImageTest extends \PHPUnit\Framework\TestCase
class ImageTest extends TestCase
{
/**
* @var \Magento\Catalog\Model\Product\Attribute\Frontend\Image
* @var Image
*/
private $model;

public function testGetUrl()
/**
* @dataProvider getUrlDataProvider
* @param string $expectedImage
* @param string $productImage
*/
public function testGetUrl(string $expectedImage, string $productImage)
{
$this->assertEquals($expectedImage, $this->model->getUrl($this->getMockedProduct($productImage)));
}

/**
* Data provider for testGetUrl
*
* @return array
*/
public function getUrlDataProvider(): array
{
$this->assertEquals('catalog/product/img.jpg', $this->model->getUrl($this->getMockedProduct()));
return [
['catalog/product/img.jpg', 'img.jpg'],
['catalog/product/img.jpg', '/img.jpg'],
];
}

protected function setUp()
{
$helper = new ObjectManager($this);
$this->model = $helper->getObject(
\Magento\Catalog\Model\Product\Attribute\Frontend\Image::class,
Image::class,
['storeManager' => $this->getMockedStoreManager()]
);
$this->model->setAttribute($this->getMockedAttribute());
}

/**
* @return \Magento\Catalog\Model\Product
* @param string $productImage
* @return Product
*/
private function getMockedProduct()
private function getMockedProduct(string $productImage): Product
{
$mockBuilder = $this->getMockBuilder(\Magento\Catalog\Model\Product::class);
$mockBuilder = $this->getMockBuilder(Product::class);
$mock = $mockBuilder->setMethods(['getData', 'getStore', '__wakeup'])
->disableOriginalConstructor()
->getMock();

$mock->expects($this->any())
->method('getData')
->will($this->returnValue('img.jpg'));
->will($this->returnValue($productImage));

$mock->expects($this->any())
->method('getStore');
Expand All @@ -50,13 +76,13 @@ private function getMockedProduct()
}

/**
* @return \Magento\Store\Model\StoreManagerInterface
* @return StoreManagerInterface
*/
private function getMockedStoreManager()
private function getMockedStoreManager(): StoreManagerInterface
{
$mockedStore = $this->getMockedStore();

$mockBuilder = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class);
$mockBuilder = $this->getMockBuilder(StoreManagerInterface::class);
$mock = $mockBuilder->setMethods(['getStore'])
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand All @@ -69,11 +95,11 @@ private function getMockedStoreManager()
}

/**
* @return \Magento\Store\Model\Store
* @return Store
*/
private function getMockedStore()
private function getMockedStore(): Store
{
$mockBuilder = $this->getMockBuilder(\Magento\Store\Model\Store::class);
$mockBuilder = $this->getMockBuilder(Store::class);
$mock = $mockBuilder->setMethods(['getBaseUrl', '__wakeup'])
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand All @@ -86,11 +112,11 @@ private function getMockedStore()
}

/**
* @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
* @return AbstractAttribute
*/
private function getMockedAttribute()
private function getMockedAttribute(): AbstractAttribute
{
$mockBuilder = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class);
$mockBuilder = $this->getMockBuilder(AbstractAttribute::class);
$mockBuilder->setMethods(['getAttributeCode', '__wakeup']);
$mockBuilder->disableOriginalConstructor();
$mock = $mockBuilder->getMockForAbstractClass();
Expand Down

0 comments on commit 8a2e547

Please sign in to comment.