forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request magento#171 from magento-engcom/prevent-delete-sto…
…ck-sales-channel Stock Removal. Throw an exception and allow to delete just unassigned stocks
- Loading branch information
Showing
8 changed files
with
136 additions
and
17 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...lugin/InventoryApi/StockRepository/PreventDeleting/AssignedToSalesChannelsStockPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Plugin\InventoryApi\StockRepository\PreventDeleting; | ||
|
||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
use Magento\InventorySales\Model\GetAssignedSalesChannelsForStockInterface; | ||
|
||
/** | ||
* Prevent deleting of Stock which assigned at least one Sales Channel | ||
*/ | ||
class AssignedToSalesChannelsStockPlugin | ||
{ | ||
/** | ||
* @var GetAssignedSalesChannelsForStockInterface | ||
*/ | ||
private $assignedSalesChannelsForStock; | ||
|
||
/** | ||
* @param GetAssignedSalesChannelsForStockInterface $assignedSalesChannelsForStock | ||
*/ | ||
public function __construct( | ||
GetAssignedSalesChannelsForStockInterface $assignedSalesChannelsForStock | ||
) { | ||
$this->assignedSalesChannelsForStock = $assignedSalesChannelsForStock; | ||
} | ||
|
||
/** | ||
* Prevent deleting of Stock which assigned at least one Sales Channel | ||
* | ||
* @param StockRepositoryInterface $subject | ||
* @param int $stockId | ||
* @return void | ||
* @throws CouldNotDeleteException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeDeleteById(StockRepositoryInterface $subject, int $stockId) | ||
{ | ||
$assignSalesChannels = $this->assignedSalesChannelsForStock->execute($stockId); | ||
if (count($assignSalesChannels)) { | ||
throw new CouldNotDeleteException(__('Stock has at least one sale channel and could not be deleted.')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ntoryCatalog/Test/Api/StockRepository/PreventAssignedToSalesChannelsStockDeletingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Test\Api\StockRepository; | ||
|
||
use Magento\Framework\Webapi\Exception; | ||
use Magento\Framework\Webapi\Rest\Request; | ||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
|
||
class PreventAssignedToSalesChannelsStockDeletingTest extends WebapiAbstract | ||
{ | ||
/** | ||
* @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_with_sales_channels.php | ||
* @throws \Exception | ||
*/ | ||
public function testCouldNotDeleteException() | ||
{ | ||
$stockId = 10; | ||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => '/V1/inventory/stock/' . $stockId, | ||
'httpMethod' => Request::HTTP_METHOD_DELETE, | ||
], | ||
'soap' => [ | ||
'service' => 'inventoryApiStockRepositoryV1', | ||
'operation' => 'inventoryApiStockRepositoryV1DeleteById', | ||
], | ||
]; | ||
$expectedMessage = 'Stock has at least one sale channel and could not be deleted.'; | ||
try { | ||
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) | ||
? $this->_webApiCall($serviceInfo) | ||
: $this->_webApiCall($serviceInfo, ['stockId' => $stockId]); | ||
$this->fail('Expected throwing exception'); | ||
} catch (\Exception $e) { | ||
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) { | ||
$errorData = $this->processRestExceptionResult($e); | ||
self::assertEquals($expectedMessage, $errorData['message']); | ||
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode()); | ||
} elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { | ||
$this->assertInstanceOf('SoapFault', $e); | ||
$this->checkSoapFault($e, $expectedMessage, 'env:Sender'); | ||
} else { | ||
throw $e; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters