Skip to content

Commit d2b6928

Browse files
committed
Merge branch 'MAGETWO-64260' into EPAM-PR-51
2 parents 3d098bb + 266c552 commit d2b6928

File tree

4 files changed

+128
-21
lines changed

4 files changed

+128
-21
lines changed

app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function getAssociatedProducts($product)
210210
$collection = $this->getAssociatedProductCollection(
211211
$product
212212
)->addAttributeToSelect(
213-
['name', 'price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
213+
['name', 'price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id', 'image']
214214
)->addFilterByRequiredOptions()->setPositionOrder()->addStoreFilter(
215215
$this->getStoreFilter($product)
216216
)->addAttributeToFilter(
@@ -475,10 +475,12 @@ public function hasWeight()
475475
* @param \Magento\Catalog\Model\Product $product
476476
* @return void
477477
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
478+
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
478479
*/
479480
public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
480481
{
481482
}
483+
//phpcs:enable
482484

483485
/**
484486
* @inheritdoc
@@ -488,6 +490,7 @@ public function beforeSave($product)
488490
//clear cached associated links
489491
$product->unsetData($this->_keyAssociatedProducts);
490492
if ($product->hasData('product_options') && !empty($product->getData('product_options'))) {
493+
//phpcs:ignore Magento2.Exceptions.DirectThrow
491494
throw new \Exception('Custom options for grouped product type are not supported');
492495
}
493496
return parent::beforeSave($product);

app/code/Magento/GroupedProduct/Test/Unit/Ui/DataProvider/Product/Form/Modifier/GroupedTest.php

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedProductType;
2222
use Magento\GroupedProduct\Ui\DataProvider\Product\Form\Modifier\Grouped;
2323
use Magento\Store\Api\Data\StoreInterface;
24+
use Magento\Catalog\Model\Product;
25+
use Magento\GroupedProduct\Model\Product\Link\CollectionProvider\Grouped as GroupedProducts;
26+
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
2427

2528
/**
2629
* Class GroupedTest
@@ -82,23 +85,38 @@ class GroupedTest extends AbstractModifierTest
8285
*/
8386
protected $storeMock;
8487

88+
/**
89+
* @var GroupedProducts|\PHPUnit_Framework_MockObject_MockObject
90+
*/
91+
private $groupedProductsMock;
92+
93+
/**
94+
* @var ProductLinkInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
95+
*/
96+
private $productLinkFactoryMock;
97+
98+
/**
99+
* @inheritdoc
100+
*/
85101
protected function setUp()
86102
{
87103
$this->objectManager = new ObjectManager($this);
88104
$this->locatorMock = $this->getMockBuilder(LocatorInterface::class)
89105
->getMockForAbstractClass();
90-
$this->productMock = $this->getMockBuilder(ProductInterface::class)
106+
$this->productMock = $this->getMockBuilder(Product::class)
91107
->setMethods(['getId', 'getTypeId'])
92-
->getMockForAbstractClass();
108+
->disableOriginalConstructor()
109+
->getMock();
93110
$this->productMock->expects($this->any())
94111
->method('getId')
95112
->willReturn(self::PRODUCT_ID);
96113
$this->productMock->expects($this->any())
97114
->method('getTypeId')
98115
->willReturn(GroupedProductType::TYPE_CODE);
99-
$this->linkedProductMock = $this->getMockBuilder(ProductInterface::class)
116+
$this->linkedProductMock = $this->getMockBuilder(Product::class)
100117
->setMethods(['getId', 'getName', 'getPrice'])
101-
->getMockForAbstractClass();
118+
->disableOriginalConstructor()
119+
->getMock();
102120
$this->linkedProductMock->expects($this->any())
103121
->method('getId')
104122
->willReturn(self::LINKED_PRODUCT_ID);
@@ -135,7 +153,7 @@ protected function setUp()
135153
$this->linkRepositoryMock->expects($this->any())
136154
->method('getList')
137155
->with($this->productMock)
138-
->willReturn([$this->linkMock]);
156+
->willReturn([$this->linkedProductMock]);
139157
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
140158
->setMethods(['get'])
141159
->getMockForAbstractClass();
@@ -155,7 +173,7 @@ protected function setUp()
155173
}
156174

157175
/**
158-
* {@inheritdoc}
176+
* @inheritdoc
159177
*/
160178
protected function createModel()
161179
{
@@ -169,6 +187,16 @@ protected function createModel()
169187
->setMethods(['init', 'getUrl'])
170188
->disableOriginalConstructor()
171189
->getMock();
190+
191+
$this->groupedProductsMock = $this->getMockBuilder(GroupedProducts::class)
192+
->setMethods(['getLinkedProducts'])
193+
->disableOriginalConstructor()
194+
->getMock();
195+
$this->productLinkFactoryMock = $this->getMockBuilder(ProductLinkInterfaceFactory::class)
196+
->setMethods(['create'])
197+
->disableOriginalConstructor()
198+
->getMockForAbstractClass();
199+
172200
$this->imageHelperMock->expects($this->any())
173201
->method('init')
174202
->willReturn($this->imageHelperMock);
@@ -189,16 +217,23 @@ protected function createModel()
189217
'localeCurrency' => $this->currencyMock,
190218
'imageHelper' => $this->imageHelperMock,
191219
'attributeSetRepository' => $this->attributeSetRepositoryMock,
220+
'groupedProducts' => $this->groupedProductsMock,
221+
'productLinkFactory' => $this->productLinkFactoryMock,
192222
]);
193223
}
194224

