From 551a7aa722a3d50fa3599271b2865d60684f5ffa Mon Sep 17 00:00:00 2001 From: Sviatoslav Mankivskyi Date: Fri, 6 Nov 2015 16:59:14 +0200 Subject: [PATCH 1/2] MAGETWO-44657: Merge New Relic integration into Merchant Beta --- .../Api/CategoryManagementInterface.php | 7 + .../Api/ProductManagementInterface.php | 20 + .../Catalog/Model/CategoryManagement.php | 18 +- .../Catalog/Model/ProductManagement.php | 44 ++ .../Unit/Model/CategoryManagementTest.php | 47 +- .../Test/Unit/Model/ProductManagementTest.php | 83 ++++ app/code/Magento/Catalog/etc/di.xml | 1 + ...ConfigurableProductManagementInterface.php | 8 + .../Model/ConfigurableProductManagement.php | 36 +- .../ConfigurableProductManagementTest.php | 87 +++- .../Api/CustomerManagementInterface.php | 19 + .../Customer/Model/CustomerManagement.php | 35 ++ .../Unit/Model/CustomerManagementTest.php | 52 +++ app/code/Magento/Customer/etc/di.xml | 2 + .../Magento/NewRelicReporting/LICENSE.txt | 48 ++ .../Magento/NewRelicReporting/LICENSE_AFL.txt | 48 ++ .../Model/Apm/Deployments.php | 98 +++++ .../NewRelicReporting/Model/Config.php | 199 +++++++++ .../NewRelicReporting/Model/Counter.php | 150 +++++++ .../NewRelicReporting/Model/Counts.php | 20 + .../Magento/NewRelicReporting/Model/Cron.php | 69 +++ .../Model/Cron/ReportCounts.php | 181 ++++++++ .../Model/Cron/ReportModulesInfo.php | 115 +++++ .../Model/Cron/ReportNewRelicCron.php | 201 +++++++++ .../NewRelicReporting/Model/CronEvent.php | 162 +++++++ .../NewRelicReporting/Model/Module.php | 20 + .../Model/Module/Collect.php | 323 ++++++++++++++ .../Model/NewRelicWrapper.php | 43 ++ .../Model/Observer/CheckConfig.php | 71 +++ .../Model/Observer/ReportConcurrentAdmins.php | 94 ++++ .../ReportConcurrentAdminsToNewRelic.php | 69 +++ .../Model/Observer/ReportConcurrentUsers.php | 112 +++++ .../ReportConcurrentUsersToNewRelic.php | 87 ++++ .../Model/Observer/ReportOrderPlaced.php | 79 ++++ .../Observer/ReportOrderPlacedToNewRelic.php | 63 +++ .../Model/Observer/ReportProductDeleted.php | 86 ++++ .../ReportProductDeletedToNewRelic.php | 53 +++ .../Model/Observer/ReportProductSaved.php | 92 ++++ .../Observer/ReportProductSavedToNewRelic.php | 66 +++ .../Model/Observer/ReportSystemCacheFlush.php | 79 ++++ .../ReportSystemCacheFlushToNewRelic.php | 67 +++ .../NewRelicReporting/Model/Orders.php | 20 + .../Model/Resource/Counts.php | 20 + .../Model/Resource/Counts/Collection.php | 20 + .../Model/Resource/Module.php | 20 + .../Model/Resource/Module/Collection.php | 20 + .../Model/Resource/Orders.php | 20 + .../Model/Resource/Orders/Collection.php | 20 + .../Model/Resource/System.php | 20 + .../Model/Resource/System/Collection.php | 20 + .../Model/Resource/Users.php | 20 + .../Model/Resource/Users/Collection.php | 20 + .../NewRelicReporting/Model/System.php | 20 + .../Magento/NewRelicReporting/Model/Users.php | 20 + app/code/Magento/NewRelicReporting/README.md | 2 + .../NewRelicReporting/Setup/InstallSchema.php | 231 ++++++++++ .../Test/Unit/Model/Apm/DeploymentsTest.php | 290 ++++++++++++ .../Test/Unit/Model/CounterTest.php | 212 +++++++++ .../Test/Unit/Model/Cron/ReportCountsTest.php | 204 +++++++++ .../Unit/Model/Cron/ReportModulesInfoTest.php | 149 +++++++ .../Model/Cron/ReportNewRelicCronTest.php | 252 +++++++++++ .../Test/Unit/Model/CronEventTest.php | 215 +++++++++ .../Test/Unit/Model/CronTest.php | 114 +++++ .../Test/Unit/Model/Module/CollectTest.php | 411 ++++++++++++++++++ .../Unit/Model/Observer/CheckConfigTest.php | 121 ++++++ .../Observer/ReportConcurrentAdminsTest.php | 167 +++++++ .../ReportConcurrentAdminsToNewRelicTest.php | 125 ++++++ .../Observer/ReportConcurrentUsersTest.php | 195 +++++++++ .../ReportConcurrentUsersToNewRelicTest.php | 165 +++++++ .../Model/Observer/ReportOrderPlacedTest.php | 164 +++++++ .../ReportOrderPlacedToNewRelicTest.php | 120 +++++ .../Observer/ReportProductDeletedTest.php | 152 +++++++ .../ReportProductDeletedToNewRelicTest.php | 88 ++++ .../Model/Observer/ReportProductSavedTest.php | 207 +++++++++ .../ReportProductSavedToNewRelicTest.php | 153 +++++++ .../Observer/ReportSystemCacheFlushTest.php | 130 ++++++ .../ReportSystemCacheFlushToNewRelicTest.php | 120 +++++ .../Magento/NewRelicReporting/composer.json | 29 ++ .../Magento/NewRelicReporting/etc/acl.xml | 22 + .../etc/adminhtml/events.xml | 28 ++ .../etc/adminhtml/system.xml | 59 +++ .../Magento/NewRelicReporting/etc/config.xml | 21 + .../Magento/NewRelicReporting/etc/crontab.xml | 14 + app/code/Magento/NewRelicReporting/etc/di.xml | 15 + .../Magento/NewRelicReporting/etc/events.xml | 13 + .../NewRelicReporting/etc/frontend/events.xml | 13 + .../Magento/NewRelicReporting/etc/module.xml | 19 + .../Store/Api/StoreManagementInterface.php | 19 + .../Store/Api/WebsiteManagementInterface.php | 19 + .../Magento/Store/Model/StoreManagement.php | 38 ++ .../Magento/Store/Model/WebsiteManagement.php | 37 ++ .../Test/Unit/Model/StoreManagementTest.php | 56 +++ .../Test/Unit/Model/WebsiteManagementTest.php | 52 +++ app/code/Magento/Store/etc/di.xml | 2 + composer.json | 1 + composer.lock | 4 +- 96 files changed, 7848 insertions(+), 14 deletions(-) create mode 100644 app/code/Magento/Catalog/Api/ProductManagementInterface.php create mode 100644 app/code/Magento/Catalog/Model/ProductManagement.php create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/ProductManagementTest.php create mode 100644 app/code/Magento/Customer/Api/CustomerManagementInterface.php create mode 100644 app/code/Magento/Customer/Model/CustomerManagement.php create mode 100644 app/code/Magento/Customer/Test/Unit/Model/CustomerManagementTest.php create mode 100644 app/code/Magento/NewRelicReporting/LICENSE.txt create mode 100644 app/code/Magento/NewRelicReporting/LICENSE_AFL.txt create mode 100644 app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Config.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Counter.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Counts.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Cron.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Cron/ReportCounts.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Cron/ReportModulesInfo.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Cron/ReportNewRelicCron.php create mode 100644 app/code/Magento/NewRelicReporting/Model/CronEvent.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Module.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Module/Collect.php create mode 100644 app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/CheckConfig.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdmins.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdminsToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsers.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsersToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlaced.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlacedToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeleted.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeletedToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSaved.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSavedToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlush.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlushToNewRelic.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Orders.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Counts.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Counts/Collection.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Module.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Module/Collection.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Orders.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Orders/Collection.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/System.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/System/Collection.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Users.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Resource/Users/Collection.php create mode 100644 app/code/Magento/NewRelicReporting/Model/System.php create mode 100644 app/code/Magento/NewRelicReporting/Model/Users.php create mode 100644 app/code/Magento/NewRelicReporting/README.md create mode 100644 app/code/Magento/NewRelicReporting/Setup/InstallSchema.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Apm/DeploymentsTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/CounterTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportCountsTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportModulesInfoTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportNewRelicCronTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/CronEventTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/CronTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Module/CollectTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/CheckConfigTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushTest.php create mode 100644 app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushToNewRelicTest.php create mode 100644 app/code/Magento/NewRelicReporting/composer.json create mode 100644 app/code/Magento/NewRelicReporting/etc/acl.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/adminhtml/events.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/config.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/crontab.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/di.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/events.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/frontend/events.xml create mode 100644 app/code/Magento/NewRelicReporting/etc/module.xml create mode 100644 app/code/Magento/Store/Api/StoreManagementInterface.php create mode 100644 app/code/Magento/Store/Api/WebsiteManagementInterface.php create mode 100644 app/code/Magento/Store/Model/StoreManagement.php create mode 100644 app/code/Magento/Store/Model/WebsiteManagement.php create mode 100644 app/code/Magento/Store/Test/Unit/Model/StoreManagementTest.php create mode 100644 app/code/Magento/Store/Test/Unit/Model/WebsiteManagementTest.php diff --git a/app/code/Magento/Catalog/Api/CategoryManagementInterface.php b/app/code/Magento/Catalog/Api/CategoryManagementInterface.php index 8671e9ea0b9f5..fbc619955b08a 100644 --- a/app/code/Magento/Catalog/Api/CategoryManagementInterface.php +++ b/app/code/Magento/Catalog/Api/CategoryManagementInterface.php @@ -33,4 +33,11 @@ public function getTree($rootCategoryId = null, $depth = null); * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function move($categoryId, $parentId, $afterId = null); + + /** + * Provide the number of category count + * + * @return int + */ + public function getCount(); } diff --git a/app/code/Magento/Catalog/Api/ProductManagementInterface.php b/app/code/Magento/Catalog/Api/ProductManagementInterface.php new file mode 100644 index 0000000000000..a0a46774de43b --- /dev/null +++ b/app/code/Magento/Catalog/Api/ProductManagementInterface.php @@ -0,0 +1,20 @@ +categoryRepository = $categoryRepository; $this->categoryTree = $categoryTree; + $this->categoriesFactory = $categoriesFactory; } /** @@ -72,4 +77,15 @@ public function move($categoryId, $parentId, $afterId = null) } return true; } + + /** + * {@inheritdoc} + */ + public function getCount() + { + $categories = $this->categoriesFactory->create(); + /** @var \Magento\Catalog\Model\Resource\Category\Collection $categories */ + $categories->addAttributeToFilter('parent_id', ['gt' => 0]); + return $categories->getSize(); + } } diff --git a/app/code/Magento/Catalog/Model/ProductManagement.php b/app/code/Magento/Catalog/Model/ProductManagement.php new file mode 100644 index 0000000000000..2d98303a3115a --- /dev/null +++ b/app/code/Magento/Catalog/Model/ProductManagement.php @@ -0,0 +1,44 @@ +productsFactory = $productsFactory; + } + + /** + * {@inheritdoc} + */ + public function getCount($status = null) + { + $products = $this->productsFactory->create(); + /** @var \Magento\Catalog\Model\Resource\Product\Collection $products */ + switch ($status) { + case Status::STATUS_ENABLED: + $products->addAttributeToFilter('status', Status::STATUS_ENABLED); + break; + case Status::STATUS_DISABLED: + $products->addAttributeToFilter('status', Status::STATUS_DISABLED); + break; + } + return $products->getSize(); + } +} diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryManagementTest.php index 70e73e59e44e1..4dd3d46c20e21 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryManagementTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryManagementTest.php @@ -13,22 +13,35 @@ class CategoryManagementTest extends \PHPUnit_Framework_TestCase protected $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $categoryRepositoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\Category\Tree|\PHPUnit_Framework_MockObject_MockObject */ protected $categoryTreeMock; + /** + * @var \Magento\Catalog\Model\Resource\Category\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject + */ + protected $categoriesFactoryMock; + protected function setUp() { - $this->categoryRepositoryMock = $this->getMock('\Magento\Catalog\Api\CategoryRepositoryInterface'); - $this->categoryTreeMock = $this->getMock('\Magento\Catalog\Model\Category\Tree', [], [], '', false); + $this->categoryRepositoryMock = $this->getMock('Magento\Catalog\Api\CategoryRepositoryInterface'); + $this->categoryTreeMock = $this->getMock('Magento\Catalog\Model\Category\Tree', [], [], '', false); + $this->categoriesFactoryMock = $this->getMock( + 'Magento\Catalog\Model\Resource\Category\CollectionFactory', + ['create'], + [], + '', + false + ); $this->model = new \Magento\Catalog\Model\CategoryManagement( $this->categoryRepositoryMock, - $this->categoryTreeMock + $this->categoryTreeMock, + $this->categoriesFactoryMock ); } @@ -171,4 +184,28 @@ public function testMoveWithCouldNotMoveException() ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message'))); $this->model->move($categoryId, $parentId, $afterId); } + + public function testGetCount() + { + $categoriesMock = $this->getMock('\Magento\Catalog\Model\Resource\Category\Collection', [], [], '', false); + + $this->categoriesFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($categoriesMock); + $categoriesMock + ->expects($this->once()) + ->method('addAttributeToFilter') + ->with('parent_id', ['gt' => 0]) + ->willReturnSelf(); + $categoriesMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount() + ); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductManagementTest.php new file mode 100644 index 0000000000000..af3caf60b77c2 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductManagementTest.php @@ -0,0 +1,83 @@ +productsFactoryMock = $this->getMock( + 'Magento\Catalog\Model\Resource\Product\CollectionFactory', + ['create'], + [], + '', + false + ); + $this->model = new \Magento\Catalog\Model\ProductManagement( + $this->productsFactoryMock + ); + } + + public function testGetEnabledCount() + { + $statusEnabled = 1; + $productsMock = $this->getMock('Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false); + + $this->productsFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($productsMock); + $productsMock + ->expects($this->once()) + ->method('addAttributeToFilter') + ->with('status', $statusEnabled) + ->willReturnSelf(); + $productsMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount($statusEnabled) + ); + } + + public function testGetDisabledCount() + { + $statusDisabled = 2; + $productsMock = $this->getMock('\Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false); + + $this->productsFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($productsMock); + $productsMock + ->expects($this->once()) + ->method('addAttributeToFilter') + ->with('status', $statusDisabled) + ->willReturnSelf(); + $productsMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount($statusDisabled) + ); + } +} diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index 40b644c226a2a..66a62bcf86223 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -42,6 +42,7 @@ + diff --git a/app/code/Magento/ConfigurableProduct/Api/ConfigurableProductManagementInterface.php b/app/code/Magento/ConfigurableProduct/Api/ConfigurableProductManagementInterface.php index 66e05cf340d40..78409ad6cc9c9 100644 --- a/app/code/Magento/ConfigurableProduct/Api/ConfigurableProductManagementInterface.php +++ b/app/code/Magento/ConfigurableProduct/Api/ConfigurableProductManagementInterface.php @@ -20,4 +20,12 @@ interface ConfigurableProductManagementInterface * @return \Magento\Catalog\Api\Data\ProductInterface[] */ public function generateVariation(\Magento\Catalog\Api\Data\ProductInterface $product, $options); + + /** + * Provide the number of product count + * + * @param null|int $status + * @return int + */ + public function getCount($status = null); } diff --git a/app/code/Magento/ConfigurableProduct/Model/ConfigurableProductManagement.php b/app/code/Magento/ConfigurableProduct/Model/ConfigurableProductManagement.php index 618977fd9d20a..1587f37adb601 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ConfigurableProductManagement.php +++ b/app/code/Magento/ConfigurableProduct/Model/ConfigurableProductManagement.php @@ -4,9 +4,13 @@ * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\ConfigurableProduct\Model; +use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Model\Product\Attribute\Source\Status; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\CollectionFactory; + class ConfigurableProductManagement implements \Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface { /** @@ -19,28 +23,54 @@ class ConfigurableProductManagement implements \Magento\ConfigurableProduct\Api\ */ private $productVariationBuilder; + /** + * @var CollectionFactory + */ + protected $productsFactory; + /** * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository * @param ProductVariationsBuilder $productVariationBuilder + * @param CollectionFactory $productsFactory */ public function __construct( \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository, - ProductVariationsBuilder $productVariationBuilder + ProductVariationsBuilder $productVariationBuilder, + CollectionFactory $productsFactory ) { $this->attributeRepository = $attributeRepository; $this->productVariationBuilder = $productVariationBuilder; + $this->productsFactory = $productsFactory; } /** * {@inheritdoc} */ - public function generateVariation(\Magento\Catalog\Api\Data\ProductInterface $product, $options) + public function generateVariation(ProductInterface $product, $options) { $attributes = $this->getAttributesForMatrix($options); $products = $this->productVariationBuilder->create($product, $attributes); return $products; } + /** + * {@inheritdoc} + */ + public function getCount($status = null) + { + $products = $this->productsFactory->create(); + /** @var \Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\Collection $products */ + switch ($status) { + case Status::STATUS_ENABLED: + $products->addAttributeToFilter('status', Status::STATUS_ENABLED); + break; + case Status::STATUS_DISABLED: + $products->addAttributeToFilter('status', Status::STATUS_DISABLED); + break; + } + return $products->getSize(); + } + /** * Prepare attribute info for variation matrix generation * diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/ConfigurableProductManagementTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/ConfigurableProductManagementTest.php index 0042ca9fc9b6b..380e5736d1d11 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/ConfigurableProductManagementTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/ConfigurableProductManagementTest.php @@ -6,6 +6,9 @@ namespace Magento\ConfigurableProduct\Test\Unit\Model; +use Magento\ConfigurableProduct\Model\ConfigurableProductManagement; +use Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\CollectionFactory; + class ConfigurableProductManagementTest extends \PHPUnit_Framework_TestCase { /** @@ -14,12 +17,12 @@ class ConfigurableProductManagementTest extends \PHPUnit_Framework_TestCase protected $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $attributeRepository; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\ConfigurableProduct\Model\ProductVariationsBuilder|\PHPUnit_Framework_MockObject_MockObject */ protected $productVariationBuilder; @@ -33,6 +36,11 @@ class ConfigurableProductManagementTest extends \PHPUnit_Framework_TestCase */ protected $option; + /** + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject + */ + protected $productsFactoryMock; + protected function setUp() { $this->attributeRepository = $this->getMock('\Magento\Catalog\Api\ProductAttributeRepositoryInterface'); @@ -51,8 +59,19 @@ protected function setUp() '', false ); + $this->productsFactoryMock = $this->getMock( + '\Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\CollectionFactory', + ['create'], + [], + '', + false + ); - $this->model = new \Magento\ConfigurableProduct\Model\ConfigurableProductManagement($this->attributeRepository, $this->productVariationBuilder); + $this->model = new ConfigurableProductManagement( + $this->attributeRepository, + $this->productVariationBuilder, + $this->productsFactoryMock + ); } public function testGenerateVariation() @@ -86,4 +105,66 @@ public function testGenerateVariation() $expected = ['someObject']; $this->assertEquals($expected, $this->model->generateVariation($this->product, [$this->option])); } + + public function testGetEnabledCount() + { + $statusEnabled = 1; + $productsMock = $this->getMock( + 'Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\Collection', + [], + [], + '', + false + ); + + $this->productsFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($productsMock); + $productsMock + ->expects($this->once()) + ->method('addAttributeToFilter') + ->with('status', $statusEnabled) + ->willReturnSelf(); + $productsMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount($statusEnabled) + ); + } + + public function testGetDisabledCount() + { + $statusDisabled = 2; + $productsMock = $this->getMock( + 'Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Product\Collection', + [], + [], + '', + false + ); + + $this->productsFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($productsMock); + $productsMock + ->expects($this->once()) + ->method('addAttributeToFilter') + ->with('status', $statusDisabled) + ->willReturnSelf(); + $productsMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount($statusDisabled) + ); + } } diff --git a/app/code/Magento/Customer/Api/CustomerManagementInterface.php b/app/code/Magento/Customer/Api/CustomerManagementInterface.php new file mode 100644 index 0000000000000..f50f19ad2e99a --- /dev/null +++ b/app/code/Magento/Customer/Api/CustomerManagementInterface.php @@ -0,0 +1,19 @@ +customersFactory = $customersFactory; + } + + /** + * {@inheritdoc} + */ + public function getCount() + { + $customers = $this->customersFactory->create(); + /** @var \Magento\Customer\Model\Resource\Customer\Collection $customers */ + return $customers->getSize(); + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/CustomerManagementTest.php b/app/code/Magento/Customer/Test/Unit/Model/CustomerManagementTest.php new file mode 100644 index 0000000000000..19abc7edb4eb9 --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/CustomerManagementTest.php @@ -0,0 +1,52 @@ +customersFactoryMock = $this->getMock( + 'Magento\Customer\Model\Resource\Customer\CollectionFactory', + ['create'], + [], + '', + false + ); + $this->model = new \Magento\Customer\Model\CustomerManagement( + $this->customersFactoryMock + ); + } + + public function testGetCount() + { + $customersMock = $this->getMock('\Magento\Customer\Model\Resource\Customer\Collection', [], [], '', false); + + $this->customersFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($customersMock); + $customersMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount() + ); + } +} diff --git a/app/code/Magento/Customer/etc/di.xml b/app/code/Magento/Customer/etc/di.xml index bf8b44c4f76b6..f9c5504a0aedc 100644 --- a/app/code/Magento/Customer/etc/di.xml +++ b/app/code/Magento/Customer/etc/di.xml @@ -37,6 +37,8 @@ type="Magento\Customer\Model\Metadata\CustomerCachedMetadata" /> + Magento\Customer\Model\Config\Share\Proxy diff --git a/app/code/Magento/NewRelicReporting/LICENSE.txt b/app/code/Magento/NewRelicReporting/LICENSE.txt new file mode 100644 index 0000000000000..49525fd99da9c --- /dev/null +++ b/app/code/Magento/NewRelicReporting/LICENSE.txt @@ -0,0 +1,48 @@ + +Open Software License ("OSL") v. 3.0 + +This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Open Software License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. \ No newline at end of file diff --git a/app/code/Magento/NewRelicReporting/LICENSE_AFL.txt b/app/code/Magento/NewRelicReporting/LICENSE_AFL.txt new file mode 100644 index 0000000000000..7232b53a673ea --- /dev/null +++ b/app/code/Magento/NewRelicReporting/LICENSE_AFL.txt @@ -0,0 +1,48 @@ + +Academic Free License ("AFL") v. 3.0 + +This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Academic Free License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright � 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. \ No newline at end of file diff --git a/app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php b/app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php new file mode 100644 index 0000000000000..cedd0739e39d0 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php @@ -0,0 +1,98 @@ +config = $config; + $this->logger = $logger; + $this->clientFactory = $clientFactory; + } + + /** + * Performs the request to make the deployment + * + * @param string $description + * @param bool $change + * @param bool $user + * + * @return bool|string + */ + public function setDeployment($description, $change = false, $user = false) + { + $apiUrl = $this->config->getNewRelicApiUrl(); + + if (empty($apiUrl)) { + $this->logger->notice('New Relic API URL is blank, using fallback URL'); + $apiUrl = self::API_URL; + } + + /** @var \Magento\Framework\HTTP\ZendClient $client */ + $client = $this->clientFactory->create(); + $client->setUri($apiUrl); + $client->setMethod(ZendClient::POST); + + $client->setHeaders(['x-api-key' => $this->config->getNewRelicApiKey()]); + + $params = [ + 'deployment[app_name]' => $this->config->getNewRelicAppName(), + 'deployment[application_id]' => $this->config->getNewRelicAppId(), + 'deployment[description]' => $description, + 'deployment[changelog]' => $change, + 'deployment[user]' => $user + ]; + + $client->setParameterPost($params); + + try { + $response = $client->request(); + } catch (\Zend_Http_Client_Exception $e) { + $this->logger->critical($e); + return false; + } + + if (($response->getStatus() < 200 || $response->getStatus() > 210)) { + $this->logger->warning('Deployment marker request did not send a 200 status code.'); + return false; + } + + return $response->getBody(); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Config.php b/app/code/Magento/NewRelicReporting/Model/Config.php new file mode 100644 index 0000000000000..ef53a1ed6756f --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Config.php @@ -0,0 +1,199 @@ +scopeConfig = $scopeConfig; + $this->encryptor = $encryptor; + $this->resourceConfig = $resourceConfig; + } + + /** + * Returns module's enabled status + * + * @return bool + */ + public function isNewRelicEnabled() + { + return (bool)$this->scopeConfig->getValue('newrelicreporting/general/enable'); + } + + /** + * Returns configured URL for API + * + * @return string + */ + public function getNewRelicApiUrl() + { + return (string)$this->scopeConfig->getValue('newrelicreporting/general/api_url'); + } + + /** + * Returns configured URL for Insights API + * + * @return string + */ + public function getInsightsApiUrl() + { + return (string)$this->scopeConfig->getValue('newrelicreporting/general/insights_api_url'); + } + + /** + * Returns configured account ID for New Relic + * + * @return string + */ + public function getNewRelicAccountId() + { + return (string)$this->scopeConfig->getValue('newrelicreporting/general/account_id'); + } + + /** + * Return configured NR Application ID + * + * @return int + */ + public function getNewRelicAppId() + { + return (int)$this->scopeConfig->getValue('newrelicreporting/general/app_id'); + } + + /** + * Returns configured API key for APM + * + * @return string + */ + public function getNewRelicApiKey() + { + return $this->encryptor->decrypt($this->scopeConfig->getValue('newrelicreporting/general/api')); + } + + /** + * Returns configured Insights insert key for New Relic events related to cron jobs + * + * @return string + */ + public function getInsightsInsertKey() + { + return $this->encryptor->decrypt($this->scopeConfig->getValue('newrelicreporting/general/insights_insert_key')); + } + + /** + * Returns configured NR Application name + * + * @return string + */ + public function getNewRelicAppName() + { + return (string)$this->scopeConfig->getValue('newrelicreporting/general/app_name'); + } + + /** + * Returns config setting for overall cron to be enabled + * + * @return bool + */ + public function isCronEnabled() + { + return (bool)$this->scopeConfig->getValue('newrelicreporting/cron/enable_cron'); + } + + /** + * Sets config value + * + * @param string $pathId + * @param mixed $value + * @param string $scope + * @param int $scopeId + * @return void + */ + protected function setConfigValue($pathId, $value, $scope = 'default', $scopeId = 0) + { + $this->resourceConfig->saveConfig($pathId, $value, $scope, $scopeId); + } + + /** + * Disable module's functionality for case when new relic extension is not available + * + * @return void + */ + public function disableModule() + { + $this->setConfigValue('newrelicreporting/general/enable', 0); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Counter.php b/app/code/Magento/NewRelicReporting/Model/Counter.php new file mode 100644 index 0000000000000..8454cd96dc3ce --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Counter.php @@ -0,0 +1,150 @@ +productManagement = $productManagement; + $this->configurableManagement = $configurableManagement; + $this->categoryManagement = $categoryManagement; + $this->customerManagement = $customerManagement; + $this->websiteManagement = $websiteManagement; + $this->storeManagement = $storeManagement; + } + + /** + * Get count of all products, no conditions + * + * @return int + */ + public function getAllProductsCount() + { + $count = $this->productManagement->getCount(); + return (int)$count; + } + + /** + * Get count of configurable products + * + * @return int + */ + public function getConfigurableCount() + { + $count = $this->configurableManagement->getCount(); + return (int)$count; + } + + /** + * Get count of products which are active + * + * @return int + */ + public function getActiveCatalogSize() + { + $count = $this->productManagement->getCount(Status::STATUS_ENABLED); + return (int)$count; + } + + /** + * Get count of categories, minus one which is the root category + * + * @return int + */ + public function getCategoryCount() + { + $count = $this->categoryManagement->getCount(); + return (int)$count; + } + + /** + * Get customer count + * + * @return int + */ + public function getCustomerCount() + { + $count = $this->customerManagement->getCount(); + return (int)$count; + } + + /** + * Get count of websites, minus one to exclude admin website + * + * @return int + */ + public function getWebsiteCount() + { + $count = $this->websiteManagement->getCount(); + return (int)$count; + } + + /** + * Get count of store views + * + * @return int + */ + public function getStoreViewsCount() + { + $count = $this->storeManagement->getCount(); + return (int)$count; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Counts.php b/app/code/Magento/NewRelicReporting/Model/Counts.php new file mode 100644 index 0000000000000..a6599a4d31d4a --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Counts.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Resource\Counts'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Cron.php b/app/code/Magento/NewRelicReporting/Model/Cron.php new file mode 100644 index 0000000000000..fa346948c4841 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Cron.php @@ -0,0 +1,69 @@ +config = $config; + $this->reportModulesInfo = $reportModulesInfo; + $this->reportCounts = $reportCounts; + $this->reportNewRelicCron = $reportNewRelicCron; + } + + /** + * The method run by the cron that fires all required events. + * + * @return \Magento\NewRelicReporting\Model\Cron + */ + public function runCron() + { + if ($this->config->isCronEnabled()) { + $this->reportNewRelicCron->report(); + $this->reportModulesInfo->report(); + $this->reportCounts->report(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Cron/ReportCounts.php b/app/code/Magento/NewRelicReporting/Model/Cron/ReportCounts.php new file mode 100644 index 0000000000000..a4e4ec30ec225 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Cron/ReportCounts.php @@ -0,0 +1,181 @@ +config = $config; + $this->productManagement = $productManagement; + $this->configurableManagement = $configurableManagement; + $this->categoryManagement = $categoryManagement; + $this->countsFactory = $countsFactory; + $this->countsCollectionFactory = $countsCollectionFactory; + $this->dateTime = $dateTime; + } + + /** + * Updates the count for a specific model in the database + * + * @param int $count + * @param \Magento\NewRelicReporting\Model\Counts $model + * @param string $type + * @return void + */ + protected function updateCount($count, \Magento\NewRelicReporting\Model\Counts $model, $type) + { + /** @var \Magento\NewRelicReporting\Model\Resource\Counts\Collection $collection */ + $collection = $this->countsCollectionFactory->create() + ->addFieldToFilter( + 'type', + ['eq' => $type] + )->addOrder( + 'updated_at', + 'DESC' + )->setPageSize(1); + $latestUpdate = $collection->getFirstItem(); + + if ((!$latestUpdate) || ($count != $latestUpdate->getCount())) { + $model->setEntityId(null); + $model->setType($type); + $model->setCount($count); + $model->setUpdatedAt($this->dateTime->formatDate(true)); + $model->save(); + } + } + + /** + * Reports product size to the database reporting_counts table + * + * @return void + */ + protected function reportProductsSize() + { + $productCount = $this->productManagement->getCount(); + /** @var \Magento\NewRelicReporting\Model\Counts $model */ + $model = $this->countsFactory->create()->load(Config::PRODUCT_COUNT, 'type'); + $this->updateCount($productCount, $model, Config::PRODUCT_COUNT); + } + + /** + * Reports configurable product size to the database reporting_counts table + * + * @return void + */ + protected function reportConfigurableProductsSize() + { + $configurableCount = $this->configurableManagement->getCount(); + /** @var \Magento\NewRelicReporting\Model\Counts $model */ + $model = $this->countsFactory->create()->load(Config::CONFIGURABLE_COUNT, 'type'); + $this->updateCount($configurableCount, $model, Config::CONFIGURABLE_COUNT); + } + + /** + * Reports number of active products to the database reporting_counts table + * + * @return void + */ + protected function reportProductsActive() + { + $productsActiveCount = $this->productManagement->getCount(Status::STATUS_ENABLED); + /** @var \Magento\NewRelicReporting\Model\Counts $model */ + $model = $this->countsFactory->create()->load(Config::ACTIVE_COUNT, 'type'); + $this->updateCount($productsActiveCount, $model, Config::ACTIVE_COUNT); + } + + /** + * Reports category size to the database reporting_counts table + * + * @return void + */ + protected function reportCategorySize() + { + $categoryCount = $this->categoryManagement->getCount(); + /** @var \Magento\NewRelicReporting\Model\Counts $model */ + $model = $this->countsFactory->create()->load(Config::CATEGORY_SIZE, 'type'); + $this->updateCount($categoryCount, $model, Config::CATEGORY_SIZE); + } + + /** + * Reports Modules and module changes to the database reporting_module_status table + * + * @return \Magento\NewRelicReporting\Model\Cron\ReportCounts + */ + public function report() + { + if ($this->config->isNewRelicEnabled()) { + $this->reportProductsSize(); + $this->reportConfigurableProductsSize(); + $this->reportProductsActive(); + $this->reportCategorySize(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Cron/ReportModulesInfo.php b/app/code/Magento/NewRelicReporting/Model/Cron/ReportModulesInfo.php new file mode 100644 index 0000000000000..95b6fd7afb264 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Cron/ReportModulesInfo.php @@ -0,0 +1,115 @@ +config = $config; + $this->collect = $collect; + $this->systemFactory = $systemFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports Modules and module changes to the database reporting_module_status table + * + * @return \Magento\NewRelicReporting\Model\Cron\ReportModulesInfo + */ + public function report() + { + if ($this->config->isNewRelicEnabled()) { + $moduleData = $this->collect->getModuleData(); + if (count($moduleData['changes']) > 0) { + foreach ($moduleData['changes'] as $change) { + switch ($change['type']) { + case Config::ENABLED: + $modelData = [ + 'type' => Config::MODULE_ENABLED, + 'action' => $this->jsonEncoder->encode($change), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + break; + case Config::DISABLED: + $modelData = [ + 'type' => Config::MODULE_DISABLED, + 'action' => $this->jsonEncoder->encode($change), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + break; + case Config::INSTALLED: + $modelData = [ + 'type' => Config::MODULE_INSTALLED, + 'action' => $this->jsonEncoder->encode($change), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + break; + case Config::UNINSTALLED: + $modelData = [ + 'type' => Config::MODULE_UNINSTALLED, + 'action' => $this->jsonEncoder->encode($change), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + break; + } + /** @var \Magento\NewRelicReporting\Model\System $systemModel */ + $systemModel = $this->systemFactory->create(); + $systemModel->setData($modelData); + $systemModel->save(); + } + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Cron/ReportNewRelicCron.php b/app/code/Magento/NewRelicReporting/Model/Cron/ReportNewRelicCron.php new file mode 100644 index 0000000000000..5db274fee1f76 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Cron/ReportNewRelicCron.php @@ -0,0 +1,201 @@ +config = $config; + $this->collect = $collect; + $this->counter = $counter; + $this->cronEventFactory = $cronEventFactory; + $this->deploymentsFactory = $deploymentsFactory; + $this->logger = $logger; + } + + /** + * Queue up custom parameters to send in API call to Insights Events + * + * @param array $data + * @return void + */ + public function addCustomParameters(array $data) + { + foreach ($data as $key => $value) { + $this->customParameters[$key] = $value; + } + } + + /** + * Reports current total module counts to Insights + * + * @return void + */ + protected function reportModules() + { + $moduleData = $this->collect->getModuleData(false); + $moduleDataChanges = $moduleData['changes']; + if (count($moduleDataChanges) > 0) { + $enabledChangeArray = []; + $disabledChangeArray = []; + $installedChangeArray = []; + $uninstalledChangeArray = []; + foreach ($moduleDataChanges as $change) { + switch ($change['type']) { + case Config::ENABLED: + $enabledChangeArray[] = $change['name'] . '-' . $change['setup_version']; + break; + case Config::DISABLED: + $disabledChangeArray[] = $change['name'] . '-' . $change['setup_version']; + break; + case Config::INSTALLED: + $installedChangeArray[] = $change['name'] . '-' . $change['setup_version']; + break; + case Config::UNINSTALLED: + $uninstalledChangeArray[] = $change['name'] . '-' . $change['setup_version']; + break; + } + } + $this->setModuleChangeStatusDeployment($enabledChangeArray, 'Modules Enabled'); + $this->setModuleChangeStatusDeployment($disabledChangeArray, 'Modules Disabled'); + $this->setModuleChangeStatusDeployment($installedChangeArray, 'Modules Installed'); + $this->setModuleChangeStatusDeployment($uninstalledChangeArray, 'Modules Uninstalled'); + } + $this->addCustomParameters([Config::MODULES_ENABLED => $moduleData[Config::ENABLED]]); + $this->addCustomParameters([Config::MODULES_DISABLED => $moduleData[Config::DISABLED]]); + $this->addCustomParameters([Config::MODULES_INSTALLED => $moduleData[Config::INSTALLED]]); + } + + /** + * Reports current module change status via deployment marker + * + * @param array $changesArray + * @param string $deploymentText + * @return void + */ + protected function setModuleChangeStatusDeployment(array $changesArray, $deploymentText = '') + { + if (count($changesArray) > 0) { + foreach ($changesArray as $change) { + $this->deploymentsFactory->create()->setDeployment( + $deploymentText, + $change, + 'cron' + ); + } + } + } + + /** + * Reports counts info to New Relic + * + * @return void + */ + protected function reportCounts() + { + $this->addCustomParameters([ + Config::PRODUCT_COUNT => $this->counter->getAllProductsCount(), + Config::CONFIGURABLE_COUNT => $this->counter->getConfigurableCount(), + Config::ACTIVE_COUNT => $this->counter->getActiveCatalogSize(), + Config::CATEGORY_COUNT => $this->counter->getCategoryCount(), + Config::WEBSITE_COUNT => $this->counter->getWebsiteCount(), + Config::STORE_VIEW_COUNT => $this->counter->getStoreViewsCount(), + Config::CUSTOMER_COUNT => $this->counter->getCustomerCount(), + ]); + if (!empty($this->customParameters)) { + try { + $this->cronEventFactory->create() + ->addData($this->customParameters) + ->sendRequest(); + } catch (\Exception $e) { + $this->logger->critical( + sprintf( + "New Relic Cron Event exception: %s\n%s", + $e->getMessage(), + $e->getTraceAsString() + ) + ); + } + } + } + + /** + * Reports info to New Relic by Cron + * + * @return \Magento\NewRelicReporting\Model\Cron\ReportCounts + */ + public function report() + { + if ($this->config->isNewRelicEnabled()) { + $this->reportModules(); + $this->reportCounts(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/CronEvent.php b/app/code/Magento/NewRelicReporting/Model/CronEvent.php new file mode 100644 index 0000000000000..3ba77a58dca3b --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/CronEvent.php @@ -0,0 +1,162 @@ +config = $config; + $this->jsonEncoder = $jsonEncoder; + $this->clientFactory = $clientFactory; + } + + /** + * Returns Insights API url with account id + * + * @return string + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function getEventsUrl() + { + if (empty($this->eventsUrl)) { + $accountId = $this->config->getNewRelicAccountId(); + if (empty($accountId)) { + throw new \Magento\Framework\Exception\LocalizedException(__( + 'No New Relic Application ID configured, cannot continue with Cron Event reporting' + )); + } + $this->eventsUrl = sprintf( + $this->config->getInsightsApiUrl(), + $accountId + ); + } + return $this->eventsUrl; + } + + /** + * Returns HTTP request to events url + * + * @return \Magento\Framework\HTTP\ZendClient + */ + protected function getRequest() + { + if (!isset($this->request)) { + $this->request = $this->clientFactory->create(); + $this->request->setUri($this->getEventsUrl()); + $insertKey = $this->config->getInsightsInsertKey(); + + $this->request->setMethod(ZendClient::POST); + $this->request->setHeaders( + [ + 'X-Insert-Key' => $insertKey, + 'Content-Type' => 'application/json', + ] + ); + } + return $this->request; + } + + /** + * Returns all set custom parameters as JSON string + * + * @return string + */ + protected function getJsonForResponse() + { + $json = [ + 'eventType' => 'Cron', + 'appName' => $this->config->getNewRelicAppName(), + 'appId' => $this->config->getNewRelicAppId(), + ]; + $jsonArrayKeys = array_keys($json); + + foreach ($jsonArrayKeys as $jsonKey) { + if (array_key_exists($jsonKey, $this->customParameters)) { + unset($this->customParameters[$jsonKey]); + } + } + + $json = array_merge($json, $this->customParameters); + + return $this->jsonEncoder->encode($json); + } + + /** + * Add custom parameters to send with API request + * + * @param array $data + * @return \Magento\NewRelicReporting\Model\CronEvent $this + */ + public function addData(array $data) + { + $this->customParameters = array_merge($this->customParameters, $data); + return $this; + } + + /** + * Instantiates request if necessary and sends off with collected data + * + * @return bool + */ + public function sendRequest() + { + $response = $this->getRequest() + ->setRawData($this->getJsonForResponse()) + ->request(); + + if ($response->getStatus() >= 200 && $response->getStatus() < 300) { + return true; + } + return false; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Module.php b/app/code/Magento/NewRelicReporting/Model/Module.php new file mode 100644 index 0000000000000..727ad55e9ccfe --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Module.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Resource\Module'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Module/Collect.php b/app/code/Magento/NewRelicReporting/Model/Module/Collect.php new file mode 100644 index 0000000000000..fd7f4d61c6d30 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Module/Collect.php @@ -0,0 +1,323 @@ +moduleList = $moduleList; + $this->fullModuleList = $fullModuleList; + $this->moduleManager = $moduleManager; + $this->dateTime = $dateTime; + $this->moduleFactory = $moduleFactory; + $this->moduleCollectionFactory = $moduleCollectionFactory; + } + + /** + * Retrieve all disabled modules from the configuration + * + * @return array + */ + protected function getDisabledModules() + { + $allModules = $this->fullModuleList->getNames(); + $enabledModules = $this->moduleList->getNames(); + $disabledModules = array_diff($allModules, $enabledModules); + + return $disabledModules; + } + + /** + * Retrieve all modules array + * + * @return array + */ + protected function getAllModules() + { + return $this->fullModuleList->getAll(); + } + + /** + * Get changes of module not in DB + * + * @param string $moduleName + * @param string $active + * @param string $setupVersion + * @param string $state + * + * @return array + */ + protected function getNewModuleChanges($moduleName, $active, $setupVersion, $state) + { + /** @var \Magento\NewRelicReporting\Model\Module $newModule */ + $newModule = $this->moduleFactory->create(); + $data = [ + 'name' => $moduleName, + 'active' => $active, + 'setup_version' => $setupVersion, + 'state' => $state, + 'updated_at' => $this->dateTime->formatDate(true) + ]; + + $newModule->setData($data); + $newModule->save(); + $moduleChanges = [ + 'name' => $data['name'], + 'setup_version' => $data['setup_version'], + 'type' => Config::INSTALLED + ]; + + return $moduleChanges; + } + + /** + * Grabs the collection items to get final counts + * + * @param array $moduleChanges + * @return array + */ + protected function getFinalCounts($moduleChanges) + { + /** @var Module[] $finalDbModuleArray */ + $finalDbModuleArray = $this->moduleCollectionFactory->create()->getItems(); + + $stateCallback = function (Module $value) { + return $value->getState(); + }; + + $stateValues = array_map($stateCallback, $finalDbModuleArray); + $installedCount = count($stateValues); + $disabledCount = $enabledCount = $uninstalledCount = 0; + + foreach ($stateValues as $state) { + switch($state){ + case Config::ENABLED: + $enabledCount++; + break; + + case Config::DISABLED: + $disabledCount++; + break; + + case Config::UNINSTALLED: + $uninstalledCount++; + break; + } + } + + $installedCount -= $uninstalledCount; + + $finalObject = [ + Config::INSTALLED => $installedCount, + Config::UNINSTALLED => $uninstalledCount, + Config::ENABLED => $enabledCount, + Config::DISABLED => $disabledCount, + 'changes' => $moduleChanges + ]; + + return $finalObject; + } + + /** + * Get module state by it name + * + * @param string $moduleName + * @return string + */ + protected function getState($moduleName) + { + if ($this->moduleManager->isOutputEnabled($moduleName)) { + $state = Config::ENABLED; + } else { + $state = Config::DISABLED; + } + + return $state; + } + + /** + * Get module active state by it name + * + * @param string $moduleName + * @return string + */ + protected function getActive($moduleName) + { + if (in_array($moduleName, $this->getDisabledModules())) { + $active = Config::FALSE; + } else { + $active = Config::TRUE; + } + + return $active; + } + + /** + * Get clean array of changes + * + * @param array $changes + * @return array mixed + */ + protected function getCleanChangesArray($changes) + { + $changesArrayKeys = array_keys($changes); + foreach ($changesArrayKeys as $changesKey) { + if ($changesKey != 'state' && $changesKey != 'active' && $changesKey != 'setup_version') { + unset($changes[$changesKey]); + } + } + + return $changes; + } + + /** + * Takes module changes if module were uninstalled + * + * @param Module[] $dbModuleArray + * @param string[] $arrayModuleNames + * @return array|bool + */ + protected function setUninstalledModuleChanges(array $dbModuleArray, array $arrayModuleNames) + { + foreach ($dbModuleArray as $module) { + /** @var Module $module */ + if (!in_array($module->getName(), $arrayModuleNames) && $module->getState() != Config::UNINSTALLED) { + $moduleChanges = [ + 'name' => $module->getName(), + 'setup_version' => $module->getSetupVersion(), + 'type' => Config::UNINSTALLED + ]; + $module->setData(['entity_id' => $module->getEntityId(), 'state' => Config::UNINSTALLED]); + $module->save(); + return $moduleChanges; + } + } + + return false; + } + + /** + * Collects required data about the modules + * + * @param bool $refresh + * @return array + */ + public function getModuleData($refresh = true) + { + $callback = function (Module $value) { + return $value->getName(); + }; + + $configModules = $this->getAllModules(); + /** @var Module[] $dbModuleArray */ + $dbModuleArray = $this->moduleCollectionFactory->create()->getItems(); + + $nameValues = array_map($callback, $dbModuleArray); + $moduleChanges = []; + + foreach ($configModules as $moduleName => $module) { + unset($module['sequence']); + $state = $this->getState($moduleName); + $active = $this->getActive($moduleName); + $module['state'] = $state; + $module['active'] = $active; + + if (!in_array($moduleName, $nameValues)) { + $moduleChanges[] = $this->getNewModuleChanges($moduleName, $active, $module['setup_version'], $state); + } else { + $dbModule = $dbModuleArray[array_search($moduleName, $nameValues)]; + $changeTest = $dbModule->getData(); + $changes = array_diff($module, $changeTest); + $changesCleanArray = $this->getCleanChangesArray($changes); + + if (count($changesCleanArray) > 0 || + ($this->moduleManager->isOutputEnabled($changeTest['name']) && + $module['setup_version'] != null)) { + $data = [ + 'entity_id' => $changeTest['entity_id'], + 'name' => $changeTest['name'], + 'active' => $active, + 'setup_version' => $module['setup_version'], + 'state' => $state, + 'updated_at' => $this->dateTime->formatDate(true) + ]; + if ($refresh) { + $dbModule->setData($data); + $dbModule->save(); + } + $moduleChanges[] = [ + 'name' => $data['name'], + 'setup_version' => $data['setup_version'], + 'type' => $state + ]; + } + } + } + + $arrayModuleNames = array_keys($configModules); + $uninstalledModuleChanges = $this->setUninstalledModuleChanges($dbModuleArray, $arrayModuleNames); + if (is_array($uninstalledModuleChanges)) { + $moduleChanges[] = $uninstalledModuleChanges; + } + + $finalObject = $this->getFinalCounts($moduleChanges); + + return $finalObject; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php b/app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php new file mode 100644 index 0000000000000..6e0465c56e017 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php @@ -0,0 +1,43 @@ +config = $config; + $this->newRelicWrapper = $newRelicWrapper; + $this->messageManager = $messageManager; + } + + /** + * Update items stock status and low stock date. + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportConcurrentAdminsToNewRelic + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + if (!$this->newRelicWrapper->isExtensionInstalled()) { + $this->config->disableModule(); + $this->messageManager->addError( + __( + 'The New Relic integration requires the newrelic-php5 agent, which is not installed. More + information on installing the agent is available here.', + 'https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-overview' + ), + $this->messageManager->getDefaultGroup() + ); + } + } + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdmins.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdmins.php new file mode 100644 index 0000000000000..11ba3db7ee910 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdmins.php @@ -0,0 +1,94 @@ +config = $config; + $this->backendAuthSession = $backendAuthSession; + $this->usersFactory = $usersFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports concurrent admins to the database reporting_users table + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportConcurrentAdmins + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + if ($this->backendAuthSession->isLoggedIn()) { + $user = $this->backendAuthSession->getUser(); + $jsonData = [ + 'id' => $user->getId(), + 'username' => $user->getUsername(), + 'name' => $user->getFirstname() . ' ' . $user->getLastname(), + ]; + + $modelData = [ + 'type' => 'admin_activity', + 'action' => $this->jsonEncoder->encode($jsonData), + 'updated_at' => $this->dateTime->formatDate(true), + ]; + + /** @var \Magento\NewRelicReporting\Model\Users $usersModel */ + $usersModel = $this->usersFactory->create(); + $usersModel->setData($modelData); + $usersModel->save(); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdminsToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdminsToNewRelic.php new file mode 100644 index 0000000000000..f3ab97bd7a6af --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentAdminsToNewRelic.php @@ -0,0 +1,69 @@ +config = $config; + $this->backendAuthSession = $backendAuthSession; + $this->newRelicWrapper = $newRelicWrapper; + } + + /** + * Adds New Relic custom parameters per adminhtml request for current admin user, if applicable + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportConcurrentAdminsToNewRelic + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + if ($this->backendAuthSession->isLoggedIn()) { + $user = $this->backendAuthSession->getUser(); + $this->newRelicWrapper->addCustomParameter(Config::ADMIN_USER_ID, $user->getId()); + $this->newRelicWrapper->addCustomParameter(Config::ADMIN_USER, $user->getUsername()); + $this->newRelicWrapper->addCustomParameter( + Config::ADMIN_NAME, + $user->getFirstname() . ' ' . $user->getLastname() + ); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsers.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsers.php new file mode 100644 index 0000000000000..300d353a5aee6 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsers.php @@ -0,0 +1,112 @@ +config = $config; + $this->customerSession = $customerSession; + $this->customerRepository = $customerRepository; + $this->storeManager = $storeManager; + $this->usersFactory = $usersFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports concurrent users to the database reporting_users table + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportConcurrentUsers + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + if ($this->customerSession->isLoggedIn()) { + $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); + + $jsonData = [ + 'id' => $customer->getId(), + 'name' => $customer->getFirstname() . ' ' . $customer->getLastname(), + 'store' => $this->storeManager->getStore()->getName(), + 'website' => $this->storeManager->getWebsite()->getName(), + ]; + + $modelData = [ + 'type' => 'user_action', + 'action' => $this->jsonEncoder->encode($jsonData), + 'updated_at' => $this->dateTime->formatDate(true), + ]; + + /** @var \Magento\NewRelicReporting\Model\Users $usersModel */ + $usersModel = $this->usersFactory->create(); + $usersModel->setData($modelData); + $usersModel->save(); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsersToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsersToNewRelic.php new file mode 100644 index 0000000000000..73eea0016f6b2 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportConcurrentUsersToNewRelic.php @@ -0,0 +1,87 @@ +config = $config; + $this->customerSession = $customerSession; + $this->customerRepository = $customerRepository; + $this->storeManager = $storeManager; + $this->newRelicWrapper = $newRelicWrapper; + } + + /** + * Adds New Relic custom parameters per request for store, website, and logged in user if applicable + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportConcurrentUsersToNewRelic + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + $this->newRelicWrapper->addCustomParameter(Config::STORE, $this->storeManager->getStore()->getName()); + $this->newRelicWrapper->addCustomParameter(Config::WEBSITE, $this->storeManager->getWebsite()->getName()); + + if ($this->customerSession->isLoggedIn()) { + $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); + $this->newRelicWrapper->addCustomParameter(Config::CUSTOMER_ID, $customer->getId()); + $this->newRelicWrapper->addCustomParameter( + Config::CUSTOMER_NAME, + $customer->getFirstname() . ' ' . $customer->getLastname() + ); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlaced.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlaced.php new file mode 100644 index 0000000000000..2fd7934dd96be --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlaced.php @@ -0,0 +1,79 @@ +config = $config; + $this->ordersFactory = $ordersFactory; + $this->dateTime = $dateTime; + } + + /** + * Reports orders placed to the database reporting_orders table + * + * @param \Magento\Framework\Event\Observer $observer + * @return \Magento\NewRelicReporting\Model\Observer\ReportOrderPlaced + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + if ($this->config->isNewRelicEnabled()) { + /** @var \Magento\Sales\Model\Order $order */ + $order = $observer->getEvent()->getOrder(); + $itemCount = $order->getTotalItemCount(); + if (!is_numeric($itemCount) && empty($itemCount)) { + $itemCount = $order->getTotalQtyOrdered(); + } + + $modelData = [ + 'customer_id' => $order->getCustomerId(), + 'total' => $order->getGrandTotal(), + 'total_base' => $order->getBaseGrandTotal(), + 'item_count' => $itemCount, + 'updated_at' => $this->dateTime->formatDate(true) + ]; + + /** @var \Magento\NewRelicReporting\Model\Orders $orderModel */ + $orderModel = $this->ordersFactory->create(); + $orderModel->setData($modelData); + $orderModel->save(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlacedToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlacedToNewRelic.php new file mode 100644 index 0000000000000..909c8d33e5193 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportOrderPlacedToNewRelic.php @@ -0,0 +1,63 @@ +config = $config; + $this->newRelicWrapper = $newRelicWrapper; + } + + /** + * Reports orders placed to New Relic + * + * @param \Magento\Framework\Event\Observer $observer + * @return \Magento\NewRelicReporting\Model\Observer\ReportOrderPlacedToNewRelic + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + if ($this->config->isNewRelicEnabled()) { + /** @var \Magento\Sales\Model\Order $order */ + $order = $observer->getEvent()->getOrder(); + $itemCount = $order->getTotalItemCount(); + if (!is_numeric($itemCount) && empty($itemCount)) { + $itemCount = $order->getTotalQtyOrdered(); + } + + $this->newRelicWrapper->addCustomParameter(Config::ORDER_PLACED, 1); + $this->newRelicWrapper->addCustomParameter(Config::ORDER_ITEMS, $itemCount); + $this->newRelicWrapper->addCustomParameter(Config::ORDER_VALUE, $order->getBaseGrandTotal()); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeleted.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeleted.php new file mode 100644 index 0000000000000..969f0a633d044 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeleted.php @@ -0,0 +1,86 @@ +config = $config; + $this->systemFactory = $systemFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports any products deleted to the database reporting_system_updates table + * + * @param \Magento\Framework\Event\Observer $observer + * @return \Magento\NewRelicReporting\Model\Observer\ReportProductDeleted + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + if ($this->config->isNewRelicEnabled()) { + /** @var \Magento\Catalog\Model\Product $product */ + $product = $observer->getEvent()->getProduct(); + + $jsonData = [ + 'id' => $product->getId(), + 'status' => 'deleted' + ]; + + $modelData = [ + 'type' => Config::PRODUCT_CHANGE, + 'action' => $this->jsonEncoder->encode($jsonData), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + + /** @var \Magento\NewRelicReporting\Model\System $systemModel */ + $systemModel = $this->systemFactory->create(); + $systemModel->setData($modelData); + $systemModel->save(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeletedToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeletedToNewRelic.php new file mode 100644 index 0000000000000..c865421b1a97a --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductDeletedToNewRelic.php @@ -0,0 +1,53 @@ +config = $config; + $this->newRelicWrapper = $newRelicWrapper; + } + + /** + * Reports any products deleted to New Relic + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportProductDeletedToNewRelic + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + $this->newRelicWrapper->addCustomParameter(Config::PRODUCT_CHANGE, 'delete'); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSaved.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSaved.php new file mode 100644 index 0000000000000..0e4d69359cd7e --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSaved.php @@ -0,0 +1,92 @@ +config = $config; + $this->systemFactory = $systemFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports any products created or updated to the database reporting_system_updates table + * + * @param \Magento\Framework\Event\Observer $observer + * @return \Magento\NewRelicReporting\Model\Observer\ReportProductSaved + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + if ($this->config->isNewRelicEnabled()) { + /** @var \Magento\Catalog\Model\Product $product */ + $product = $observer->getEvent()->getProduct(); + + $jsonData = [ + 'name' => $product->getName(), + ]; + + if ($product->isObjectNew()) { + $jsonData['status'] = 'created'; + } else { + $jsonData['id'] = $product->getId(); + $jsonData['status'] = 'updated'; + } + + $modelData = [ + 'type' => Config::PRODUCT_CHANGE, + 'action' => $this->jsonEncoder->encode($jsonData), + 'updated_at' => $this->dateTime->formatDate(true) + ]; + + /** @var \Magento\NewRelicReporting\Model\System $systemModel */ + $systemModel = $this->systemFactory->create(); + $systemModel->setData($modelData); + $systemModel->save(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSavedToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSavedToNewRelic.php new file mode 100644 index 0000000000000..1f985ebde3e87 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportProductSavedToNewRelic.php @@ -0,0 +1,66 @@ +config = $config; + $this->newRelicWrapper = $newRelicWrapper; + } + + /** + * Reports any products created or updated to New Relic + * + * @param \Magento\Framework\Event\Observer $observer + * @return \Magento\NewRelicReporting\Model\Observer\ReportProductSavedToNewRelic + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + if ($this->config->isNewRelicEnabled()) { + /** @var \Magento\Catalog\Model\Product $product */ + $product = $observer->getEvent()->getProduct(); + if ($product->isObjectNew()) { + $this->newRelicWrapper->addCustomParameter( + \Magento\NewRelicReporting\Model\Config::PRODUCT_CHANGE, + 'create' + ); + } else { + $this->newRelicWrapper->addCustomParameter( + \Magento\NewRelicReporting\Model\Config::PRODUCT_CHANGE, + 'update' + ); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlush.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlush.php new file mode 100644 index 0000000000000..36f5106c5ff19 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlush.php @@ -0,0 +1,79 @@ +config = $config; + $this->systemFactory = $systemFactory; + $this->jsonEncoder = $jsonEncoder; + $this->dateTime = $dateTime; + } + + /** + * Reports a system cache flush to the database reporting_system_updates table + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportSystemCacheFlush + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + $jsonData = ['status' => 'updated']; + + $modelData = [ + 'type' => Config::FLUSH_CACHE, + 'action' => $this->jsonEncoder->encode($jsonData), + 'updated_at' => $this->dateTime->formatDate(true), + ]; + + /** @var \Magento\NewRelicReporting\Model\System $systemModel */ + $systemModel = $this->systemFactory->create(); + $systemModel->setData($modelData); + $systemModel->save(); + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlushToNewRelic.php b/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlushToNewRelic.php new file mode 100644 index 0000000000000..e562378c10bf5 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlushToNewRelic.php @@ -0,0 +1,67 @@ +config = $config; + $this->backendAuthSession = $backendAuthSession; + $this->deploymentsFactory = $deploymentsFactory; + } + + /** + * Report system cache is flushed to New Relic + * + * @return \Magento\NewRelicReporting\Model\Observer\ReportSystemCacheFlushToNewRelic + */ + public function execute() + { + if ($this->config->isNewRelicEnabled()) { + $user = $this->backendAuthSession->getUser(); + if ($user->getId()) { + $this->deploymentsFactory->create()->setDeployment( + 'Cache Flush', + $user->getUsername() . ' flushed the cache.', + $user->getUsername() + ); + } + } + + return $this; + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Orders.php b/app/code/Magento/NewRelicReporting/Model/Orders.php new file mode 100644 index 0000000000000..b7bda47cf54c5 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Orders.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Resource\Orders'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Counts.php b/app/code/Magento/NewRelicReporting/Model/Resource/Counts.php new file mode 100644 index 0000000000000..920a4db1dc88e --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Counts.php @@ -0,0 +1,20 @@ +_init('reporting_counts', 'entity_id'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Counts/Collection.php b/app/code/Magento/NewRelicReporting/Model/Resource/Counts/Collection.php new file mode 100644 index 0000000000000..65f9d48d6a7d0 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Counts/Collection.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Counts', 'Magento\NewRelicReporting\Model\Resource\Counts'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Module.php b/app/code/Magento/NewRelicReporting/Model/Resource/Module.php new file mode 100644 index 0000000000000..af9f95a2fe1d5 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Module.php @@ -0,0 +1,20 @@ +_init('reporting_module_status', 'entity_id'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Module/Collection.php b/app/code/Magento/NewRelicReporting/Model/Resource/Module/Collection.php new file mode 100644 index 0000000000000..7611c55c17119 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Module/Collection.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Module', 'Magento\NewRelicReporting\Model\Resource\Module'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Orders.php b/app/code/Magento/NewRelicReporting/Model/Resource/Orders.php new file mode 100644 index 0000000000000..8ece835396d90 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Orders.php @@ -0,0 +1,20 @@ +_init('reporting_orders', 'entity_id'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Orders/Collection.php b/app/code/Magento/NewRelicReporting/Model/Resource/Orders/Collection.php new file mode 100644 index 0000000000000..84c5707e8cbe3 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Orders/Collection.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Orders', 'Magento\NewRelicReporting\Model\Resource\Orders'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/System.php b/app/code/Magento/NewRelicReporting/Model/Resource/System.php new file mode 100644 index 0000000000000..f803f46fcdc85 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/System.php @@ -0,0 +1,20 @@ +_init('reporting_system_updates', 'entity_id'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/System/Collection.php b/app/code/Magento/NewRelicReporting/Model/Resource/System/Collection.php new file mode 100644 index 0000000000000..a329edb838091 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/System/Collection.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\System', 'Magento\NewRelicReporting\Model\Resource\System'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Users.php b/app/code/Magento/NewRelicReporting/Model/Resource/Users.php new file mode 100644 index 0000000000000..6472e1c478044 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Users.php @@ -0,0 +1,20 @@ +_init('reporting_users', 'entity_id'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Resource/Users/Collection.php b/app/code/Magento/NewRelicReporting/Model/Resource/Users/Collection.php new file mode 100644 index 0000000000000..efb7b2178d74e --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Resource/Users/Collection.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Users', 'Magento\NewRelicReporting\Model\Resource\Users'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/System.php b/app/code/Magento/NewRelicReporting/Model/System.php new file mode 100644 index 0000000000000..65264441e6032 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/System.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Resource\System'); + } +} diff --git a/app/code/Magento/NewRelicReporting/Model/Users.php b/app/code/Magento/NewRelicReporting/Model/Users.php new file mode 100644 index 0000000000000..ef4b2216eff8d --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Model/Users.php @@ -0,0 +1,20 @@ +_init('Magento\NewRelicReporting\Model\Resource\Users'); + } +} diff --git a/app/code/Magento/NewRelicReporting/README.md b/app/code/Magento/NewRelicReporting/README.md new file mode 100644 index 0000000000000..053a86be3368f --- /dev/null +++ b/app/code/Magento/NewRelicReporting/README.md @@ -0,0 +1,2 @@ +Module Magento\NewRelicReporting implements integration New Relic APM and New Relic Insights with Magento, giving +real-time visibility into business and performance metrics for data-driven decision making. \ No newline at end of file diff --git a/app/code/Magento/NewRelicReporting/Setup/InstallSchema.php b/app/code/Magento/NewRelicReporting/Setup/InstallSchema.php new file mode 100644 index 0000000000000..2ce68d061a3db --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Setup/InstallSchema.php @@ -0,0 +1,231 @@ +startSetup(); + + /** + * Create table 'reporting_counts' + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('reporting_counts') + )->addColumn( + 'entity_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Entity Id' + )->addColumn( + 'type', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Item Reported' + )->addColumn( + 'count', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['unsigned' => true], + 'Count Value' + )->addColumn( + 'updated_at', + \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], + 'Updated At' + )->setComment( + 'Reporting for all count related events generated via the cron job' + ); + $installer->getConnection()->createTable($table); + + /** + * Create table 'reporting_module_status' + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('reporting_module_status') + )->addColumn( + 'entity_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Module Id' + )->addColumn( + 'name', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Module Name' + )->addColumn( + 'active', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Module Active Status' + )->addColumn( + 'setup_version', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Module Version' + )->addColumn( + 'state', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Module State' + )->addColumn( + 'updated_at', + \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], + 'Updated At' + )->setComment( + 'Module Status Table' + ); + $installer->getConnection()->createTable($table); + + /** + * Create table 'reporting_orders' + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('reporting_orders') + )->addColumn( + 'entity_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Entity Id' + )->addColumn( + 'customer_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['unsigned' => true, 'nullable' => true], + 'Customer Id' + )->addColumn( + 'total', + \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, + '20,2', + ['unsigned' => true], + 'Total From Store' + )->addColumn( + 'total_base', + \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, + '20,2', + ['unsigned' => true], + 'Total From Base Currency' + )->addColumn( + 'item_count', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['unsigned' => true, 'nullable' => false], + 'Line Item Count' + )->addColumn( + 'updated_at', + \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], + 'Updated At' + )->setComment( + 'Reporting for all orders' + ); + $installer->getConnection()->createTable($table); + + /** + * Create table 'reporting_users' + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('reporting_users') + )->addColumn( + 'entity_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Entity Id' + )->addColumn( + 'type', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'User Type' + )->addColumn( + 'action', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Action Performed' + )->addColumn( + 'updated_at', + \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], + 'Updated At' + )->setComment( + 'Reporting for user actions' + ); + $installer->getConnection()->createTable($table); + + /** + * Create table 'reporting_system_updates' + */ + $table = $installer->getConnection()->newTable( + $installer->getTable('reporting_system_updates') + )->addColumn( + 'entity_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Entity Id' + )->addColumn( + 'type', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Update Type' + )->addColumn( + 'action', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + [], + 'Action Performed' + )->addColumn( + 'updated_at', + \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, + null, + ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], + 'Updated At' + )->setComment( + 'Reporting for system updates' + ); + $installer->getConnection()->createTable($table); + + /** + * Prepare database after install + */ + $installer->endSetup(); + + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Apm/DeploymentsTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Apm/DeploymentsTest.php new file mode 100644 index 0000000000000..13f8581c32103 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Apm/DeploymentsTest.php @@ -0,0 +1,290 @@ +zendClientFactoryMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClientFactory') + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + + $this->zendClientMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClient') + ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setParameterPost']) + ->disableOriginalConstructor() + ->getMock(); + + $this->loggerMock = $this->getMockBuilder('Psr\Log\LoggerInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->configMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->setMethods(['getNewRelicApiUrl', 'getNewRelicApiKey', 'getNewRelicAppName', 'getNewRelicAppId']) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new Deployments( + $this->configMock, + $this->loggerMock, + $this->zendClientFactoryMock + ); + } + + /** + * Tests client request with Ok status + * + * @return void + */ + public function testSetDeploymentRequestOk() + { + $data = $this->getDataVariables(); + + $this->zendClientMock->expects($this->once()) + ->method('setUri') + ->with($data['self_uri']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setMethod') + ->with($data['method']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setHeaders') + ->with($data['headers']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setParameterPost') + ->with($data['params']) + ->willReturnSelf(); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiUrl') + ->willReturn(''); + + $this->loggerMock->expects($this->once())->method('notice'); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiKey') + ->willReturn($data['api_key']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppName') + ->willReturn($data['app_name']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppId') + ->willReturn($data['app_id']); + + $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock(); + $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_ok']); + $zendHttpResponseMock->expects($this->once())->method('getBody')->willReturn($data['response_body']); + + $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock); + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + + $this->assertInternalType( + 'string', + $this->model->setDeployment($data['description'], $data['change'], $data['user']) + ); + } + + /** + * Tests client request with bad status + * + * @return void + */ + public function testSetDeploymentBadStatus() + { + $data = $this->getDataVariables(); + + $this->zendClientMock->expects($this->once()) + ->method('setUri') + ->with($data['uri']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setMethod') + ->with($data['method']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setHeaders') + ->with($data['headers']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setParameterPost') + ->with($data['params']) + ->willReturnSelf(); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiUrl') + ->willReturn($data['uri']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiKey') + ->willReturn($data['api_key']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppName') + ->willReturn($data['app_name']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppId') + ->willReturn($data['app_id']); + + $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock(); + $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_bad']); + + $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock); + $this->loggerMock->expects($this->once())->method('warning'); + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + + $this->assertInternalType( + 'bool', + $this->model->setDeployment($data['description'], $data['change'], $data['user']) + ); + } + + /** + * Tests client request will fail + */ + public function testSetDeploymentRequestFail() + { + $data = $this->getDataVariables(); + + $this->zendClientMock->expects($this->once()) + ->method('setUri') + ->with($data['uri']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setMethod') + ->with($data['method']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setHeaders') + ->with($data['headers']) + ->willReturnSelf(); + + $this->zendClientMock->expects($this->once()) + ->method('setParameterPost') + ->with($data['params']) + ->willReturnSelf(); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiUrl') + ->willReturn($data['uri']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicApiKey') + ->willReturn($data['api_key']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppName') + ->willReturn($data['app_name']); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppId') + ->willReturn($data['app_id']); + + $this->zendClientMock->expects($this->once())->method('request')->willThrowException( + new \Zend_Http_Client_Exception() + ); + $this->loggerMock->expects($this->once())->method('critical'); + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + + $this->assertInternalType( + 'bool', + $this->model->setDeployment($data['description'], $data['change'], $data['user']) + ); + } + + private function getDataVariables() + { + $description = 'Event description'; + $change = 'flush the cache username'; + $user = 'username'; + $uri = 'https://example.com/listener'; + $selfUri = 'https://api.newrelic.com/deployments.xml'; + $apiKey = '1234'; + $appName = 'app_name'; + $appId = 'application_id'; + $method = ZendClient::POST; + $headers = ['x-api-key' => $apiKey]; + $responseBody = 'Response body content'; + $statusOk = '200'; + $statusBad = '401'; + $params = [ + 'deployment[app_name]' => $appName, + 'deployment[application_id]' => $appId, + 'deployment[description]' => $description, + 'deployment[changelog]' => $change, + 'deployment[user]' => $user + ]; + + return ['description' => $description, + 'change' => $change, + 'user' => $user, + 'uri' => $uri, + 'self_uri' => $selfUri, + 'api_key' => $apiKey, + 'app_name' => $appName, + 'app_id' => $appId, + 'method' => $method, + 'headers' => $headers, + 'status_ok' => $statusOk, + 'status_bad' => $statusBad, + 'response_body' => $responseBody, + 'params' => $params + ]; + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/CounterTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CounterTest.php new file mode 100644 index 0000000000000..ab80b898c13f7 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CounterTest.php @@ -0,0 +1,212 @@ +productManagement = $this->getMockBuilder('Magento\Catalog\Api\ProductManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->configurableManagement = $this + ->getMockBuilder('Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->categoryManagement = $this->getMockBuilder('Magento\Catalog\Api\CategoryManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->customerManagement = $this->getMockBuilder('Magento\Customer\Api\CustomerManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->websiteManagement = $this->getMockBuilder('Magento\Store\Api\WebsiteManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->storeManagement = $this->getMockBuilder('Magento\Store\Api\StoreManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new Counter( + $this->productManagement, + $this->configurableManagement, + $this->categoryManagement, + $this->customerManagement, + $this->websiteManagement, + $this->storeManagement + ); + } + + /** + * Tests all products count will return int + * + * @return void + */ + public function testGetAllProductsCount() + { + $this->productManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getAllProductsCount() + ); + } + + /** + * Tests all configurable products count will return int + * + * @return void + */ + public function testGetConfigurableCount() + { + $this->configurableManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getConfigurableCount() + ); + } + + /** + * Tests all active products count will return int + * + * @return void + */ + public function testGetActiveCatalogSize() + { + $this->productManagement->expects($this->once()) + ->method('getCount') + ->with(1) + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getActiveCatalogSize() + ); + } + + /** + * Tests categories count will return int + * + * @return void + */ + public function testGetCategoryCount() + { + $this->categoryManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getCategoryCount() + ); + } + + /** + * Tests customers count will return int + * + * @return void + */ + public function testGetCustomerCount() + { + $this->customerManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getCustomerCount() + ); + } + + /** + * Tests websites count will return int + * + * @return void + */ + public function testGetWebsiteCount() + { + $this->websiteManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getWebsiteCount() + ); + } + + /** + * Tests stores count will return int + * + * @return void + */ + public function testGetStoreViewsCount() + { + $this->storeManagement->expects($this->once()) + ->method('getCount') + ->willReturn(1); + + $this->assertInternalType( + 'int', + $this->model->getStoreViewsCount() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportCountsTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportCountsTest.php new file mode 100644 index 0000000000000..600d886b4be1e --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportCountsTest.php @@ -0,0 +1,204 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->productManagement = $this->getMockBuilder('Magento\Catalog\Api\ProductManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->configurableManagement = $this + ->getMockBuilder('Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->categoryManagement = $this->getMockBuilder('Magento\Catalog\Api\CategoryManagementInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->countsFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\CountsFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->countsModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Counts') + ->disableOriginalConstructor() + ->getMock(); + $this->countsCollectionFactory = $this + ->getMockBuilder('Magento\NewRelicReporting\Model\Resource\Counts\CollectionFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->countsCollection = $this->getMockBuilder('Magento\NewRelicReporting\Model\Resource\Counts\Collection') + ->disableOriginalConstructor() + ->setMethods(['addFieldToFilter', 'addOrder', 'setPageSize', 'getFirstItem']) + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + + $this->countsFactory->expects($this->any()) + ->method('create') + ->willReturn($this->countsModel); + $this->countsModel->expects($this->any()) + ->method('load') + ->willReturnSelf(); + $this->countsCollectionFactory->expects($this->any()) + ->method('create') + ->willReturn($this->countsCollection); + $this->countsCollection->expects($this->any()) + ->method('addFieldToFilter') + ->willReturnSelf(); + $this->countsCollection->expects($this->any()) + ->method('addOrder') + ->willReturnSelf(); + $this->countsCollection->expects($this->any()) + ->method('setPageSize') + ->willReturnSelf(); + $this->countsCollection->expects($this->any()) + ->method('getFirstItem') + ->willReturn($this->countsModel); + + $this->model = new ReportCounts( + $this->config, + $this->productManagement, + $this->configurableManagement, + $this->categoryManagement, + $this->countsFactory, + $this->countsCollectionFactory, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportCountsTestsModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } + + /** + * Test case when module is enabled + * + * @return void + */ + public function testReportCountsTest() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->productManagement->expects($this->exactly(2)) + ->method('getCount') + ->willReturn(2); + $this->configurableManagement->expects($this->once()) + ->method('getCount') + ->willReturn(2); + $this->categoryManagement->expects($this->once()) + ->method('getCount') + ->willReturn(2); + + $this->countsModel->expects($this->any()) + ->method('getCount') + ->willReturn(1); + $this->countsModel->expects($this->any()) + ->method('setEntityId') + ->willReturnSelf(); + $this->countsModel->expects($this->any()) + ->method('setType') + ->willReturnSelf(); + $this->countsModel->expects($this->any()) + ->method('setCount') + ->willReturnSelf(); + $this->countsModel->expects($this->any()) + ->method('setUpdatedAt') + ->willReturnSelf(); + $this->countsModel->expects($this->any()) + ->method('save') + ->willReturnSelf(); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportModulesInfoTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportModulesInfoTest.php new file mode 100644 index 0000000000000..2c085230e9bd7 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportModulesInfoTest.php @@ -0,0 +1,149 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->collect = $this->getMockBuilder('Magento\NewRelicReporting\Model\Module\Collect') + ->disableOriginalConstructor() + ->setMethods(['getModuleData']) + ->getMock(); + $this->systemFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\SystemFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->systemModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\System') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + + $this->systemFactory->expects($this->any()) + ->method('create') + ->willReturn($this->systemModel); + + $this->jsonEncoder->expects($this->any()) + ->method('encode') + ->willReturn('json_string'); + + $this->dateTime->expects($this->any()) + ->method('formatDate') + ->willReturn('1970-01-01 00:00:00'); + + $this->model = new ReportModulesInfo( + $this->config, + $this->collect, + $this->systemFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportModulesInfoModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } + + /** + * Test case when module is enabled + * + * @return void + */ + public function testReportReportModulesInfo() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->collect->expects($this->once()) + ->method('getModuleData') + ->willReturn([ + 'installed' => '1', + 'uninstalled' => '1', + 'enabled' => '1', + 'disabled' => '1', + 'changes' => [ + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'enabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'disabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'installed'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'uninstalled'], + ] + ]); + $this->systemModel->expects($this->any())->method('setData')->willReturnSelf(); + $this->systemModel->expects($this->any())->method('save')->willReturnSelf(); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportNewRelicCronTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportNewRelicCronTest.php new file mode 100644 index 0000000000000..2b86c0c3bfb4b --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportNewRelicCronTest.php @@ -0,0 +1,252 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->collect = $this->getMockBuilder('Magento\NewRelicReporting\Model\Module\Collect') + ->disableOriginalConstructor() + ->setMethods(['getModuleData']) + ->getMock(); + $this->counter = $this->getMockBuilder('Magento\NewRelicReporting\Model\Counter') + ->disableOriginalConstructor() + ->setMethods([ + 'getAllProductsCount', + 'getConfigurableCount', + 'getActiveCatalogSize', + 'getCategoryCount', + 'getWebsiteCount', + 'getStoreViewsCount', + 'getCustomerCount', + ]) + ->getMock(); + $this->cronEventFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\CronEventFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->cronEventModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\CronEvent') + ->disableOriginalConstructor() + ->setMethods(['addData', 'sendRequest']) + ->getMock(); + $this->deploymentsFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\Apm\DeploymentsFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->deploymentsModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Apm\Deployments') + ->disableOriginalConstructor() + ->setMethods(['setDeployment']) + ->getMock(); + $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface') + ->getMock(); + + $this->cronEventFactory->expects($this->any()) + ->method('create') + ->willReturn($this->cronEventModel); + $this->deploymentsFactory->expects($this->any()) + ->method('create') + ->willReturn($this->deploymentsModel); + + $this->model = new ReportNewRelicCron( + $this->config, + $this->collect, + $this->counter, + $this->cronEventFactory, + $this->deploymentsFactory, + $this->logger + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportNewRelicCronModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } + + /** + * Test case when module is enabled + * + * @return void + */ + public function testReportNewRelicCron() + { + $testModuleData = [ + 'changes' => [ + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'enabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'disabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'installed'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'uninstalled'], + ], + 'enabled' => 1, + 'disabled' => 1, + 'installed' => 1, + ]; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->collect->expects($this->once()) + ->method('getModuleData') + ->willReturn($testModuleData); + $this->counter->expects($this->once()) + ->method('getAllProductsCount'); + $this->counter->expects($this->once()) + ->method('getConfigurableCount'); + $this->counter->expects($this->once()) + ->method('getActiveCatalogSize'); + $this->counter->expects($this->once()) + ->method('getCategoryCount'); + $this->counter->expects($this->once()) + ->method('getWebsiteCount'); + $this->counter->expects($this->once()) + ->method('getStoreViewsCount'); + $this->counter->expects($this->once()) + ->method('getCustomerCount'); + $this->cronEventModel->expects($this->once()) + ->method('addData') + ->willReturnSelf(); + $this->cronEventModel->expects($this->once()) + ->method('sendRequest'); + + $this->deploymentsModel->expects($this->any()) + ->method('setDeployment'); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } + + /** + * Test case when module is enabled and request is failed + * + * @return void + */ + public function testReportNewRelicCronRequestFailed() + { + $testModuleData = [ + 'changes' => [ + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'enabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'disabled'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'installed'], + ['name' => 'name', 'setup_version' => '2.0.0', 'type' => 'uninstalled'], + ], + 'enabled' => 1, + 'disabled' => 1, + 'installed' => 1, + ]; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->collect->expects($this->once()) + ->method('getModuleData') + ->willReturn($testModuleData); + $this->counter->expects($this->once()) + ->method('getAllProductsCount'); + $this->counter->expects($this->once()) + ->method('getConfigurableCount'); + $this->counter->expects($this->once()) + ->method('getActiveCatalogSize'); + $this->counter->expects($this->once()) + ->method('getCategoryCount'); + $this->counter->expects($this->once()) + ->method('getWebsiteCount'); + $this->counter->expects($this->once()) + ->method('getStoreViewsCount'); + $this->counter->expects($this->once()) + ->method('getCustomerCount'); + $this->cronEventModel->expects($this->once()) + ->method('addData') + ->willReturnSelf(); + $this->cronEventModel->expects($this->once()) + ->method('sendRequest'); + + $this->cronEventModel->expects($this->once())->method('sendRequest')->willThrowException( + new \Exception() + ); + $this->logger->expects($this->once())->method('critical'); + + $this->deploymentsModel->expects($this->any()) + ->method('setDeployment'); + + $this->assertSame( + $this->model, + $this->model->report() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronEventTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronEventTest.php new file mode 100644 index 0000000000000..ba88aa261839f --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronEventTest.php @@ -0,0 +1,215 @@ +zendClientFactoryMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClientFactory') + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + + $this->zendClientMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClient') + ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setRawData']) + ->disableOriginalConstructor() + ->getMock(); + + $this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->configMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->setMethods([ + 'getNewRelicAccountId', + 'getInsightsApiUrl', + 'getInsightsInsertKey', + 'getNewRelicAppName', + 'getNewRelicAppId' + ]) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new CronEvent( + $this->configMock, + $this->jsonEncoderMock, + $this->zendClientFactoryMock + ); + } + + /** + * Tests client request with Ok status + * + * @return void + */ + public function testSendRequestStatusOk() + { + $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}'; + $statusOk = '200'; + $uri = 'https://example.com/listener'; + $method = ZendClient::POST; + $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json']; + $accId = 'acc_id'; + $appId = 'app_id'; + $appName = 'app_name'; + $insightApiKey = 'insert_key_value'; + + $this->model->addData(['eventType'=>'Cron']); + + $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf(); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAccountId') + ->willReturn($accId); + + $this->configMock->expects($this->once()) + ->method('getInsightsApiUrl') + ->willReturn($uri); + + $this->configMock->expects($this->once()) + ->method('getInsightsInsertKey') + ->willReturn($insightApiKey); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppName') + ->willReturn($appName); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppId') + ->willReturn($appId); + + $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json); + + $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock(); + $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusOk); + + $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock); + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + + $this->assertInternalType( + 'bool', + $this->model->sendRequest() + ); + } + + /** + * Tests client request with Bad status + * + * @return void + */ + public function testSendRequestStatusBad() + { + $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}'; + $statusBad = '401'; + $uri = 'https://example.com/listener'; + $method = ZendClient::POST; + $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json']; + $accId = 'acc_id'; + $appId = 'app_id'; + $appName = 'app_name'; + $insightApiKey = 'insert_key_value'; + + $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf(); + $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf(); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAccountId') + ->willReturn($accId); + + $this->configMock->expects($this->once()) + ->method('getInsightsApiUrl') + ->willReturn($uri); + + $this->configMock->expects($this->once()) + ->method('getInsightsInsertKey') + ->willReturn($insightApiKey); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppName') + ->willReturn($appName); + + $this->configMock->expects($this->once()) + ->method('getNewRelicAppId') + ->willReturn($appId); + + $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json); + + $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock(); + $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad); + + $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock); + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + + $this->assertInternalType( + 'bool', + $this->model->sendRequest() + ); + } + + /** + * Tests client request with exception + * + * @return void + */ + public function testSendRequestException() + { + $accId = ''; + + $this->zendClientFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->zendClientMock); + $this->configMock->expects($this->once()) + ->method('getNewRelicAccountId') + ->willReturn($accId); + + $this->setExpectedException('Magento\Framework\Exception\LocalizedException'); + + $this->model->sendRequest(); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronTest.php new file mode 100644 index 0000000000000..9e124d8013464 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/CronTest.php @@ -0,0 +1,114 @@ +configMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->setMethods([ + 'isCronEnabled' + ]) + ->disableOriginalConstructor() + ->getMock(); + + $this->reportModulesInfoMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Cron\ReportModulesInfo') + ->disableOriginalConstructor() + ->getMock(); + + $this->reportCountsMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Cron\ReportCounts') + ->disableOriginalConstructor() + ->getMock(); + + $this->reportNewRelicCronMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Cron\ReportNewRelicCron') + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new Cron( + $this->configMock, + $this->reportModulesInfoMock, + $this->reportCountsMock, + $this->reportNewRelicCronMock + ); + } + + /** + * Test case when cron is disabled in config + * + * @return \Magento\NewRelicReporting\Model\Cron + */ + public function testRunCronCronDisabledFromConfig() + { + $this->configMock->expects($this->once()) + ->method('isCronEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->runCron() + ); + } + + /** + * Test case when cron is enabled in config + * + * @return \Magento\NewRelicReporting\Model\Cron + */ + public function testRunCronCronEnabledFromConfig() + { + $this->configMock->expects($this->once()) + ->method('isCronEnabled') + ->willReturn(true); + + $this->reportModulesInfoMock->expects($this->once()) + ->method('report') + ->willReturnSelf(); + $this->reportCountsMock->expects($this->once()) + ->method('report') + ->willReturnSelf(); + $this->reportNewRelicCronMock->expects($this->once()) + ->method('report') + ->willReturnSelf(); + + $this->assertSame( + $this->model, + $this->model->runCron() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Module/CollectTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Module/CollectTest.php new file mode 100644 index 0000000000000..fced09fc57416 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Module/CollectTest.php @@ -0,0 +1,411 @@ +moduleListMock = $this->getMockBuilder('Magento\Framework\Module\ModuleListInterface') + ->setMethods(['getNames', 'has', 'getAll', 'getOne']) + ->disableOriginalConstructor() + ->getMock(); + + $this->fullModuleListMock = $this->getMockBuilder('Magento\Framework\Module\FullModuleList') + ->setMethods(['getNames', 'getAll']) + ->disableOriginalConstructor() + ->getMock(); + + $this->moduleManagerMock = $this->getMockBuilder('Magento\Framework\Module\Manager') + ->setMethods(['isOutputEnabled']) + ->disableOriginalConstructor() + ->getMock(); + + $this->dateTimeMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->setMethods(['formatDate']) + ->disableOriginalConstructor() + ->getMock(); + + $this->moduleFactoryMock = $this->getMock( + 'Magento\NewRelicReporting\Model\ModuleFactory', + ['create'], + [], + '', + false + ); + + $this->moduleCollectionFactoryMock = $this->getMock( + 'Magento\NewRelicReporting\Model\Resource\Module\CollectionFactory', + ['create'], + [], + '', + false + ); + + $this->model = new Collect( + $this->moduleListMock, + $this->fullModuleListMock, + $this->moduleManagerMock, + $this->dateTimeMock, + $this->moduleFactoryMock, + $this->moduleCollectionFactoryMock + ); + } + + /** + * Tests modules data returns array + * + * @return void + */ + public function testGetModuleDataWithoutRefresh() + { + $moduleCollectionMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Resource\Module\Collection') + ->disableOriginalConstructor() + ->getMock(); + $itemMock = $this->getMock('Magento\NewRelicReporting\Model\Module', [], [], '', false); + $modulesMockArray = [ + 'Module_Name' => [ + 'name' => 'Name', + 'setup_version' => '2.0.0', + 'sequence' => [] + ] + ]; + $testChangesMockArray = [ + ['entity' => '3', + 'name' => 'Name', + 'active' => 'true', + 'state' => 'enabled', + 'setup_version' => '2.0.0', + 'updated_at' => '2015-09-02 18:38:17'], + ['entity' => '4', + 'name' => 'Name', + 'active' => 'true', + 'state' => 'disabled', + 'setup_version' => '2.0.0', + 'updated_at' => '2015-09-02 18:38:17'], + ['entity' => '5', + 'name' => 'Name', + 'active' => 'true', + 'state' => 'uninstalled', + 'setup_version' => '2.0.0', + 'updated_at' => '2015-09-02 18:38:17'] + ]; + $itemMockArray = [$itemMock]; + $enabledModulesMockArray = []; + + $this->moduleCollectionFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($moduleCollectionMock); + + $this->moduleFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($itemMock); + + $itemMock->expects($this->any()) + ->method('setData') + ->willReturnSelf(); + + $itemMock->expects($this->any()) + ->method('save') + ->willReturnSelf(); + + $moduleCollectionMock->expects($this->any()) + ->method('getItems') + ->willReturn($itemMockArray); + + $moduleCollectionMock->expects($this->any()) + ->method('getData') + ->willReturn($testChangesMockArray); + + $this->fullModuleListMock->expects($this->once()) + ->method('getAll') + ->willReturn($modulesMockArray); + + $this->fullModuleListMock->expects($this->once()) + ->method('getNames') + ->willReturn($enabledModulesMockArray); + + $this->moduleListMock->expects($this->once()) + ->method('getNames') + ->willReturn($enabledModulesMockArray); + + $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will( + $this->returnValue(false) + ); + + $this->assertInternalType( + 'array', + $this->model->getModuleData() + ); + } + + /** + * Tests modules data returns array and saving in DB + * + * @dataProvider itemDataProvider + * @return void + */ + public function testGetModuleDataRefresh($data) + { + $moduleCollectionMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Resource\Module\Collection') + ->disableOriginalConstructor() + ->getMock(); + /** @var \Magento\NewRelicReporting\Model\Module|\PHPUnit_Framework_MockObject_MockObject $itemMock */ + $itemMock = $this->getMock( + 'Magento\NewRelicReporting\Model\Module', + ['getName', 'getData', 'setData', 'getState', 'save'], + [], + '', + false + ); + $modulesMockArray = [ + 'Module_Name1' => [ + 'name' => 'Module_Name1', + 'setup_version' => '2.0.0', + 'sequence' => [] + ] + ]; + $itemMock->setData($data); + $testChangesMockArray = [ + 'entity_id' => '3', + 'name' => 'Name', + 'active' => 'true', + 'state' => 'uninstalled', + 'setup_version' => '2.0.0', + 'some_param' => 'some_value', + 'updated_at' => '2015-09-02 18:38:17' + ]; + $itemMockArray = [$itemMock]; + + $enabledModulesMockArray = ['Module_Name2']; + $allModulesMockArray = ['Module_Name1','Module_Name2']; + + $this->moduleCollectionFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($moduleCollectionMock); + + $this->moduleFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($itemMock); + + $itemMock->expects($this->any()) + ->method('setData') + ->willReturnSelf(); + + $itemMock->expects($this->any()) + ->method('save') + ->willReturnSelf(); + + $itemMock->expects($this->any()) + ->method('getState') + ->willReturn($data['state']); + + $itemMock->expects($this->any()) + ->method('getName') + ->willReturn($data['name']); + + $moduleCollectionMock->expects($this->any()) + ->method('getItems') + ->willReturn($itemMockArray); + + $itemMock->expects($this->any()) + ->method('getData') + ->willReturn($testChangesMockArray); + + $this->fullModuleListMock->expects($this->once()) + ->method('getAll') + ->willReturn($modulesMockArray); + + $this->fullModuleListMock->expects($this->any()) + ->method('getNames') + ->willReturn($allModulesMockArray); + + $this->moduleListMock->expects($this->any()) + ->method('getNames') + ->willReturn($enabledModulesMockArray); + + $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will( + $this->returnValue(true) + ); + + $this->assertInternalType( + 'array', + $this->model->getModuleData() + ); + } + + /** + * Tests modules data returns array and saving in DB + * + * @dataProvider itemDataProvider + * @return void + */ + public function testGetModuleDataRefreshOrStatement($data) + { + $moduleCollectionMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Resource\Module\Collection') + ->disableOriginalConstructor() + ->getMock(); + /** @var \Magento\NewRelicReporting\Model\Module|\PHPUnit_Framework_MockObject_MockObject $itemMock */ + $itemMock = $this->getMock( + 'Magento\NewRelicReporting\Model\Module', + ['getName', 'getData', 'setData', 'getState', 'save'], + [], + '', + false + ); + $modulesMockArray = [ + 'Module_Name1' => [ + 'name' => 'Module_Name1', + 'setup_version' => '2.0.0', + 'sequence' => [] + ] + ]; + $itemMock->setData($data); + $testChangesMockArray = [ + 'entity_id' => '3', + 'name' => 'Name', + 'active' => 'false', + 'state' => 'enabled', + 'setup_version' => '2.0.0', + 'some_param' => 'some_value', + 'updated_at' => '2015-09-02 18:38:17' + ]; + $itemMockArray = [$itemMock]; + + $enabledModulesMockArray = ['Module_Name2']; + $allModulesMockArray = ['Module_Name1','Module_Name2']; + + $this->moduleCollectionFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($moduleCollectionMock); + + $this->moduleFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($itemMock); + + $itemMock->expects($this->any()) + ->method('setData') + ->willReturnSelf(); + + $itemMock->expects($this->any()) + ->method('save') + ->willReturnSelf(); + + $itemMock->expects($this->any()) + ->method('getState') + ->willReturn($data['state']); + + $itemMock->expects($this->any()) + ->method('getName') + ->willReturn($data['name']); + + $moduleCollectionMock->expects($this->any()) + ->method('getItems') + ->willReturn($itemMockArray); + + $itemMock->expects($this->any()) + ->method('getData') + ->willReturn($testChangesMockArray); + + $this->fullModuleListMock->expects($this->once()) + ->method('getAll') + ->willReturn($modulesMockArray); + + $this->fullModuleListMock->expects($this->any()) + ->method('getNames') + ->willReturn($allModulesMockArray); + + $this->moduleListMock->expects($this->any()) + ->method('getNames') + ->willReturn($enabledModulesMockArray); + + $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will( + $this->returnValue(true) + ); + + $this->assertInternalType( + 'array', + $this->model->getModuleData() + ); + } + + public function itemDataProvider() + { + return [ + [ + [ + 'entity_id' => '1', + 'name' => 'Module_Name1', + 'active' => 'true', + 'state' => 'enabled', + 'setup_version' => '2.0.0' + ] + ], + [ + [ + 'entity_id' => '2', + 'name' => 'Module_Name2', + 'active' => 'true', + 'state' => 'disabled', + 'setup_version' => '2.0.0' + ] + ], + [ + [ + 'entity_id' => '2', + 'name' => 'Module_Name2', + 'active' => 'true', + 'state' => 'uninstalled', + 'setup_version' => '2.0.0' + ] + ] + ]; + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/CheckConfigTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/CheckConfigTest.php new file mode 100644 index 0000000000000..7ed90642abf61 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/CheckConfigTest.php @@ -0,0 +1,121 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled', 'disableModule']) + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['isExtensionInstalled']) + ->getMock(); + $this->messageManager = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new CheckConfig( + $this->config, + $this->newRelicWrapper, + $this->messageManager + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testCheckConfigModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled in config but php extension is not installed + * + * @return void + */ + public function testCheckConfigExtensionNotInstalled() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->newRelicWrapper->expects($this->once()) + ->method('isExtensionInstalled') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled in config and php extension is installed + * + * @return void + */ + public function testCheckConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->newRelicWrapper->expects($this->once()) + ->method('isExtensionInstalled') + ->willReturn(false); + $this->config->expects($this->once()) + ->method('disableModule'); + $this->messageManager->expects($this->once()) + ->method('addError'); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsTest.php new file mode 100644 index 0000000000000..64e8509a1f1cf --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsTest.php @@ -0,0 +1,167 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->backendAuthSession = $this->getMockBuilder('Magento\Backend\Model\Auth\Session') + ->disableOriginalConstructor() + ->setMethods(['isLoggedIn', 'getUser']) + ->getMock(); + $this->usersFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\UsersFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->usersModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Users') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + + $this->usersFactory->expects($this->any()) + ->method('create') + ->willReturn($this->usersModel); + + $this->model = new ReportConcurrentAdmins( + $this->config, + $this->backendAuthSession, + $this->usersFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportConcurrentAdminsModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when user is not logged in + * + * @return void + */ + public function testReportConcurrentAdminsUserIsNotLoggedIn() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->backendAuthSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled and user is logged in + * + * @return void + */ + public function testReportConcurrentAdmins() + { + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->backendAuthSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $userMock = $this->getMockBuilder('Magento\User\Model\User')->disableOriginalConstructor()->getMock(); + $this->backendAuthSession->expects($this->once()) + ->method('getUser') + ->willReturn($userMock); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->usersModel->expects($this->once()) + ->method('setData') + ->with(['type' => 'admin_activity', 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->usersModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsToNewRelicTest.php new file mode 100644 index 0000000000000..68f3d64bbcb55 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentAdminsToNewRelicTest.php @@ -0,0 +1,125 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->backendAuthSession = $this->getMockBuilder('Magento\Backend\Model\Auth\Session') + ->disableOriginalConstructor() + ->setMethods(['isLoggedIn', 'getUser']) + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['addCustomParameter']) + ->getMock(); + + $this->model = new ReportConcurrentAdminsToNewRelic( + $this->config, + $this->backendAuthSession, + $this->newRelicWrapper + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportConcurrentAdminsToNewRelicModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when user is not logged in + * + * @return void + */ + public function testReportConcurrentAdminsToNewRelicUserIsNotLoggedIn() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->backendAuthSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled and user is logged in + * + * @return void + */ + public function testReportConcurrentAdminsToNewRelic() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->backendAuthSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $userMock = $this->getMockBuilder('Magento\User\Model\User')->disableOriginalConstructor()->getMock(); + $this->backendAuthSession->expects($this->once()) + ->method('getUser') + ->willReturn($userMock); + $this->newRelicWrapper->expects($this->exactly(3)) + ->method('addCustomParameter') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersTest.php new file mode 100644 index 0000000000000..e911e64cb6ffc --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersTest.php @@ -0,0 +1,195 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->customerSession = $this->getMockBuilder('Magento\Customer\Model\Session') + ->disableOriginalConstructor() + ->setMethods(['isLoggedIn', 'getCustomerId']) + ->getMock(); + $this->customerRepository = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface') + ->getMock(); + $this->storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') + ->getMock(); + $this->usersFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\UsersFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->usersModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Users') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + + $this->usersFactory->expects($this->any()) + ->method('create') + ->willReturn($this->usersModel); + + $this->model = new ReportConcurrentUsers( + $this->config, + $this->customerSession, + $this->customerRepository, + $this->storeManager, + $this->usersFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportConcurrentUsersModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when user is not logged in + * + * @return void + */ + public function testReportConcurrentUsersUserIsNotLoggedIn() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled and user is logged in + * + * @return void + */ + public function testReportConcurrentUsers() + { + $testCustomerId = 1; + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('getCustomerId') + ->willReturn($testCustomerId); + $customerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')->getMock(); + $this->customerRepository->expects($this->once()) + ->method('getById') + ->willReturn($customerMock); + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getWebsite') + ->willReturn($websiteMock); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->usersModel->expects($this->once()) + ->method('setData') + ->with(['type' => 'user_action', 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->usersModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersToNewRelicTest.php new file mode 100644 index 0000000000000..b79615e17de97 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportConcurrentUsersToNewRelicTest.php @@ -0,0 +1,165 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->customerSession = $this->getMockBuilder('Magento\Customer\Model\Session') + ->disableOriginalConstructor() + ->setMethods(['isLoggedIn', 'getCustomerId']) + ->getMock(); + $this->customerRepository = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface') + ->getMock(); + $this->storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['addCustomParameter']) + ->getMock(); + + $this->model = new ReportConcurrentUsersToNewRelic( + $this->config, + $this->customerSession, + $this->customerRepository, + $this->storeManager, + $this->newRelicWrapper + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportConcurrentUsersToNewRelicModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertInstanceOf( + 'Magento\NewRelicReporting\Model\Observer\ReportConcurrentUsersToNewRelic', + $this->model->execute() + ); + } + + /** + * Test case when user is not logged in + * + * @return void + */ + public function testReportConcurrentUsersToNewRelicUserIsNotLoggedIn() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getWebsite') + ->willReturn($websiteMock); + $this->newRelicWrapper->expects($this->exactly(2)) + ->method('addCustomParameter') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled and user is logged in + * + * @return void + */ + public function testReportConcurrentUsersToNewRelic() + { + $testCustomerId = 1; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->customerSession->expects($this->once()) + ->method('getCustomerId') + ->willReturn($testCustomerId); + $customerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')->getMock(); + $this->customerRepository->expects($this->once()) + ->method('getById') + ->willReturn($customerMock); + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website')->disableOriginalConstructor()->getMock(); + $this->storeManager->expects($this->once()) + ->method('getWebsite') + ->willReturn($websiteMock); + $this->newRelicWrapper->expects($this->exactly(4)) + ->method('addCustomParameter') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedTest.php new file mode 100644 index 0000000000000..c7acbe1c90759 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedTest.php @@ -0,0 +1,164 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->ordersFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\OrdersFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->ordersModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Orders') + ->disableOriginalConstructor() + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + $this->ordersFactory->expects($this->any()) + ->method('create') + ->willReturn($this->ordersModel); + + $this->model = new ReportOrderPlaced( + $this->config, + $this->ordersFactory, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportOrderPlacedModuleDisabledFromConfig() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $event */ + $event = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute($event) + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportOrderPlaced() + { + $testCustomerId = 1; + $testTotal = '1.00'; + $testBaseTotal = '1.00'; + $testItemCount = null; + $testTotalQtyOrderedCount = 1; + $testUpdated = '1970-01-01 00:00:00'; + + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getOrder']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $order = $this->getMockBuilder('Magento\Sales\Model\Order')->disableOriginalConstructor()->getMock(); + $event->expects($this->once()) + ->method('getOrder') + ->willReturn($order); + $order->expects($this->once()) + ->method('getCustomerId') + ->willReturn($testCustomerId); + $order->expects($this->once()) + ->method('getGrandTotal') + ->willReturn($testTotal); + $order->expects($this->once()) + ->method('getBaseGrandTotal') + ->willReturn($testBaseTotal); + $order->expects($this->once()) + ->method('getTotalItemCount') + ->willReturn($testItemCount); + $order->expects($this->once()) + ->method('getTotalQtyOrdered') + ->willReturn($testTotalQtyOrderedCount); + $this->ordersModel->expects($this->once()) + ->method('setData') + ->with( + [ + 'customer_id' => $testCustomerId, + 'total' => $testTotal, + 'total_base' => $testBaseTotal, + 'item_count' => $testTotalQtyOrderedCount, + 'updated_at' => $testUpdated + ] + ) + ->willReturnSelf(); + $this->ordersModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedToNewRelicTest.php new file mode 100644 index 0000000000000..2e7044faef8f0 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportOrderPlacedToNewRelicTest.php @@ -0,0 +1,120 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['addCustomParameter']) + ->getMock(); + + $this->model = new ReportOrderPlacedToNewRelic( + $this->config, + $this->newRelicWrapper + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportOrderPlacedToNewRelicModuleDisabledFromConfig() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportOrderPlacedToNewRelic() + { + $testTotal = '1.00'; + $testItemCount = null; + $testTotalQtyOrderedCount = 1; + + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getOrder']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $order = $this->getMockBuilder('Magento\Sales\Model\Order')->disableOriginalConstructor()->getMock(); + $event->expects($this->once()) + ->method('getOrder') + ->willReturn($order); + $order->expects($this->once()) + ->method('getBaseGrandTotal') + ->willReturn($testTotal); + $order->expects($this->once()) + ->method('getTotalItemCount') + ->willReturn($testItemCount); + $order->expects($this->once()) + ->method('getTotalQtyOrdered') + ->willReturn($testTotalQtyOrderedCount); + $this->newRelicWrapper->expects($this->exactly(3)) + ->method('addCustomParameter') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedTest.php new file mode 100644 index 0000000000000..dabe468f698c7 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedTest.php @@ -0,0 +1,152 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->systemFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\SystemFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->systemModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\System') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + $this->systemFactory->expects($this->any()) + ->method('create') + ->willReturn($this->systemModel); + + $this->model = new ReportProductDeleted( + $this->config, + $this->systemFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportProductDeletedModuleDisabledFromConfig() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportProductDeleted() + { + $testType = 'adminProductChange'; + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getProduct']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + $event->expects($this->once()) + ->method('getProduct') + ->willReturn($product); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->systemModel->expects($this->once()) + ->method('setData') + ->with(['type' => $testType, 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->systemModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedToNewRelicTest.php new file mode 100644 index 0000000000000..b4ba0e2d50faf --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductDeletedToNewRelicTest.php @@ -0,0 +1,88 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['addCustomParameter']) + ->getMock(); + + $this->model = new ReportProductDeletedToNewRelic( + $this->config, + $this->newRelicWrapper + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportProductDeletedToNewRelicModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportProductDeletedToNewRelic() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->newRelicWrapper->expects($this->once()) + ->method('addCustomParameter') + ->willReturn(true); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedTest.php new file mode 100644 index 0000000000000..79468bcb6c1dd --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedTest.php @@ -0,0 +1,207 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->systemFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\SystemFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->systemModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\System') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + $this->systemFactory->expects($this->any()) + ->method('create') + ->willReturn($this->systemModel); + + $this->model = new ReportProductSaved( + $this->config, + $this->systemFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportProductSavedModuleDisabledFromConfig() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportProductSaved() + { + $testType = 'adminProductChange'; + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getProduct']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + $product->isObjectNew(true); + $event->expects($this->once()) + ->method('getProduct') + ->willReturn($product); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->systemModel->expects($this->once()) + ->method('setData') + ->with(['type' => $testType, 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->systemModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config and product updated + * + * @return void + */ + public function testReportProductUpdated() + { + $testType = 'adminProductChange'; + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getProduct']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + $product->isObjectNew(false); + $event->expects($this->once()) + ->method('getProduct') + ->willReturn($product); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->systemModel->expects($this->once()) + ->method('setData') + ->with(['type' => $testType, 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->systemModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedToNewRelicTest.php new file mode 100644 index 0000000000000..27b1b1f4e39e4 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportProductSavedToNewRelicTest.php @@ -0,0 +1,153 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->newRelicWrapper = $this->getMockBuilder('Magento\NewRelicReporting\Model\NewRelicWrapper') + ->disableOriginalConstructor() + ->setMethods(['addCustomParameter']) + ->getMock(); + + $this->model = new ReportProductSavedToNewRelic( + $this->config, + $this->newRelicWrapper + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportProductSavedToNewRelicModuleDisabledFromConfig() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportProductSavedToNewRelic() + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getProduct']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->disableOriginalConstructor() + ->getMock(); + $event->expects($this->once()) + ->method('getProduct') + ->willReturn($product); + $this->newRelicWrapper->expects($this->once()) + ->method('addCustomParameter') + ->willReturn(true); + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + /** + * Test case when module is enabled in config and product updating + * + * @dataProvider actionDataProvider + * @return void + */ + public function testReportProductUpdatedToNewRelic($isNewObject) + { + /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ + $eventObserver = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->getMock(); + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $event = $this->getMockBuilder('Magento\Framework\Event') + ->setMethods(['getProduct']) + ->disableOriginalConstructor() + ->getMock(); + $eventObserver->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') + ->disableOriginalConstructor() + ->getMock(); + $product->expects($this->any()) + ->method('isObjectNew') + ->willReturn($isNewObject); + $event->expects($this->once()) + ->method('getProduct') + ->willReturn($product); + $this->newRelicWrapper->expects($this->once()) + ->method('addCustomParameter') + ->willReturn(true); + $this->assertSame( + $this->model, + $this->model->execute($eventObserver) + ); + } + + public function actionDataProvider() + { + return [[true], [false]]; + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushTest.php new file mode 100644 index 0000000000000..e2bcd410e3513 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushTest.php @@ -0,0 +1,130 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->systemFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\SystemFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->systemModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\System') + ->disableOriginalConstructor() + ->getMock(); + $this->jsonEncoder = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->getMock(); + $this->dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime') + ->disableOriginalConstructor() + ->setMethods(['formatDate']) + ->getMock(); + $this->systemFactory->expects($this->any()) + ->method('create') + ->willReturn($this->systemModel); + + $this->model = new ReportSystemCacheFlush( + $this->config, + $this->systemFactory, + $this->jsonEncoder, + $this->dateTime + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportSystemCacheFlushModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportSystemCacheFlush() + { + $testType = 'systemCacheFlush'; + $testAction = 'JSON string'; + $testUpdated = '1970-01-01 00:00:00'; + + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $this->dateTime->expects($this->once()) + ->method('formatDate') + ->willReturn($testUpdated); + $this->jsonEncoder->expects($this->once()) + ->method('encode') + ->willReturn($testAction); + $this->systemModel->expects($this->once()) + ->method('setData') + ->with(['type' => $testType, 'action' => $testAction, 'updated_at' => $testUpdated]) + ->willReturnSelf(); + $this->systemModel->expects($this->once()) + ->method('save'); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushToNewRelicTest.php b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushToNewRelicTest.php new file mode 100644 index 0000000000000..b85293a7cc8f5 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/Test/Unit/Model/Observer/ReportSystemCacheFlushToNewRelicTest.php @@ -0,0 +1,120 @@ +config = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config') + ->disableOriginalConstructor() + ->setMethods(['isNewRelicEnabled']) + ->getMock(); + $this->backendAuthSession = $this->getMockBuilder('Magento\Backend\Model\Auth\Session') + ->disableOriginalConstructor() + ->setMethods(['getUser']) + ->getMock(); + $this->deploymentsFactory = $this->getMockBuilder('Magento\NewRelicReporting\Model\Apm\DeploymentsFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->deploymentsModel = $this->getMockBuilder('Magento\NewRelicReporting\Model\Apm\Deployments') + ->disableOriginalConstructor() + ->setMethods(['setDeployment']) + ->getMock(); + $this->deploymentsFactory->expects($this->any()) + ->method('create') + ->willReturn($this->deploymentsModel); + + $this->model = new ReportSystemCacheFlushToNewRelic( + $this->config, + $this->backendAuthSession, + $this->deploymentsFactory + ); + } + + /** + * Test case when module is disabled in config + * + * @return void + */ + public function testReportSystemCacheFlushToNewRelicModuleDisabledFromConfig() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(false); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } + + /** + * Test case when module is enabled in config + * + * @return void + */ + public function testReportSystemCacheFlushToNewRelic() + { + $this->config->expects($this->once()) + ->method('isNewRelicEnabled') + ->willReturn(true); + $userMock = $this->getMockBuilder('Magento\User\Model\User')->disableOriginalConstructor()->getMock(); + $this->backendAuthSession->expects($this->once()) + ->method('getUser') + ->willReturn($userMock); + $userMock->expects($this->once()) + ->method('getId') + ->willReturn('2'); + $this->deploymentsFactory->expects($this->once()) + ->method('create') + ->willReturn($this->deploymentsModel); + $this->deploymentsModel->expects($this->once()) + ->method('setDeployment') + ->willReturnSelf(); + + $this->assertSame( + $this->model, + $this->model->execute() + ); + } +} diff --git a/app/code/Magento/NewRelicReporting/composer.json b/app/code/Magento/NewRelicReporting/composer.json new file mode 100644 index 0000000000000..35f969663caaa --- /dev/null +++ b/app/code/Magento/NewRelicReporting/composer.json @@ -0,0 +1,29 @@ +{ + "name": "magento/module-new-relic-reporting", + "description": "N/A", + "require": { + "php": "~5.5.0|~5.6.0", + "magento/module-store": "1.0.0-beta", + "magento/module-backend": "1.0.0-beta", + "magento/module-customer": "1.0.0-beta", + "magento/module-configurable-product": "1.0.0-beta", + "magento/module-catalog": "1.0.0-beta", + "magento/module-config": "1.0.0-beta", + "magento/framework": "1.0.0-beta", + "magento/magento-composer-installer": "*" + }, + "type": "magento2-module", + "version": "1.0.0-beta", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "extra": { + "map": [ + [ + "*", + "Magento/NewRelicReporting" + ] + ] + } +} diff --git a/app/code/Magento/NewRelicReporting/etc/acl.xml b/app/code/Magento/NewRelicReporting/etc/acl.xml new file mode 100644 index 0000000000000..95f173a505bb7 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/acl.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + diff --git a/app/code/Magento/NewRelicReporting/etc/adminhtml/events.xml b/app/code/Magento/NewRelicReporting/etc/adminhtml/events.xml new file mode 100644 index 0000000000000..3c6f9edcd09a5 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/adminhtml/events.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml b/app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml new file mode 100644 index 0000000000000..8a7c5411ce8c1 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml @@ -0,0 +1,59 @@ + + + + +
+ + general + Magento_NewRelicReporting::config_newrelicreporting + + + + + Magento\Config\Model\Config\Source\Yesno + + + + + + + Use %s to replace the account ID in the URL + + + + Click here to get one]]> + + + + This can commonly be found at the end of the URL when viewing the APM after "/applications/" + + + + Magento\Config\Model\Config\Backend\Encrypted + This is located by navigating to Events -> Deployments from the New Relic APM website + + + + Magento\Config\Model\Config\Backend\Encrypted + Generated under Insights in Manage data -> API Keys -> Insert Keys + + + + This is located by navigating to Settings from the New Relic APM website + + + + + + + Magento\Config\Model\Config\Source\Yesno + + +
+
+
diff --git a/app/code/Magento/NewRelicReporting/etc/config.xml b/app/code/Magento/NewRelicReporting/etc/config.xml new file mode 100644 index 0000000000000..047783546245d --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/config.xml @@ -0,0 +1,21 @@ + + + + + + + 0 + https://api.newrelic.com/deployments.xml + https://insights-collector.newrelic.com/v1/accounts/%s/events + + + 0 + + + + diff --git a/app/code/Magento/NewRelicReporting/etc/crontab.xml b/app/code/Magento/NewRelicReporting/etc/crontab.xml new file mode 100644 index 0000000000000..e9e89039ded3d --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/crontab.xml @@ -0,0 +1,14 @@ + + + + + + */2 * * * * + + + diff --git a/app/code/Magento/NewRelicReporting/etc/di.xml b/app/code/Magento/NewRelicReporting/etc/di.xml new file mode 100644 index 0000000000000..c78ff649c5aec --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/di.xml @@ -0,0 +1,15 @@ + + + + + + + Magento\Framework\Module\FullModuleList + + + diff --git a/app/code/Magento/NewRelicReporting/etc/events.xml b/app/code/Magento/NewRelicReporting/etc/events.xml new file mode 100644 index 0000000000000..90dcf0625d666 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/events.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/app/code/Magento/NewRelicReporting/etc/frontend/events.xml b/app/code/Magento/NewRelicReporting/etc/frontend/events.xml new file mode 100644 index 0000000000000..6e6ef76851efa --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/frontend/events.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/app/code/Magento/NewRelicReporting/etc/module.xml b/app/code/Magento/NewRelicReporting/etc/module.xml new file mode 100644 index 0000000000000..d9674d30ac315 --- /dev/null +++ b/app/code/Magento/NewRelicReporting/etc/module.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + diff --git a/app/code/Magento/Store/Api/StoreManagementInterface.php b/app/code/Magento/Store/Api/StoreManagementInterface.php new file mode 100644 index 0000000000000..6be9f0f69356a --- /dev/null +++ b/app/code/Magento/Store/Api/StoreManagementInterface.php @@ -0,0 +1,19 @@ +storesFactory = $storesFactory; + } + /** + * {@inheritdoc} + */ + public function getCount() + { + $stores = $this->storesFactory->create(); + /** @var \Magento\Store\Model\Resource\Store\Collection $stores */ + $stores->setWithoutDefaultFilter(); + return $stores->getSize(); + } +} diff --git a/app/code/Magento/Store/Model/WebsiteManagement.php b/app/code/Magento/Store/Model/WebsiteManagement.php new file mode 100644 index 0000000000000..6d4cf6d883653 --- /dev/null +++ b/app/code/Magento/Store/Model/WebsiteManagement.php @@ -0,0 +1,37 @@ +websitesFactory = $websitesFactory; + } + /** + * {@inheritdoc} + */ + public function getCount() + { + $websites = $this->websitesFactory->create(); + /** @var \Magento\Store\Model\Resource\Website\Collection $websites */ + return $websites->getSize(); + } +} diff --git a/app/code/Magento/Store/Test/Unit/Model/StoreManagementTest.php b/app/code/Magento/Store/Test/Unit/Model/StoreManagementTest.php new file mode 100644 index 0000000000000..2520ae86d7741 --- /dev/null +++ b/app/code/Magento/Store/Test/Unit/Model/StoreManagementTest.php @@ -0,0 +1,56 @@ +storesFactoryMock = $this->getMock( + 'Magento\Store\Model\Resource\Store\CollectionFactory', + ['create'], + [], + '', + false + ); + $this->model = new \Magento\Store\Model\StoreManagement( + $this->storesFactoryMock + ); + } + + public function testGetCount() + { + $storesMock = $this->getMock('\Magento\Store\Model\Resource\Store\Collection', [], [], '', false); + + $this->storesFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($storesMock); + $storesMock + ->expects($this->once()) + ->method('setWithoutDefaultFilter') + ->willReturnSelf(); + $storesMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount() + ); + } +} diff --git a/app/code/Magento/Store/Test/Unit/Model/WebsiteManagementTest.php b/app/code/Magento/Store/Test/Unit/Model/WebsiteManagementTest.php new file mode 100644 index 0000000000000..72f1ce5a14866 --- /dev/null +++ b/app/code/Magento/Store/Test/Unit/Model/WebsiteManagementTest.php @@ -0,0 +1,52 @@ +websitesFactoryMock = $this->getMock( + 'Magento\Store\Model\Resource\Website\CollectionFactory', + ['create'], + [], + '', + false + ); + $this->model = new \Magento\Store\Model\WebsiteManagement( + $this->websitesFactoryMock + ); + } + + public function testGetCount() + { + $websitesMock = $this->getMock('\Magento\Store\Model\Resource\Website\Collection', [], [], '', false); + + $this->websitesFactoryMock + ->expects($this->once()) + ->method('create') + ->willReturn($websitesMock); + $websitesMock + ->expects($this->once()) + ->method('getSize') + ->willReturn('expected'); + + $this->assertEquals( + 'expected', + $this->model->getCount() + ); + } +} diff --git a/app/code/Magento/Store/etc/di.xml b/app/code/Magento/Store/etc/di.xml index f065cea681b43..31fd5bc9a4d78 100644 --- a/app/code/Magento/Store/etc/di.xml +++ b/app/code/Magento/Store/etc/di.xml @@ -9,6 +9,8 @@ + + diff --git a/composer.json b/composer.json index df60745d7053c..74ad4e5e17cf7 100644 --- a/composer.json +++ b/composer.json @@ -117,6 +117,7 @@ "magento/module-media-storage": "self.version", "magento/module-msrp": "self.version", "magento/module-multishipping": "self.version", + "magento/module-new-relic-reporting": "self.version", "magento/module-newsletter": "self.version", "magento/module-offline-payments": "self.version", "magento/module-offline-shipping": "self.version", diff --git a/composer.lock b/composer.lock index 8f628813efa82..5f55f0bed6d0e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "18bdb41169450e6d1e7556887de94da7", - "content-hash": "ff6e10ad396bbc831cbe385b910ed6ca", + "hash": "54eb882293256afd6256337f3fe7825d", + "content-hash": "81bce4b68227708d0a4ddcbfe7f2dcdb", "packages": [ { "name": "braintree/braintree_php", From 5b90cce4a2ebe0a258332353c13de91870cf1958 Mon Sep 17 00:00:00 2001 From: Sviatoslav Mankivskyi Date: Fri, 6 Nov 2015 17:30:13 +0200 Subject: [PATCH 2/2] MAGETWO-44657: Merge New Relic integration into Merchant Beta --- app/code/Magento/NewRelicReporting/composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/NewRelicReporting/composer.json b/app/code/Magento/NewRelicReporting/composer.json index 35f969663caaa..e4fed4b7ed422 100644 --- a/app/code/Magento/NewRelicReporting/composer.json +++ b/app/code/Magento/NewRelicReporting/composer.json @@ -3,17 +3,17 @@ "description": "N/A", "require": { "php": "~5.5.0|~5.6.0", - "magento/module-store": "1.0.0-beta", - "magento/module-backend": "1.0.0-beta", - "magento/module-customer": "1.0.0-beta", - "magento/module-configurable-product": "1.0.0-beta", - "magento/module-catalog": "1.0.0-beta", - "magento/module-config": "1.0.0-beta", - "magento/framework": "1.0.0-beta", + "magento/module-store": "1.0.0-beta6", + "magento/module-backend": "1.0.0-beta6", + "magento/module-customer": "1.0.0-beta6", + "magento/module-configurable-product": "1.0.0-beta6", + "magento/module-catalog": "1.0.0-beta6", + "magento/module-config": "1.0.0-beta6", + "magento/framework": "1.0.0-beta6", "magento/magento-composer-installer": "*" }, "type": "magento2-module", - "version": "1.0.0-beta", + "version": "1.0.0-beta6", "license": [ "OSL-3.0", "AFL-3.0"