Skip to content

Commit 2d62730

Browse files
authored
Merge pull request #9397 from adobe-commerce-tier-4/PR-2024-12-03
[Support Tier-4 APLAPANA] 12-03-2024 Regular delivery of bugfixes and improvements
2 parents 971142b + c36b5d2 commit 2d62730

File tree

16 files changed

+511
-140
lines changed

16 files changed

+511
-140
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Attribute\Backend;
9+
10+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
11+
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator;
12+
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
13+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
17+
class Url extends AbstractBackend
18+
{
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private ScopeConfigInterface $config;
23+
24+
/**
25+
* @param ScopeConfigInterface $config
26+
*/
27+
public function __construct(ScopeConfigInterface $config)
28+
{
29+
$this->config = $config;
30+
}
31+
32+
/**
33+
* Set Attribute instance, Rewrite for redefine attribute scope
34+
*
35+
* @param Attribute $attribute
36+
* @return $this
37+
*/
38+
public function setAttribute($attribute)
39+
{
40+
parent::setAttribute($attribute);
41+
$this->setScope($attribute);
42+
return $this;
43+
}
44+
45+
/**
46+
* Redefine Attribute scope
47+
*
48+
* @param Attribute $attribute
49+
* @return void
50+
*/
51+
private function setScope(Attribute $attribute): void
52+
{
53+
if ($this->config->getValue(
54+
ProductScopeRewriteGenerator::URL_REWRITE_SCOPE_CONFIG_PATH,
55+
ScopeInterface::SCOPE_STORE
56+
) == ProductScopeRewriteGenerator::WEBSITE_URL_REWRITE_SCOPE) {
57+
$attribute->setIsGlobal(ScopedAttributeInterface::SCOPE_WEBSITE);
58+
} else {
59+
$attribute->setIsGlobal(ScopedAttributeInterface::SCOPE_STORE);
60+
}
61+
}
62+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Setup\Patch\Data;
9+
10+
use Magento\Catalog\Model\Product\Attribute\Backend\Url;
11+
use Magento\Eav\Setup\EavSetupFactory;
12+
use Magento\Framework\Setup\ModuleDataSetupInterface;
13+
use Magento\Framework\Setup\Patch\DataPatchInterface;
14+
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
15+
16+
class UpdateProductUrlKeyBackendModel implements DataPatchInterface, PatchRevertableInterface
17+
{
18+
/**
19+
* @var ModuleDataSetupInterface
20+
*/
21+
private ModuleDataSetupInterface $moduleDataSetup;
22+
23+
/**
24+
* @var EavSetupFactory
25+
*/
26+
private EavSetupFactory $eavSetupFactory;
27+
28+
/**
29+
* @param ModuleDataSetupInterface $moduleDataSetup
30+
* @param EavSetupFactory $eavSetupFactory
31+
*/
32+
public function __construct(
33+
ModuleDataSetupInterface $moduleDataSetup,
34+
EavSetupFactory $eavSetupFactory
35+
) {
36+
$this->moduleDataSetup = $moduleDataSetup;
37+
$this->eavSetupFactory = $eavSetupFactory;
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public static function getDependencies()
44+
{
45+
return [];
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function getAliases()
52+
{
53+
return [];
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function apply()
60+
{
61+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
62+
63+
$eavSetup->updateAttribute(
64+
\Magento\Catalog\Model\Product::ENTITY,
65+
'url_key',
66+
[
67+
'backend_model' => Url::class
68+
]
69+
);
70+
return $this;
71+
}
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function revert()
77+
{
78+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
79+
80+
$eavSetup->updateAttribute(
81+
\Magento\Catalog\Model\Product::ENTITY,
82+
'url_key',
83+
[
84+
'backend_model' => ''
85+
]
86+
);
87+
return $this;
88+
}
89+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Backend;
9+
10+
use Magento\Catalog\Model\Product\Attribute\Backend\Url;
11+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
12+
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator;
13+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
use PHPUnit\Framework\MockObject\Exception;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class UrlTest extends TestCase
21+
{
22+
/**
23+
* @var ScopeConfigInterface|MockObject
24+
*/
25+
private ScopeConfigInterface $config;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function setUp(): void
31+
{
32+
$this->config = $this->createMock(ScopeConfigInterface::class);
33+
parent::setUp();
34+
}
35+
36+
/**
37+
* @return void
38+
* @throws Exception
39+
*/
40+
public function testSetAttribute(): void
41+
{
42+
$attribute = $this->createMock(Attribute::class);
43+
$attribute->expects($this->once())
44+
->method('__call')
45+
->with('setIsGlobal', [ScopedAttributeInterface::SCOPE_WEBSITE]);
46+
$this->config->expects($this->once())
47+
->method('getValue')
48+
->with(ProductScopeRewriteGenerator::URL_REWRITE_SCOPE_CONFIG_PATH, ScopeInterface::SCOPE_STORE)
49+
->willReturn(ProductScopeRewriteGenerator::WEBSITE_URL_REWRITE_SCOPE);
50+
51+
$url = new Url($this->config);
52+
$url->setAttribute($attribute);
53+
}
54+
}

0 commit comments

Comments
 (0)