-
Notifications
You must be signed in to change notification settings - Fork 248
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 #1344 from magento-engcom/issue-1306-rename-websit…
…e-breaks-grid Issue 1306 rename website breaks grid
- Loading branch information
Showing
8 changed files
with
276 additions
and
14 deletions.
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