diff --git a/lib/internal/Magento/Framework/Event/Config/Converter.php b/lib/internal/Magento/Framework/Event/Config/Converter.php index 35d056b61e057..2fb60880c7c49 100644 --- a/lib/internal/Magento/Framework/Event/Config/Converter.php +++ b/lib/internal/Magento/Framework/Event/Config/Converter.php @@ -1,7 +1,7 @@ attributes->getNamedItem('ifconfig'); + if ($ifconfigAttribute && !empty($ifconfigAttribute->nodeValue)) { + $output['ifconfig'] = $ifconfigAttribute->nodeValue; + } + return $output; } } diff --git a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php index acd0a61633557..6d62390f2b081 100644 --- a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php +++ b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php @@ -1,13 +1,12 @@ _observerFactory = $observerFactory; $this->_appState = $appState; + $this->scopeConfig = $scopeConfig ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(ScopeConfigInterface::class); $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(LoggerInterface::class); } @@ -66,6 +74,10 @@ public function dispatch(array $configuration, Observer $observer) return; } + if (isset($configuration['ifconfig']) && $this->scopeConfig->isSetFlag($configuration['ifconfig']) === false) { + return; + } + if (isset($configuration['shared']) && false === $configuration['shared']) { $object = $this->_observerFactory->create($configuration['instance']); } else { diff --git a/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php b/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php index 1d65c82c200b0..54df5ebe5b417 100644 --- a/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php +++ b/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php @@ -1,8 +1,7 @@ _appStateMock = $this->createMock(State::class); + $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config::class); $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); $this->_invokerDefault = new InvokerDefault( $this->_observerFactoryMock, $this->_appStateMock, + $this->_scopeConfigMock, $this->loggerMock ); } @@ -215,4 +222,64 @@ public static function dataProviderForMethodIsNotDefined() { return ['shared' => [true], 'non shared' => [false]]; } + + public function testDispatchWithIfconfigFalse() + { + $this->_observerFactoryMock->expects($this->never())->method('get'); + $this->_observerFactoryMock->expects($this->never())->method('create'); + + $this->_scopeConfigMock->expects( + $this->any() + )->method( + 'isSetFlag' + )->with( + 'test/path/value' + )->willReturn( + false + ); + + $this->_invokerDefault->dispatch(['ifconfig' => 'test/path/value'], $this->_observerMock); + } + + public function testDispatchWithIfconfigTrue() + { + $this->_listenerMock->expects($this->once())->method('execute'); + $this->_scopeConfigMock->expects( + $this->any() + )->method( + 'isSetFlag' + )->with( + 'test/path/value' + )->willReturn( + true + ); + $this->_observerFactoryMock->expects( + $this->once() + )->method( + 'get' + )->with( + 'class_name' + )->willReturn( + $this->_listenerMock + ); + + $this->_invokerDefault->dispatch(['instance' => 'class_name', + 'ifconfig' => 'test/path/value'], $this->_observerMock); + } + + public function testDispatchWithIfconfigMissing() + { + $this->_listenerMock->expects($this->once())->method('execute'); + $this->_observerFactoryMock->expects( + $this->once() + )->method( + 'get' + )->with( + 'class_name' + )->willReturn( + $this->_listenerMock + ); + + $this->_invokerDefault->dispatch(['instance' => 'class_name'], $this->_observerMock); + } } diff --git a/lib/internal/Magento/Framework/Event/etc/events.xsd b/lib/internal/Magento/Framework/Event/etc/events.xsd index cac62af356760..f43d03d1f9326 100644 --- a/lib/internal/Magento/Framework/Event/etc/events.xsd +++ b/lib/internal/Magento/Framework/Event/etc/events.xsd @@ -1,8 +1,8 @@ @@ -55,6 +55,7 @@ +