-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent Default Source/Stock to be deleted #126
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2b3fd2
Prevent Default Source/Stock to be deleted
bartekszymanski 41e1e72
plugin configuration name and class fix
bartekszymanski 8e23d97
webapi test
bartekszymanski 1966b35
prevento show delete button on default stock detail page
bartekszymanski fed2f2b
Merge remote-tracking branch 'remotes/origin/develop' into prevent-de…
bartekszymanski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
app/code/Magento/InventoryCatalog/Plugin/Model/StockRepositoryPlugin.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,47 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\InventoryCatalog\Plugin\Model; | ||
|
||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; | ||
|
||
/** | ||
* Class provide Before Plugin on StockRepositoryInterface::deleteByItem to prevent default stock could be deleted | ||
*/ | ||
class StockRepositoryPlugin | ||
{ | ||
/** | ||
* @var DefaultStockProviderInterface | ||
*/ | ||
private $defaultStockProvider; | ||
|
||
/** | ||
* @param DefaultStockProviderInterface $defaultStockProvider | ||
*/ | ||
public function __construct(DefaultStockProviderInterface $defaultStockProvider) | ||
{ | ||
$this->defaultStockProvider = $defaultStockProvider; | ||
} | ||
|
||
/** | ||
* Prevent default source to be deleted | ||
* | ||
* @param StockRepositoryInterface $subject | ||
* @param int $stockId | ||
* | ||
* @return void | ||
* @throws CouldNotDeleteException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeDeleteById(StockRepositoryInterface $subject, int $stockId) | ||
{ | ||
if ($stockId === $this->defaultStockProvider->getId()) { | ||
throw new CouldNotDeleteException(__('Default Stock could not be deleted.')); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...nto/InventoryCatalog/Test/Api/StockRepository/CouldNotDeleteDefaultStockExceptionTest.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,69 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\InventoryCatalog\Test\Api\StockRepository; | ||
|
||
use Magento\Framework\Webapi\Exception; | ||
use Magento\Framework\Webapi\Rest\Request; | ||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; | ||
|
||
/** | ||
* Class CouldNotDeleteDefaultStockExceptionTest | ||
*/ | ||
class CouldNotDeleteDefaultStockExceptionTest extends WebapiAbstract | ||
{ | ||
/** | ||
* @var DefaultStockProviderInterface | ||
*/ | ||
private $defaultStockProvider; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class); | ||
} | ||
|
||
/** | ||
* Test that default Stock could not be deleted | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function testCouldNotDeleteException() | ||
{ | ||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => '/V1/inventory/stock/' . $this->defaultStockProvider->getId(), | ||
'httpMethod' => Request::HTTP_METHOD_DELETE, | ||
], | ||
'soap' => [ | ||
'service' => 'inventoryApiStockRepositoryV1', | ||
'operation' => 'inventoryApiStockRepositoryV1DeleteById', | ||
], | ||
]; | ||
$expectedMessage = 'Default Stock could not be deleted.'; | ||
try { | ||
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) ? $this->_webApiCall($serviceInfo) : | ||
$this->_webApiCall($serviceInfo, ['stockId' => $this->defaultStockProvider->getId()]); | ||
$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; | ||
} | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/code/Magento/InventoryCatalog/Ui/Component/Control/Stock/DeleteButton.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,62 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\InventoryCatalog\Ui\Component\Control\Stock; | ||
|
||
use Magento\Backend\Ui\Component\Control\DeleteButton as StockDeleteButton; | ||
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface; | ||
use Magento\InventoryApi\Api\Data\StockInterface; | ||
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
|
||
/** | ||
* Represents delete button with pre-configured options | ||
* Provide an ability to show delete button only when stock id is not default | ||
*/ | ||
class DeleteButton implements ButtonProviderInterface | ||
{ | ||
/** | ||
* @var StockDeleteButton | ||
*/ | ||
private $deleteButton; | ||
|
||
/** | ||
* @var DefaultStockProviderInterface | ||
*/ | ||
private $defaultStockProvider; | ||
|
||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @param StockDeleteButton $deleteButton | ||
* @param DefaultStockProviderInterface $defaultStockProvider | ||
* @param RequestInterface $request | ||
*/ | ||
public function __construct( | ||
StockDeleteButton $deleteButton, | ||
DefaultStockProviderInterface $defaultStockProvider, | ||
RequestInterface $request | ||
) { | ||
$this->deleteButton = $deleteButton; | ||
$this->defaultStockProvider = $defaultStockProvider; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getButtonData() | ||
{ | ||
if ((int)$this->request->getParam(StockInterface::STOCK_ID) === $this->defaultStockProvider->getId()) { | ||
return []; | ||
} | ||
|
||
return $this->deleteButton->getButtonData(); | ||
} | ||
} |
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,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\InventoryCatalog\Ui\Component\Control\Stock\DeleteButton"> | ||
<arguments> | ||
<argument name="deleteButton" xsi:type="object">Magento\Inventory\Ui\Component\Control\Stock\DeleteButton</argument> | ||
</arguments> | ||
</type> | ||
</config> |
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
14 changes: 14 additions & 0 deletions
14
app/code/Magento/InventoryCatalog/view/adminhtml/ui_component/inventory_stock_form.xml
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> | ||
<settings> | ||
<buttons> | ||
<button name="delete" class="Magento\InventoryCatalog\Ui\Component\Control\Stock\DeleteButton"/> | ||
</buttons> | ||
</settings> | ||
</form> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to cover this check with web-api test
Example of negative scenarios you could find in MSI web-api tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done