public function testGetIntegrationsFromConfigReader() { $integrations = ['foo', 'bar', 'baz']; $this->configCacheTypeMock->expects($this->once())->method('load')->with(IntegrationConfig::CACHE_ID)->will($this->returnValue(null)); $this->configCacheTypeMock->expects($this->once())->method('save')->with(serialize($integrations), IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG])->will($this->returnValue(null)); $this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue($integrations)); $this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations()); }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterProcessIntegrationConfigSuccess() { $testIntegration1Resource = ['Magento_Customer::manage', 'Magento_Customer::online', 'Magento_Sales::create', 'Magento_SalesRule::quote']; $testIntegration2Resource = ['Magento_Catalog::product_read']; $this->integrationConfigMock->expects($this->once())->method('getIntegrations')->will($this->returnValue(['TestIntegration1' => ['resources' => $testIntegration1Resource], 'TestIntegration2' => ['resources' => $testIntegration2Resource]])); $firstInegrationId = 1; $integrationsData1 = new \Magento\Framework\Object(['id' => $firstInegrationId, Integration::NAME => 'TestIntegration1', Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::SETUP_TYPE => 1]); $secondIntegrationId = 2; $integrationsData2 = new \Magento\Framework\Object(['id' => $secondIntegrationId, Integration::NAME => 'TestIntegration2', Integration::EMAIL => '*****@*****.**', Integration::SETUP_TYPE => 1]); $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($integrationsData1)); $this->integrationServiceMock->expects($this->at(1))->method('findByName')->with('TestIntegration2')->will($this->returnValue($integrationsData2)); $this->apiSetupPlugin->afterProcessIntegrationConfig($this->subjectMock, ['TestIntegration1', 'TestIntegration2']); }
public function testProcessIntegrationConfigCreateNewIntegrations() { $integrations = ['TestIntegration1' => [Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity'], 'TestIntegration2' => [Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity']]; $newResources = ['Magento_Customer::manage', 'Magento_Customer::customer']; $this->_integrationConfigMock->expects($this->once())->method('getIntegrations')->willReturn($newResources); $integrationObject = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods([])->getMock(); // Integration1 does not exist, so create it $this->_integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($integrationObject)); $integrationObject->expects($this->any())->method('getId')->willReturn(false); $this->_integrationServiceMock->expects($this->any())->method('create'); // Integration2 does not exist, so create it $this->_integrationServiceMock->expects($this->at(2))->method('findByName')->with('TestIntegration2')->will($this->returnValue($integrationObject)); $this->_integrationManager->processIntegrationConfig($integrations); }
/** * Process integration resource permissions after the integration is created * * @param ConfigBasedIntegrationManager $subject * @param array $integrations integrations passed as array from the invocation chain * * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterProcessIntegrationConfig(ConfigBasedIntegrationManager $subject, $integrations) { if (empty($integrations)) { return []; } /** @var array $integrationsResource */ $integrationsResource = $this->_integrationConfig->getIntegrations(); foreach (array_keys($integrations) as $name) { if (isset($integrationsResource[$name])) { $integration = $this->_integrationService->findByName($name); if ($integration->getId()) { $this->integrationAuthorizationService->grantPermissions($integration->getId(), $integrationsResource[$name]['resource']); } } } return $integrations; }
/** * Return available resourses for integration model * * @param IntegrationModel $integration * @return string[] */ private function getIntegrationApiResource(IntegrationModel $integration) { $resources = []; $integrationResources = $this->integrationApiConfig->getIntegrations(); $integrationName = $integration->getData('name'); if (!empty($integrationResources[$integrationName]['resource'])) { $resources = $integrationResources[$integrationName]['resource']; } return $resources; }
/** * Add the list of allowed resources to the integration object data by 'resource' key. * * @param IntegrationModel $integration * @return void */ protected function _addAllowedResources(IntegrationModel $integration) { $integrations = array_merge($this->integrationConfig->getIntegrations(), $this->consolidatedConfig->getIntegrations()); if ($integration->getId()) { if ($integration->getSetupType() == IntegrationModel::TYPE_CONFIG) { $integration->setData('resource', $integrations[$integration->getData('name')]['resource']); } else { $integration->setData('resource', $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, (int) $integration->getId())); } } }
/** * Process integrations from config files for the given array of integration names * * @param array $integrations * @return array */ public function processIntegrationConfig(array $integrations) { if (empty($integrations)) { return []; } /** @var array $integrationsResource */ $integrationsResource = $this->integrationConfig->getIntegrations(); foreach (array_keys($integrations) as $name) { $integrationDetails = $integrations[$name]; $integrationData = [Integration::NAME => $name]; if (isset($integrationDetails[Converter::KEY_EMAIL])) { $integrationData[Integration::EMAIL] = $integrationDetails[Converter::KEY_EMAIL]; } if (isset($integrationDetails[Converter::KEY_AUTHENTICATION_ENDPOINT_URL])) { $integrationData[Integration::ENDPOINT] = $integrationDetails[Converter::KEY_AUTHENTICATION_ENDPOINT_URL]; } if (isset($integrationDetails[Converter::KEY_IDENTITY_LINKING_URL])) { $integrationData[Integration::IDENTITY_LINK_URL] = $integrationDetails[Converter::KEY_IDENTITY_LINKING_URL]; } if (isset($integrationsResource[$name]['resource'])) { $integrationData['resource'] = $integrationsResource[$name]['resource']; } $integrationData[Integration::SETUP_TYPE] = Integration::TYPE_CONFIG; $integration = $this->integrationService->findByName($name); if ($integration->getId()) { $originalResources = $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, $integration->getId()); $updateData = $integrationData; $updateData[Integration::ID] = $integration->getId(); $integration = $this->integrationService->update($updateData); // If there were any changes, delete then recreate integration if ($this->hasDataChanged($integration, $originalResources)) { $this->integrationService->delete($integration->getId()); $integrationData[Integration::STATUS] = Integration::STATUS_RECREATED; } else { continue; } } $this->integrationService->create($integrationData); } return $integrations; }
public function testAfterGet() { $integrationId = 1; $integrationModelMock = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->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 => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::SETUP_TYPE => 1, 'resource' => ['testResource']]; $consolidatedIntegrationsData = [Integration::ID => 2, Integration::NAME => 'TestIntegration2', Integration::EMAIL => '*****@*****.**', 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); }