Skip to content

Commit 95b1233

Browse files
authored
Merge pull request #6498 from magento-tsg/2.4-develop-sidecar-pr9
[Sidecar] Fixes for 2.4 (pr9)
2 parents 427f9bf + 18ee9b1 commit 95b1233

File tree

17 files changed

+1449
-10
lines changed

17 files changed

+1449
-10
lines changed

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryRepositoryTest.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,51 @@ public function testDeleteNoSuchEntityException()
184184

185185
/**
186186
* @dataProvider deleteSystemOrRootDataProvider
187+
*
188+
* @param int $categoryId
189+
* @param string $exceptionMsg
190+
* @return void
187191
*/
188-
public function testDeleteSystemOrRoot()
192+
public function testDeleteSystemOrRoot(int $categoryId, string $exceptionMsg): void
189193
{
190194
$this->expectException(\Exception::class);
195+
$this->expectExceptionMessage($exceptionMsg);
191196

192-
$this->deleteCategory($this->modelId);
197+
$this->deleteCategory($categoryId);
193198
}
194199

195-
public function deleteSystemOrRootDataProvider()
200+
/**
201+
* @return array
202+
*/
203+
public function deleteSystemOrRootDataProvider(): array
196204
{
197205
return [
198-
[Category::TREE_ROOT_ID],
199-
[2] //Default root category
206+
'system_category' => [
207+
'category_id' => Category::TREE_ROOT_ID,
208+
'exception_message' => $this->buildExceptionMessage(Category::TREE_ROOT_ID),
209+
],
210+
'root_category' => [
211+
'category_id' => 2,
212+
'exception_message' => $this->buildExceptionMessage(2),
213+
],
200214
];
201215
}
202216

