Skip to content

Commit 0d3d7bb

Browse files
authored
Merge pull request #6621 from magento-tsg/2.4-develop-sidecar-pr15
[Sidecar] Fixes for 2.4 (pr15)
2 parents 52ce622 + 0a8125a commit 0d3d7bb

File tree

5 files changed

+430
-21
lines changed

5 files changed

+430
-21
lines changed

dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/LinkManagementTest.php

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\ConfigurableProduct\Api;
810

911
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
@@ -43,8 +45,10 @@ protected function setUp(): void
4345

4446
/**
4547
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
48+
*
49+
* @return void
4650
*/
47-
public function testGetChildren()
51+
public function testGetChildren(): void
4852
{
4953
$productSku = 'configurable';
5054

@@ -72,8 +76,10 @@ public function testGetChildren()
7276
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
7377
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
7478
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
79+
*
80+
* @return void
7581
*/
76-
public function testAddChild()
82+
public function testAddChild(): void
7783
{
7884
$productSku = 'configurable';
7985
$childSku = 'simple_77';
@@ -85,8 +91,10 @@ public function testAddChild()
8591
* Test the full flow of creating a configurable product and adding a child via REST
8692
*
8793
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
94+
*
95+
* @return void
8896
*/
89-
public function testAddChildFullRestCreation()
97+
public function testAddChildFullRestCreation(): void
9098
{
9199
$productSku = 'configurable-product-sku';
92100
$childSku = 'simple-product-sku';
@@ -96,7 +104,7 @@ public function testAddChildFullRestCreation()
96104

97105
$this->addOptionToConfigurableProduct(
98106
$productSku,
99-
$attribute->getAttributeId(),
107+
(int)$attribute->getAttributeId(),
100108
[
101109
[
102110
'value_index' => $attribute->getOptions()[1]->getValue()
@@ -139,8 +147,10 @@ public function testAddChildFullRestCreation()
139147
* configurable product.
140148
*
141149
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attributes_for_position_test.php
150+
*
151+
* @return void
142152
*/
143-
public function testConfigurableOptionPositionPreservation()
153+
public function testConfigurableOptionPositionPreservation(): void
144154
{
145155
$productSku = 'configurable-product-sku';
146156
$childProductSkus = [
@@ -160,19 +170,19 @@ public function testConfigurableOptionPositionPreservation()
160170
/** @var Attribute $attribute */
161171
$attribute = $this->attributeRepository->get('catalog_product', $attributeToAdd);
162172

163-
/** @var Option $options[] */
173+
/** @var Option $options [] */
164174
$options = $attribute->getOptions();
165175
array_shift($options);
166176

167177
$attributeOptions[$attributeToAdd] = $options;
168178

169179
$valueIndexesData = [];
170180
foreach ($options as $option) {
171-
$valueIndexesData []['value_index']= $option->getValue();
181+
$valueIndexesData[]['value_index'] = $option->getValue();
172182
}
173183
$this->addOptionToConfigurableProduct(
174184
$productSku,
175-
$attribute->getAttributeId(),
185+
(int)$attribute->getAttributeId(),
176186
$valueIndexesData,
177187
$position
178188
);
@@ -284,7 +294,14 @@ protected function getConfigurableAttribute(string $productSku): array
284294
return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
285295
}
286296

287-
private function addChild($productSku, $childSku)
297+
/**
298+
* Perform add child product Api call
299+
*
300+
* @param string $productSku
301+
* @param string $childSku
302+
* @return array|int|string|float|bool
303+
*/
304+
private function addChild(string $productSku, string $childSku)
288305
{
289306
$serviceInfo = [
290307
'rest' => [
@@ -300,7 +317,13 @@ private function addChild($productSku, $childSku)
300317
return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'childSku' => $childSku]);
301318
}
302319

303-
protected function createConfigurableProduct($productSku)
320+
/**
321+
* Perform create configurable product api call
322+
*
323+
* @param string $productSku
324+
* @return array|bool|float|int|string
325+
*/
326+
protected function createConfigurableProduct(string $productSku)
304327
{
305328
$requestData = [
306329
'product' => [
@@ -325,8 +348,21 @@ protected function createConfigurableProduct($productSku)
325348
return $this->_webApiCall($serviceInfo, $requestData);
326349
}
327350

328-
protected function addOptionToConfigurableProduct($productSku, $attributeId, $attributeValues, $position = 0)
329-
{
351+
/**
352+
* Add option to configurable product
353+
*
354+
* @param string $productSku
355+
* @param int $attributeId
356+
* @param array $attributeValues
357+
* @param int $position
358+
* @return array|bool|float|int|string
359+
*/
360+
protected function addOptionToConfigurableProduct(
361+
string $productSku,
362+
int $attributeId,
363+
array $attributeValues,
364+
int $position = 0
365+
) {
330366
$requestData = [
331367
'sku' => $productSku,
332368
'option' => [
@@ -339,7 +375,7 @@ protected function addOptionToConfigurableProduct($productSku, $attributeId, $at
339375
];
340376
$serviceInfo = [
341377
'rest' => [
342-
'resourcePath' => '/V1/configurable-products/'. $productSku .'/options',
378+
'resourcePath' => '/V1/configurable-products/' . $productSku . '/options',
343379
'httpMethod' => Request::HTTP_METHOD_POST,
344380
],
345381
'soap' => [
@@ -381,15 +417,67 @@ protected function createSimpleProduct($sku, $customAttributes)
381417

382418
/**
383419
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
420+
*
421+
* @return void
384422
*/
385-
public function testRemoveChild()
423+
public function testRemoveChild(): void
386424
{
387425
$productSku = 'configurable';
388426
$childSku = 'simple_10';
389427
$this->assertTrue($this->removeChild($productSku, $childSku));
390428
}
391429

392-
protected function removeChild($productSku, $childSku)
430+
/**
431+
* @dataProvider errorsDataProvider
432+
*
433+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
434+
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
435+
*
436+
* @param string $parentSku
437+
* @param string $childSku
438+
* @param string $errorMessage
439+
* @return void
440+
*/
441+
public function testAddChildWithError(string $parentSku, string $childSku, string $errorMessage): void
442+
{
443+
$this->expectException(\Exception::class);
444+
$this->expectExceptionMessage($errorMessage);
445+
$this->addChild($parentSku, $childSku);
446+
}
447+
448+
/**
449+
* @return array
450+
*/
451+
public function errorsDataProvider(): array
452+
{
453+
return [
454+
'simple_instead_of_configurable' => [
455+
'parent_sku' => 'simple2',
456+
'child_sku' => 'configurable',
457+
'error_message' => (string)__("The parent product doesn't have configurable product options."),
458+
],
459+
'simple_with_empty_configurable_attribute_value' => [
460+
'parent_sku' => 'configurable',
461+
'child_sku' => 'simple2',
462+
'error_message' => TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
463+
? (string)__(
464+
'The child product doesn\'t have the "%1" attribute value. Verify the value and try again.'
465+
)
466+
: (string)__(
467+
'The child product doesn\'t have the \\"%1\\" attribute value. Verify the value and try again.'
468+
),
469+
],
470+
];
471+
}
472+
473+
/**
474+
* Remove child product
475+
*
476+
* @param string $productSku
477+
* @param string $childSku
478+
* @return array|bool|float|int|string
479+
*/
480+
protected function removeChild(string $productSku, string $childSku)
393481
{
394482
$resourcePath = self::RESOURCE_PATH . '/%s/children/%s';
395483
$serviceInfo = [
@@ -408,14 +496,16 @@ protected function removeChild($productSku, $childSku)
408496
}
409497

410498
/**
499+
* Get child products
500+
*
411501
* @param string $productSku
412502
* @return string[]
413503
*/
414-
protected function getChildren($productSku)
504+
protected function getChildren(string $productSku)
415505
{
416506
$serviceInfo = [
417507
'rest' => [
418-
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
508+
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
419509
'httpMethod' => Request::HTTP_METHOD_GET
420510
],
421511
'soap' => [

dev/tests/integration/testsuite/Magento/Catalog/Controller/Product/CompareTest.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
namespace Magento\Catalog\Controller\Product;
88

9+
use Laminas\Stdlib\ParametersFactory;
10+
use Magento\Catalog\Api\Data\ProductInterface;
911
use Magento\Catalog\Model\ProductRepository;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Customer\Model\Visitor;
1014
use Magento\Framework\App\Request\Http as HttpRequest;
1115
use Magento\Framework\Data\Form\FormKey;
1216
use Magento\Framework\Message\MessageInterface;
17+
use Magento\Framework\Registry;
1318
use Magento\TestFramework\TestCase\AbstractController;
14-
use Magento\Customer\Model\Session;
15-
use Magento\Customer\Model\Visitor;
16-
use Laminas\Stdlib\ParametersFactory;
1719

1820
/**
1921
* Test compare product.
@@ -39,6 +41,9 @@ class CompareTest extends AbstractController
3941
/** @var ParametersFactory */
4042
private $parametersFactory;
4143

44+
/** @var Registry */
45+
private $registry;
46+
4247
/**
4348
* @inheritDoc
4449
*/
@@ -51,6 +56,7 @@ protected function setUp(): void
5156
$this->customerSession = $this->_objectManager->get(Session::class);
5257
$this->visitor = $this->_objectManager->get(Visitor::class);
5358
$this->parametersFactory = $this->_objectManager->get(ParametersFactory::class);
59+
$this->registry = $this->_objectManager->get(Registry::class);
5460
}
5561

5662
/**
@@ -85,7 +91,7 @@ public function testAddAction(): void
8591
$this->assertSessionMessages(
8692
$this->equalTo(
8793
[
88-
'You added product Simple Product 1 Name to the '.
94+
'You added product Simple Product 1 Name to the ' .
8995
'<a href="http://localhost/index.php/catalog/product_compare/">comparison list</a>.'
9096
]
9197
),
@@ -259,6 +265,31 @@ public function testRemoveActionProductNameXss(): void
259265
);
260266
}
261267

268+
/**
269+
* Test removing a product wich does not exist from compare list.
270+
*
271+
* @return void
272+
*/
273+
public function testRemoveActionWithNonExistentProduct(): void
274+
{
275+
$this->_requireVisitorWithTwoProducts();
276+
$removedProduct = $this->productRepository->get('simple_product_1');
277+
$redirectUrl = 'http://localhost/index.php/catalog/product_compare/index';
278+
$this->assertTrue($this->deleteProduct($removedProduct), "The product must be removed.");
279+
280+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
281+
$this->getRequest()->setParams(['product' => $removedProduct->getId()]);
282+
$server = $this->getRequest()->getServer();
283+
$server['HTTP_REFERER'] = $redirectUrl;
284+
$this->getRequest()->setServer($server);
285+
$this->dispatch('catalog/product_compare/remove/');
286+
287+
$this->assertSessionMessages($this->isEmpty());
288+
$this->assertRedirect($this->equalTo($redirectUrl));
289+
$restProduct = $this->productRepository->get('simple_product_2');
290+
$this->_assertCompareListEquals([$restProduct->getId()]);
291+
}
292+
262293
/**
263294
* Add not existing product to list of compared.
264295
*
@@ -486,4 +517,27 @@ protected function _assertCompareListEquals(array $expectedProductIds): void
486517
}
487518
$this->assertEquals($expectedProductIds, $actualProductIds, "Products in current visitor's compare list.");
488519
}
520+
521+
/**
522+
* Delete product in secure area
523+
*
524+
* @param ProductInterface $product
525+
* @return bool
526+
*/
527+
private function deleteProduct(ProductInterface $product): bool
528+
{
529+
$this->registry->unregister('isSecureArea');
530+
$this->registry->register('isSecureArea', true);
531+
532+
try {
533+
$result = $this->productRepository->delete($product);
534+
} catch (\Exception $e) {
535+
$result = false;
536+
}
537+
538+
$this->registry->unregister('isSecureArea');
539+
$this->registry->register('isSecureArea', false);
540+
541+
return $result;
542+
}
489543
}

0 commit comments

Comments
 (0)