225+
/**
226+
* Assert array has key
227+
*
228+
* @return void
229+
*/
195230
public function testModifyMeta()
196231
{
197232
$this->assertArrayHasKey(Grouped::GROUP_GROUPED, $this->getModel()->modifyMeta([]));
198233
}
199234

200235
/**
201-
* {@inheritdoc}
236+
* @inheritdoc
202237
*/
203238
public function testModifyData()
204239
{
@@ -226,6 +261,42 @@ public function testModifyData()
226261
],
227262
],
228263
];
229-
$this->assertSame($expectedData, $this->getModel()->modifyData([]));
264+
$model = $this->getModel();
265+
$linkedProductMock = $this->getMockBuilder(Product::class)
266+
->setMethods(['getId', 'getName', 'getPrice', 'getSku', 'getImage', 'getPosition', 'getQty'])
267+
->disableOriginalConstructor()
268+
->getMock();
269+
$linkedProductMock->expects($this->once())
270+
->method('getId')
271+
->willReturn(self::LINKED_PRODUCT_ID);
272+
$linkedProductMock->expects($this->once())
273+
->method('getName')
274+
->willReturn(self::LINKED_PRODUCT_NAME);
275+
$linkedProductMock->expects($this->once())
276+
->method('getPrice')
277+
->willReturn(self::LINKED_PRODUCT_PRICE);
278+
$linkedProductMock->expects($this->once())
279+
->method('getSku')
280+
->willReturn(self::LINKED_PRODUCT_SKU);
281+
$linkedProductMock->expects($this->once())
282+
->method('getImage')
283+
->willReturn('');
284+
$linkedProductMock->expects($this->exactly(2))
285+
->method('getPosition')
286+
->willReturn(self::LINKED_PRODUCT_POSITION);
287+
$linkedProductMock->expects($this->once())
288+
->method('getQty')
289+
->willReturn(self::LINKED_PRODUCT_QTY);
290+
$this->groupedProductsMock->expects($this->once())
291+
->method('getLinkedProducts')
292+
->willReturn([$linkedProductMock]);
293+
$linkMock = $this->getMockBuilder(ProductLinkInterface::class)
294+
->getMockForAbstractClass();
295+
296+
$this->productLinkFactoryMock->expects($this->once())
297+
->method('create')
298+
->willReturn($linkMock);
299+
300+
$this->assertSame($expectedData, $model->modifyData([]));
230301
}
231302
}

app/code/Magento/GroupedProduct/Ui/DataProvider/Product/Form/Modifier/Grouped.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use Magento\Eav\Api\AttributeSetRepositoryInterface;
2222
use Magento\Catalog\Model\Product\Attribute\Source\Status;
2323
use Magento\Framework\Locale\CurrencyInterface;
24+
use Magento\GroupedProduct\Model\Product\Link\CollectionProvider\Grouped as GroupedProducts;
25+
use Magento\Framework\App\ObjectManager;
26+
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
2427

2528
/**
2629
* Data provider for Grouped products
@@ -99,6 +102,16 @@ class Grouped extends AbstractModifier
99102
*/
100103
private static $codeQty = 'qty';
101104