217+
/**
218+
* Build response error message
219+
*
220+
* @param int $categoryId
221+
* @return string
222+
*/
223+
private function buildExceptionMessage(int $categoryId): string
224+
{
225+
$translatedMsg = (string)__('Cannot delete category with id %1');
226+
227+
return TESTS_WEB_API_ADAPTER === self::ADAPTER_REST
228+
? sprintf('{"message":"%s","parameters":["%u"]}', $translatedMsg, $categoryId)
229+
: $translatedMsg;
230+
}
231+
203232
/**
204233
* @magentoApiDataFixture Magento/Catalog/_files/category.php
205234
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\Api;
9+
10+
use Magento\Catalog\Model\ProductWebsiteLink;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Webapi\Rest\Request;
13+
use Magento\Store\Api\WebsiteRepositoryInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\WebapiAbstract;
16+
17+
/**
18+
* Tests to check products to websites assigning.
19+
*
20+
* @see \Magento\Catalog\Model\ProductWebsiteLinkRepository
21+
*
22+
* @magentoAppIsolation enabled
23+
*/
24+
class ProductWebsiteLinkRepositoryTest extends WebapiAbstract
25+
{
26+
const SERVICE_NAME = 'catalogProductWebsiteLinkRepositoryV1';
27+
const SERVICE_VERSION = 'V1';
28+
29+
/** @var ObjectManagerInterface */
30+
private $objectManager;
31+
32+
/** @var ProductRepositoryInterface */
33+
private $productRepository;
34+
35+
/** @var WebsiteRepositoryInterface */
36+
private $websiteRepository;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
47+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
48+
}
49+
50+
/**
51+
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
52+
*
53+
* @return void
54+
*/
55+
public function testSaveWebsiteLinkWithUnexistingWebsiteId(): void
56+
{
57+
$pattern = '/(Could\\snot\\sassign\\sproduct)+([\\s\\S]*)(to\\swebsites)+([\\s\\S]*)/';
58+
$unexistingWebsiteId = 8932568989;
59+
$serviceInfo = $this->fillServiceInfo('/V1/products/:sku/websites', Request::HTTP_METHOD_POST, 'Save');
60+
$requestData = [
61+
'productWebsiteLink' => [
62+
ProductWebsiteLink::KEY_SKU => 'simple2',
63+
ProductWebsiteLink::WEBSITE_ID => $unexistingWebsiteId,
64+
],
65+
];
66+
$this->expectException(\Exception::class);
67+
$this->expectExceptionMessageMatches($pattern);
68+
$this->_webApiCall($serviceInfo, $requestData);
69+
}
70+
71+
/**
72+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
73+
*
74+
* @return void
75+
*/
76+
public function testDeleteWebsiteLink(): void
77+
{
78+
$productSku = 'unique-simple-azaza';
79+
$websiteId = (int)$this->websiteRepository->get('second_website')->getId();
80+
$resourcePath = sprintf('/V1/products/%s/websites/%u', $productSku, $websiteId);
81+
$serviceInfo = $this->fillServiceInfo($resourcePath, Request::HTTP_METHOD_DELETE, 'DeleteById');
82+
$this->_webApiCall(
83+
$serviceInfo,
84+
[ProductWebsiteLink::KEY_SKU => $productSku, ProductWebsiteLink::WEBSITE_ID => $websiteId]
85+
);
86+
$product = $this->productRepository->get($productSku, false, null, true);
87+
$this->assertNotContains($websiteId, $product->getWebsiteIds());
88+
}
89+
90+
/**
91+
* Fill service information
92+
*
93+
* @param string $resourcePath
94+
* @param string $httpMethod
95+
* @param string $operation
96+
* @return array
97+
*/
98+
private function fillServiceInfo(string $resourcePath, string $httpMethod, string $operation): array
99+
{
100+
return [
101+
'rest' => ['resourcePath' => $resourcePath, 'httpMethod' => $httpMethod],
102+
'soap' => [
103+
'service' => self::SERVICE_NAME,
104+
'serviceVersion' => self::SERVICE_VERSION,
105+
'operation' => self::SERVICE_NAME . $operation,
106+
],
107+
];
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Api\ProductRepositoryInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Check stock alert grid
19+
*
20+
* @see \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Stock
21+
*
22+
* @magentoAppArea adminhtml
23+
*/
24+
class StockTest extends TestCase
25+
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var Stock */
30+
private $block;
31+
32+
/** @var StoreManagerInterface */
33+
private $storeManager;
34+
35+
/** @var ProductRepositoryInterface */
36+
private $productRepository;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Stock::class);
47+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
48+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
49+
}
50+
51+
/**
52+
* @dataProvider alertsDataProvider
53+
*
54+
* @magentoDbIsolation disabled
55+
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
56+
* @magentoDataFixture Magento/ProductAlert/_files/stock_alert_on_second_website.php
57+
*
58+
* @param string $sku
59+
* @param string $expectedEmail
60+
* @param string|null $storeCode
61+
* @return void
62+
*/
63+
public function testGridCollectionWithStoreId(string $sku, string $expectedEmail, ?string $storeCode = null): void
64+
{
65+
$productId = (int)$this->productRepository->get($sku)->getId();
66+
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : null;
67+
$this->block->getRequest()->setParams(['id' => $productId, 'store' => $storeId]);
68+
$collection = $this->block->getPreparedCollection();
69+
$this->assertCount(1, $collection);
70+
$this->assertEquals($expectedEmail, $collection->getFirstItem()->getEmail());
71+
}
72+
73+
/**
74+
* @return array
75+
*/
76+
public function alertsDataProvider(): array
77+
{
78+
return [
79+
'without_store_id_filter' => [
80+
'product_sku' => 'simple',
81+
'expected_customer_emails' => 'customer@example.com',
82+
],
83+
'with_store_id_filter' => [
84+
'product_sku' => 'simple_on_second_website',
85+
'expected_customer_emails' => 'customer_second_ws_with_addr@example.com',
86+
'store_code' => 'fixture_third_store',
87+
],
88+
];
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Model\Product\Attribute\Backend;
9+
10+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Eav\Model\Config;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test class for backend stock attribute model.
20+
*
21+
* @see \Magento\Catalog\Model\Product\Attribute\Backend\Stock
22+
*
23+
* @magentoAppArea adminhtml
24+
*/
25+
class StockTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var ProductInterfaceFactory */
31+
private $productFactory;
32+
33+
/** @var Stock */
34+
private $model;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp(): void
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->productFactory = $this->objectManager->get(ProductInterfaceFactory::class);
45+
$this->model = $this->objectManager->get(Stock::class);
46+
$this->model->setAttribute(
47+
$this->objectManager->get(Config::class)->getAttribute(Product::ENTITY, 'quantity_and_stock_status')
48+
);
49+
}
50+
51+
/**
52+
* @return void
53+
*/
54+
public function testValidate(): void
55+
{
56+
$this->expectException(LocalizedException::class);
57+
$this->expectErrorMessage((string)__('Please enter a valid number in this field.'));
58+
$product = $this->productFactory->create();
59+
$product->setQuantityAndStockStatus(['qty' => 'string']);
60+
$this->model->validate($product);
61+
}
62+
}

0 commit comments

Comments
 (0)