-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Images in XML sitemap are always linked to base store in multistore on Schedule #19598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d5b0ffd
16f11f7
c8afe9f
fda07a2
5c7527e
d2d04b6
b88fa48
85a2a56
a04cbf2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Magento\Sitemap\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Translate\Inline\StateInterface; | ||
use Magento\Framework\Mail\Template\TransportBuilder; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Backend\App\Area\FrontNameResolver; | ||
use Magento\Sitemap\Model\Observer as Observer; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Sends emails for the scheduled generation of the sitemap file | ||
*/ | ||
class EmailNotification | ||
{ | ||
/** | ||
* @var \Magento\Framework\Translate\Inline\StateInterface | ||
*/ | ||
private $inlineTranslation; | ||
|
||
/** | ||
* Core store config | ||
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @var \Magento\Framework\Mail\Template\TransportBuilder | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private $transportBuilder; | ||
|
||
/** | ||
* @var \Psr\Log\LoggerInterface $logger | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private $logger; | ||
|
||
/** | ||
* EmailNotification constructor. | ||
* @param StateInterface $inlineTranslation | ||
* @param TransportBuilder $transportBuilder | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
StateInterface $inlineTranslation, | ||
TransportBuilder $transportBuilder, | ||
ScopeConfigInterface $scopeConfig, | ||
LoggerInterface $logger | ||
) { | ||
$this->inlineTranslation = $inlineTranslation; | ||
$this->scopeConfig = $scopeConfig; | ||
$this->transportBuilder = $transportBuilder; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Send's error email if sitemap generated with errors. | ||
* | ||
* @param array| $errors | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function sendErrors($errors) | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$this->inlineTranslation->suspend(); | ||
try { | ||
$this->transportBuilder->setTemplateIdentifier( | ||
$this->scopeConfig->getValue( | ||
Observer::XML_PATH_ERROR_TEMPLATE, | ||
ScopeInterface::SCOPE_STORE | ||
) | ||
)->setTemplateOptions( | ||
[ | ||
'area' => FrontNameResolver::AREA_CODE, | ||
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, | ||
] | ||
)->setTemplateVars( | ||
['warnings' => join("\n", $errors)] | ||
)->setFrom( | ||
$this->scopeConfig->getValue( | ||
Observer::XML_PATH_ERROR_IDENTITY, | ||
ScopeInterface::SCOPE_STORE | ||
) | ||
)->addTo( | ||
$this->scopeConfig->getValue( | ||
Observer::XML_PATH_ERROR_RECIPIENT, | ||
ScopeInterface::SCOPE_STORE | ||
) | ||
); | ||
|
||
$transport = $this->transportBuilder->getTransport(); | ||
$transport->sendMessage(); | ||
} catch (\Exception $e) { | ||
$this->logger->error('Sitemap sendErrors: '.$e->getMessage()); | ||
} finally { | ||
$this->inlineTranslation->resume(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,12 @@ | |
*/ | ||
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; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
/** | ||
Nazar65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Sitemap module observer | ||
* | ||
|
@@ -44,47 +50,40 @@ class Observer | |
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
protected $_scopeConfig; | ||
private $scopeConfig; | ||
|
||
/** | ||
* @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory | ||
*/ | ||
protected $_collectionFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\Mail\Template\TransportBuilder | ||
*/ | ||
protected $_transportBuilder; | ||
private $collectionFactory; | ||
sidolov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @var \Magento\Store\Model\StoreManagerInterface | ||
* @var Emulation | ||
*/ | ||
protected $_storeManager; | ||
private $appEmulation; | ||
|
||
/** | ||
* @var \Magento\Framework\Translate\Inline\StateInterface | ||
* @var $emailNotification | ||
*/ | ||
protected $inlineTranslation; | ||
private $emailNotification; | ||
|
||
/** | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | ||
* @param \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory | ||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager | ||
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder | ||
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | ||
* Observer constructor. | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param CollectionFactory $collectionFactory | ||
* @param EmailNotification $emailNotification | ||
* @param Emulation $appEmulation | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
\Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory, | ||
\Magento\Store\Model\StoreManagerInterface $storeManager, | ||
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, | ||
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | ||
ScopeConfigInterface $scopeConfig, | ||
CollectionFactory $collectionFactory, | ||
SitemapEmail $emailNotification, | ||
Emulation $appEmulation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new parameter should be optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here if i add optional, as this have been in older commit the codeMess test will fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) { | ||
$this->_scopeConfig = $scopeConfig; | ||
$this->_collectionFactory = $collectionFactory; | ||
$this->_storeManager = $storeManager; | ||
$this->_transportBuilder = $transportBuilder; | ||
$this->inlineTranslation = $inlineTranslation; | ||
$this->scopeConfig = $scopeConfig; | ||
$this->collectionFactory = $collectionFactory; | ||
$this->appEmulation = $appEmulation; | ||
$this->emailNotification = $emailNotification; | ||
} | ||
|
||
/** | ||
|
@@ -97,61 +96,39 @@ public function __construct( | |
public function scheduledGenerateSitemaps() | ||
{ | ||
$errors = []; | ||
|
||
$recipient = $this->scopeConfig->getValue( | ||
Observer::XML_PATH_ERROR_RECIPIENT, | ||
ScopeInterface::SCOPE_STORE | ||
); | ||
// check if scheduled generation enabled | ||
if (!$this->_scopeConfig->isSetFlag( | ||
if (!$this->scopeConfig->isSetFlag( | ||
self::XML_PATH_GENERATION_ENABLED, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
ScopeInterface::SCOPE_STORE | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
$collection = $this->_collectionFactory->create(); | ||
$collection = $this->collectionFactory->create(); | ||
/* @var $collection \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection */ | ||
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 && $this->_scopeConfig->getValue( | ||
self::XML_PATH_ERROR_RECIPIENT, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
) | ||
) { | ||
$this->inlineTranslation->suspend(); | ||
|
||
$this->_transportBuilder->setTemplateIdentifier( | ||
$this->_scopeConfig->getValue( | ||
self::XML_PATH_ERROR_TEMPLATE, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
) | ||
)->setTemplateOptions( | ||
[ | ||
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, | ||
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, | ||
] | ||
)->setTemplateVars( | ||
['warnings' => join("\n", $errors)] | ||
)->setFrom( | ||
$this->_scopeConfig->getValue( | ||
self::XML_PATH_ERROR_IDENTITY, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
) | ||
)->addTo( | ||
$this->_scopeConfig->getValue( | ||
self::XML_PATH_ERROR_RECIPIENT, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
) | ||
); | ||
$transport = $this->_transportBuilder->getTransport(); | ||
$transport->sendMessage(); | ||
|
||
$this->inlineTranslation->resume(); | ||
if ($errors && $recipient) { | ||
$this->emailNotification->sendErrors($errors); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Magento\Sitemap\Test\Unit\Model; | ||
|
||
use Magento\Backend\App\Area\FrontNameResolver; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Mail\Template\TransportBuilder; | ||
use Magento\Framework\Mail\TransportInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Framework\Translate\Inline\StateInterface; | ||
use Magento\Sitemap\Model\EmailNotification; | ||
use Magento\Sitemap\Model\Observer; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Store\Model\Store; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test for Magento\Sitemap\Model\EmailNotification | ||
*/ | ||
class EmailNotificationTest extends TestCase | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var EmailNotification | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $scopeConfigMock; | ||
|
||
/** | ||
* @var TransportBuilder|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $transportBuilderMock; | ||
|
||
/** | ||
* @var StateInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $inlineTranslationMock; | ||
|
||
/** | ||
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $objectManagerMock; | ||
|
||
protected function setUp() | ||
{ | ||
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class) | ||
->getMock(); | ||
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) | ||
->getMock(); | ||
$this->transportBuilderMock = $this->getMockBuilder(TransportBuilder::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->inlineTranslationMock = $this->getMockBuilder(StateInterface::class) | ||
->getMock(); | ||
|
||
$this->objectManager = new ObjectManager($this); | ||
$this->model = $this->objectManager->getObject( | ||
EmailNotification::class, | ||
[ | ||
'inlineTranslation' => $this->inlineTranslationMock, | ||
'scopeConfig' => $this->scopeConfigMock, | ||
'transportBuilder' => $this->transportBuilderMock, | ||
] | ||
); | ||
} | ||
|
||
public function testSendErrors() | ||
{ | ||
$exception = 'Sitemap Exception'; | ||
$transport = $this->createMock(TransportInterface::class); | ||
|
||
$this->scopeConfigMock->expects($this->at(0)) | ||
->method('getValue') | ||
->with( | ||
Observer::XML_PATH_ERROR_TEMPLATE, | ||
ScopeInterface::SCOPE_STORE | ||
) | ||
->willReturn('[email protected]'); | ||
|
||
$this->inlineTranslationMock->expects($this->once()) | ||
->method('suspend'); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('setTemplateIdentifier') | ||
->will($this->returnSelf()); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('setTemplateOptions') | ||
->with([ | ||
'area' => FrontNameResolver::AREA_CODE, | ||
'store' => Store::DEFAULT_STORE_ID, | ||
]) | ||
->will($this->returnSelf()); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('setTemplateVars') | ||
->with(['warnings' => $exception]) | ||
->will($this->returnSelf()); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('setFrom') | ||
->will($this->returnSelf()); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('addTo') | ||
->will($this->returnSelf()); | ||
|
||
$this->transportBuilderMock->expects($this->once()) | ||
->method('getTransport') | ||
->willReturn($transport); | ||
|
||
$transport->expects($this->once()) | ||
->method('sendMessage'); | ||
|
||
$this->inlineTranslationMock->expects($this->once()) | ||
->method('resume'); | ||
|
||
$this->model->sendErrors(['Sitemap Exception']); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.