Skip to content
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 5 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'));
Copy link

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
}
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;
}
}
}
}
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();
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/InventoryCatalog/etc/adminhtml/di.xml
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>
3 changes: 3 additions & 0 deletions app/code/Magento/InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\InventoryCatalog\Api\DefaultStockProviderInterface" type="Magento\InventoryCatalog\Model\DefaultStockProvider"/>
<preference for="Magento\InventoryCatalog\Api\DefaultSourceProviderInterface" type="Magento\InventoryCatalog\Model\DefaultSourceProvider"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
<plugin name="prevent_default_stock_deletion" type="Magento\InventoryCatalog\Plugin\Model\StockRepositoryPlugin" sortOrder="1"/>
</type>
</config>
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>