From 01c167ade67eabb071d8975bdce2aec93dd86933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 10:35:17 +0700 Subject: [PATCH 1/7] Fix coding standards --- .../Adminhtml/Config/DisableAdminUsage.php | 15 ++++++++------- .../Adminhtml/Config/EnableAdminUsage.php | 17 +++++++++-------- .../Model/Condition/CanViewNotification.php | 10 ++++++---- .../Model/ResourceModel/Viewer/Logger.php | 9 ++++++--- .../Magento/AdminAnalytics/Model/Viewer/Log.php | 6 +++--- .../Unit/Condition/CanViewNotificationTest.php | 6 +++--- .../AdminUsageNotificationDataProvider.php | 2 +- .../AdminAnalytics/ViewModel/Metadata.php | 12 +++++++----- .../AdminAnalytics/ViewModel/Notification.php | 8 ++++---- 9 files changed, 47 insertions(+), 38 deletions(-) diff --git a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php index 34a9ef4f75b98..085295ab6cf27 100644 --- a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php +++ b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php @@ -7,16 +7,16 @@ namespace Magento\AdminAnalytics\Controller\Adminhtml\Config; +use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger; use Magento\Backend\App\Action; +use Magento\Config\Model\Config\Factory; use Magento\Framework\App\Action\HttpPostActionInterface; -use Magento\Framework\Controller\ResultFactory; -use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger; use Magento\Framework\App\ProductMetadataInterface; +use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultInterface; -use Magento\Config\Model\Config\Factory; /** - * Controller to record Admin analytics usage log + * Controller to record Admin analytics usage log. */ class DisableAdminUsage extends Action implements HttpPostActionInterface { @@ -56,7 +56,7 @@ public function __construct( } /** - * Change the value of config/admin/usage/enabled + * Change the value of config/admin/usage/enabled. */ private function disableAdminUsage() { @@ -66,7 +66,7 @@ private function disableAdminUsage() } /** - * Log information about the last admin usage selection + * Log information about the last admin usage selection. * * @return ResultInterface */ @@ -80,11 +80,12 @@ private function markUserNotified(): ResultInterface ]; $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); + return $resultJson->setData($responseContent); } /** - * Log information about the last shown advertisement + * Log information about the last shown advertisement. * * @return ResultInterface */ diff --git a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php index f70dd57aa59d6..fd525ac0251bc 100644 --- a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php +++ b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php @@ -7,16 +7,16 @@ namespace Magento\AdminAnalytics\Controller\Adminhtml\Config; +use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger; use Magento\Backend\App\Action; +use Magento\Config\Model\Config\Factory; use Magento\Framework\App\Action\HttpPostActionInterface; -use Magento\Framework\Controller\ResultFactory; -use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger; use Magento\Framework\App\ProductMetadataInterface; +use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultInterface; -use Magento\Config\Model\Config\Factory; /** - * Controller to record that the current admin user has responded to Admin Analytics notice + * Controller to record that the current admin user has responded to Admin Analytics notice. */ class EnableAdminUsage extends Action implements HttpPostActionInterface { @@ -54,7 +54,7 @@ public function __construct( } /** - * Change the value of config/admin/usage/enabled + * Change the value of config/admin/usage/enabled. */ private function enableAdminUsage() { @@ -64,7 +64,7 @@ private function enableAdminUsage() } /** - * Log information about the last user response + * Log information about the last user response. * * @return ResultInterface */ @@ -74,15 +74,16 @@ private function markUserNotified(): ResultInterface 'success' => $this->notificationLogger->log( $this->productMetadata->getVersion() ), - 'error_message' => '' + 'error_message' => '', ]; $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); + return $resultJson->setData($responseContent); } /** - * Log information about the last shown advertisement + * Log information about the last shown advertisement. * * @return \Magento\Framework\Controller\ResultInterface */ diff --git a/app/code/Magento/AdminAnalytics/Model/Condition/CanViewNotification.php b/app/code/Magento/AdminAnalytics/Model/Condition/CanViewNotification.php index 222261d4abfb5..36fadb2975d01 100644 --- a/app/code/Magento/AdminAnalytics/Model/Condition/CanViewNotification.php +++ b/app/code/Magento/AdminAnalytics/Model/Condition/CanViewNotification.php @@ -8,8 +8,8 @@ namespace Magento\AdminAnalytics\Model\Condition; use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger; -use Magento\Framework\View\Layout\Condition\VisibilityConditionInterface; use Magento\Framework\App\CacheInterface; +use Magento\Framework\View\Layout\Condition\VisibilityConditionInterface; /** * Dynamic validator for UI admin analytics notification, control UI component visibility. @@ -24,7 +24,7 @@ class CanViewNotification implements VisibilityConditionInterface private static $conditionName = 'can_view_admin_usage_notification'; /** - * Prefix for cache + * Prefix for cache. * * @var string */ @@ -53,7 +53,7 @@ public function __construct( } /** - * Validate if notification popup can be shown and set the notification flag + * Validate if notification popup can be shown and set the notification flag. * * @param array $arguments Attributes from element node. * @inheritdoc @@ -67,13 +67,15 @@ public function isVisible(array $arguments): bool if ($logExists) { $this->cacheStorage->save('log-exists', $cacheKey); } + return !$logExists; } + return false; } /** - * Get condition name + * Get condition name. * * @return string */ diff --git a/app/code/Magento/AdminAnalytics/Model/ResourceModel/Viewer/Logger.php b/app/code/Magento/AdminAnalytics/Model/ResourceModel/Viewer/Logger.php index c66f31e3d3bcb..a08a4b62f7c93 100644 --- a/app/code/Magento/AdminAnalytics/Model/ResourceModel/Viewer/Logger.php +++ b/app/code/Magento/AdminAnalytics/Model/ResourceModel/Viewer/Logger.php @@ -19,7 +19,7 @@ class Logger { /** - * Admin Analytics usage version log table name + * Admin Analytics usage version log table name. */ const LOG_TABLE_NAME = 'admin_analytics_usage_version_log'; @@ -64,6 +64,7 @@ public function log(string $lastViewVersion): bool 'last_viewed_in_version', ] ); + return true; } @@ -80,17 +81,18 @@ public function get(): Log /** * Checks is log already exists. * - * @return boolean + * @return bool */ public function checkLogExists(): bool { $data = $this->logFactory->create(['data' => $this->loadLatestLogData()]); $lastViewedVersion = $data->getLastViewVersion(); + return isset($lastViewedVersion); } /** - * Load release notification viewer log data by last view version + * Load release notification viewer log data by last view version. * * @return array */ @@ -106,6 +108,7 @@ private function loadLatestLogData(): array if (!$data) { $data = []; } + return $data; } } diff --git a/app/code/Magento/AdminAnalytics/Model/Viewer/Log.php b/app/code/Magento/AdminAnalytics/Model/Viewer/Log.php index 0c3b6b81ec811..2340f5069a910 100644 --- a/app/code/Magento/AdminAnalytics/Model/Viewer/Log.php +++ b/app/code/Magento/AdminAnalytics/Model/Viewer/Log.php @@ -10,12 +10,12 @@ use Magento\Framework\DataObject; /** - * Admin Analytics log resource + * Admin Analytics log resource. */ class Log extends DataObject { /** - * Get log id + * Get log id. * * @return int */ @@ -25,7 +25,7 @@ public function getId() : ?int } /** - * Get last viewed product version + * Get last viewed product version. * * @return string */ diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php index 07ef6d6c4fe74..b5c602f56b141 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php @@ -21,16 +21,16 @@ class CanViewNotificationTest extends TestCase /** @var CanViewNotification */ private $canViewNotification; - /** @var Logger|MockObject */ + /** @var Logger|MockObject */ private $viewerLoggerMock; /** @var ProductMetadataInterface|MockObject */ private $productMetadataMock; - /** @var Log|MockObject */ + /** @var Log|MockObject */ private $logMock; - /** @var MockObject|CacheInterface $cacheStorageMock */ + /** @var MockObject|CacheInterface */ private $cacheStorageMock; protected function setUp(): void diff --git a/app/code/Magento/AdminAnalytics/Ui/DataProvider/AdminUsageNotificationDataProvider.php b/app/code/Magento/AdminAnalytics/Ui/DataProvider/AdminUsageNotificationDataProvider.php index 961a5663730a2..352b162d95fef 100644 --- a/app/code/Magento/AdminAnalytics/Ui/DataProvider/AdminUsageNotificationDataProvider.php +++ b/app/code/Magento/AdminAnalytics/Ui/DataProvider/AdminUsageNotificationDataProvider.php @@ -6,8 +6,8 @@ namespace Magento\AdminAnalytics\Ui\DataProvider; -use Magento\Ui\DataProvider\AbstractDataProvider; use Magento\Framework\Api\Filter; +use Magento\Ui\DataProvider\AbstractDataProvider; /** * Data Provider for the Admin usage UI component. diff --git a/app/code/Magento/AdminAnalytics/ViewModel/Metadata.php b/app/code/Magento/AdminAnalytics/ViewModel/Metadata.php index 9b1accbe0c823..8e5a075c61242 100644 --- a/app/code/Magento/AdminAnalytics/ViewModel/Metadata.php +++ b/app/code/Magento/AdminAnalytics/ViewModel/Metadata.php @@ -3,15 +3,16 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\AdminAnalytics\ViewModel; -use Magento\Framework\App\ProductMetadataInterface; use Magento\Backend\Model\Auth\Session; +use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\App\State; use Magento\Framework\View\Element\Block\ArgumentInterface; /** - * Gets user version and mode + * Gets user version and mode. */ class Metadata implements ArgumentInterface { @@ -46,7 +47,7 @@ public function __construct( } /** - * Get product version + * Get product version. * * @return string */ @@ -56,7 +57,7 @@ public function getMagentoVersion() :string } /** - * Get current user id (hash generated from email) + * Get current user id (hash generated from email). * * @return string */ @@ -64,8 +65,9 @@ public function getCurrentUser() :string { return hash('sha512', 'ADMIN_USER' . $this->authSession->getUser()->getEmail()); } + /** - * Get Magento mode that the user is using + * Get Magento mode that the user is using. * * @return string */ diff --git a/app/code/Magento/AdminAnalytics/ViewModel/Notification.php b/app/code/Magento/AdminAnalytics/ViewModel/Notification.php index 030e027b83fce..b6ac4ec1ccc57 100644 --- a/app/code/Magento/AdminAnalytics/ViewModel/Notification.php +++ b/app/code/Magento/AdminAnalytics/ViewModel/Notification.php @@ -7,12 +7,12 @@ namespace Magento\AdminAnalytics\ViewModel; -use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\AdminAnalytics\Model\Condition\CanViewNotification as AdminAnalyticsNotification; +use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\ReleaseNotification\Model\Condition\CanViewNotification as ReleaseNotification; /** - * Control display of admin analytics and release notification modals + * Control display of admin analytics and release notification modals. */ class Notification implements ArgumentInterface { @@ -39,7 +39,7 @@ public function __construct( } /** - * Determine if the analytics popup is visible + * Determine if the analytics popup is visible. * * @return bool */ @@ -49,7 +49,7 @@ public function isAnalyticsVisible(): bool } /** - * Determine if the release popup is visible + * Determine if the release popup is visible. * * @return bool */ From 3e8212ee4794c214c46b04af8bb2bff339860e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 10:45:36 +0700 Subject: [PATCH 2/7] remove trailing_comma_in_multiline_array --- .../Controller/Adminhtml/Config/EnableAdminUsage.php | 2 +- .../Test/Unit/Condition/CanViewNotificationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php index fd525ac0251bc..0190a79f39a80 100644 --- a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php +++ b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php @@ -74,7 +74,7 @@ private function markUserNotified(): ResultInterface 'success' => $this->notificationLogger->log( $this->productMetadata->getVersion() ), - 'error_message' => '', + 'error_message' => '' ]; $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php index b5c602f56b141..a8740faafeba7 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php @@ -30,7 +30,7 @@ class CanViewNotificationTest extends TestCase /** @var Log|MockObject */ private $logMock; - /** @var MockObject|CacheInterface */ + /** @var CacheInterface|MockObject */ private $cacheStorageMock; protected function setUp(): void From b532170f5c3ab51679ff89499c6a3a6cda1b84ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 11:30:06 +0700 Subject: [PATCH 3/7] Add test for Model\Viewer\Log --- .../Adminhtml/Config/DisableAdminUsage.php | 38 +++++----- .../Adminhtml/Config/EnableAdminUsage.php | 38 +++++----- .../Condition/CanViewNotificationTest.php | 20 ++++-- .../Test/Unit/Viewer/LogTest.php | 70 +++++++++++++++++++ 4 files changed, 123 insertions(+), 43 deletions(-) create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/Viewer/LogTest.php diff --git a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php index 085295ab6cf27..2da0392d5d81b 100644 --- a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php +++ b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/DisableAdminUsage.php @@ -55,6 +55,25 @@ public function __construct( $this->notificationLogger = $notificationLogger; } + /** + * Log information about the last shown advertisement. + * + * @return ResultInterface + */ + public function execute() + { + $this->disableAdminUsage(); + $this->markUserNotified(); + } + + /** + * @inheritDoc + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed(static::ADMIN_RESOURCE); + } + /** * Change the value of config/admin/usage/enabled. */ @@ -83,23 +102,4 @@ private function markUserNotified(): ResultInterface return $resultJson->setData($responseContent); } - - /** - * Log information about the last shown advertisement. - * - * @return ResultInterface - */ - public function execute() - { - $this->disableAdminUsage(); - $this->markUserNotified(); - } - - /** - * @inheritDoc - */ - protected function _isAllowed() - { - return $this->_authorization->isAllowed(static::ADMIN_RESOURCE); - } } diff --git a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php index 0190a79f39a80..e926dfcef11dd 100644 --- a/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php +++ b/app/code/Magento/AdminAnalytics/Controller/Adminhtml/Config/EnableAdminUsage.php @@ -53,6 +53,25 @@ public function __construct( $this->notificationLogger = $notificationLogger; } + /** + * Log information about the last shown advertisement. + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + $this->enableAdminUsage(); + $this->markUserNotified(); + } + + /** + * @inheritDoc + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed(static::ADMIN_RESOURCE); + } + /** * Change the value of config/admin/usage/enabled. */ @@ -81,23 +100,4 @@ private function markUserNotified(): ResultInterface return $resultJson->setData($responseContent); } - - /** - * Log information about the last shown advertisement. - * - * @return \Magento\Framework\Controller\ResultInterface - */ - public function execute() - { - $this->enableAdminUsage(); - $this->markUserNotified(); - } - - /** - * @inheritDoc - */ - protected function _isAllowed() - { - return $this->_authorization->isAllowed(static::ADMIN_RESOURCE); - } } diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php index a8740faafeba7..a71debbd90d52 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php @@ -27,19 +27,21 @@ class CanViewNotificationTest extends TestCase /** @var ProductMetadataInterface|MockObject */ private $productMetadataMock; - /** @var Log|MockObject */ - private $logMock; - /** @var CacheInterface|MockObject */ private $cacheStorageMock; + /** @var Log|MockObject */ + private $logMock; + protected function setUp(): void { + $this->viewerLoggerMock = $this->createMock(Logger::class); + $this->productMetadataMock = $this->getMockForAbstractClass(ProductMetadataInterface::class); $this->cacheStorageMock = $this->getMockBuilder(CacheInterface::class) ->getMockForAbstractClass(); + $this->logMock = $this->createMock(Log::class); - $this->viewerLoggerMock = $this->createMock(Logger::class); - $this->productMetadataMock = $this->getMockForAbstractClass(ProductMetadataInterface::class); + $objectManager = new ObjectManager($this); $this->canViewNotification = $objectManager->getObject( CanViewNotification::class, @@ -55,6 +57,7 @@ protected function setUp(): void * @param $expected * @param $cacheResponse * @param $logExists + * * @dataProvider isVisibleProvider */ public function testIsVisibleLoadDataFromLog($expected, $cacheResponse, $logExists) @@ -72,6 +75,13 @@ public function testIsVisibleLoadDataFromLog($expected, $cacheResponse, $logExis $this->assertEquals($expected, $this->canViewNotification->isVisible([])); } + public function testGetName() + { + $result = $this->canViewNotification->getName(); + + $this->assertSame('can_view_admin_usage_notification', $result); + } + /** * @return array */ diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Viewer/LogTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Viewer/LogTest.php new file mode 100644 index 0000000000000..f1816d74e5dad --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Viewer/LogTest.php @@ -0,0 +1,70 @@ +log = $objectManager->getObject(Log::class); + } + + /** + * @dataProvider provideDataToGetId + * + * @param int|null $value + * @param int|null $expected + */ + public function testGetId(int $value = null, int $expected = null) + { + $this->log->setId($value); + + $actualValue = $this->log->getId(); + + $this->assertSame($expected, $actualValue); + } + + /** + * @dataProvider provideGetLastViewVersion + * + * @param mixed $value + * @param string|null $expected + */ + public function testGetLastViewVersion($value = null, string $expected = null) + { + $this->log->setData('last_viewed_in_version', $value); + + $actualValue = $this->log->getLastViewVersion(); + + $this->assertSame($expected, $actualValue); + } + + public function provideDataToGetId() + { + return [ + [123, 123], + [null, null], + ]; + } + + public function provideGetLastViewVersion() + { + return [ + ['test_fake', 'test_fake'], + [null, null], + ]; + } +} From 533eb6324019c127ab7556618402ea56fcf9387d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 14:58:59 +0700 Subject: [PATCH 4/7] Add unit tests for Model, Action, Ui, ViewModel --- .../Config/DisableAdminUsageTest.php | 126 ++++++++++++++++++ .../Adminhtml/Config/EnableAdminUsageTest.php | 126 ++++++++++++++++++ ...AdminUsageNotificationDataProviderTest.php | 47 +++++++ .../Test/Unit/ViewModel/MetadataTest.php | 102 ++++++++++++++ .../Test/Unit/ViewModel/NotificationTest.php | 70 ++++++++++ 5 files changed, 471 insertions(+) create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/DisableAdminUsageTest.php create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/EnableAdminUsageTest.php create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php create mode 100644 app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/NotificationTest.php diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/DisableAdminUsageTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/DisableAdminUsageTest.php new file mode 100644 index 0000000000000..bd938b934377c --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/DisableAdminUsageTest.php @@ -0,0 +1,126 @@ +context = $this->createMock(Context::class); + $this->productMetadata = $this->createMock(ProductMetadataInterface::class); + $this->notificationLogger = $this->createMock(NotificationLogger::class); + $this->configFactory = $this->createMock(Factory::class); + + $this->resultFactory = $this->createMock(ResultFactory::class); + $this->context + ->method('getResultFactory') + ->willReturn($this->resultFactory); + + $objectManager = new ObjectManager($this); + $this->disableAdminUsage = $objectManager->getObject( + DisableAdminUsage::class, + [ + 'context' => $this->context, + 'productMetadata' => $this->productMetadata, + 'notificationLogger' => $this->notificationLogger, + 'configFactory' => $this->configFactory, + ] + ); + } + + public function testExecute() + { + $configModel = $this->createMock(Config::class); + $this->configFactory + ->expects($this->once()) + ->method('create') + ->willReturn($configModel); + $configModel + ->expects($this->once()) + ->method('setDataByPath') + ->with('admin/usage/enabled', 0) + ->willReturnSelf(); + $configModel + ->expects($this->once()) + ->method('save') + ->willReturnSelf(); + + $version = '1.1.1'; + $this->productMetadata + ->expects($this->once()) + ->method('getVersion') + ->willReturn($version); + $this->notificationLogger + ->expects($this->once()) + ->method('log') + ->with($version) + ->willReturn(true); + + $resultJson = $this->createMock(Json::class); + $this->resultFactory + ->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_JSON) + ->willReturn($resultJson); + + $responseContent = [ + 'success' => true, + 'error_message' => '' + ]; + + $resultJson + ->expects($this->once()) + ->method('setData') + ->with($responseContent) + ->willReturnSelf(); + + $this->disableAdminUsage->execute(); + } +} diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/EnableAdminUsageTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/EnableAdminUsageTest.php new file mode 100644 index 0000000000000..27c0d76d4d4cc --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Controller/Adminhtml/Config/EnableAdminUsageTest.php @@ -0,0 +1,126 @@ +context = $this->createMock(Context::class); + $this->productMetadata = $this->createMock(ProductMetadataInterface::class); + $this->notificationLogger = $this->createMock(NotificationLogger::class); + $this->configFactory = $this->createMock(Factory::class); + + $this->resultFactory = $this->createMock(ResultFactory::class); + $this->context + ->method('getResultFactory') + ->willReturn($this->resultFactory); + + $objectManager = new ObjectManager($this); + $this->enableAdminUsage = $objectManager->getObject( + EnableAdminUsage::class, + [ + 'context' => $this->context, + 'productMetadata' => $this->productMetadata, + 'notificationLogger' => $this->notificationLogger, + 'configFactory' => $this->configFactory, + ] + ); + } + + public function testExecute() + { + $configModel = $this->createMock(Config::class); + $this->configFactory + ->expects($this->once()) + ->method('create') + ->willReturn($configModel); + $configModel + ->expects($this->once()) + ->method('setDataByPath') + ->with('admin/usage/enabled', 1) + ->willReturnSelf(); + $configModel + ->expects($this->once()) + ->method('save') + ->willReturnSelf(); + + $version = '2.2.2'; + $this->productMetadata + ->expects($this->once()) + ->method('getVersion') + ->willReturn($version); + $this->notificationLogger + ->expects($this->once()) + ->method('log') + ->with($version) + ->willReturn(false); + + $resultJson = $this->createMock(Json::class); + $this->resultFactory + ->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_JSON) + ->willReturn($resultJson); + + $responseContent = [ + 'success' => false, + 'error_message' => '' + ]; + + $resultJson + ->expects($this->once()) + ->method('setData') + ->with($responseContent) + ->willReturnSelf(); + + $this->enableAdminUsage->execute(); + } +} diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php new file mode 100644 index 0000000000000..9171b32804d41 --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php @@ -0,0 +1,47 @@ +data = ['test']; + + $objectManager = new ObjectManager($this); + $this->adminUsageNotificationDataProvider = $objectManager->getObject( + AdminUsageNotificationDataProvider::class, + ['data' => $this->data] + ); + } + + public function testGetData() + { + $this->assertSame($this->data, $this->adminUsageNotificationDataProvider->getData()); + } + + public function testAddFilter() + { + $this->assertNull($this->adminUsageNotificationDataProvider->addFilter($this->createMock(Filter::class))); + } +} diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php new file mode 100644 index 0000000000000..acc720bbf100b --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php @@ -0,0 +1,102 @@ +productMetadata = $this->createMock(ProductMetadataInterface::class); + $this->authSession = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->setMethods(['getUser']) + ->getMock(); + $this->appState = $this->createMock(State::class); + + $objectManager = new ObjectManager($this); + $this->metadata = $objectManager->getObject( + Metadata::class, + [ + 'productMetadata' => $this->productMetadata, + 'authSession' => $this->authSession, + 'appState' => $this->appState, + ] + ); + } + + public function testGetMagentoVersion() + { + $version = '1.1.1'; + $this->productMetadata + ->expects($this->once()) + ->method('getVersion') + ->willReturn($version); + + $this->assertSame($version, $this->metadata->getMagentoVersion()); + } + + public function testGetCurrentUser() + { + $user = $this->createMock(User::class); + $email = 'phamkhien@hotmail.com'; + + $this->authSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $user->expects($this->once()) + ->method('getEmail') + ->willReturn($email); + + $this->assertSame( + hash('sha512', 'ADMIN_USER' . $email), + $this->metadata->getCurrentUser() + ); + } + + public function testGetMode() + { + $mode = 'developer_test'; + + $this->appState + ->expects($this->once()) + ->method('getMode') + ->willReturn($mode); + + $this->assertSame($mode, $this->metadata->getMode()); + } +} diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/NotificationTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/NotificationTest.php new file mode 100644 index 0000000000000..d17c2a2923a8a --- /dev/null +++ b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/NotificationTest.php @@ -0,0 +1,70 @@ +canViewNotificationAnalytics = $this->createMock(AdminAnalyticsNotification::class); + $this->canViewNotificationRelease = $this->createMock(ReleaseNotification::class); + + $objectManager = new ObjectManager($this); + $this->notification = $objectManager->getObject( + Notification::class, + [ + 'canViewNotificationAnalytics' => $this->canViewNotificationAnalytics, + 'canViewNotificationRelease' => $this->canViewNotificationRelease + ] + ); + } + + public function testIsAnalyticsVisible() + { + $this->canViewNotificationAnalytics + ->expects($this->once()) + ->method('isVisible') + ->with([]) + ->willReturn(true); + + $this->assertSame(true, $this->notification->isAnalyticsVisible()); + } + + public function testIsReleaseVisible() + { + $this->canViewNotificationRelease + ->expects($this->once()) + ->method('isVisible') + ->with([]) + ->willReturn(false); + + $this->assertSame(false, $this->notification->isReleaseVisible()); + } +} From 5462836c153b478c2344955411c7d57c0654b8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 15:00:03 +0700 Subject: [PATCH 5/7] Sort imported class --- .../Ui/DataProvider/AdminUsageNotificationDataProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php index 9171b32804d41..caf6be1d40764 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Ui/DataProvider/AdminUsageNotificationDataProviderTest.php @@ -8,9 +8,9 @@ namespace Magento\AdminAnalytics\Test\Unit\Ui\DataProvider; use Magento\AdminAnalytics\Ui\DataProvider\AdminUsageNotificationDataProvider; +use Magento\Framework\Api\Filter; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit\Framework\TestCase; -use Magento\Framework\Api\Filter; class AdminUsageNotificationDataProviderTest extends TestCase { From 061b1ec24c9aa59ba0889bf4a1433a5eaf9b7f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 26 Nov 2020 15:04:43 +0700 Subject: [PATCH 6/7] Remove ProductMetadataInterface in CanViewNotificationTest --- .../Test/Unit/Condition/CanViewNotificationTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php index a71debbd90d52..ffc08e0b577fb 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/Condition/CanViewNotificationTest.php @@ -11,7 +11,6 @@ use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger; use Magento\AdminAnalytics\Model\Viewer\Log; use Magento\Framework\App\CacheInterface; -use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -24,9 +23,6 @@ class CanViewNotificationTest extends TestCase /** @var Logger|MockObject */ private $viewerLoggerMock; - /** @var ProductMetadataInterface|MockObject */ - private $productMetadataMock; - /** @var CacheInterface|MockObject */ private $cacheStorageMock; @@ -36,7 +32,6 @@ class CanViewNotificationTest extends TestCase protected function setUp(): void { $this->viewerLoggerMock = $this->createMock(Logger::class); - $this->productMetadataMock = $this->getMockForAbstractClass(ProductMetadataInterface::class); $this->cacheStorageMock = $this->getMockBuilder(CacheInterface::class) ->getMockForAbstractClass(); @@ -47,7 +42,6 @@ protected function setUp(): void CanViewNotification::class, [ 'viewerLogger' => $this->viewerLoggerMock, - 'productMetadata' => $this->productMetadataMock, 'cacheStorage' => $this->cacheStorageMock, ] ); From 50af59c2deb2b2a88213436d583c76398395ae60 Mon Sep 17 00:00:00 2001 From: Khien Pham Date: Mon, 7 Dec 2020 17:07:19 +0700 Subject: [PATCH 7/7] Update unit tests for MetadataTest.php --- .../Test/Unit/ViewModel/MetadataTest.php | 131 +++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php index acc720bbf100b..489715618d6fb 100644 --- a/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php +++ b/app/code/Magento/AdminAnalytics/Test/Unit/ViewModel/MetadataTest.php @@ -8,10 +8,14 @@ namespace Magento\AdminAnalytics\Test\Unit\ViewModel; use Magento\AdminAnalytics\ViewModel\Metadata; +use Magento\Authorization\Model\Role; use Magento\Backend\Model\Auth\Session; +use Magento\Config\Model\Config\Backend\Admin\Custom; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\App\State; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Store\Model\Information; use Magento\User\Model\User; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -38,6 +42,11 @@ class MetadataTest extends TestCase */ private $appState; + /** + * @var ScopeConfigInterface|MockObject + */ + private $config; + protected function setUp(): void { $this->productMetadata = $this->createMock(ProductMetadataInterface::class); @@ -46,6 +55,7 @@ protected function setUp(): void ->setMethods(['getUser']) ->getMock(); $this->appState = $this->createMock(State::class); + $this->config = $this->createMock(ScopeConfigInterface::class); $objectManager = new ObjectManager($this); $this->metadata = $objectManager->getObject( @@ -54,6 +64,7 @@ protected function setUp(): void 'productMetadata' => $this->productMetadata, 'authSession' => $this->authSession, 'appState' => $this->appState, + 'config' => $this->config, ] ); } @@ -69,6 +80,21 @@ public function testGetMagentoVersion() $this->assertSame($version, $this->metadata->getMagentoVersion()); } + public function testGetProductEdition() + { + $productEdition = 'edition_fake'; + + $this->productMetadata + ->expects($this->once()) + ->method('getEdition') + ->willReturn($productEdition); + + $this->assertSame( + $productEdition, + $this->metadata->getProductEdition() + ); + } + public function testGetCurrentUser() { $user = $this->createMock(User::class); @@ -83,11 +109,114 @@ public function testGetCurrentUser() ->willReturn($email); $this->assertSame( - hash('sha512', 'ADMIN_USER' . $email), + hash('sha256', 'ADMIN_USER' . $email), $this->metadata->getCurrentUser() ); } + public function testGetCurrentUserCreatedDate() + { + $user = $this->createMock(User::class); + + $createdDate = '2020-12-19 13-03-45'; + $this->authSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $user->expects($this->once()) + ->method('getCreated') + ->willReturn($createdDate); + + $this->assertSame( + $createdDate, + $this->metadata->getCurrentUserCreatedDate() + ); + } + + public function testGetCurrentUserLogDate() + { + $user = $this->getMockBuilder(User::class) + ->setMethods(['getLogdate']) + ->disableOriginalConstructor() + ->getMock(); + + $createdDate = '2020-12-01 13-01-01'; + $this->authSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $user->expects($this->once()) + ->method('getLogdate') + ->willReturn($createdDate); + + $this->assertSame( + $createdDate, + $this->metadata->getCurrentUserLogDate() + ); + } + + public function testGetSecureBaseUrlForScope() + { + $scope = 'default'; + $scopeCode = 'th'; + $result = 'test'; + + $this->config + ->expects($this->once()) + ->method('getValue') + ->with(Custom::XML_PATH_SECURE_BASE_URL, $scope, $scopeCode) + ->willReturn($result); + + $this->assertSame( + $result, + $this->metadata->getSecureBaseUrlForScope($scope, $scopeCode) + ); + } + + public function testGetStoreNameForScope() + { + $scope = 'default'; + $scopeCode = 'th'; + $result = 'test_fake_GetStoreNameForScope'; + + $this->config + ->expects($this->once()) + ->method('getValue') + ->with(Information::XML_PATH_STORE_INFO_NAME, $scope, $scopeCode) + ->willReturn($result); + + $this->assertSame( + $result, + $this->metadata->getStoreNameForScope($scope, $scopeCode) + ); + } + + public function testGetCurrentUserRoleName() + { + $user = $this->createMock(User::class); + $role = $this->getMockBuilder(Role::class) + ->setMethods(['getRoleName']) + ->disableOriginalConstructor() + ->getMock(); + $roleName = 'khien'; + + $this->authSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $user->expects($this->once()) + ->method('getRole') + ->willReturn($role); + $role->expects($this->once()) + ->method('getRoleName') + ->willReturn($roleName); + + $this->assertSame( + $roleName, + $this->metadata->getCurrentUserRoleName() + ); + } + public function testGetMode() { $mode = 'developer_test';