diff --git a/app/code/Magento/Authorization/Model/Acl/Loader/Role.php b/app/code/Magento/Authorization/Model/Acl/Loader/Role.php index a5f8fbd6eefec..e4b1103491230 100644 --- a/app/code/Magento/Authorization/Model/Acl/Loader/Role.php +++ b/app/code/Magento/Authorization/Model/Acl/Loader/Role.php @@ -3,14 +3,23 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Authorization\Model\Acl\Loader; use Magento\Authorization\Model\Acl\Role\Group as RoleGroup; +use Magento\Authorization\Model\Acl\Role\GroupFactory; use Magento\Authorization\Model\Acl\Role\User as RoleUser; -use Magento\Framework\App\ObjectManager; +use Magento\Authorization\Model\Acl\Role\UserFactory; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\Acl\LoaderInterface; +use Magento\Framework\App\ResourceConnection; use Magento\Framework\Serialize\Serializer\Json; -class Role implements \Magento\Framework\Acl\LoaderInterface +/** + * Acl Role Loader + */ +class Role implements LoaderInterface { /** * Cache key for ACL roles cache @@ -18,22 +27,22 @@ class Role implements \Magento\Framework\Acl\LoaderInterface const ACL_ROLES_CACHE_KEY = 'authorization_role_cached_data'; /** - * @var \Magento\Framework\App\ResourceConnection + * @var ResourceConnection */ protected $_resource; /** - * @var \Magento\Authorization\Model\Acl\Role\GroupFactory + * @var GroupFactory */ protected $_groupFactory; /** - * @var \Magento\Authorization\Model\Acl\Role\UserFactory + * @var UserFactory */ protected $_roleFactory; /** - * @var \Magento\Framework\Acl\Data\CacheInterface + * @var CacheInterface */ private $aclDataCache; @@ -48,28 +57,26 @@ class Role implements \Magento\Framework\Acl\LoaderInterface private $cacheKey; /** - * @param \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory - * @param \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory - * @param \Magento\Framework\App\ResourceConnection $resource - * @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache + * @param GroupFactory $groupFactory + * @param UserFactory $roleFactory + * @param ResourceConnection $resource + * @param CacheInterface $aclDataCache * @param Json $serializer * @param string $cacheKey */ public function __construct( - \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory, - \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory, - \Magento\Framework\App\ResourceConnection $resource, - \Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null, - Json $serializer = null, + GroupFactory $groupFactory, + UserFactory $roleFactory, + ResourceConnection $resource, + CacheInterface $aclDataCache, + Json $serializer, $cacheKey = self::ACL_ROLES_CACHE_KEY ) { - $this->_resource = $resource; $this->_groupFactory = $groupFactory; $this->_roleFactory = $roleFactory; - $this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get( - \Magento\Framework\Acl\Data\CacheInterface::class - ); - $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); + $this->_resource = $resource; + $this->aclDataCache = $aclDataCache; + $this->serializer = $serializer; $this->cacheKey = $cacheKey; } diff --git a/app/code/Magento/Authorization/Model/Acl/Loader/Rule.php b/app/code/Magento/Authorization/Model/Acl/Loader/Rule.php index 57bdcdbb3d9b8..b8fd974c5da6c 100644 --- a/app/code/Magento/Authorization/Model/Acl/Loader/Rule.php +++ b/app/code/Magento/Authorization/Model/Acl/Loader/Rule.php @@ -3,12 +3,20 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Authorization\Model\Acl\Loader; -use Magento\Framework\App\ObjectManager; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\Acl\LoaderInterface; +use Magento\Framework\Acl\RootResource; +use Magento\Framework\App\ResourceConnection; use Magento\Framework\Serialize\Serializer\Json; -class Rule implements \Magento\Framework\Acl\LoaderInterface +/** + * Acl Rule Loader + */ +class Rule implements LoaderInterface { /** * Rules array cache key @@ -16,17 +24,17 @@ class Rule implements \Magento\Framework\Acl\LoaderInterface const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data'; /** - * @var \Magento\Framework\App\ResourceConnection + * @var ResourceConnection */ protected $_resource; /** - * @var \Magento\Framework\Acl\RootResource + * @var RootResource */ private $_rootResource; /** - * @var \Magento\Framework\Acl\Data\CacheInterface + * @var CacheInterface */ private $aclDataCache; @@ -41,28 +49,26 @@ class Rule implements \Magento\Framework\Acl\LoaderInterface private $cacheKey; /** - * @param \Magento\Framework\Acl\RootResource $rootResource - * @param \Magento\Framework\App\ResourceConnection $resource - * @param array $data - * @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache + * @param RootResource $rootResource + * @param ResourceConnection $resource + * @param CacheInterface $aclDataCache * @param Json $serializer + * @param array $data * @param string $cacheKey * @SuppressWarnings(PHPMD.UnusedFormalParameter): */ public function __construct( - \Magento\Framework\Acl\RootResource $rootResource, - \Magento\Framework\App\ResourceConnection $resource, + RootResource $rootResource, + ResourceConnection $resource, + CacheInterface $aclDataCache, + Json $serializer, array $data = [], - \Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null, - Json $serializer = null, $cacheKey = self::ACL_RULE_CACHE_KEY ) { - $this->_resource = $resource; $this->_rootResource = $rootResource; - $this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get( - \Magento\Framework\Acl\Data\CacheInterface::class - ); - $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); + $this->_resource = $resource; + $this->aclDataCache = $aclDataCache; + $this->serializer = $serializer; $this->cacheKey = $cacheKey; } @@ -104,7 +110,7 @@ private function getRulesArray() return $this->serializer->unserialize($rulesCachedData); } - $ruleTable = $this->_resource->getTableName("authorization_rule"); + $ruleTable = $this->_resource->getTableName('authorization_rule'); $connection = $this->_resource->getConnection(); $select = $connection->select() ->from(['r' => $ruleTable]); diff --git a/app/code/Magento/Authorization/Model/ResourceModel/Rules.php b/app/code/Magento/Authorization/Model/ResourceModel/Rules.php index 5e4ae77731a0f..46470d885650b 100644 --- a/app/code/Magento/Authorization/Model/ResourceModel/Rules.php +++ b/app/code/Magento/Authorization/Model/ResourceModel/Rules.php @@ -6,58 +6,63 @@ namespace Magento\Authorization\Model\ResourceModel; -use Magento\Framework\App\ObjectManager; +use Magento\Backend\App\AbstractAction; +use Magento\Framework\Acl\Builder; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\Acl\RootResource; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\Framework\Model\ResourceModel\Db\Context; +use Psr\Log\LoggerInterface; /** * Admin rule resource model */ -class Rules extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb +class Rules extends AbstractDb { /** * Root ACL resource * - * @var \Magento\Framework\Acl\RootResource + * @var RootResource */ protected $_rootResource; /** - * @var \Magento\Framework\Acl\Builder + * @var Builder */ protected $_aclBuilder; /** - * @var \Psr\Log\LoggerInterface + * @var LoggerInterface */ protected $_logger; /** - * @var \Magento\Framework\Acl\Data\CacheInterface + * @var CacheInterface */ private $aclDataCache; /** - * @param \Magento\Framework\Model\ResourceModel\Db\Context $context - * @param \Magento\Framework\Acl\Builder $aclBuilder - * @param \Psr\Log\LoggerInterface $logger - * @param \Magento\Framework\Acl\RootResource $rootResource + * @param Context $context + * @param Builder $aclBuilder + * @param LoggerInterface $logger + * @param RootResource $rootResource + * @param CacheInterface $aclDataCache * @param string $connectionName - * @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache */ public function __construct( - \Magento\Framework\Model\ResourceModel\Db\Context $context, - \Magento\Framework\Acl\Builder $aclBuilder, - \Psr\Log\LoggerInterface $logger, - \Magento\Framework\Acl\RootResource $rootResource, - $connectionName = null, - \Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null + Context $context, + Builder $aclBuilder, + LoggerInterface $logger, + RootResource $rootResource, + CacheInterface $aclDataCache, + $connectionName = null ) { $this->_aclBuilder = $aclBuilder; parent::__construct($context, $connectionName); $this->_rootResource = $rootResource; $this->_logger = $logger; - $this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get( - \Magento\Framework\Acl\Data\CacheInterface::class - ); + $this->aclDataCache = $aclDataCache; } /** @@ -75,7 +80,7 @@ protected function _construct() * * @param \Magento\Authorization\Model\Rules $rule * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ public function saveRel(\Magento\Authorization\Model\Rules $rule) { @@ -107,7 +112,7 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule) $connection->insert($this->getMainTable(), $insertData); } else { /** Give basic admin permissions to any admin */ - $postedResources[] = \Magento\Backend\App\AbstractAction::ADMIN_RESOURCE; + $postedResources[] = AbstractAction::ADMIN_RESOURCE; $acl = $this->_aclBuilder->getAcl(); /** @var $resource \Magento\Framework\Acl\AclResource */ foreach ($acl->getResources() as $resourceId) { @@ -125,7 +130,7 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule) $connection->commit(); $this->aclDataCache->clean(); - } catch (\Magento\Framework\Exception\LocalizedException $e) { + } catch (LocalizedException $e) { $connection->rollBack(); throw $e; } catch (\Exception $e) { diff --git a/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php b/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php index a5ae7f8e86a6e..e1841b895dd07 100644 --- a/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php +++ b/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php @@ -3,199 +3,221 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Authorization\Test\Unit\Model\Acl\Loader; -class RoleTest extends \PHPUnit\Framework\TestCase -{ - /** - * @var \Magento\Authorization\Model\Acl\Loader\Role - */ - protected $_model; +use Magento\Authorization\Model\Acl\Loader\Role; +use Magento\Authorization\Model\Acl\Role\GroupFactory; +use Magento\Authorization\Model\Acl\Role\UserFactory; +use Magento\Framework\Acl; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\App\ResourceConnection; +use Magento\Framework\DB\Adapter\Pdo\Mysql; +use Magento\Framework\DB\Select; +use Magento\Framework\Serialize\Serializer\Json; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +/** + * Test class for \Magento\Authorization\Model\Acl\Loader\Role + */ +class RoleTest extends TestCase +{ /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var Role */ - protected $_resourceMock; + private $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var GroupFactory|MockObject */ - protected $_adapterMock; + private $groupFactoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var UserFactory|MockObject */ - protected $_roleFactoryMock; + private $roleFactoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ResourceConnection|MockObject */ - protected $_groupFactoryMock; + private $resourceMock; /** - * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject + * @var CacheInterface|MockObject */ private $aclDataCacheMock; /** - * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject + * @var Json|MockObject */ private $serializerMock; /** - * @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject + * @var Select|MockObject */ private $selectMock; + /** + * @var Mysql|MockObject + */ + private $adapterMock; + + /** + * @inheritDoc + */ protected function setUp() { - $this->_resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class); - $this->_groupFactoryMock = $this->getMockBuilder(\Magento\Authorization\Model\Acl\Role\GroupFactory::class) + $this->groupFactoryMock = $this->getMockBuilder(GroupFactory::class) ->setMethods(['create', 'getModelInstance']) ->disableOriginalConstructor() ->getMock(); - $this->_roleFactoryMock = $this->getMockBuilder(\Magento\Authorization\Model\Acl\Role\UserFactory::class) + $this->roleFactoryMock = $this->getMockBuilder(UserFactory::class) ->setMethods(['create', 'getModelInstance']) ->disableOriginalConstructor() ->getMock(); - - $this->selectMock = $this->createMock(\Magento\Framework\DB\Select::class); - $this->selectMock->expects($this->any()) - ->method('from') - ->will($this->returnValue($this->selectMock)); - - $this->_adapterMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class); - + $this->resourceMock = $this->createMock(ResourceConnection::class); + $this->aclDataCacheMock = $this->createMock(CacheInterface::class); $this->serializerMock = $this->createPartialMock( - \Magento\Framework\Serialize\Serializer\Json::class, + Json::class, ['serialize', 'unserialize'] ); - $this->serializerMock->expects($this->any()) - ->method('serialize') - ->will( - $this->returnCallback( - function ($value) { - return json_encode($value); - } - ) - ); - $this->serializerMock->expects($this->any()) - ->method('unserialize') - ->will( - $this->returnCallback( - function ($value) { - return json_decode($value, true); - } - ) + $this->serializerMock->method('serialize') + ->willReturnCallback( + static function ($value) { + return json_encode($value); + } ); - $this->aclDataCacheMock = $this->createMock(\Magento\Framework\Acl\Data\CacheInterface::class); + $this->serializerMock->method('unserialize') + ->willReturnCallback( + static function ($value) { + return json_decode($value, true); + } + ); - $this->_model = new \Magento\Authorization\Model\Acl\Loader\Role( - $this->_groupFactoryMock, - $this->_roleFactoryMock, - $this->_resourceMock, - $this->aclDataCacheMock, - $this->serializerMock + $this->selectMock = $this->createMock(Select::class); + $this->selectMock->method('from') + ->willReturn($this->selectMock); + + $this->adapterMock = $this->createMock(Mysql::class); + + $objectManager = new ObjectManager($this); + $this->model = $objectManager->getObject( + Role::class, + [ + 'groupFactory' => $this->groupFactoryMock, + 'roleFactory' => $this->roleFactoryMock, + 'resource' => $this->resourceMock, + 'aclDataCache' => $this->aclDataCacheMock, + 'serializer' => $this->serializerMock + ] ); } + /** + * Test populating acl roles with children + */ public function testPopulateAclAddsRolesAndTheirChildren() { - $this->_resourceMock->expects($this->once()) + $this->resourceMock->expects($this->once()) ->method('getTableName') ->with($this->equalTo('authorization_role')) - ->will($this->returnArgument(1)); + ->willReturnArgument(1); - $this->_adapterMock->expects($this->once()) + $this->adapterMock->expects($this->once()) ->method('select') - ->will($this->returnValue($this->selectMock)); + ->willReturn($this->selectMock); - $this->_resourceMock->expects($this->once()) + $this->resourceMock->expects($this->once()) ->method('getConnection') - ->will($this->returnValue($this->_adapterMock)); + ->willReturn($this->adapterMock); - $this->_adapterMock->expects($this->once()) + $this->adapterMock->expects($this->once()) ->method('fetchAll') - ->will( - $this->returnValue( - [ - ['role_id' => 1, 'role_type' => 'G', 'parent_id' => null], - ['role_id' => 2, 'role_type' => 'U', 'parent_id' => 1, 'user_id' => 1], - ] - ) + ->willReturn( + [ + ['role_id' => 1, 'role_type' => 'G', 'parent_id' => null], + ['role_id' => 2, 'role_type' => 'U', 'parent_id' => 1, 'user_id' => 1], + ] ); - $this->_groupFactoryMock->expects($this->once())->method('create')->with(['roleId' => '1']); - $this->_roleFactoryMock->expects($this->once())->method('create')->with(['roleId' => '2']); + $this->groupFactoryMock->expects($this->once())->method('create')->with(['roleId' => '1']); + $this->roleFactoryMock->expects($this->once())->method('create')->with(['roleId' => '2']); - $aclMock = $this->createMock(\Magento\Framework\Acl::class); + $aclMock = $this->createMock(Acl::class); $aclMock->expects($this->at(0))->method('addRole')->with($this->anything(), null); $aclMock->expects($this->at(2))->method('addRole')->with($this->anything(), '1'); - $this->_model->populateAcl($aclMock); + $this->model->populateAcl($aclMock); } + /** + * Test populating acl role with multiple parents + */ public function testPopulateAclAddsMultipleParents() { - $this->_resourceMock->expects($this->once()) + $this->resourceMock->expects($this->once()) ->method('getTableName') ->with($this->equalTo('authorization_role')) - ->will($this->returnArgument(1)); + ->willReturnArgument(1); - $this->_adapterMock->expects($this->once()) + $this->adapterMock->expects($this->once()) ->method('select') - ->will($this->returnValue($this->selectMock)); + ->willReturn($this->selectMock); - $this->_resourceMock->expects($this->once()) + $this->resourceMock->expects($this->once()) ->method('getConnection') - ->will($this->returnValue($this->_adapterMock)); + ->willReturn($this->adapterMock); - $this->_adapterMock->expects($this->once()) + $this->adapterMock->expects($this->once()) ->method('fetchAll') - ->will($this->returnValue([['role_id' => 1, 'role_type' => 'U', 'parent_id' => 2, 'user_id' => 3]])); + ->willReturn([['role_id' => 1, 'role_type' => 'U', 'parent_id' => 2, 'user_id' => 3]]); - $this->_roleFactoryMock->expects($this->never())->method('getModelInstance'); - $this->_groupFactoryMock->expects($this->never())->method('getModelInstance'); + $this->roleFactoryMock->expects($this->never())->method('getModelInstance'); + $this->groupFactoryMock->expects($this->never())->method('getModelInstance'); - $aclMock = $this->createMock(\Magento\Framework\Acl::class); - $aclMock->expects($this->at(0))->method('hasRole')->with('1')->will($this->returnValue(true)); + $aclMock = $this->createMock(Acl::class); + $aclMock->expects($this->at(0))->method('hasRole')->with('1')->willReturn(true); $aclMock->expects($this->at(1))->method('addRoleParent')->with('1', '2'); - $this->_model->populateAcl($aclMock); + $this->model->populateAcl($aclMock); } + /** + * Test populating acl role from cache + */ public function testPopulateAclFromCache() { - $this->_resourceMock->expects($this->never())->method('getConnection'); - $this->_resourceMock->expects($this->never())->method('getTableName'); - $this->_adapterMock->expects($this->never())->method('fetchAll'); + $this->resourceMock->expects($this->never())->method('getConnection'); + $this->resourceMock->expects($this->never())->method('getTableName'); + $this->adapterMock->expects($this->never())->method('fetchAll'); $this->aclDataCacheMock->expects($this->once()) ->method('load') - ->with(\Magento\Authorization\Model\Acl\Loader\Role::ACL_ROLES_CACHE_KEY) - ->will( - $this->returnValue( - json_encode( + ->with(Role::ACL_ROLES_CACHE_KEY) + ->willReturn( + json_encode( + [ [ - [ - 'role_id' => 1, - 'role_type' => 'U', - 'parent_id' => 2, - 'user_id' => 3 - ] + 'role_id' => 1, + 'role_type' => 'U', + 'parent_id' => 2, + 'user_id' => 3 ] - ) + ] ) ); - $this->_roleFactoryMock->expects($this->never())->method('getModelInstance'); - $this->_groupFactoryMock->expects($this->never())->method('getModelInstance'); + $this->roleFactoryMock->expects($this->never())->method('getModelInstance'); + $this->groupFactoryMock->expects($this->never())->method('getModelInstance'); - $aclMock = $this->createMock(\Magento\Framework\Acl::class); - $aclMock->expects($this->at(0))->method('hasRole')->with('1')->will($this->returnValue(true)); + $aclMock = $this->createMock(Acl::class); + $aclMock->expects($this->at(0))->method('hasRole')->with('1')->willReturn(true); $aclMock->expects($this->at(1))->method('addRoleParent')->with('1', '2'); - $this->_model->populateAcl($aclMock); + $this->model->populateAcl($aclMock); } } diff --git a/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php b/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php index 51801e1842eff..c7a9b80c6597c 100644 --- a/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php +++ b/app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php @@ -3,105 +3,121 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Authorization\Test\Unit\Model\Acl\Loader; -class RuleTest extends \PHPUnit\Framework\TestCase +use Magento\Authorization\Model\Acl\Loader\Rule; +use Magento\Framework\Acl; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\Acl\RootResource; +use Magento\Framework\App\ResourceConnection; +use Magento\Framework\Serialize\Serializer\Json; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +/** + * Test class for \Magento\Authorization\Model\Acl\Loader\Rule + */ +class RuleTest extends TestCase { /** - * @var \Magento\Authorization\Model\Acl\Loader\Rule + * @var Rule */ - protected $_model; + private $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var RootResource */ - protected $_resourceMock; + private $rootResource; /** - * @var \Magento\Framework\Acl\RootResource + * @var ResourceConnection|MockObject */ - protected $_rootResourceMock; + private $resourceMock; /** - * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject + * @var CacheInterface|MockObject */ private $aclDataCacheMock; /** - * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject + * @var Json|MockObject */ private $serializerMock; + /** + * @inheritDoc + */ protected function setUp() { - $this->_resourceMock = $this->createPartialMock( - \Magento\Framework\App\ResourceConnection::class, + $this->rootResource = new RootResource('Magento_Backend::all'); + $this->resourceMock = $this->createPartialMock( + ResourceConnection::class, ['getTable', 'getConnection'] ); + $this->aclDataCacheMock = $this->createMock(CacheInterface::class); $this->serializerMock = $this->createPartialMock( - \Magento\Framework\Serialize\Serializer\Json::class, + Json::class, ['serialize', 'unserialize'] ); - $this->serializerMock->expects($this->any()) - ->method('serialize') - ->will( - $this->returnCallback( - function ($value) { - return json_encode($value); - } - ) - ); - $this->serializerMock->expects($this->any()) - ->method('unserialize') - ->will( - $this->returnCallback( - function ($value) { - return json_decode($value, true); - } - ) + $this->serializerMock->method('serialize') + ->willReturnCallback( + static function ($value) { + return json_encode($value); + } ); - $this->aclDataCacheMock = $this->createMock(\Magento\Framework\Acl\Data\CacheInterface::class); + $this->serializerMock->method('unserialize') + ->willReturnCallback( + static function ($value) { + return json_decode($value, true); + } + ); - $this->_rootResourceMock = new \Magento\Framework\Acl\RootResource('Magento_Backend::all'); - $this->_model = new \Magento\Authorization\Model\Acl\Loader\Rule( - $this->_rootResourceMock, - $this->_resourceMock, - [], - $this->aclDataCacheMock, - $this->serializerMock + $objectManager = new ObjectManager($this); + $this->model = $objectManager->getObject( + Rule::class, + [ + 'rootResource' => $this->rootResource, + 'resource' => $this->resourceMock, + 'aclDataCache' => $this->aclDataCacheMock, + 'serializer' => $this->serializerMock + ] ); } + /** + * Test populating acl rule from cache + */ public function testPopulateAclFromCache() { - $this->_resourceMock->expects($this->never())->method('getTable'); - $this->_resourceMock->expects($this->never()) + $this->resourceMock->expects($this->never())->method('getTable'); + $this->resourceMock->expects($this->never()) ->method('getConnection'); $this->aclDataCacheMock->expects($this->once()) ->method('load') - ->with(\Magento\Authorization\Model\Acl\Loader\Rule::ACL_RULE_CACHE_KEY) - ->will( - $this->returnValue( - json_encode( - [ - ['role_id' => 1, 'resource_id' => 'Magento_Backend::all', 'permission' => 'allow'], - ['role_id' => 2, 'resource_id' => 1, 'permission' => 'allow'], - ['role_id' => 3, 'resource_id' => 1, 'permission' => 'deny'], - ] - ) + ->with(Rule::ACL_RULE_CACHE_KEY) + ->willReturn( + json_encode( + [ + ['role_id' => 1, 'resource_id' => 'Magento_Backend::all', 'permission' => 'allow'], + ['role_id' => 2, 'resource_id' => 1, 'permission' => 'allow'], + ['role_id' => 3, 'resource_id' => 1, 'permission' => 'deny'], + ] ) ); - $aclMock = $this->createMock(\Magento\Framework\Acl::class); - $aclMock->expects($this->any())->method('has')->will($this->returnValue(true)); + $aclMock = $this->createMock(Acl::class); + $aclMock->method('has')->willReturn(true); $aclMock->expects($this->at(1))->method('allow')->with('1', null, null); $aclMock->expects($this->at(2))->method('allow')->with('1', 'Magento_Backend::all', null); $aclMock->expects($this->at(4))->method('allow')->with('2', 1, null); $aclMock->expects($this->at(6))->method('deny')->with('3', 1, null); - $this->_model->populateAcl($aclMock); + $this->model->populateAcl($aclMock); } } diff --git a/app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php b/app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php index 260691608537e..36cd789042f43 100644 --- a/app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php +++ b/app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php @@ -3,9 +3,23 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Authorization\Test\Unit\Model\ResourceModel; +use Magento\Authorization\Model\ResourceModel\Rules; +use Magento\Framework\Acl\Builder; +use Magento\Framework\Acl\Data\CacheInterface; +use Magento\Framework\Acl\RootResource; +use Magento\Framework\App\ResourceConnection; +use Magento\Framework\DB\Adapter\AdapterInterface; +use Magento\Framework\Model\ResourceModel\Db\Context; +use Magento\Framework\Phrase; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; + /** * Unit test for Rules resource model. * @@ -14,7 +28,7 @@ * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class RulesTest extends \PHPUnit\Framework\TestCase +class RulesTest extends TestCase { /** * Test constants @@ -22,121 +36,125 @@ class RulesTest extends \PHPUnit\Framework\TestCase const TEST_ROLE_ID = 13; /** - * @var \Magento\Authorization\Model\ResourceModel\Rules + * @var Rules */ private $model; /** - * @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject + * @var Context|MockObject */ private $contextMock; /** - * @var \Magento\Framework\Acl\Builder|\PHPUnit_Framework_MockObject_MockObject + * @var Builder|MockObject */ private $aclBuilderMock; /** - * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var LoggerInterface|MockObject */ private $loggerMock; /** - * @var \Magento\Framework\Acl\RootResource|\PHPUnit_Framework_MockObject_MockObject + * @var RootResource|MockObject */ private $rootResourceMock; /** - * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject + * @var CacheInterface|MockObject */ private $aclDataCacheMock; /** - * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject + * @var ResourceConnection|MockObject */ private $resourceConnectionMock; /** - * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject + * @var AdapterInterface|MockObject */ private $connectionMock; /** - * @var \Magento\Authorization\Model\Rules|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Authorization\Model\Rules|MockObject */ private $ruleMock; + /** + * @inheritDoc + */ protected function setUp() { - $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class) + $this->contextMock = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->setMethods(['getResources']) ->getMock(); - $this->resourceConnectionMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class) + $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class) ->disableOriginalConstructor() ->setMethods(['getConnection', 'getTableName']) ->getMock(); $this->contextMock->expects($this->once()) ->method('getResources') - ->will($this->returnValue($this->resourceConnectionMock)); + ->willReturn($this->resourceConnectionMock); - $this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class) + $this->connectionMock = $this->getMockBuilder(AdapterInterface::class) ->disableOriginalConstructor() ->setMethods([]) ->getMock(); $this->resourceConnectionMock->expects($this->once()) ->method('getConnection') - ->with('connection') - ->will($this->returnValue($this->connectionMock)); + ->with('default') + ->willReturn($this->connectionMock); - $this->resourceConnectionMock->expects($this->any()) - ->method('getTableName') - ->with('authorization_rule', 'connection') + $this->resourceConnectionMock->method('getTableName') + ->with('authorization_rule', 'default') ->will($this->returnArgument(0)); - $this->aclBuilderMock = $this->getMockBuilder(\Magento\Framework\Acl\Builder::class) + $this->aclBuilderMock = $this->getMockBuilder(Builder::class) ->disableOriginalConstructor() ->setMethods(['getConfigCache']) ->getMock(); - $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) ->disableOriginalConstructor() ->setMethods([]) ->getMock(); - $this->rootResourceMock = $this->getMockBuilder(\Magento\Framework\Acl\RootResource::class) + $this->rootResourceMock = $this->getMockBuilder(RootResource::class) ->disableOriginalConstructor() ->setMethods([]) ->getMock(); - $this->aclDataCacheMock = $this->getMockBuilder(\Magento\Framework\Acl\Data\CacheInterface::class) + $this->aclDataCacheMock = $this->getMockBuilder(CacheInterface::class) ->disableOriginalConstructor() ->setMethods([]) ->getMock(); - $this->aclBuilderMock->expects($this->any()) - ->method('getConfigCache') - ->will($this->returnValue($this->aclDataCacheMock)); + $this->aclBuilderMock->method('getConfigCache') + ->willReturn($this->aclDataCacheMock); $this->ruleMock = $this->getMockBuilder(\Magento\Authorization\Model\Rules::class) ->disableOriginalConstructor() ->setMethods(['getRoleId']) ->getMock(); - $this->ruleMock->expects($this->any()) - ->method('getRoleId') - ->will($this->returnValue(self::TEST_ROLE_ID)); - - $this->model = new \Magento\Authorization\Model\ResourceModel\Rules( - $this->contextMock, - $this->aclBuilderMock, - $this->loggerMock, - $this->rootResourceMock, - 'connection', - $this->aclDataCacheMock + $this->ruleMock->method('getRoleId') + ->willReturn(self::TEST_ROLE_ID); + + $objectManager = new ObjectManager($this); + $this->model = $objectManager->getObject( + Rules::class, + [ + 'context' => $this->contextMock, + 'aclBuilder' => $this->aclBuilderMock, + 'logger' => $this->loggerMock, + 'rootResource' => $this->rootResourceMock, + 'aclDataCache' => $this->aclDataCacheMock, + 'default' + ] ); } @@ -167,12 +185,12 @@ public function testSaveRelNoResources() */ public function testLocalizedExceptionOccurance() { - $exceptionPhrase = $this->getMockBuilder(\Magento\Framework\Phrase::class) + $exceptionPhrase = $this->getMockBuilder(Phrase::class) ->disableOriginalConstructor() ->setMethods(['render']) ->getMock(); - $exceptionPhrase->expects($this->any())->method('render')->will($this->returnValue('TestException')); + $exceptionPhrase->method('render')->willReturn('TestException'); $exception = new \Magento\Framework\Exception\LocalizedException($exceptionPhrase);