-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
- Loading branch information
Showing
16 changed files
with
511 additions
and
140 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
app/code/Magento/Catalog/Model/Product/Attribute/Backend/Url.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 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Product\Attribute\Backend; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute; | ||
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator; | ||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; | ||
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
class Url extends AbstractBackend | ||
{ | ||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private ScopeConfigInterface $config; | ||
|
||
/** | ||
* @param ScopeConfigInterface $config | ||
*/ | ||
public function __construct(ScopeConfigInterface $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Set Attribute instance, Rewrite for redefine attribute scope | ||
* | ||
* @param Attribute $attribute | ||
* @return $this | ||
*/ | ||
public function setAttribute($attribute) | ||
{ | ||
parent::setAttribute($attribute); | ||
$this->setScope($attribute); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Redefine Attribute scope | ||
* | ||
* @param Attribute $attribute | ||
* @return void | ||
*/ | ||
private function setScope(Attribute $attribute): void | ||
{ | ||
if ($this->config->getValue( | ||
ProductScopeRewriteGenerator::URL_REWRITE_SCOPE_CONFIG_PATH, | ||
ScopeInterface::SCOPE_STORE | ||
) == ProductScopeRewriteGenerator::WEBSITE_URL_REWRITE_SCOPE) { | ||
$attribute->setIsGlobal(ScopedAttributeInterface::SCOPE_WEBSITE); | ||
} else { | ||
$attribute->setIsGlobal(ScopedAttributeInterface::SCOPE_STORE); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
app/code/Magento/Catalog/Setup/Patch/Data/UpdateProductUrlKeyBackendModel.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,89 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Setup\Patch\Data; | ||
|
||
use Magento\Catalog\Model\Product\Attribute\Backend\Url; | ||
use Magento\Eav\Setup\EavSetupFactory; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
use Magento\Framework\Setup\Patch\PatchRevertableInterface; | ||
|
||
class UpdateProductUrlKeyBackendModel implements DataPatchInterface, PatchRevertableInterface | ||
{ | ||
/** | ||
* @var ModuleDataSetupInterface | ||
*/ | ||
private ModuleDataSetupInterface $moduleDataSetup; | ||
|
||
/** | ||
* @var EavSetupFactory | ||
*/ | ||
private EavSetupFactory $eavSetupFactory; | ||
|
||
/** | ||
* @param ModuleDataSetupInterface $moduleDataSetup | ||
* @param EavSetupFactory $eavSetupFactory | ||
*/ | ||
public function __construct( | ||
ModuleDataSetupInterface $moduleDataSetup, | ||
EavSetupFactory $eavSetupFactory | ||
) { | ||
$this->moduleDataSetup = $moduleDataSetup; | ||
$this->eavSetupFactory = $eavSetupFactory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getDependencies() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getAliases() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function apply() | ||
{ | ||
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); | ||
|
||
$eavSetup->updateAttribute( | ||
\Magento\Catalog\Model\Product::ENTITY, | ||
'url_key', | ||
[ | ||
'backend_model' => Url::class | ||
] | ||
); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function revert() | ||
{ | ||
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); | ||
|
||
$eavSetup->updateAttribute( | ||
\Magento\Catalog\Model\Product::ENTITY, | ||
'url_key', | ||
[ | ||
'backend_model' => '' | ||
] | ||
); | ||
return $this; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/UrlTest.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,54 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Backend; | ||
|
||
use Magento\Catalog\Model\Product\Attribute\Backend\Url; | ||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute; | ||
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator; | ||
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
/** | ||
* @var ScopeConfigInterface|MockObject | ||
*/ | ||
private ScopeConfigInterface $config; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->config = $this->createMock(ScopeConfigInterface::class); | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testSetAttribute(): void | ||
{ | ||
$attribute = $this->createMock(Attribute::class); | ||
$attribute->expects($this->once()) | ||
->method('__call') | ||
->with('setIsGlobal', [ScopedAttributeInterface::SCOPE_WEBSITE]); | ||
$this->config->expects($this->once()) | ||
->method('getValue') | ||
->with(ProductScopeRewriteGenerator::URL_REWRITE_SCOPE_CONFIG_PATH, ScopeInterface::SCOPE_STORE) | ||
->willReturn(ProductScopeRewriteGenerator::WEBSITE_URL_REWRITE_SCOPE); | ||
|
||
$url = new Url($this->config); | ||
$url->setAttribute($attribute); | ||
} | ||
} |
Oops, something went wrong.