-
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
Issue 1306 rename website breaks grid #1344
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
00646b4
FIX for issue https://github.com/magento-engcom/msi/issues/1306
phoenix128 6397e66
Keep data consistency with inventory_stock_sales_channel when a websi…
phoenix128 e22d31c
Refactoring for AssignWebsiteToDefaultStock moving it from observer t…
phoenix128 222da52
FIX PHPMD + PHPCS validation
phoenix128 965dd8d
Modified plugin class name to reflect its responsability + switched t…
phoenix128 e079a73
Return null from getCodeFromDatabase if the required website does not…
phoenix128 fabd833
Created interface and preference for UpdateSalesChannelWebsiteCode
phoenix128 9998600
Integration test for website rename scenario and stock link consistency
phoenix128 7166977
MSI-1306: Rename website breaks grid
40fbe9d
Merge remote-tracking branch 'origin/2.3-develop' into issue-1306-ren…
389e299
MSI-1306: Rename website breaks grid
81d2cb8
MSI-1306: Rename website breaks grid
3a44479
MSI-1306: Rename website breaks grid
ff0b71f
MSI-1306: Rename website breaks grid
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
44 changes: 44 additions & 0 deletions
44
app/code/Magento/InventorySales/Model/ResourceModel/GetWebsiteCodeByWebsiteId.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,44 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model\ResourceModel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
|
||
/** | ||
* Get website code by website id | ||
*/ | ||
class GetWebsiteCodeByWebsiteId | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @param int $websiteId | ||
* @return string|null | ||
*/ | ||
public function execute(int $websiteId): ?string | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTableName('store_website'); | ||
$selectQry = $connection->select()->from($tableName, 'code')->where('website_id = ?', $websiteId); | ||
|
||
$result = $connection->fetchOne($selectQry); | ||
return (false === $result) ? null : $result; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/code/Magento/InventorySales/Model/ResourceModel/UpdateSalesChannelWebsiteCode.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,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model\ResourceModel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
|
||
/** | ||
* This class handles website code change and should not be used directly, but only | ||
* from \Magento\InventorySales\Plugin\Store\WebsiteResourcePlugin to keep a soft integrity | ||
* between 'store_website' table and 'inventory_stock_sales_channel' table on changes. | ||
*/ | ||
class UpdateSalesChannelWebsiteCode | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @param string $oldCode | ||
* @param string $newCode | ||
* @return void | ||
*/ | ||
public function execute( | ||
string $oldCode, | ||
string $newCode | ||
): void { | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel'); | ||
|
||
$connection->update( | ||
$tableName, | ||
[ | ||
SalesChannelInterface::CODE => $newCode, | ||
], | ||
[ | ||
SalesChannelInterface::TYPE . ' = ?' => SalesChannelInterface::TYPE_WEBSITE, | ||
SalesChannelInterface::CODE . ' = ?' => $oldCode, | ||
] | ||
); | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
...orySales/Plugin/Store/Model/ResourceModel/Website/UpdateSalesChannelWebsiteCodePlugin.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,73 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website; | ||
|
||
use Magento\Framework\Exception\CouldNotSaveException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\Model\AbstractModel; | ||
use Magento\Framework\Validation\ValidationException; | ||
use Magento\InventorySales\Model\ResourceModel\GetWebsiteCodeByWebsiteId; | ||
use Magento\InventorySales\Model\ResourceModel\UpdateSalesChannelWebsiteCode; | ||
use Magento\Store\Api\Data\WebsiteInterface; | ||
use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel; | ||
use Magento\Store\Model\Website; | ||
|
||
class UpdateSalesChannelWebsiteCodePlugin | ||
{ | ||
/** | ||
* @var UpdateSalesChannelWebsiteCode | ||
*/ | ||
private $updateSalesChannelWebsiteCode; | ||
|
||
/** | ||
* @var GetWebsiteCodeByWebsiteId | ||
*/ | ||
private $getWebsiteCodeByWebsiteId; | ||
|
||
/** | ||
* @param UpdateSalesChannelWebsiteCode $updateSalesChannelWebsiteCode | ||
* @param GetWebsiteCodeByWebsiteId $getWebsiteCodeByWebsiteId | ||
*/ | ||
public function __construct( | ||
UpdateSalesChannelWebsiteCode $updateSalesChannelWebsiteCode, | ||
GetWebsiteCodeByWebsiteId $getWebsiteCodeByWebsiteId | ||
) { | ||
$this->updateSalesChannelWebsiteCode = $updateSalesChannelWebsiteCode; | ||
$this->getWebsiteCodeByWebsiteId = $getWebsiteCodeByWebsiteId; | ||
} | ||
|
||
/** | ||
* @param WebsiteResourceModel $subject | ||
* @param callable $proceed | ||
* @param Website|AbstractModel $website | ||
* @return WebsiteResourceModel | ||
* @throws CouldNotSaveException | ||
* @throws NoSuchEntityException | ||
* @throws ValidationException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function aroundSave( | ||
WebsiteResourceModel $subject, | ||
callable $proceed, | ||
AbstractModel $website | ||
) { | ||
$newCode = $website->getCode(); | ||
$oldCode = null; | ||
|
||
if (null !== $website->getId()) { | ||
$oldCode = $this->getWebsiteCodeByWebsiteId->execute((int)$website->getId()); | ||
} | ||
|
||
$result = $proceed($website); | ||
|
||
if (($oldCode !== null) && ($oldCode !== WebsiteInterface::ADMIN_CODE) && ($oldCode !== $newCode)) { | ||
$this->updateSalesChannelWebsiteCode->execute($oldCode, $newCode); | ||
} | ||
return $result; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/code/Magento/InventorySales/Test/Integration/Website/RenameWebsiteToStockLinkTest.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,73 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Test\Integration\Website; | ||
|
||
use Magento\InventorySalesApi\Model\GetAssignedStockIdForWebsiteInterface; | ||
use Magento\Store\Model\Website; | ||
use Magento\Store\Model\ResourceModel\Website as WebsiteResource; | ||
use Magento\Store\Model\WebsiteFactory; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RenameWebsiteToStockLinkTest extends TestCase | ||
{ | ||
/** | ||
* @var WebsiteFactory | ||
*/ | ||
private $websiteFactory; | ||
|
||
/** | ||
* @var GetAssignedStockIdForWebsiteInterface | ||
*/ | ||
private $getAssignedStockIdForWebsite; | ||
|
||
/** | ||
* @var WebsiteResource | ||
*/ | ||
private $websiteResource; | ||
|
||
protected function setUp() | ||
{ | ||
$this->websiteFactory = Bootstrap::getObjectManager()->get(WebsiteFactory::class); | ||
$this->getAssignedStockIdForWebsite = Bootstrap::getObjectManager()->get( | ||
GetAssignedStockIdForWebsiteInterface::class | ||
); | ||
$this->websiteResource = Bootstrap::getObjectManager()->get(WebsiteResource::class); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
* @magentoDbIsolation enabled | ||
*/ | ||
public function testRenameWebsiteToStockLink() | ||
{ | ||
$oldWebsiteCode = 'old_website_code'; | ||
$newWebsiteCode = 'new_website_code'; | ||
|
||
/** @var Website $website */ | ||
$website = $this->websiteFactory->create(); | ||
$website->setCode($oldWebsiteCode); | ||
$this->websiteResource->save($website); | ||
$websiteId = $website->getId(); | ||
|
||
$website = $this->websiteFactory->create(); | ||
$this->websiteResource->load($website, $websiteId); | ||
$website->setCode($newWebsiteCode); | ||
$this->websiteResource->save($website); | ||
|
||
self::assertNull( | ||
$this->getAssignedStockIdForWebsite->execute($oldWebsiteCode), | ||
'Old website link was not removed' | ||
); | ||
|
||
self::assertNotNull( | ||
$this->getAssignedStockIdForWebsite->execute($newWebsiteCode), | ||
'Website link was not renamed' | ||
); | ||
} | ||
} |
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
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.
Looks like we also need to update delete logic
website_delete_after
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.
An observer after delete seems to be an efficient way and seems to be working, do you prefer to pluginize it?
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 in: #1356