From 7b19f1629849ada8208de4aff2420832d30d22c6 Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Fri, 18 Dec 2020 14:08:25 +0530 Subject: [PATCH 1/2] Added inspection error for invalid disabled observer name --- resources/magento2/inspection.properties | 1 + .../xml/ObserverDeclarationInspection.java | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/resources/magento2/inspection.properties b/resources/magento2/inspection.properties index f28b881a6..a0531ee7e 100644 --- a/resources/magento2/inspection.properties +++ b/resources/magento2/inspection.properties @@ -15,6 +15,7 @@ inspection.plugin.error.redundantParameter=Redundant parameter inspection.plugin.error.typeIncompatibility=Possible type incompatibility. Consider changing the parameter according to the target method. inspection.observer.duplicateInSameFile=The observer name already used in this file. For more details see Inspection Description. inspection.observer.duplicateInOtherPlaces=The observer name "{0}" for event "{1}" is already used in the module "{2}" ({3} scope). For more details see Inspection Description. +inspection.observer.disabledObserverDoesNotExist=This observer does not exist to be disabled. For more details, see Inspection Description. inspection.cache.disabledCache=Cacheable false attribute on the default layout will disable cache site-wide inspection.moduleDeclaration.warning.wrongModuleName=Provided module name "{0}" does not match expected "{1}" inspection.moduleDeclaration.fix=Fix module name diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java index f67cf017e..a489bef89 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java @@ -94,10 +94,7 @@ public void visitFile(final PsiFile file) { final XmlAttribute observerDisabledAttribute = observerXmlTag.getAttribute("disabled"); - if (observerNameAttribute == null || ( - observerDisabledAttribute != null//NOPMD - && observerDisabledAttribute.getValue().equals("true")) - ) { + if (observerNameAttribute == null) { continue; } @@ -122,6 +119,21 @@ public void visitFile(final PsiFile file) { eventIndex, file ); + + if (observerDisabledAttribute != null + && observerDisabledAttribute.getValue() != null + && observerDisabledAttribute.getValue().equals("true") + && modulesWithSameObserverName.isEmpty() + ) { + problemsHolder.registerProblem( + observerNameAttribute.getValueElement(), + inspectionBundle.message( + "inspection.observer.disabledObserverDoesNotExist" + ), + errorSeverity + ); + } + for (final HashMap moduleEntry: modulesWithSameObserverName) { final Map.Entry module = moduleEntry From 16727bcd3921e91b4d5ebe32adbf4f8429b9ea58 Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Fri, 18 Dec 2020 14:19:40 +0530 Subject: [PATCH 2/2] Added test for disabled observer inspection warning --- .../disablingNonExistingObserver/events.xml | 14 ++++++++++++++ .../xml/ObserverDeclarationInspectionTest.java | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 testData/inspections/xml/ObserverDeclarationInspection/disablingNonExistingObserver/events.xml diff --git a/testData/inspections/xml/ObserverDeclarationInspection/disablingNonExistingObserver/events.xml b/testData/inspections/xml/ObserverDeclarationInspection/disablingNonExistingObserver/events.xml new file mode 100644 index 000000000..6e73e7a2c --- /dev/null +++ b/testData/inspections/xml/ObserverDeclarationInspection/disablingNonExistingObserver/events.xml @@ -0,0 +1,14 @@ + + + + + "test_non_existing_observer" + instance="Magento\Catalog\Observer\TestObserver" + disabled="true"/> + + diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspectionTest.java index 0468825da..48e30fa17 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspectionTest.java @@ -31,4 +31,12 @@ public void testObserverNameUsedInDifferentFile() { myFixture.configureByFile(getFixturePath(ModuleEventsXml.FILE_NAME)); myFixture.testHighlighting(true, false, false); } + + /** + * Tests warning for disabling of non-existing observer. + */ + public void testDisablingNonExistingObserver() { + myFixture.configureByFile(getFixturePath(ModuleEventsXml.FILE_NAME)); + myFixture.testHighlighting(true, false, false); + } }