123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Test\Unit\Model\Plugin;
- use Magento\Integration\Model\Integration;
- /**
- * Unit test for \Magento\Integration\Model\Plugin\Integration
- */
- class IntegrationTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * API setup plugin
- *
- * @var \Magento\Integration\Model\Plugin\Integration
- */
- protected $integrationPlugin;
- /**
- * @var \Magento\Integration\Api\IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $subjectMock;
- /**
- * @var \Magento\Authorization\Model\Acl\AclRetriever|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $aclRetrieverMock;
- /**
- * @var \Magento\Integration\Api\AuthorizationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $integrationAuthServiceMock;
- /**
- * @var \Magento\Integration\Model\IntegrationConfig|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $integrationConfigMock;
- /**
- * @var \Magento\Integration\Model\ConsolidatedConfig|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $consolidatedConfigMock;
- protected function setUp()
- {
- $this->subjectMock = $this->createMock(\Magento\Integration\Model\IntegrationService::class);
- $this->integrationAuthServiceMock = $this->createPartialMock(
- \Magento\Integration\Api\AuthorizationServiceInterface::class,
- ['removePermissions', 'grantAllPermissions', 'grantPermissions']
- );
- $this->aclRetrieverMock = $this->createPartialMock(
- \Magento\Authorization\Model\Acl\AclRetriever::class,
- ['getAllowedResourcesByUser']
- );
- $this->integrationConfigMock = $this->getMockBuilder(\Magento\Integration\Model\IntegrationConfig::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->consolidatedConfigMock = $this->getMockBuilder(\Magento\Integration\Model\ConsolidatedConfig::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->integrationPlugin = $objectManagerHelper->getObject(
- \Magento\Integration\Model\Plugin\Integration::class,
- [
- 'integrationAuthorizationService' => $this->integrationAuthServiceMock,
- 'aclRetriever' => $this->aclRetrieverMock,
- 'integrationConfig' => $this->integrationConfigMock,
- 'consolidatedConfig' => $this->consolidatedConfigMock
- ]
- );
- }
- public function testAfterDelete()
- {
- $integrationId = 1;
- $integrationsData = [
- Integration::ID => $integrationId,
- Integration::NAME => 'TestIntegration1',
- Integration::EMAIL => 'test-integration1@magento.com',
- Integration::ENDPOINT => 'http://endpoint.com',
- Integration::SETUP_TYPE => 1,
- ];
- $this->integrationAuthServiceMock->expects($this->once())
- ->method('removePermissions')
- ->with($integrationId);
- $this->integrationPlugin->afterDelete($this->subjectMock, $integrationsData);
- }
- public function testAfterCreateAllResources()
- {
- $integrationId = 1;
- $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
- ->disableOriginalConstructor()
- ->getMock();
- $integrationModelMock->expects($this->exactly(2))
- ->method('getId')
- ->will($this->returnValue($integrationId));
- $integrationModelMock->expects($this->once())
- ->method('getData')
- ->with('all_resources')
- ->will($this->returnValue(1));
- $this->integrationAuthServiceMock->expects($this->once())
- ->method('grantAllPermissions')
- ->with($integrationId);
- $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
- }
- public function testAfterCreateSomeResources()
- {
- $integrationId = 1;
- $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
- ->disableOriginalConstructor()
- ->getMock();
- $integrationModelMock->expects($this->exactly(2))
- ->method('getId')
- ->will($this->returnValue($integrationId));
- $integrationModelMock->expects($this->at(2))
- ->method('getData')
- ->with('all_resources')
- ->will($this->returnValue(null));
- $integrationModelMock->expects($this->at(3))
- ->method('getData')
- ->with('resource')
- ->will($this->returnValue(['testResource']));
- $integrationModelMock->expects($this->at(5))
- ->method('getData')
- ->with('resource')
- ->will($this->returnValue(['testResource']));
- $this->integrationAuthServiceMock->expects($this->once())
- ->method('grantPermissions')
- ->with($integrationId, ['testResource']);
- $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
- }
- public function testAfterCreateNoResource()
- {
- $integrationId = 1;
- $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
- ->disableOriginalConstructor()
- ->getMock();
- $integrationModelMock->expects($this->exactly(2))
- ->method('getId')
- ->will($this->returnValue($integrationId));
- $integrationModelMock->expects($this->at(2))
- ->method('getData')
- ->with('all_resources')
- ->will($this->returnValue(null));
- $integrationModelMock->expects($this->at(3))
- ->method('getData')
- ->with('resource')
- ->will($this->returnValue(null));
- $this->integrationAuthServiceMock->expects($this->once())
- ->method('grantPermissions')
- ->with($integrationId, []);
- $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
- }
- public function testAfterUpdateAllResources()
- {
- $integrationId = 1;
- $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
- ->disableOriginalConstructor()
- ->getMock();
- $integrationModelMock->expects($this->exactly(2))
- ->method('getId')
- ->will($this->returnValue($integrationId));
- $integrationModelMock->expects($this->once())
- ->method('getData')
- ->with('all_resources')
- ->will($this->returnValue(1));
- $this->integrationAuthServiceMock->expects($this->once())
- ->method('grantAllPermissions')
- ->with($integrationId);
- $this->integrationPlugin->afterUpdate($this->subjectMock, $integrationModelMock);
- }
- public function testAfterGet()
- {
- $integrationId = 1;
- $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
- ->disableOriginalConstructor()
- ->getMock();
- $integrationModelMock->expects($this->exactly(2))
- ->method('getId')
- ->will($this->returnValue($integrationId));
- $integrationModelMock->expects($this->once())
- ->method('setData')
- ->with('resource', ['testResource']);
- $deprecatedIntegrationsData = [
- Integration::ID => $integrationId,
- Integration::NAME => 'TestIntegration1',
- Integration::EMAIL => 'test-integration1@magento.com',
- Integration::ENDPOINT => 'http://endpoint.com',
- Integration::SETUP_TYPE => 1,
- 'resource' => ['testResource']
- ];
- $consolidatedIntegrationsData = [
- Integration::ID => 2,
- Integration::NAME => 'TestIntegration2',
- Integration::EMAIL => 'test-integration2@magento.com',
- Integration::ENDPOINT => 'http://endpoint2.com',
- Integration::SETUP_TYPE => 1,
- 'resource' => ['testResource']
- ];
- $this->integrationConfigMock->method('getIntegrations')->willReturn($deprecatedIntegrationsData);
- $this->consolidatedConfigMock->method('getIntegrations')->willReturn($consolidatedIntegrationsData);
- $this->aclRetrieverMock->expects($this->once())
- ->method('getAllowedResourcesByUser')
- ->with(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION, $integrationId)
- ->will($this->returnValue(['testResource']));
- $this->integrationPlugin->afterGet($this->subjectMock, $integrationModelMock);
- }
- }
|