Skip to content

Commit 7775f7d

Browse files
authored
Merge pull request #3455 from magento-tango/PR_15_11_2018
[tango] Bug fixes
2 parents da0b4f1 + 3b6c90b commit 7775f7d

File tree

7 files changed

+152
-68
lines changed

7 files changed

+152
-68
lines changed

app/code/Magento/Catalog/Model/Category/Link/SaveHandler.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,19 @@ private function getCategoryLinksPositions($entity)
106106
*/
107107
private function mergeCategoryLinks($newCategoryPositions, $oldCategoryPositions)
108108
{
109-
$result = [];
110109
if (empty($newCategoryPositions)) {
111-
return $result;
110+
return [];
112111
}
113112

113+
$categoryPositions = array_combine(array_column($oldCategoryPositions, 'category_id'), $oldCategoryPositions);
114114
foreach ($newCategoryPositions as $newCategoryPosition) {
115-
$key = array_search(
116-
$newCategoryPosition['category_id'],
117-
array_column($oldCategoryPositions, 'category_id')
118-
);
119-
120-
if ($key === false) {
121-
$result[] = $newCategoryPosition;
122-
} elseif (isset($oldCategoryPositions[$key])
123-
&& $oldCategoryPositions[$key]['position'] != $newCategoryPosition['position']
124-
) {
125-
$result[] = $newCategoryPositions[$key];
126-
unset($oldCategoryPositions[$key]);
115+
$categoryId = $newCategoryPosition['category_id'];
116+
if (!isset($categoryPositions[$categoryId])) {
117+
$categoryPositions[$categoryId] = ['category_id' => $categoryId];
127118
}
119+
$categoryPositions[$categoryId]['position'] = $newCategoryPosition['position'];
128120
}
129-
$result = array_merge($result, $oldCategoryPositions);
121+
$result = array_values($categoryPositions);
130122

131123
return $result;
132124
}

app/code/Magento/Catalog/Model/ResourceModel/Product/CategoryLink.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ private function getCategoryLinkMetadata()
114114
private function processCategoryLinks($newCategoryPositions, &$oldCategoryPositions)
115115
{
116116
$result = ['changed' => [], 'updated' => []];
117+
118+
$oldCategoryPositions = array_values($oldCategoryPositions);
119+
$oldCategoryList = array_column($oldCategoryPositions, 'category_id');
117120
foreach ($newCategoryPositions as $newCategoryPosition) {
118-
$key = array_search(
119-
$newCategoryPosition['category_id'],
120-
array_column($oldCategoryPositions, 'category_id')
121-
);
121+
$key = array_search($newCategoryPosition['category_id'], $oldCategoryList);
122122

123123
if ($key === false) {
124124
$result['changed'][] = $newCategoryPosition;
125125
} elseif ($oldCategoryPositions[$key]['position'] != $newCategoryPosition['position']) {
126-
$result['updated'][] = $newCategoryPositions[$key];
126+
$result['updated'][] = $newCategoryPosition;
127127
unset($oldCategoryPositions[$key]);
128128
}
129129
}

app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminProductGridActionGroup.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,27 @@
5151
<see selector="{{AdminMessagesSection.success}}" userInput="A total of 1 record(s) have been deleted." stepKey="seeSuccessMessage"/>
5252
<conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial2"/>
5353
</actionGroup>
54+
55+
<!--Disabled a product by filtering grid and using change status action-->
56+
<actionGroup name="ChangeStatusProductUsingProductGridActionGroup">
57+
<arguments>
58+
<argument name="product"/>
59+
<argument name="status" defaultValue="Enable" type="string" />
60+
</arguments>
61+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="visitAdminProductPage"/>
62+
<waitForPageLoad time="60" stepKey="waitForPageLoadInitial"/>
63+
<conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial"/>
64+
<click selector="{{AdminProductGridFilterSection.filters}}" stepKey="openProductFilters"/>
65+
<fillField selector="{{AdminProductGridFilterSection.skuFilter}}" userInput="{{product.sku}}" stepKey="fillProductSkuFilter"/>
66+
<click selector="{{AdminProductGridFilterSection.applyFilters}}" stepKey="clickApplyFilters"/>
67+
<see selector="{{AdminProductGridSection.productGridCell('1', 'SKU')}}" userInput="{{product.sku}}" stepKey="seeProductSkuInGrid"/>
68+
<click selector="{{AdminProductGridSection.multicheckDropdown}}" stepKey="openMulticheckDropdown"/>
69+
<click selector="{{AdminProductGridSection.multicheckOption('Select All')}}" stepKey="selectAllProductInFilteredGrid"/>
70+
71+
<click selector="{{AdminProductGridSection.bulkActionDropdown}}" stepKey="clickActionDropdown"/>
72+
<click selector="{{AdminProductGridSection.bulkActionOption('Change status')}}" stepKey="clickChangeStatusAction"/>
73+
<click selector="{{AdminProductGridSection.changeStatus('status')}}" stepKey="clickChangeStatusDisabled" parameterized="true"/>
74+
<see selector="{{AdminMessagesSection.success}}" userInput="A total of 1 record(s) have been updated." stepKey="seeSuccessMessage"/>
75+
<conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial2"/>
76+
</actionGroup>
5477
</actionGroups>

app/code/Magento/Catalog/Test/Mftf/Section/AdminProductGridSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
<element name="productGridNameProduct" type="input" selector="//tbody//tr//td//div[contains(., '{{var1}}')]" parameterized="true" timeout="30"/>
2727
<element name="adminImgGridThumbnail" type="text" selector="img.admin__control-thumbnail[src*='/{{var1}}']" parameterized="true"/>
2828
<element name="selectRowBasedOnName" type="input" selector="//td/div[text()='{{var1}}']" parameterized="true"/>
29+
<element name="changeStatus" type="button" selector="//div[contains(@class,'admin__data-grid-header-row') and contains(@class, 'row')]//div[contains(@class, 'action-menu-item')]//ul/li/span[text() = '{{status}}']" parameterized="true"/>
2930
</section>
3031
</sections>

app/code/Magento/Quote/Test/Mftf/ActionGroup/ChangeStatusProductUsingProductGridActionGroup.xml

Lines changed: 0 additions & 34 deletions
This file was deleted.

app/code/Magento/Quote/Test/Mftf/Section/AdminProductGridSection.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Category\Link;
7+
8+
use Magento\Catalog\Api\Data\CategoryLinkInterfaceFactory;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\EntityManager\MetadataPool;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @magentoDataFixture Magento/Catalog/_files/categories_no_products.php
17+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
18+
*/
19+
class SaveHandlerTest extends TestCase
20+
{
21+
/**
22+
* @var ProductRepositoryInterface
23+
*/
24+
private $productRepository;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $productLinkField;
30+
31+
/**
32+
* @var CategoryLinkInterfaceFactory
33+
*/
34+
private $categoryLinkFactory;
35+
36+
/**
37+
* @var SaveHandler
38+
*/
39+
private $saveHandler;
40+
41+
protected function setUp()
42+
{
43+
$this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
44+
$metadataPool = Bootstrap::getObjectManager()->create(MetadataPool::class);
45+
$this->productLinkField = $metadataPool->getMetadata(ProductInterface::class)->getLinkField();
46+
$this->categoryLinkFactory = Bootstrap::getObjectManager()->create(CategoryLinkInterfaceFactory::class);
47+
$this->saveHandler = Bootstrap::getObjectManager()->create(SaveHandler::class);
48+
}
49+
50+
public function testExecute()
51+
{
52+
$product = $this->productRepository->get('simple2');
53+
$product->setCategoryIds([3, 4, 6]);
54+
$this->productRepository->save($product);
55+
$categoryPositions = [
56+
3 => [
57+
'category_id' => 3,
58+
'position' => 0,
59+
],
60+
4 => [
61+
'category_id' => 4,
62+
'position' => 0,
63+
],
64+
6 => [
65+
'category_id' => 6,
66+
'position' => 0,
67+
],
68+
];
69+
70+
$categoryLinks = $product->getExtensionAttributes()->getCategoryLinks();
71+
$this->assertEmpty($categoryLinks);
72+
73+
$categoryLinks = [];
74+
$categoryPositions[4]['position'] = 1;
75+
$categoryPositions[6]['position'] = 1;
76+
foreach ($categoryPositions as $categoryPosition) {
77+
$categoryLink = $this->categoryLinkFactory->create()
78+
->setCategoryId($categoryPosition['category_id'])
79+
->setPosition($categoryPosition['position']);
80+
$categoryLinks[] = $categoryLink;
81+
}
82+
$categoryLinks = $this->updateCategoryLinks($product, $categoryLinks);
83+
foreach ($categoryLinks as $categoryLink) {
84+
$categoryPosition = $categoryPositions[$categoryLink->getCategoryId()];
85+
$this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId());
86+
$this->assertEquals($categoryPosition['position'], $categoryLink->getPosition());
87+
}
88+
89+
$categoryPositions[4]['position'] = 2;
90+
$categoryLink = $this->categoryLinkFactory->create()
91+
->setCategoryId(4)
92+
->setPosition($categoryPositions[4]['position']);
93+
$categoryLinks = $this->updateCategoryLinks($product, [$categoryLink]);
94+
foreach ($categoryLinks as $categoryLink) {
95+
$categoryPosition = $categoryPositions[$categoryLink->getCategoryId()];
96+
$this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId());
97+
$this->assertEquals($categoryPosition['position'], $categoryLink->getPosition());
98+
}
99+
}
100+
101+
/**
102+
* @param ProductInterface $product
103+
* @param \Magento\Catalog\Api\Data\CategoryLinkInterface[] $categoryLinks
104+
* @return \Magento\Catalog\Api\Data\CategoryLinkInterface[]
105+
*/
106+
private function updateCategoryLinks(ProductInterface $product, array $categoryLinks): array
107+
{
108+
$product->getExtensionAttributes()->setCategoryLinks($categoryLinks);
109+
$arguments = [$this->productLinkField => $product->getData($this->productLinkField)];
110+
$this->saveHandler->execute($product, $arguments);
111+
$product = $this->productRepository->get($product->getSku(), false, null, true);
112+
$categoryLinks = $product->getExtensionAttributes()->getCategoryLinks();
113+
114+
return $categoryLinks;
115+
}
116+
}

0 commit comments

Comments
 (0)