Skip to content

Commit fe68f5b

Browse files
MAGETWO-96290: [Magento Cloud] Cannot update category positions via REST API
1 parent 143f991 commit fe68f5b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/Category/Link/SaveHandlerTest.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ class SaveHandlerTest extends TestCase
4747
*/
4848
protected function setUp()
4949
{
50-
$this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
51-
$metadataPool = Bootstrap::getObjectManager()->create(MetadataPool::class);
52-
$this->productLinkField = $metadataPool->getMetadata(ProductInterface::class)->getLinkField();
53-
$this->categoryLinkFactory = Bootstrap::getObjectManager()->create(CategoryLinkInterfaceFactory::class);
54-
$this->saveHandler = Bootstrap::getObjectManager()->create(SaveHandler::class);
50+
$objectManager = Bootstrap::getObjectManager();
51+
$this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
52+
$metadataPool = $objectManager->create(MetadataPool::class);
53+
$this->productLinkField = $metadataPool->getMetadata(ProductInterface::class)
54+
->getLinkField();
55+
$this->categoryLinkFactory = $objectManager->create(CategoryLinkInterfaceFactory::class);
56+
$this->saveHandler = $objectManager->create(SaveHandler::class);
5557
}
5658

5759
/**
@@ -92,22 +94,14 @@ public function testExecute(): void
9294
$categoryLinks[] = $categoryLink;
9395
}
9496
$categoryLinks = $this->updateCategoryLinks($product, $categoryLinks);
95-
foreach ($categoryLinks as $categoryLink) {
96-
$categoryPosition = $categoryPositions[$categoryLink->getCategoryId()];
97-
$this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId());
98-
$this->assertEquals($categoryPosition['position'], $categoryLink->getPosition());
99-
}
97+
$this->assertPositions($categoryPositions, $categoryLinks);
10098

10199
$categoryPositions[4]['position'] = 2;
102100
$categoryLink = $this->categoryLinkFactory->create()
103101
->setCategoryId(4)
104102
->setPosition($categoryPositions[4]['position']);
105103
$categoryLinks = $this->updateCategoryLinks($product, [$categoryLink]);
106-
foreach ($categoryLinks as $categoryLink) {
107-
$categoryPosition = $categoryPositions[$categoryLink->getCategoryId()];
108-
$this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId());
109-
$this->assertEquals($categoryPosition['position'], $categoryLink->getPosition());
110-
}
104+
$this->assertPositions($categoryPositions, $categoryLinks);
111105
}
112106

113107
/**
@@ -123,8 +117,25 @@ private function updateCategoryLinks(ProductInterface $product, array $categoryL
123117
$arguments = [$this->productLinkField => $product->getData($this->productLinkField)];
124118
$this->saveHandler->execute($product, $arguments);
125119
$product = $this->productRepository->get($product->getSku(), false, null, true);
126-
$categoryLinks = $product->getExtensionAttributes()->getCategoryLinks();
120+
$categoryLinks = $product->getExtensionAttributes()
121+
->getCategoryLinks();
127122

128123
return $categoryLinks;
129124
}
125+
126+
/**
127+
* Assert positions
128+
*
129+
* @param array $categoryPositions
130+
* @param array $categoryLinks
131+
* @return void
132+
*/
133+
private function assertPositions(array $categoryPositions, array $categoryLinks): void
134+
{
135+
foreach ($categoryLinks as $categoryLink) {
136+
$categoryPosition = $categoryPositions[$categoryLink->getCategoryId()];
137+
$this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId());
138+
$this->assertEquals($categoryPosition['position'], $categoryLink->getPosition());
139+
}
140+
}
130141
}

0 commit comments

Comments
 (0)