Skip to content

Cover magento/module-admin-analytics with unit tests #31065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
01c167a
Fix coding standards
binhtd-smartosc Nov 26, 2020
3e8212e
remove trailing_comma_in_multiline_array
binhtd-smartosc Nov 26, 2020
b532170
Add test for Model\Viewer\Log
binhtd-smartosc Nov 26, 2020
533eb63
Add unit tests for Model, Action, Ui, ViewModel
binhtd-smartosc Nov 26, 2020
5462836
Sort imported class
binhtd-smartosc Nov 26, 2020
061b1ec
Remove ProductMetadataInterface in CanViewNotificationTest
binhtd-smartosc Nov 26, 2020
a910e64
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Nov 27, 2020
acdc483
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Nov 30, 2020
02eac6d
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 1, 2020
a5c44a7
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 2, 2020
da3ad3d
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 3, 2020
0d46f43
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 3, 2020
af574fe
Merge branch '2.4-develop' of github.com:magento/magento2 into cover-…
khienpc2 Dec 7, 2020
50af59c
Update unit tests for MetadataTest.php
khienpc2 Dec 7, 2020
6a2ff65
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 9, 2020
6f78b0e
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 10, 2020
f08c58f
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 17, 2020
3ec916d
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 20, 2020
634e08c
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 22, 2020
a1fa710
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 23, 2020
f2532d9
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Dec 28, 2020
872e301
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Jan 4, 2021
9323f0f
Merge branch '2.4-develop' into cover-admin-analytics-unit-tests
mbvb1223 Jan 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -56,7 +56,26 @@ public function __construct(
}

/**
* Change the value of config/admin/usage/enabled
* Log information about the last shown advertisement.
*
* @return ResultInterface
*/
public function execute()
{
$this->disableAdminUsage();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved private - disableAdminUsage, markUserNotified method down

$this->markUserNotified();
}

/**
* @inheritDoc
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed(static::ADMIN_RESOURCE);
}

/**
* Change the value of config/admin/usage/enabled.
*/
private function disableAdminUsage()
{
Expand All @@ -66,7 +85,7 @@ private function disableAdminUsage()
}

/**
* Log information about the last admin usage selection
* Log information about the last admin usage selection.
*
* @return ResultInterface
*/
Expand All @@ -80,25 +99,7 @@ private function markUserNotified(): ResultInterface
];

$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
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);
return $resultJson->setData($responseContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -54,7 +54,26 @@ public function __construct(
}

/**
* Change the value of config/admin/usage/enabled
* 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.
*/
private function enableAdminUsage()
{
Expand All @@ -64,7 +83,7 @@ private function enableAdminUsage()
}

/**
* Log information about the last user response
* Log information about the last user response.
*
* @return ResultInterface
*/
Expand All @@ -78,25 +97,7 @@ private function markUserNotified(): ResultInterface
];

$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
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);
return $resultJson->setData($responseContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,7 +24,7 @@ class CanViewNotification implements VisibilityConditionInterface
private static $conditionName = 'can_view_admin_usage_notification';

/**
* Prefix for cache
* Prefix for cache.
*
* @var string
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -64,6 +64,7 @@ public function log(string $lastViewVersion): bool
'last_viewed_in_version',
]
);

return true;
}

Expand All @@ -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
*/
Expand All @@ -106,6 +108,7 @@ private function loadLatestLogData(): array
if (!$data) {
$data = [];
}

return $data;
}
}
6 changes: 3 additions & 3 deletions app/code/Magento/AdminAnalytics/Model/Viewer/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -25,7 +25,7 @@ public function getId() : ?int
}

/**
* Get last viewed product version
* Get last viewed product version.
*
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,31 +20,28 @@ class CanViewNotificationTest extends TestCase
/** @var CanViewNotification */
private $canViewNotification;

/** @var Logger|MockObject */
/** @var Logger|MockObject */
private $viewerLoggerMock;

/** @var ProductMetadataInterface|MockObject */
private $productMetadataMock;
/** @var CacheInterface|MockObject */
private $cacheStorageMock;

/** @var Log|MockObject */
/** @var Log|MockObject */
private $logMock;

/** @var MockObject|CacheInterface $cacheStorageMock */
private $cacheStorageMock;

protected function setUp(): void
{
$this->viewerLoggerMock = $this->createMock(Logger::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,
[
'viewerLogger' => $this->viewerLoggerMock,
'productMetadata' => $this->productMetadataMock,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have productMetadata at Constructor

'cacheStorage' => $this->cacheStorageMock,
]
);
Expand All @@ -55,6 +51,7 @@ protected function setUp(): void
* @param $expected
* @param $cacheResponse
* @param $logExists
*
* @dataProvider isVisibleProvider
*/
public function testIsVisibleLoadDataFromLog($expected, $cacheResponse, $logExists)
Expand All @@ -72,6 +69,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
*/
Expand Down
Loading