Skip to content

Commit 733d72a

Browse files
fix 24922
1 parent 29e1368 commit 733d72a

File tree

2 files changed

+228
-18
lines changed
  • app/code/Magento/CatalogUrlRewrite

2 files changed

+228
-18
lines changed

app/code/Magento/CatalogUrlRewrite/Plugin/Catalog/Block/Adminhtml/Category/Tab/Attributes.php

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,91 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\CatalogUrlRewrite\Plugin\Catalog\Block\Adminhtml\Category\Tab;
710

11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Catalog\Model\Category\DataProvider;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\CatalogUrlRewrite\Block\UrlKeyRenderer;
15+
use Magento\Store\Model\ScopeInterface;
16+
817
/**
9-
* Class Attributes
18+
* Category tab attributes
1019
*/
1120
class Attributes
1221
{
1322
/**
14-
* @param \Magento\Catalog\Model\Category\DataProvider $subject
15-
* @param array $result
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @param ScopeConfigInterface $scopeConfig
29+
*/
30+
public function __construct(ScopeConfigInterface $scopeConfig)
31+
{
32+
$this->scopeConfig = $scopeConfig;
33+
}
34+
35+
/**
36+
* Adds attributes meta if url_key exist
1637
*
38+
* @param DataProvider $subject
39+
* @param array $result
1740
* @return array
1841
*/
19-
public function afterGetAttributesMeta(
20-
\Magento\Catalog\Model\Category\DataProvider $subject,
21-
$result
22-
) {
23-
/** @var \Magento\Catalog\Model\Category $category */
42+
public function afterGetAttributesMeta(DataProvider $subject, $result): array
43+
{
44+
if (!isset($result['url_key'])) {
45+
return $result;
46+
}
47+
2448
$category = $subject->getCurrentCategory();
25-
if (isset($result['url_key'])) {
26-
if ($category && $category->getId()) {
27-
if ($category->getLevel() == 1) {
28-
$result['url_key_group']['componentDisabled'] = true;
29-
} else {
30-
$result['url_key_create_redirect']['valueMap']['true'] = $category->getUrlKey();
31-
$result['url_key_create_redirect']['value'] = $category->getUrlKey();
32-
$result['url_key_create_redirect']['disabled'] = true;
33-
}
49+
if ($category && $category->getId()) {
50+
if ((int) $category->getLevel() === 1) {
51+
$result['url_key_group']['componentDisabled'] = true;
3452
} else {
35-
$result['url_key_create_redirect']['visible'] = false;
53+
$result['url_key_create_redirect'] = $this->getUrlRewriteMeta($category);
3654
}
55+
} else {
56+
$result['url_key_create_redirect']['visible'] = false;
3757
}
58+
3859
return $result;
3960
}
61+
62+
/**
63+
* Returns url rewrite meta
64+
*
65+
* @param CategoryInterface $category
66+
* @return array
67+
*/
68+
private function getUrlRewriteMeta(CategoryInterface $category): array
69+
{
70+
return [
71+
'value' => $category->getUrlKey(),
72+
'valueMap' => [
73+
'true' => $this->isSaveRewriteHistory($category->getStoreId()) ? $category->getUrlKey() : false
74+
],
75+
'disabled' => true,
76+
];
77+
}
78+
79+
/**
80+
* Returns Create Permanent Redirect for URLs if changed config enabled
81+
*
82+
* @param int $storeId
83+
* @return bool
84+
*/
85+
private function isSaveRewriteHistory(int $storeId): bool
86+
{
87+
return $this->scopeConfig->isSetFlag(
88+
UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY,
89+
ScopeInterface::SCOPE_STORE,
90+
$storeId
91+
);
92+
}
4093
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Catalog\Block\Adminhtml\Category\Tab;
10+
11+
use Magento\Catalog\Model\Category;
12+
use Magento\Catalog\Model\Category\DataProvider as CategoryDataProvider;
13+
use Magento\CatalogUrlRewrite\Plugin\Catalog\Block\Adminhtml\Category\Tab\Attributes;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Test for \Magento\CatalogUrlRewrite\Plugin\Catalog\Block\Adminhtml\Category\Tab\Attributes.
21+
*/
22+
class AttributesTest extends TestCase
23+
{
24+
private const STUB_CATEGORY_META = ['url_key' => 'url_key_test'];
25+
private const STUB_URL_KEY = 'url_key_777';
26+
27+
/**
28+
* @var Attributes
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var Category|MockObject
34+
*/
35+
private $categoryMock;
36+
37+
/**
38+
* @var CategoryDataProvider|MockObject
39+
*/
40+
private $dataProviderMock;
41+
42+
/**
43+
* @var ScopeConfigInterface|MockObject
44+
*/
45+
private $scopeConfigMock;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp(): void
51+
{
52+
$objectManager = new ObjectManager($this);
53+
54+
$this->categoryMock = $this->createMock(Category::class);
55+
$this->dataProviderMock = $this->createMock(CategoryDataProvider::class);
56+
$this->dataProviderMock->expects($this->any())
57+
->method('getCurrentCategory')
58+
->willReturn($this->categoryMock);
59+
60+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
61+
$this->model = $objectManager->getObject(Attributes::class, ['scopeConfig' => $this->scopeConfigMock]);
62+
}
63+
64+
/**
65+
* Test get attributes meta
66+
*
67+
* @dataProvider attributesMetaDataProvider
68+
*
69+
* @param bool $configEnabled
70+
* @param bool|string $expected
71+
* @return void
72+
*/
73+
public function testGetAttributesMeta(bool $configEnabled, $expected): void
74+
{
75+
$this->categoryMock->expects($this->once())
76+
->method('getId')
77+
->willReturn(1);
78+
$this->categoryMock->expects($this->once())
79+
->method('getLevel')
80+
->willReturn(2);
81+
$this->categoryMock->expects($this->atMost(2))
82+
->method('getUrlKey')
83+
->willReturn(self::STUB_URL_KEY);
84+
$this->scopeConfigMock->expects($this->once())
85+
->method('isSetFlag')
86+
->willReturn($configEnabled);
87+
$this->categoryMock->expects($this->once())
88+
->method('getStoreId')
89+
->willReturn(1);
90+
91+
$result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META);
92+
93+
$this->assertArrayHasKey('url_key_create_redirect', $result);
94+
95+
$this->assertArrayHasKey('value', $result['url_key_create_redirect']);
96+
$this->assertEquals(self::STUB_URL_KEY, $result['url_key_create_redirect']['value']);
97+
98+
$this->assertArrayHasKey('valueMap', $result['url_key_create_redirect']);
99+
$this->assertArrayHasKey('true', $result['url_key_create_redirect']['valueMap']);
100+
$this->assertEquals($expected, $result['url_key_create_redirect']['valueMap']['true']);
101+
102+
$this->assertArrayHasKey('disabled', $result['url_key_create_redirect']);
103+
$this->assertTrue($result['url_key_create_redirect']['disabled']);
104+
}
105+
106+
/**
107+
* DataProvider for testGetAttributesMeta
108+
*
109+
* @return array
110+
*/
111+
public function attributesMetaDataProvider(): array
112+
{
113+
return [
114+
'save rewrite history config enabled' => [true, self::STUB_URL_KEY],
115+
'save rewrite history config disabled' => [false, false]
116+
];
117+
}
118+
119+
/**
120+
* Test get category without id attributes meta
121+
*
122+
* @return void
123+
*/
124+
public function testGetAttributesMetaWithoutCategoryId(): void
125+
{
126+
$this->categoryMock->expects($this->once())
127+
->method('getId')
128+
->willReturn(null);
129+
130+
$result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META);
131+
132+
$this->assertArrayHasKey('url_key_create_redirect', $result);
133+
$this->assertArrayHasKey('visible', $result['url_key_create_redirect']);
134+
$this->assertFalse($result['url_key_create_redirect']['visible']);
135+
}
136+
137+
/**
138+
* Test get root category attributes meta
139+
*
140+
* @return void
141+
*/
142+
public function testGetAttributesMetaRootCategory(): void
143+
{
144+
$this->categoryMock->expects($this->once())
145+
->method('getId')
146+
->willReturn(1);
147+
$this->categoryMock->expects($this->once())
148+
->method('getLevel')
149+
->willReturn(1);
150+
151+
$result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META);
152+
153+
$this->assertArrayHasKey('url_key_group', $result);
154+
$this->assertArrayHasKey('componentDisabled', $result['url_key_group']);
155+
$this->assertTrue($result['url_key_group']['componentDisabled']);
156+
}
157+
}

0 commit comments

Comments
 (0)