Skip to content

Commit 00a70ec

Browse files
authored
Merge pull request #6537 from magento-tsg/2.4-develop-sidecar-pr12
[Sidecar] Fixes for 2.4 (pr12)
2 parents 1c78cdb + 1051f5b commit 00a70ec

24 files changed

+1579
-90
lines changed

dev/tests/api-functional/testsuite/Magento/Swatches/Api/ProductAttributeOptionManagementInterfaceTest.php

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Eav\Api\Data\AttributeOptionInterface;
1313
use Magento\Eav\Api\Data\AttributeOptionLabelInterface;
1414
use Magento\Eav\Model\AttributeRepository;
15+
use Magento\Framework\DataObject;
1516
use Magento\Framework\Webapi\Rest\Request;
1617
use Magento\Swatches\Model\ResourceModel\Swatch\Collection;
1718
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory;
@@ -25,29 +26,31 @@
2526
class ProductAttributeOptionManagementInterfaceTest extends WebapiAbstract
2627
{
2728
private const ATTRIBUTE_CODE = 'select_attribute';
29+
private const SERVICE_NAME_UPDATE = 'catalogProductAttributeOptionUpdateV1';
2830
private const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1';
2931
private const SERVICE_VERSION = 'V1';
3032
private const RESOURCE_PATH = '/V1/products/attributes';
3133

3234
/**
3335
* Test add option to swatch attribute
3436
*
37+
* @dataProvider addDataProvider
3538
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
3639
* @param array $data
3740
* @param array $payload
3841
* @param int $expectedSwatchType
3942
* @param string $expectedLabel
4043
* @param string $expectedValue
4144
*
42-
* @dataProvider addDataProvider
45+
* @return void
4346
*/
4447
public function testAdd(
4548
array $data,
4649
array $payload,
4750
int $expectedSwatchType,
4851
string $expectedLabel,
4952
string $expectedValue
50-
) {
53+
): void {
5154
$objectManager = Bootstrap::getObjectManager();
5255
/** @var $attributeRepository AttributeRepository */
5356
$attributeRepository = $objectManager->get(AttributeRepository::class);
@@ -74,7 +77,7 @@ public function testAdd(
7477
);
7578

7679
$this->assertNotNull($response);
77-
$optionId = (int) ltrim($response, 'id_');
80+
$optionId = (int)ltrim($response, 'id_');
7881
$swatch = $this->getSwatch($optionId);
7982
$this->assertEquals($expectedValue, $swatch->getValue());
8083
$this->assertEquals($expectedSwatchType, $swatch->getType());
@@ -83,11 +86,47 @@ public function testAdd(
8386
$this->assertEquals($expectedLabel, $options[2]->getLabel());
8487
}
8588

89+
/**
90+
* @magentoApiDataFixture Magento/Swatches/_files/text_swatch_attribute.php
91+
* @return void
92+
*/
93+
public function testUpdate(): void
94+
{
95+
$testAttributeCode = 'test_configurable';
96+
$optionData = [
97+
AttributeOptionInterface::LABEL => 'Fixture Option Changed',
98+
AttributeOptionInterface::VALUE => 'option_value',
99+
];
100+
101+
$existOptionLabel = 'option 1';
102+
$existAttributeOption = $this->getAttributeOption($testAttributeCode, $existOptionLabel);
103+
$optionId = $existAttributeOption['value'];
104+
105+
$response = $this->webApiCallAttributeOptions(
106+
$testAttributeCode,
107+
Request::HTTP_METHOD_PUT,
108+
'update',
109+
[
110+
'attributeCode' => $testAttributeCode,
111+
'optionId' => $optionId,
112+
'option' => $optionData,
113+
],
114+
$optionId
115+
);
116+
$this->assertTrue($response);
117+
$this->assertNotNull(
118+
$this->getAttributeOption(
119+
$testAttributeCode,
120+
$optionData[AttributeOptionInterface::LABEL]
121+
)
122+
);
123+
}
124+
86125
/**
87126
* @return array
88127
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
89128
*/
90-
public function addDataProvider()
129+
public function addDataProvider(): array
91130
{
92131
return [
93132
'visual swatch option with value' => [
@@ -212,14 +251,99 @@ public function addDataProvider()
212251
* Get swatch model
213252
*
214253
* @param int $optionId
215-
* @return Swatch
254+
* @return DataObject
216255
*/
217-
private function getSwatch(int $optionId)
256+
private function getSwatch(int $optionId): DataObject
218257
{
219258
/** @var Collection $collection */
220259
$collection = Bootstrap::getObjectManager()->get(CollectionFactory::class)->create();
221260
$collection->addFieldToFilter('option_id', $optionId);
222261
$collection->setPageSize(1);
262+
223263
return $collection->getFirstItem();
224264
}
265+
266+
/**
267+
* Perform Web API call to the system under test
268+
*
269+
* @param string $attributeCode
270+
* @param string $httpMethod
271+
* @param string $soapMethod
272+
* @param array $arguments
273+
* @param null $storeCode
274+
* @param null $optionId
275+
* @return array|bool|float|int|string
276+
*/
277+
private function webApiCallAttributeOptions(
278+
string $attributeCode,
279+
string $httpMethod,
280+
string $soapMethod,
281+
array $arguments = [],
282+
$optionId = null,
283+
$storeCode = null
284+
) {
285+
$resourcePath = self::RESOURCE_PATH . "/{$attributeCode}/options";
286+
if ($optionId) {
287+
$resourcePath .= '/' . $optionId;
288+
}
289+
$serviceName = $soapMethod === 'update' ? self::SERVICE_NAME_UPDATE : self::SERVICE_NAME;
290+
$serviceInfo = [
291+
'rest' => [
292+
'resourcePath' => $resourcePath,
293+
'httpMethod' => $httpMethod,
294+
],
295+
'soap' => [
296+
'service' => $serviceName,
297+
'serviceVersion' => self::SERVICE_VERSION,
298+
'operation' => $serviceName . $soapMethod,
299+
],
300+
];
301+
302+
return $this->_webApiCall($serviceInfo, $arguments, null, $storeCode);
303+
}
304+
305+
/**
306+
* Get Attribute options by attribute code
307+
*
308+
* @param string $testAttributeCode
309+
* @param string|null $storeCode
310+
* @return array|bool|float|int|string
311+
*/
312+
private function getAttributeOptions(string $testAttributeCode, ?string $storeCode = null)
313+
{
314+
return $this->webApiCallAttributeOptions(
315+
$testAttributeCode,
316+
Request::HTTP_METHOD_GET,
317+
'getItems',
318+
['attributeCode' => $testAttributeCode],
319+
null,
320+
$storeCode
321+
);
322+
}
323+
324+
/**
325+
* Get Attribute option by attribute code
326+
*
327+
* @param string $attributeCode
328+
* @param string $optionLabel
329+
* @param string|null $storeCode
330+
* @return array|null
331+
*/
332+
private function getAttributeOption(
333+
string $attributeCode,
334+
string $optionLabel,
335+
?string $storeCode = null
336+
): ?array {
337+
$attributeOptions = $this->getAttributeOptions($attributeCode, $storeCode);
338+
$option = null;
339+
/** @var array $attributeOption */
340+
foreach ($attributeOptions as $attributeOption) {
341+
if ($attributeOption['label'] === $optionLabel) {
342+
$option = $attributeOption;
343+
break;
344+
}
345+
}
346+
347+
return $option;
348+
}
225349
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Module\Manager;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Base alert's tab test logic
21+
*/
22+
abstract class AbstractAlertTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
protected $objectManager;
26+
27+
/** @var RequestInterface */
28+
protected $request;
29+
30+
/** @var StoreManagerInterface */
31+
private $storeManager;
32+
33+
/** @var ProductResource */
34+
private $productResource;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public static function setUpBeforeClass(): void
40+
{
41+
parent::setUpBeforeClass();
42+
43+
$objectManager = Bootstrap::getObjectManager();
44+
/** @var Manager $moduleManager */
45+
$moduleManager = $objectManager->get(Manager::class);
46+
//This check is needed because module Magento_Catalog is independent of Magento_ProductAlert
47+
if (!$moduleManager->isEnabled('Magento_ProductAlert')) {
48+
self::markTestSkipped('Magento_ProductAlert module disabled.');
49+
}
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp(): void
56+
{
57+
parent::setUp();
58+
59+
$this->objectManager = Bootstrap::getObjectManager();
60+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
61+
$this->request = $this->objectManager->get(RequestInterface::class);
62+
$this->productResource = $this->objectManager->get(ProductResource::class);
63+
}
64+
65+
/**
66+
* Prepare request
67+
*
68+
* @param string|null $sku
69+
* @param string|null $storeCode
70+
* @return void
71+
*/
72+
protected function prepareRequest(?string $sku = null, ?string $storeCode = null): void
73+
{
74+
$productId = (int)$this->productResource->getIdBySku($sku);
75+
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : null;
76+
$this->request->setParams(['id' => $productId, 'store' => $storeId]);
77+
}
78+
79+
/**
80+
* Assert grid url
81+
*
82+
* @param string $url
83+
* @param string|null $storeCode
84+
* @return void
85+
*/
86+
protected function assertGridUrl(string $url, ?string $storeCode): void
87+
{
88+
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : Store::DEFAULT_STORE_ID;
89+
$this->assertStringContainsString(sprintf('/store/%s', $storeId), $url);
90+
}
91+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts;
9+
10+
use Magento\Framework\View\LayoutInterface;
11+
12+
/**
13+
* Check price alert grid
14+
*
15+
* @see \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Price
16+
*
17+
* @magentoAppArea adminhtml
18+
*/
19+
class PriceTest extends AbstractAlertTest
20+
{
21+
/** @var Price */
22+
private $block;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Price::class);
32+
}
33+
34+
/**
35+
* @dataProvider alertsDataProvider
36+
*
37+
* @magentoDbIsolation disabled
38+
*
39+
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
40+
* @magentoDataFixture Magento/ProductAlert/_files/price_alert_on_second_website.php
41+
*
42+
* @param string $sku
43+
* @param string $expectedEmail
44+
* @param string|null $storeCode
45+
* @return void
46+
*/
47+
public function testGridCollectionWithStoreId(string $sku, string $expectedEmail, ?string $storeCode = null): void
48+
{
49+
$this->prepareRequest($sku, $storeCode);
50+
$collection = $this->block->getPreparedCollection();
51+
$this->assertCount(1, $collection);
52+
$this->assertEquals($expectedEmail, $collection->getFirstItem()->getEmail());
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public function alertsDataProvider(): array
59+
{
60+
return [
61+
'without_store_id_filter' => [
62+
'product_sku' => 'simple',
63+
'expected_customer_emails' => 'customer@example.com',
64+
],
65+
'with_store_id_filter' => [
66+
'product_sku' => 'simple_on_second_website_for_price_alert',
67+
'expected_customer_emails' => 'customer_second_ws_with_addr@example.com',
68+
'store_code' => 'fixture_third_store',
69+
],
70+
];
71+
}
72+
73+
/**
74+
* @dataProvider storeProvider
75+
*
76+
* @param string|null $storeCode
77+
* @return void
78+
*/
79+
public function testGetGridUrl(?string $storeCode): void
80+
{
81+
$this->prepareRequest(null, $storeCode);
82+
$this->assertGridUrl($this->block->getGridUrl(), $storeCode);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function storeProvider(): array
89+
{
90+
return [
91+
'without_store_id_param' => [
92+
'store_code' => null,
93+
],
94+
'with_store_id_param' => [
95+
'store_code' => 'default',
96+
],
97+
];
98+
}
99+
}

0 commit comments

Comments
 (0)