Skip to content

Commit

Permalink
MAGETWO-64403: [GitHub] [PR] Sorting the returning array by the posit…
Browse files Browse the repository at this point in the history
…ion of the linked products. Fix for magento2/issues/8392 #8467
  • Loading branch information
Magento CICD authored Feb 20, 2017
2 parents 67baf87 + 308b70b commit 15de93d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,21 @@ public function getCollection(\Magento\Catalog\Model\Product $product, $type)
$products = $this->providers[$type]->getLinkedProducts($product);
$converter = $this->converterPool->getConverter($type);
$output = [];
$sorterItems = [];
foreach ($products as $item) {
$output[$item->getId()] = $converter->convert($item);
}
return $output;

foreach ($output as $item) {
$itemPosition = $item['position'];
if (!isset($sorterItems[$itemPosition])) {
$sorterItems[$itemPosition] = $item;
} else {
$newPosition = $itemPosition + 1;
$sorterItems[$newPosition] = $item;
}
}
ksort($sorterItems);
return $sorterItems;
}
}
110 changes: 110 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/CollectionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model;

use Magento\Catalog\Model\ProductLink\CollectionProvider;
use Magento\Catalog\Model\ProductLink\CollectionProviderInterface;
use Magento\Catalog\Model\ProductLink\Converter\ConverterInterface;
use Magento\Catalog\Model\ProductLink\Converter\ConverterPool;
use Magento\Catalog\Model\Product;

class CollectionProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CollectionProvider
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $converterPoolMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $providerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $productMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $converterMock;

protected function setUp()
{
$this->productMock = $this->getMock(Product::class, [], [], '', false, false);
$this->converterPoolMock = $this->getMock(ConverterPool::class, [], [], '', false, false);
$this->providerMock = $this->getMock(CollectionProviderInterface::class);
$this->converterMock = $this->getMock(ConverterInterface::class);

$this->model = new CollectionProvider($this->converterPoolMock, ['crosssell' => $this->providerMock]);
}

/**
* Test sort order of linked products based on configured item position.
*/
public function testGetCollection()
{
$linkedProductOneMock = $this->getMock(Product::class, [], [], '', false, false);
$linkedProductTwoMock = $this->getMock(Product::class, [], [], '', false, false);
$linkedProductThreeMock = $this->getMock(Product::class, [], [], '', false, false);

$linkedProductOneMock->expects($this->once())->method('getId')->willReturn(1);
$linkedProductTwoMock->expects($this->once())->method('getId')->willReturn(2);
$linkedProductThreeMock->expects($this->once())->method('getId')->willReturn(3);

$this->converterPoolMock->expects($this->once())
->method('getConverter')
->with('crosssell')
->willReturn($this->converterMock);

$map = [
[$linkedProductOneMock, ['name' => 'Product One', 'position' => 10]],
[$linkedProductTwoMock, ['name' => 'Product Two', 'position' => 2]],
[$linkedProductThreeMock, ['name' => 'Product Three', 'position' => 2]],
];

$this->converterMock->expects($this->exactly(3))->method('convert')->willReturnMap($map);

$this->providerMock->expects($this->once())
->method('getLinkedProducts')
->with($this->productMock)
->willReturn(
[
$linkedProductOneMock,
$linkedProductTwoMock,
$linkedProductThreeMock
]
);

$expectedResult = [
2 => ['name' => 'Product Two', 'position' => 2],
3 => ['name' => 'Product Three', 'position' => 2],
10 => ['name' => 'Product One', 'position' => 10],
];

$actualResult = $this->model->getCollection($this->productMock, 'crosssell');

$this->assertEquals($expectedResult, $actualResult, 'Sort order of linked products in incorrect');
}

/**
* Test exception when collection provider is not configured for product link type.
*
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
* @expectedExceptionMessage Collection provider is not registered
*/
public function testGetCollectionWithMissingProviders()
{
$this->model->getCollection($this->productMock, 'upsell');
}
}

0 comments on commit 15de93d

Please sign in to comment.