/** * Update items stock status and low stock date. * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { 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 <a target="_blank" href="%1">here</a>.', 'https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-overview'), $this->messageManager->getDefaultGroup()); } } }
/** * 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->model->execute($eventObserver); }
/** * 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(); }
/** * 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; }
/** * 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()); }
/** * Test case when module is enabled in config * * @return void */ public function testReportProductDeletedToNewRelic() { /** @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->newRelicWrapper->expects($this->once())->method('addCustomParameter')->willReturn(true); $this->model->execute($eventObserver); }
/** * Test case when module is enabled * * @return void */ public function testReportReportModulesInfo() { $this->config->expects($this->once())->method('isNewRelicEnabled')->willReturn(true); $this->collectMock->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->systemModelMock->expects($this->any())->method('setData')->willReturnSelf(); $this->systemModelMock->expects($this->any())->method('save')->willReturnSelf(); $this->assertSame($this->model, $this->model->report()); }
/** * 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; }
/** * Report system cache is flushed to New Relic * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { if ($this->config->isNewRelicEnabled()) { $user = $this->backendAuthSession->getUser(); if ($user->getId()) { $this->deploymentsFactory->create()->setDeployment('Cache Flush', $user->getUsername() . ' flushed the cache.', $user->getUsername()); } } }
/** * Test case when module is enabled in config and php extension is installed * * @return void */ public function testCheckConfig() { /** @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->newRelicWrapper->expects($this->once())->method('isExtensionInstalled')->willReturn(false); $this->config->expects($this->once())->method('disableModule'); $this->messageManager->expects($this->once())->method('addError'); $this->model->execute($eventObserver); }
/** * Reports a system cache flush to the database reporting_system_updates table * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { if ($this->config->isNewRelicEnabled()) { $jsonData = ['status' => 'updated']; $modelData = ['type' => Config::FLUSH_CACHE, 'action' => $this->jsonEncoder->encode($jsonData)]; /** @var \Magento\NewRelicReporting\Model\System $systemModel */ $systemModel = $this->systemFactory->create(); $systemModel->setData($modelData); $systemModel->save(); } }
/** * Test case when module is enabled and user is logged in * * @return void */ public function testReportConcurrentAdminsToNewRelic() { /** @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->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->model->execute($eventObserver); }
/** * Adds New Relic custom parameters per adminhtml request for current admin user, if applicable * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { 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()); } } }
/** * Test case when module is enabled in config * * @return void */ public function testReportSystemCacheFlushToNewRelic() { /** @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); $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->model->execute($eventObserver); }
/** * Reports any products created or updated to New Relic * * @param Observer $observer * @return void */ public function execute(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'); } } }
/** * Adds New Relic custom parameters per request for store, website, and logged in user if applicable * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { 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()); } } }
/** * Test case when module is enabled in config * * @return void */ public function testReportSystemCacheFlush() { $testType = 'systemCacheFlush'; $testAction = 'JSON string'; /** @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->jsonEncoder->expects($this->once())->method('encode')->willReturn($testAction); $this->systemModel->expects($this->once())->method('setData')->with(['type' => $testType, 'action' => $testAction])->willReturnSelf(); $this->systemModel->expects($this->once())->method('save'); $this->model->execute($eventObserver); }
/** * Reports any products deleted to the database reporting_system_updates table * * @param Observer $observer * @return void */ public function execute(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(); } }
/** * Test case when module is enabled in config and product updating * * @dataProvider actionDataProvider * @return void */ public function testReportProductUpdatedToNewRelic($isNewObject) { /** @var 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->model->execute($eventObserver); }
/** * Reports concurrent users to the database reporting_users table * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { 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(); } } }
/** * Reports concurrent admins to the database reporting_users table * * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(Observer $observer) { 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)]; /** @var \Magento\NewRelicReporting\Model\Users $usersModel */ $usersModel = $this->usersFactory->create(); $usersModel->setData($modelData); $usersModel->save(); } } }
/** * Reports orders placed to New Relic * * @param Observer $observer * @return void */ public function execute(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()); } }
/** * Reports orders placed to the database reporting_orders table * * @param Observer $observer * @return void */ public function execute(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]; /** @var \Magento\NewRelicReporting\Model\Orders $orderModel */ $orderModel = $this->ordersFactory->create(); $orderModel->setData($modelData); $orderModel->save(); } }
/** * Test case when module is enabled in config * * @return void */ public function testReportProductDeleted() { $testType = 'adminProductChange'; $testAction = 'JSON string'; /** @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->jsonEncoder->expects($this->once())->method('encode')->willReturn($testAction); $this->systemModel->expects($this->once())->method('setData')->with(['type' => $testType, 'action' => $testAction])->willReturnSelf(); $this->systemModel->expects($this->once())->method('save'); $this->model->execute($eventObserver); }
/** * 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; }
/** * 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(); }
/** * 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->model->execute($eventObserver); }
/** * Reports any products created or updated to the database reporting_system_updates table * * @param Observer $observer * @return void */ public function execute(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)]; /** @var \Magento\NewRelicReporting\Model\System $systemModel */ $systemModel = $this->systemFactory->create(); $systemModel->setData($modelData); $systemModel->save(); } }
/** * 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; }
/** * 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); }