105+
/**
106+
* @var GroupedProducts
107+
*/
108+
private $groupedProducts;
109+
110+
/**
111+
* @var ProductLinkInterfaceFactory
112+
*/
113+
private $productLinkFactory;
114+
102115
/**
103116
* @param LocatorInterface $locator
104117
* @param UrlInterface $urlBuilder
@@ -109,6 +122,9 @@ class Grouped extends AbstractModifier
109122
* @param AttributeSetRepositoryInterface $attributeSetRepository
110123
* @param CurrencyInterface $localeCurrency
111124
* @param array $uiComponentsConfig
125+
* @param GroupedProducts $groupedProducts
126+
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory|null $productLinkFactory
127+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112128
*/
113129
public function __construct(
114130
LocatorInterface $locator,
@@ -119,7 +135,9 @@ public function __construct(
119135
Status $status,
120136
AttributeSetRepositoryInterface $attributeSetRepository,
121137
CurrencyInterface $localeCurrency,
122-
array $uiComponentsConfig = []
138+
array $uiComponentsConfig = [],
139+
GroupedProducts $groupedProducts = null,
140+
\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory = null
123141
) {
124142
$this->locator = $locator;
125143
$this->urlBuilder = $urlBuilder;
@@ -130,6 +148,11 @@ public function __construct(
130148
$this->status = $status;
131149
$this->localeCurrency = $localeCurrency;
132150
$this->uiComponentsConfig = array_replace_recursive($this->uiComponentsConfig, $uiComponentsConfig);
151+
$this->groupedProducts = $groupedProducts ?: ObjectManager::getInstance()->get(
152+
\Magento\GroupedProduct\Model\Product\Link\CollectionProvider\Grouped::class
153+
);
154+
$this->productLinkFactory = $productLinkFactory ?: ObjectManager::getInstance()
155+
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
133156
}
134157

135158
/**
@@ -143,18 +166,15 @@ public function modifyData(array $data)
143166
if ($modelId) {
144167
$storeId = $this->locator->getStore()->getId();
145168
$data[$product->getId()]['links'][self::LINK_TYPE] = [];
146-
$linkedItems = $this->productLinkRepository->getList($product);
169+
$linkedItems = $this->groupedProducts->getLinkedProducts($product);
147170
usort($linkedItems, function ($a, $b) {
148171
return $a->getPosition() <=> $b->getPosition();
149172
});
173+
$productLink = $this->productLinkFactory->create();
150174
foreach ($linkedItems as $index => $linkItem) {
151-
if ($linkItem->getLinkType() !== self::LINK_TYPE) {
152-
continue;
153-
}
154175
/** @var \Magento\Catalog\Api\Data\ProductInterface $linkedProduct */
155-
$linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $storeId);
156176
$linkItem->setPosition($index);
157-
$data[$modelId]['links'][self::LINK_TYPE][] = $this->fillData($linkedProduct, $linkItem);
177+
$data[$modelId]['links'][self::LINK_TYPE][] = $this->fillData($linkItem, $productLink);
158178
}
159179
$data[$modelId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $storeId;
160180
}
@@ -167,6 +187,7 @@ public function modifyData(array $data)
167187
* @param ProductInterface $linkedProduct
168188
* @param ProductLinkInterface $linkItem
169189
* @return array
190+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
170191
*/
171192
protected function fillData(ProductInterface $linkedProduct, ProductLinkInterface $linkItem)
172193
{
@@ -176,12 +197,15 @@ protected function fillData(ProductInterface $linkedProduct, ProductLinkInterfac
176197
return [
177198
'id' => $linkedProduct->getId(),
178199
'name' => $linkedProduct->getName(),
179-
'sku' => $linkItem->getLinkedProductSku(),
200+
'sku' => $linkedProduct->getSku(),
180201
'price' => $currency->toCurrency(sprintf("%f", $linkedProduct->getPrice())),
181-
'qty' => $linkItem->getExtensionAttributes()->getQty(),
182-
'position' => $linkItem->getPosition(),
183-
'positionCalculated' => $linkItem->getPosition(),
184-
'thumbnail' => $this->imageHelper->init($linkedProduct, 'product_listing_thumbnail')->getUrl(),
202+
'qty' => $linkedProduct->getQty(),
203+
'position' => $linkedProduct->getPosition(),
204+
'positionCalculated' => $linkedProduct->getPosition(),
205+
'thumbnail' => $this->imageHelper
206+
->init($linkedProduct, 'product_listing_thumbnail')
207+
->setImageFile($linkedProduct->getImage())
208+
->getUrl(),
185209
'type_id' => $linkedProduct->getTypeId(),
186210
'status' => $this->status->getOptionText($linkedProduct->getStatus()),
187211
'attribute_set' => $this->attributeSetRepository

app/code/Magento/Ui/view/base/web/js/dynamic-rows/dynamic-rows-grid.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ define([
3333
}
3434
},
3535

36+
/**
37+
* @inheritdoc
38+
*/
39+
initialize: function () {
40+
this.setToInsertData = _.debounce(this.setToInsertData, 200);
41+
42+
return this._super();
43+
},
44+
3645
/**
3746
* Calls 'initObservable' of parent
3847
*

0 commit comments

Comments
 (0)