Skip to content

Commit 15de93d

Browse files
author
Magento CICD
authored
MAGETWO-64403: [GitHub] [PR] Sorting the returning array by the position of the linked products. Fix for magento2/issues/8392 #8467
2 parents 67baf87 + 308b70b commit 15de93d

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

app/code/Magento/Catalog/Model/ProductLink/CollectionProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,21 @@ public function getCollection(\Magento\Catalog\Model\Product $product, $type)
4848
$products = $this->providers[$type]->getLinkedProducts($product);
4949
$converter = $this->converterPool->getConverter($type);
5050
$output = [];
51+
$sorterItems = [];
5152
foreach ($products as $item) {
5253
$output[$item->getId()] = $converter->convert($item);
5354
}
54-
return $output;
55+
56+
foreach ($output as $item) {
57+
$itemPosition = $item['position'];
58+
if (!isset($sorterItems[$itemPosition])) {
59+
$sorterItems[$itemPosition] = $item;
60+
} else {
61+
$newPosition = $itemPosition + 1;
62+
$sorterItems[$newPosition] = $item;
63+
}
64+
}
65+
ksort($sorterItems);
66+
return $sorterItems;
5567
}
5668
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model;
8+
9+
use Magento\Catalog\Model\ProductLink\CollectionProvider;
10+
use Magento\Catalog\Model\ProductLink\CollectionProviderInterface;
11+
use Magento\Catalog\Model\ProductLink\Converter\ConverterInterface;
12+
use Magento\Catalog\Model\ProductLink\Converter\ConverterPool;
13+
use Magento\Catalog\Model\Product;
14+
15+
class CollectionProviderTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var CollectionProvider
19+
*/
20+
private $model;
21+
22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $converterPoolMock;
26+
27+
/**
28+
* @var \PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $providerMock;
31+
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $productMock;
36+
37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $converterMock;
41+
42+
protected function setUp()
43+
{
44+
$this->productMock = $this->getMock(Product::class, [], [], '', false, false);
45+
$this->converterPoolMock = $this->getMock(ConverterPool::class, [], [], '', false, false);
46+
$this->providerMock = $this->getMock(CollectionProviderInterface::class);
47+
$this->converterMock = $this->getMock(ConverterInterface::class);
48+
49+
$this->model = new CollectionProvider($this->converterPoolMock, ['crosssell' => $this->providerMock]);
50+
}
51+
52+
/**
53+
* Test sort order of linked products based on configured item position.
54+
*/
55+
public function testGetCollection()
56+
{
57+
$linkedProductOneMock = $this->getMock(Product::class, [], [], '', false, false);
58+
$linkedProductTwoMock = $this->getMock(Product::class, [], [], '', false, false);
59+
$linkedProductThreeMock = $this->getMock(Product::class, [], [], '', false, false);
60+
61+
$linkedProductOneMock->expects($this->once())->method('getId')->willReturn(1);
62+
$linkedProductTwoMock->expects($this->once())->method('getId')->willReturn(2);
63+
$linkedProductThreeMock->expects($this->once())->method('getId')->willReturn(3);
64+
65+
$this->converterPoolMock->expects($this->once())
66+
->method('getConverter')
67+
->with('crosssell')
68+
->willReturn($this->converterMock);
69+
70+
$map = [
71+
[$linkedProductOneMock, ['name' => 'Product One', 'position' => 10]],
72+
[$linkedProductTwoMock, ['name' => 'Product Two', 'position' => 2]],
73+
[$linkedProductThreeMock, ['name' => 'Product Three', 'position' => 2]],
74+
];
75+
76+
$this->converterMock->expects($this->exactly(3))->method('convert')->willReturnMap($map);
77+
78+
$this->providerMock->expects($this->once())
79+
->method('getLinkedProducts')
80+
->with($this->productMock)
81+
->willReturn(
82+
[
83+
$linkedProductOneMock,
84+
$linkedProductTwoMock,
85+
$linkedProductThreeMock
86+
]
87+
);
88+
89+
$expectedResult = [
90+
2 => ['name' => 'Product Two', 'position' => 2],
91+
3 => ['name' => 'Product Three', 'position' => 2],
92+
10 => ['name' => 'Product One', 'position' => 10],
93+
];
94+
95+
$actualResult = $this->model->getCollection($this->productMock, 'crosssell');
96+
97+
$this->assertEquals($expectedResult, $actualResult, 'Sort order of linked products in incorrect');
98+
}
99+
100+
/**
101+
* Test exception when collection provider is not configured for product link type.
102+
*
103+
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
104+
* @expectedExceptionMessage Collection provider is not registered
105+
*/
106+
public function testGetCollectionWithMissingProviders()
107+
{
108+
$this->model->getCollection($this->productMock, 'upsell');
109+
}
110+
}

0 commit comments

Comments
 (0)