Skip to content

Commit 90ab6fc

Browse files
authored
Merge pull request #6145 from magento-tango/TANGO-PR-09-15-2020_24
TANGO PR 09-15-2020 v2.4
2 parents 57c527e + c3c3d6f commit 90ab6fc

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

app/code/Magento/Sitemap/Model/Observer.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sitemap\Model;
79

8-
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
10+
use Magento\Framework\App\Area;
911
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
1013
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
14+
use Magento\Store\Model\App\Emulation;
1115
use Magento\Store\Model\ScopeInterface;
1216

1317
/**
@@ -61,20 +65,28 @@ class Observer
6165
*/
6266
private $emailNotification;
6367

68+
/**
69+
* @var Emulation
70+
*/
71+
private $appEmulation;
72+
6473
/**
6574
* Observer constructor.
6675
* @param ScopeConfigInterface $scopeConfig
6776
* @param CollectionFactory $collectionFactory
6877
* @param EmailNotification $emailNotification
78+
* @param Emulation $appEmulation
6979
*/
7080
public function __construct(
7181
ScopeConfigInterface $scopeConfig,
7282
CollectionFactory $collectionFactory,
73-
SitemapEmail $emailNotification
83+
SitemapEmail $emailNotification,
84+
Emulation $appEmulation
7485
) {
7586
$this->scopeConfig = $scopeConfig;
7687
$this->collectionFactory = $collectionFactory;
7788
$this->emailNotification = $emailNotification;
89+
$this->appEmulation = $appEmulation;
7890
}
7991

8092
/**
@@ -105,9 +117,16 @@ public function scheduledGenerateSitemaps()
105117
foreach ($collection as $sitemap) {
106118
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
107119
try {
120+
$this->appEmulation->startEnvironmentEmulation(
121+
$sitemap->getStoreId(),
122+
Area::AREA_FRONTEND,
123+
true
124+
);
108125
$sitemap->generateXml();
109126
} catch (\Exception $e) {
110127
$errors[] = $e->getMessage();
128+
} finally {
129+
$this->appEmulation->stopEnvironmentEmulation();
111130
}
112131
}
113132
if ($errors && $recipient) {

app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,39 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
142142

143143
$this->observer->scheduledGenerateSitemaps();
144144
}
145+
146+
/**
147+
* Test if cron scheduled XML sitemap generation will start and stop the store environment emulation
148+
*
149+
* @throws \Exception
150+
*/
151+
public function testCronGenerateSitemapEnvironmentEmulation()
152+
{
153+
$storeId = 1;
154+
155+
$this->scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
156+
157+
$this->collectionFactoryMock->expects($this->once())
158+
->method('create')
159+
->willReturn($this->sitemapCollectionMock);
160+
161+
$this->sitemapCollectionMock->expects($this->any())
162+
->method('getIterator')
163+
->willReturn(new \ArrayIterator([$this->sitemapMock]));
164+
165+
$this->sitemapMock->expects($this->at(0))
166+
->method('getStoreId')
167+
->willReturn($storeId);
168+
169+
$this->sitemapMock->expects($this->once())
170+
->method('generateXml');
171+
172+
$this->appEmulationMock->expects($this->once())
173+
->method('startEnvironmentEmulation');
174+
175+
$this->appEmulationMock->expects($this->once())
176+
->method('stopEnvironmentEmulation');
177+
178+
$this->observer->scheduledGenerateSitemaps();
179+
}
145180
}

0 commit comments

Comments
 (0)