Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Jan 18, 2025
2 parents 3858d37 + 2d62730 commit e07282c
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 140 deletions.
62 changes: 62 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Attribute/Backend/Url.php
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);
}
}
}
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;
}
}
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);
}
}
Loading

0 comments on commit e07282c

Please sign in to comment.