From 0888e8ce84d9f29c97e7c1997b7ff243990c5201 Mon Sep 17 00:00:00 2001 From: marina Date: Mon, 9 Oct 2017 23:57:23 +0300 Subject: [PATCH 1/5] Stop throwing exception in order to send sitemap errors by email --- app/code/Magento/Sitemap/Model/Observer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sitemap/Model/Observer.php b/app/code/Magento/Sitemap/Model/Observer.php index 3ae3061310a0b..54f91bebea744 100644 --- a/app/code/Magento/Sitemap/Model/Observer.php +++ b/app/code/Magento/Sitemap/Model/Observer.php @@ -113,7 +113,6 @@ public function scheduledGenerateSitemaps() $sitemap->generateXml(); } catch (\Exception $e) { $errors[] = $e->getMessage(); - throw $e; } } From 3fc3c1dc76bf643915b752dbe13b064e3cbc858e Mon Sep 17 00:00:00 2001 From: marina Date: Tue, 10 Oct 2017 00:06:27 +0300 Subject: [PATCH 2/5] Remove undefined _translateModel property The lines were a reminiscence from when the translation logic was in the core module. The logic was replaced with the correct use of the inlineTranslation property that is also present at the end of the method. --- app/code/Magento/Sitemap/Model/Observer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/Sitemap/Model/Observer.php b/app/code/Magento/Sitemap/Model/Observer.php index 54f91bebea744..840a6a1858fae 100644 --- a/app/code/Magento/Sitemap/Model/Observer.php +++ b/app/code/Magento/Sitemap/Model/Observer.php @@ -121,8 +121,7 @@ public function scheduledGenerateSitemaps() \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) ) { - $translate = $this->_translateModel->getTranslateInline(); - $this->_translateModel->setTranslateInline(false); + $this->inlineTranslation->suspend(); $this->_transportBuilder->setTemplateIdentifier( $this->_scopeConfig->getValue( From 3c3168bdfbe43379e485ff91e708a045dd767e67 Mon Sep 17 00:00:00 2001 From: marina Date: Fri, 13 Oct 2017 20:43:15 +0300 Subject: [PATCH 3/5] Update scheduledGenerateSitemaps unit test --- .../Sitemap/Test/Unit/Model/ObserverTest.php | 56 +++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php index 92e6f4e2e2293..904360ffb3804 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php +++ b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php @@ -96,11 +96,11 @@ protected function setUp() ); } - /** - * @expectedException \Exception - */ - public function testScheduledGenerateSitemapsThrowsException() + public function testScheduledGenerateSitemapsSendsExceptionEmail() { + $exception = 'Sitemap Exception'; + $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class); + $this->scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true); $this->collectionFactoryMock->expects($this->once()) @@ -111,7 +111,53 @@ public function testScheduledGenerateSitemapsThrowsException() ->method('getIterator') ->willReturn(new \ArrayIterator([$this->sitemapMock])); - $this->sitemapMock->expects($this->once())->method('generateXml')->willThrowException(new \Exception()); + $this->sitemapMock->expects($this->once())->method('generateXml')->willThrowException(new \Exception($exception)); + + $this->scopeConfigMock->expects($this->at(1)) + ->method('getValue') + ->with( + \Magento\Sitemap\Model\Observer::XML_PATH_ERROR_RECIPIENT, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ) + ->willReturn('error-recipient@example.com'); + + $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' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, + 'store' => \Magento\Store\Model\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->observer->scheduledGenerateSitemaps(); } From 86f1419e2a3ef386f1ebe3b68b23ab3050c2f7bb Mon Sep 17 00:00:00 2001 From: marina Date: Sun, 15 Oct 2017 16:27:05 +0300 Subject: [PATCH 4/5] Fix code style --- app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php index 904360ffb3804..e099d5b902544 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php +++ b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php @@ -111,7 +111,9 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail() ->method('getIterator') ->willReturn(new \ArrayIterator([$this->sitemapMock])); - $this->sitemapMock->expects($this->once())->method('generateXml')->willThrowException(new \Exception($exception)); + $this->sitemapMock->expects($this->once()) + ->method('generateXml') + ->willThrowException(new \Exception($exception)); $this->scopeConfigMock->expects($this->at(1)) ->method('getValue') From 16590f018ab3c110d6999ebb467cd18592287f1c Mon Sep 17 00:00:00 2001 From: marina Date: Sun, 15 Oct 2017 17:27:25 +0300 Subject: [PATCH 5/5] Add suppress warning for coupling between objects --- app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php index e099d5b902544..ac88f23ff9d69 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php +++ b/app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php @@ -7,6 +7,10 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +/** + * Class ObserverTest + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class ObserverTest extends \PHPUnit\Framework\TestCase { /**