Skip to content

Commit 1eac804

Browse files
committed
ACP2E-645: Filtering products by colour swatch on search page does not display correct image for some products
1 parent 41631d5 commit 1eac804

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

app/code/Magento/Swatches/Block/Product/Renderer/Listing/Configurable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ public function __construct(
102102
$this->layerResolver = $layerResolver ?: ObjectManager::getInstance()->get(Resolver::class);
103103
}
104104

105+
/**
106+
* @inheritdoc
107+
*/
108+
public function getCacheKey()
109+
{
110+
$key = parent::getCacheKey();
111+
$configurableAttributes = $this->getLayeredAttributesIfExists(
112+
$this->getProduct(),
113+
$this->getRequest()->getQuery()->toArray()
114+
);
115+
if (!empty($configurableAttributes)) {
116+
$key .= '-' . sha1(json_encode($configurableAttributes));
117+
}
118+
119+
return $key;
120+
}
121+
105122
/**
106123
* @inheritdoc
107124
*/

app/code/Magento/Swatches/Test/Unit/Block/Product/Renderer/Listing/ConfigurableTest.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Swatches\Test\Unit\Block\Product\Renderer\Listing;
99

10+
use Magento\Catalog\Block\Product\Context;
1011
use Magento\Catalog\Helper\Image;
1112
use Magento\Catalog\Helper\Product;
1213
use Magento\Catalog\Model\Product\Attribute\Source\Status;
@@ -17,13 +18,18 @@
1718
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
1819
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices;
1920
use Magento\Customer\Helper\Session\CurrentCustomer;
21+
use Magento\Eav\Api\Data\AttributeInterface;
2022
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
2123
use Magento\Framework\App\Config\ScopeConfigInterface;
24+
use Magento\Framework\App\Request\Http;
25+
use Magento\Framework\App\RequestInterface;
2226
use Magento\Framework\Json\EncoderInterface;
27+
use Magento\Framework\Model\AbstractModel;
2328
use Magento\Framework\Pricing\PriceCurrencyInterface;
2429
use Magento\Framework\Pricing\PriceInfo\Base;
2530
use Magento\Framework\Stdlib\ArrayUtils;
2631
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32+
use Magento\Store\Model\StoreManagerInterface;
2733
use Magento\Swatches\Block\Product\Renderer\Configurable;
2834
use Magento\Swatches\Block\Product\Renderer\Listing\Configurable as ConfigurableRenderer;
2935
use Magento\Swatches\Helper\Media;
@@ -84,6 +90,11 @@ class ConfigurableTest extends TestCase
8490
/** @var MockObject */
8591
private $variationPricesMock;
8692

93+
/**
94+
* @var RequestInterface|MockObject
95+
*/
96+
private $request;
97+
8798
protected function setUp(): void
8899
{
89100
$this->arrayUtils = $this->createMock(ArrayUtils::class);
@@ -105,11 +116,22 @@ protected function setUp(): void
105116
$this->variationPricesMock = $this->createMock(
106117
Prices::class
107118
);
119+
$customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
120+
$this->request = $this->getMockBuilder(Http::class)
121+
->addMethods(['toArray'])
122+
->onlyMethods(['getQuery'])
123+
->disableOriginalConstructor()
124+
->getMock();
125+
126+
$this->request->method('getQuery')->willReturnSelf();
127+
$context = $this->getContextMock();
128+
$context->method('getRequest')->willReturn($this->request);
108129

109130
$objectManagerHelper = new ObjectManager($this);
110131
$this->configurable = $objectManagerHelper->getObject(
111132
ConfigurableRenderer::class,
112133
[
134+
'context' => $context,
113135
'scopeConfig' => $this->scopeConfig,
114136
'imageHelper' => $this->imageHelper,
115137
'imageUrlBuilder' => $this->imageUrlBuilder,
@@ -123,7 +145,8 @@ protected function setUp(): void
123145
'priceCurrency' => $this->priceCurrency,
124146
'configurableAttributeData' => $this->configurableAttributeData,
125147
'data' => [],
126-
'variationPrices' => $this->variationPricesMock
148+
'variationPrices' => $this->variationPricesMock,
149+
'customerSession' => $customerSession,
127150
]
128151
);
129152
}
@@ -262,4 +285,61 @@ public function testGetPricesJson()
262285
$this->jsonEncoder->expects($this->once())->method('encode')->with($expectedPrices);
263286
$this->configurable->getPricesJson();
264287
}
288+
289+
/**
290+
* Tests that cache key contains query params.
291+
*
292+
* @return void
293+
*/
294+
public function testGetCacheKey()
295+
{
296+
$requestParams = ['color' => 59, 'size' => 1, 'random_param' => '123'];
297+
298+
$attr1 = $this->getMockForAbstractClass(AttributeInterface::class);
299+
$attr1->method('getAttributeCode')->willReturn('color');
300+
$attr2 = $this->getMockForAbstractClass(AttributeInterface::class);
301+
$attr2->method('getAttributeCode')->willReturn('size');
302+
$configurableAttributes = [$attr1, $attr2];
303+
304+
$currency = $this->createMock(AbstractModel::class);
305+
$this->priceCurrency->method('getCurrency')->willReturn($currency);
306+
$this->swatchHelper->method('getAttributesFromConfigurable')
307+
->with($this->product)
308+
->willReturn($configurableAttributes);
309+
310+
$this->request->method('toArray')->willReturn($requestParams);
311+
$this->assertStringContainsString(
312+
sha1(json_encode(['color' => 59, 'size' => 1])),
313+
$this->configurable->getCacheKey()
314+
);
315+
}
316+
317+
/**
318+
* Returns context object mock.
319+
*
320+
* @return Context|MockObject
321+
*/
322+
private function getContextMock()
323+
{
324+
$context = $this->createMock(Context::class);
325+
$storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
326+
$store = $this->getMockForAbstractClass(\Magento\Store\Api\Data\StoreInterface::class);
327+
$storeManager->method('getStore')->willReturn($store);
328+
$appState = $this->createMock(\Magento\Framework\App\State::class);
329+
$resolver = $this->createMock(\Magento\Framework\View\Element\Template\File\Resolver::class);
330+
$urlBuilder = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
331+
$registry = $this->createMock(\Magento\Framework\Registry::class);
332+
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
333+
$productType = $this->createMock(\Magento\Catalog\Model\Product\Type\AbstractType::class);
334+
$product->method('getTypeInstance')->willReturn($productType);
335+
$product->method('getId')->willReturn(1);
336+
$registry->method('registry')->with('product')->willReturn($product);
337+
$context->method('getStoreManager')->willReturn($storeManager);
338+
$context->method('getAppState')->willReturn($appState);
339+
$context->method('getResolver')->willReturn($resolver);
340+
$context->method('getUrlBuilder')->willReturn($urlBuilder);
341+
$context->method('getRegistry')->willReturn($registry);
342+
343+
return $context;
344+
}
265345
}

0 commit comments

Comments
 (0)