|
| 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 | +} |
0 commit comments