Skip to content

Commit 6427ae6

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-45666-v2' into EPAM-PR-71
2 parents 2ee062e + a3f89fb commit 6427ae6

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
131131
'page_layout',
132132
'custom_layout_update',
133133
'custom_apply_to_products',
134+
'custom_use_parent_settings',
134135
];
135136

136137
/**
@@ -331,9 +332,11 @@ protected function getCustomAttributesCodes()
331332
* @throws \Magento\Framework\Exception\LocalizedException
332333
* @return \Magento\Catalog\Model\ResourceModel\Category
333334
* @deprecated because resource models should be used directly
335+
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
334336
*/
335337
protected function _getResource()
336338
{
339+
//phpcs:enable Generic.CodeAnalysis.UselessOverridingMethod
337340
return parent::_getResource();
338341
}
339342
// phpcs:enable
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Observer;
9+
10+
use Magento\Framework\Event\Observer;
11+
use Magento\Framework\Event\ObserverInterface;
12+
13+
/**
14+
* Observer for invalidating cache on catalog category design change
15+
*/
16+
class InvalidateCacheOnCategoryDesignChange implements ObserverInterface
17+
{
18+
/**
19+
* @var \Magento\Framework\App\Cache\TypeListInterface
20+
*/
21+
private $cacheTypeList;
22+
23+
/**
24+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
30+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
31+
*/
32+
public function __construct(
33+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
34+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
35+
) {
36+
$this->cacheTypeList = $cacheTypeList;
37+
$this->scopeConfig = $scopeConfig;
38+
}
39+
40+
/**
41+
* Get default category design attribute values
42+
*
43+
* @return array
44+
*/
45+
private function getDefaultAttributeValues()
46+
{
47+
return [
48+
'custom_apply_to_products' => '0',
49+
'custom_use_parent_settings' => '0',
50+
'page_layout' => $this->scopeConfig->getValue(
51+
'web/default_layouts/default_category_layout',
52+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
53+
)
54+
];
55+
}
56+
57+
/**
58+
* Invalidate cache on category design attribute value changed
59+
*
60+
* @param \Magento\Framework\Event\Observer $observer
61+
*/
62+
public function execute(Observer $observer)
63+
{
64+
$category = $observer->getEvent()->getEntity();
65+
if (!$category->isObjectNew()) {
66+
foreach ($category->getDesignAttributes() as $designAttribute) {
67+
if ($this->isCategoryAttributeChanged($designAttribute->getAttributeCode(), $category)) {
68+
$this->cacheTypeList->invalidate(
69+
[
70+
\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER,
71+
\Magento\Framework\App\Cache\Type\Layout::TYPE_IDENTIFIER
72+
]
73+
);
74+
break;
75+
}
76+
}
77+
}
78+
}
79+
80+
/**
81+
* Check if category attribute changed
82+
*
83+
* @param string $attributeCode
84+
* @param \Magento\Catalog\Api\Data\CategoryInterface $category
85+
* @return bool
86+
*/
87+
private function isCategoryAttributeChanged($attributeCode, $category)
88+
{
89+
if (!array_key_exists($attributeCode, $category->getOrigData())) {
90+
$defaultValue = $this->getDefaultAttributeValues()[$attributeCode] ?? null;
91+
if ($category->getData($attributeCode) !== $defaultValue) {
92+
return true;
93+
}
94+
} else {
95+
if ($category->dataHasChangedFor($attributeCode)) {
96+
return true;
97+
}
98+
}
99+
100+
return false;
101+
}
102+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="DisplayRefreshCacheAfterChangingCategoryPageLayoutTest">
12+
<annotations>
13+
<features value="Catalog"/>
14+
<title value="'Refresh cache' admin notification is displayed when changing category page layout"/>
15+
<description value="'Refresh cache' message is not displayed when changing category page layout"/>
16+
<severity value="MAJOR"/>
17+
<testCaseId value="MC-17031"/>
18+
<useCaseId value="MAGETWO-45666"/>
19+
<group value="catalog"/>
20+
</annotations>
21+
<before>
22+
<!-- Create category, flush cache and log in -->
23+
<comment userInput="Create category, flush cache and log in" stepKey="createCategoryAndLogIn"/>
24+
<createData entity="SimpleSubCategory" stepKey="simpleCategory"/>
25+
<actionGroup ref="LoginAsAdmin" stepKey="logInAsAdmin"/>
26+
<magentoCLI command="cache:flush" stepKey="flushCache"/>
27+
</before>
28+
<after>
29+
<!-- Delete category and log out -->
30+
<comment userInput="Delete category and log out" stepKey="deleteCategoryAndLogOut"/>
31+
<deleteData createDataKey="simpleCategory" stepKey="deleteCategory"/>
32+
<actionGroup ref="logout" stepKey="logOutFromAdmin"/>
33+
<magentoCLI command="cache:flush" stepKey="flushCache"/>
34+
</after>
35+
<!-- Navigate to category details page -->
36+
<comment userInput="Navigate to category details page" stepKey="navigateToAdminCategoryPage"/>
37+
<actionGroup ref="goToAdminCategoryPageById" stepKey="goToAdminCategoryPage">
38+
<argument name="id" value="$$simpleCategory.id$$"/>
39+
</actionGroup>
40+
<!-- Open design tab and set layout -->
41+
<comment userInput="Open design tab and set layout" stepKey="setLayoutAndSave"/>
42+
<click selector="{{CategoryDesignSection.DesignTab}}" stepKey="clickOnDesignTab"/>
43+
<waitForElementVisible selector="{{CategoryDesignSection.LayoutDropdown}}" stepKey="waitForLayoutDropDown" />
44+
<selectOption selector="{{CategoryDesignSection.LayoutDropdown}}" userInput="2 columns with right bar" stepKey="selectAnOption" />
45+
<click selector="{{ContentManagementSection.Save}}" stepKey="clickSaveConfig" />
46+
<waitForPageLoad stepKey="waitSaveToApply"/>
47+
<!-- See if warning message displays -->
48+
<comment userInput="See if warning message displays" stepKey="checkWarningMessagePresence"/>
49+
<see selector="{{AdminMessagesSection.warningMessage}}" userInput="Please go to Cache Management and refresh cache types" stepKey="seeWarningMessage"/>
50+
</test>
51+
</tests>

app/code/Magento/Catalog/etc/events.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
</event>
2727
<event name="magento_catalog_api_data_categoryinterface_save_after">
2828
<observer name="legacy_category_save_after" instance="Magento\Framework\EntityManager\Observer\AfterEntitySave" />
29+
<observer name="invalidate_cache_on_category_design_change" instance="Magento\Catalog\Observer\InvalidateCacheOnCategoryDesignChange" />
2930
</event>
3031
<event name="magento_catalog_api_data_categoryinterface_delete_before">
3132
<observer name="legacy_category_delete_before" instance="Magento\Framework\EntityManager\Observer\BeforeEntityDelete" />

0 commit comments

Comments
 (0)