From 6c820d361171287a89a1f89c0202867c02df2044 Mon Sep 17 00:00:00 2001 From: Nazarn96 Date: Fri, 1 Mar 2019 15:55:16 +0200 Subject: [PATCH 1/2] Remove Environment emulation to better performance --- .../Controller/Adminhtml/Sitemap/Generate.php | 12 +++--------- app/code/Magento/Sitemap/Model/Observer.php | 19 +------------------ .../Model/ResourceModel/Catalog/Product.php | 1 + .../Sitemap/Test/Unit/Model/ObserverTest.php | 13 +------------ 4 files changed, 6 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php b/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php index 9592ab6f57c55..c8edefa00cc86 100644 --- a/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php +++ b/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php @@ -10,6 +10,9 @@ use Magento\Store\Model\App\Emulation; use Magento\Framework\App\ObjectManager; +/** + * Generate sitemap file + */ class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap { /** @var \Magento\Store\Model\App\Emulation $appEmulation */ @@ -44,14 +47,7 @@ public function execute() // if sitemap record exists if ($sitemap->getId()) { try { - //We need to emulate to get the correct frontend URL for the product images - $this->appEmulation->startEnvironmentEmulation( - $sitemap->getStoreId(), - \Magento\Framework\App\Area::AREA_FRONTEND, - true - ); $sitemap->generateXml(); - $this->messageManager->addSuccessMessage( __('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename()) ); @@ -59,8 +55,6 @@ public function execute() $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.')); - } finally { - $this->appEmulation->stopEnvironmentEmulation(); } } else { $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.')); diff --git a/app/code/Magento/Sitemap/Model/Observer.php b/app/code/Magento/Sitemap/Model/Observer.php index bd7a84f601b77..ce74d738c4bc3 100644 --- a/app/code/Magento/Sitemap/Model/Observer.php +++ b/app/code/Magento/Sitemap/Model/Observer.php @@ -5,7 +5,6 @@ */ namespace Magento\Sitemap\Model; -use Magento\Store\Model\App\Emulation; use Magento\Sitemap\Model\EmailNotification as SitemapEmail; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory; @@ -57,11 +56,6 @@ class Observer */ private $collectionFactory; - /** - * @var Emulation - */ - private $appEmulation; - /** * @var $emailNotification */ @@ -72,17 +66,14 @@ class Observer * @param ScopeConfigInterface $scopeConfig * @param CollectionFactory $collectionFactory * @param EmailNotification $emailNotification - * @param Emulation $appEmulation */ public function __construct( ScopeConfigInterface $scopeConfig, CollectionFactory $collectionFactory, - SitemapEmail $emailNotification, - Emulation $appEmulation + SitemapEmail $emailNotification ) { $this->scopeConfig = $scopeConfig; $this->collectionFactory = $collectionFactory; - $this->appEmulation = $appEmulation; $this->emailNotification = $emailNotification; } @@ -114,17 +105,9 @@ public function scheduledGenerateSitemaps() foreach ($collection as $sitemap) { /* @var $sitemap \Magento\Sitemap\Model\Sitemap */ try { - $this->appEmulation->startEnvironmentEmulation( - $sitemap->getStoreId(), - \Magento\Framework\App\Area::AREA_FRONTEND, - true - ); - $sitemap->generateXml(); } catch (\Exception $e) { $errors[] = $e->getMessage(); - } finally { - $this->appEmulation->stopEnvironmentEmulation(); } } if ($errors && $recipient) { diff --git a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php index 82024b3b015e5..4d678f5c1cb94 100644 --- a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php +++ b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php @@ -389,6 +389,7 @@ protected function _prepareProduct(array $productRow, $storeId) */ protected function _loadProductImages($product, $storeId) { + $this->_storeManager->setCurrentStore($storeId); /** @var $helper \Magento\Sitemap\Helper\Data */ $helper = $this->_sitemapData; $imageIncludePolicy = $helper->getProductImageIncludePolicy($storeId); diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php index 5fae54ff3c5d0..09f5418bbd762 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php +++ b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php @@ -118,7 +118,7 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail() ->method('getStoreId') ->willReturn($storeId); - $this->sitemapMock->expects($this->at(1)) + $this->sitemapMock->expects($this->once()) ->method('generateXml') ->willThrowException(new \Exception($exception)); @@ -130,17 +130,6 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail() ) ->willReturn('error-recipient@example.com'); - $this->appEmulationMock->expects($this->at(0)) - ->method('startEnvironmentEmulation') - ->with( - $storeId, - Area::AREA_FRONTEND, - true - ); - - $this->appEmulationMock->expects($this->at(1)) - ->method('stopEnvironmentEmulation'); - $this->observer->scheduledGenerateSitemaps(); } } From 348eeb6d5769882b91c90cc0fe40c49c95af9279 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 11 Mar 2019 12:30:48 +0200 Subject: [PATCH 2/2] ENGCOM-4438: Static test fix. --- .../Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php b/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php index c8edefa00cc86..c8e2d356b3ffb 100644 --- a/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php +++ b/app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php @@ -7,13 +7,15 @@ namespace Magento\Sitemap\Controller\Adminhtml\Sitemap; use Magento\Backend\App\Action; +use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Sitemap\Controller\Adminhtml\Sitemap; use Magento\Store\Model\App\Emulation; use Magento\Framework\App\ObjectManager; /** * Generate sitemap file */ -class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap +class Generate extends Sitemap implements HttpGetActionInterface { /** @var \Magento\Store\Model\App\Emulation $appEmulation */ private $appEmulation;