Skip to content

Commit

Permalink
Merge pull request #82 from magento-tsg/MC-40776
Browse files Browse the repository at this point in the history
[Arrows] MC-40776: [MSI] Inventory sources with the same names can be created
  • Loading branch information
zakdma authored Mar 11, 2021
2 parents e5f5ee7 + 5c25197 commit febea46
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
11 changes: 9 additions & 2 deletions Inventory/Model/ResourceModel/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\InventoryApi\Model\SourceCarrierLinkManagementInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\Framework\Model\ResourceModel\PredefinedId;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Model\SourceCarrierLinkManagementInterface;

/**
* Implementation of basic operations for Source entity for specific db layer
Expand Down Expand Up @@ -63,6 +63,12 @@ public function __construct(
protected function _construct()
{
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_CODE);
$this->addUniqueField(
[
'field' => SourceInterface::NAME,
'title' => 'Name'
]
);
}

/**
Expand All @@ -84,6 +90,7 @@ public function save(AbstractModel $object)
$connection = $this->getConnection();
$connection->beginTransaction();
try {
$object->isObjectNew(!$this->isObjectNotNew($object));
parent::save($object);
/** @var SourceInterface $object */
$this->sourceCarrierLinkManagement->saveCarrierLinksBySource($object);
Expand Down
19 changes: 10 additions & 9 deletions InventoryAdminUi/Controller/Adminhtml/Source/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Validation\ValidationException;
use Magento\Inventory\Model\Source;
use Magento\InventoryAdminUi\Model\Source\SourceHydrator;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryAdminUi\Model\Source\SourceHydrator;
use Magento\Framework\App\Action\HttpPostActionInterface;

/**
* Source save controller.
Expand Down Expand Up @@ -99,15 +100,15 @@ public function execute(): ResultInterface
$this->messageManager->addErrorMessage($localizedError->getMessage());
}
$this->_session->setSourceFormData($requestData);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCode);
$this->processRedirectAfterFailureSave($resultRedirect, $source);
} catch (CouldNotSaveException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_session->setSourceFormData($requestData);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCode);
$this->processRedirectAfterFailureSave($resultRedirect, $source);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Could not save Source.'));
$this->_session->setSourceFormData($requestData);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCode);
$this->processRedirectAfterFailureSave($resultRedirect, $source);
}
return $resultRedirect;
}
Expand Down Expand Up @@ -177,18 +178,18 @@ private function processRedirectAfterSuccessSave(Redirect $resultRedirect, strin
* Get redirect url after unsuccessful source save.
*
* @param Redirect $resultRedirect
* @param string|null $sourceCode
* @param null|SourceInterface $source
* @return void
*/
private function processRedirectAfterFailureSave(Redirect $resultRedirect, string $sourceCode = null)
private function processRedirectAfterFailureSave(Redirect $resultRedirect, ?SourceInterface $source = null)
{
if (null === $sourceCode) {
if (!$source || $source->isObjectNew()) {
$resultRedirect->setPath('*/*/new');
} else {
$resultRedirect->setPath(
'*/*/edit',
[
SourceInterface::SOURCE_CODE => $sourceCode,
SourceInterface::SOURCE_CODE => $source->getSourceCode(),
'_current' => true,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\InventoryAdminUi\Test\Integration\Controller\Adminhtml\Source;

use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Message\MessageInterface;
use Magento\InventoryAdminUi\Controller\Adminhtml\Source\Save;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\TestFramework\TestCase\AbstractBackendController;
Expand Down Expand Up @@ -54,6 +55,25 @@ public function testExecute(): void
$this->assertEquals('182', $source->getRegionId());
}

/**
* Verify, source will not be saved with name that already exists in database.
*
* @return void
*/
public function testValidateUniqueName(): void
{
$requestData = $this->getRequestData();
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($requestData);
$this->dispatch('backend/inventory/source/save');
$this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
$requestData['general']['source_code'] .= '_new';
$this->getRequest()->setPostValue($requestData);
$this->dispatch('backend/inventory/source/save');
$this->assertSessionMessages($this->equalTo(['Could not save Source']), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('inventory/source/new'));
}

/**
* Data for test.
*
Expand All @@ -62,25 +82,24 @@ public function testExecute(): void
private function getRequestData(): array
{
return [
'general' =>
[
'source_code' => 'test_source_with_region_id_and_region',
'name' => 'Test Source With Region ID And Region',
'latitude' => '',
'longitude' => '',
'contact_name' => '',
'email' => '',
'phone' => '',
'fax' => '',
'region' => 'Ain',
'city' => '',
'street' => '',
'postcode' => '12345',
'enabled' => '1',
'description' => '',
'country_id' => 'FR',
'region_id' => '182',
],
'general' => [
'source_code' => 'test_source_with_region_id_and_region',
'name' => 'Test Source With Region ID And Region',
'latitude' => '',
'longitude' => '',
'contact_name' => '',
'email' => '',
'phone' => '',
'fax' => '',
'region' => 'Ain',
'city' => '',
'street' => '',
'postcode' => '12345',
'enabled' => '1',
'description' => '',
'country_id' => 'FR',
'region_id' => '182',
],
];
}
}

0 comments on commit febea46

Please sign in to comment.